標籤: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 1struct 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學習記錄