码迷,mamicode.com
首页 > 其他好文 > 详细

R2学习记录

时间:2018-10-16 01:34:23      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:ack   locking   not   The   cond   libev   callbacks   fprintf   unsigned   

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\n", 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学习记录

标签:ack   locking   not   The   cond   libev   callbacks   fprintf   unsigned   

原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/9795337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!