R2學習記錄

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.