R2学习记录

Posted wuwangchuxin0924

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R2学习记录相关的知识,希望对你有一定的参考价值。

Setting up the Libevent library
1.重写log行为

#include <event2/event.h>
#include <stdio.h>
static void discard_cb(int severity, const char *msg) {
    /* This callback does nothing. */
}
static FILE *logfile = NULL;
static void write_to_file_cb(int severity, const char *msg) {
    const char *s;
    if (!logfile)
        return;
    switch (severity) {
        case _EVENT_LOG_DEBUG: 
            s = "debug";
        case _EVENT_LOG_MSG:
            s = "msg";
        case _EVENT_LOG_WARN: 
            s = "warn";
        case _EVENT_LOG_ERR:
            s = "error";
        default:    /* never reached */
            s = "?";
    }
    fprintf(logfile, "[%s] %s
", s, msg);
}

/* Turn off all logging from Libevent. */
void suppress_logging(void) {
    event_set_log_callback(discard_cb);
}
/* Redirect all Libevent log messages to the C stdio file ‘f‘. */
void set_logfile(FILE *f) {
    logfile = f;
    event_set_log_callback(write_to_file_cb);
}

 

2.重写异常退出动作:

typedef void (*event_fatal_cb)(int err);
void event_set_fatal_callback(event_fatal_cb cb);

 

3.重写内存管理方式:

void event_set_mem_functions(void *(*malloc_fn)(size_t sz),
void *(*realloc_fn)(void *ptr, size_t sz),
void (*free_fn)(void *ptr));

 

4.锁和线程
libevent默认支持win和类unix的线程库, 如果想自己实现线程库的话需要实现:
Locks
locking
unlocking
lock allocation
lock destruction
Conditions
condition variable creation
condition variable destruction
waiting on a condition variable
signaling/broadcasting to a condition variable
Threads
thread ID detection

#define EVTHREAD_WRITE     0x04
#define EVTHREAD_READ    0x08    
#define EVTHREAD_TRY    0x10

#define EVTHREAD_LOCKTYPE_RECURSIVE 1
#define EVTHREAD_LOCKTYPE_READWRITE 2

#define EVTHREAD_LOCK_API_VERSION 1

struct evthread_lock_callbacks {
        int lock_api_version;
        unsigned supported_locktypes;
        void *(*alloc)(unsigned locktype);
        void (*free)(void *lock, unsigned locktype);
        int (*lock)(unsigned mode, void *lock);
        int (*unlock)(unsigned mode, void *lock);
};
int evthread_set_lock_callbacks(const struct evthread_lock_callbacks *);
void evthread_set_id_callback(unsigned long (*id_fn)(void));
struct evthread_condition_callbacks {
        int condition_api_version;
        void *(*alloc_condition)(unsigned condtype);
        void (*free_condition)(void *cond);
        int (*signal_condition)(void *cond, int broadcast);
        int (*wait_condition)(void *cond, void *lock,
                        const struct timeval *timeout);
};
int evthread_set_condition_callbacks(
        const struct evthread_condition_callbacks *);

 

以上是关于R2学习记录的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server2008R2学习记录

python小白学习记录 多线程爬取ts片段

golang代码片段(摘抄)

QT 实用代码片段

CSP核心代码片段记录

在 SQL Server 2008 R2 的 MERGE 语句中更新插入的记录