分析libevent的源代码,我的想法的是先分析各种结构体,struct event_base、struct event,然后是event_base_new函数、event_new函数、event_add函数,最后分析event_base_dispatch函数。

一、各种结构体

1、event_base

  1. struct event_base {
  2. /** Function pointers and other data to describe this event_base's
  3. * backend. */
  4. const struct eventop *evsel;
  5. /** Pointer to backend-specific data. */
  6. void *evbase;
  7.  
  8. /** List of changes to tell backend about at next dispatch. Only used
  9. * by the O(1) backends. */
  10. struct event_changelist changelist;
  11.  
  12. /** Function pointers used to describe the backend that this event_base
  13. * uses for signals */
  14. const struct eventop *evsigsel;
  15. /** Data to implement the common signal handelr code. */
  16. struct evsig_info sig;
  17.  
  18. /** Number of virtual events */
  19. int virtual_event_count;
  20. /** Maximum number of virtual events active */
  21. int virtual_event_count_max;
  22. /** Number of total events added to this event_base */
  23. int event_count;
  24. /** Maximum number of total events added to this event_base */
  25. int event_count_max;
  26. /** Number of total events active in this event_base */
  27. int event_count_active;
  28. /** Maximum number of total events active in this event_base */
  29. int event_count_active_max;
  30.  
  31. /** Set if we should terminate the loop once we're done processing
  32. * events. */
  33. int event_gotterm;
  34. /** Set if we should terminate the loop immediately */
  35. int event_break;
  36. /** Set if we should start a new instance of the loop immediately. */
  37. int event_continue;
  38.  
  39. /** The currently running priority of events */
  40. int event_running_priority;
  41.  
  42. /** Set if we're running the event_base_loop function, to prevent
  43. * reentrant invocation. */
  44. int running_loop;
  45.  
  46. /** Set to the number of deferred_cbs we've made 'active' in the
  47. * loop. This is a hack to prevent starvation; it would be smarter
  48. * to just use event_config_set_max_dispatch_interval's max_callbacks
  49. * feature */
  50. int n_deferreds_queued;
  51.  
  52. /* Active event management. */
  53. /** An array of nactivequeues queues for active event_callbacks (ones
  54. * that have triggered, and whose callbacks need to be called). Low
  55. * priority numbers are more important, and stall higher ones.
  56. */
  57. struct evcallback_list *activequeues;
  58. /** The length of the activequeues array */
  59. int nactivequeues;
  60. /** A list of event_callbacks that should become active the next time
  61. * we process events, but not this time. */
  62. struct evcallback_list active_later_queue;
  63.  
  64. /* common timeout logic */
  65.  
  66. /** An array of common_timeout_list* for all of the common timeout
  67. * values we know. */
  68. struct common_timeout_list **common_timeout_queues;
  69. /** The number of entries used in common_timeout_queues */
  70. int n_common_timeouts;
  71. /** The total size of common_timeout_queues. */
  72. int n_common_timeouts_allocated;
  73.  
  74. /** Mapping from file descriptors to enabled (added) events */
  75. struct event_io_map io;
  76.  
  77. /** Mapping from signal numbers to enabled (added) events. */
  78. struct event_signal_map sigmap;
  79.  
  80. /** Priority queue of events with timeouts. */
  81. struct min_heap timeheap;
  82.  
  83. /** Stored timeval: used to avoid calling gettimeofday/clock_gettime
  84. * too often. */
  85. struct timeval tv_cache;
  86.  
  87. struct evutil_monotonic_timer monotonic_timer;
  88.  
  89. /** Difference between internal time (maybe from clock_gettime) and
  90. * gettimeofday. */
  91. struct timeval tv_clock_diff;
  92. /** Second in which we last updated tv_clock_diff, in monotonic time. */
  93. time_t last_updated_clock_diff;
  94.  
  95. #ifndef EVENT__DISABLE_THREAD_SUPPORT
  96. /* threading support */
  97. /** The thread currently running the event_loop for this base */
  98. unsigned long th_owner_id;
  99. /** A lock to prevent conflicting accesses to this event_base */
  100. void *th_base_lock;
  101. /** A condition that gets signalled when we're done processing an
  102. * event with waiters on it. */
  103. void *current_event_cond;
  104. /** Number of threads blocking on current_event_cond. */
  105. int current_event_waiters;
  106. #endif
  107. /** The event whose callback is executing right now */
  108. struct event_callback *current_event;
  109.  
  110. #ifdef _WIN32
  111. /** IOCP support structure, if IOCP is enabled. */
  112. struct event_iocp_port *iocp;
  113. #endif
  114.  
  115. /** Flags that this base was configured with */
  116. enum event_base_config_flag flags;
  117.  
  118. struct timeval max_dispatch_time;
  119. int max_dispatch_callbacks;
  120. int limit_callbacks_after_prio;
  121.  
  122. /* Notify main thread to wake up break, etc. */
  123. /** True if the base already has a pending notify, and we don't need
  124. * to add any more. */
  125. int is_notify_pending;
  126. /** A socketpair used by some th_notify functions to wake up the main
  127. * thread. */
  128. evutil_socket_t th_notify_fd[];
  129. /** An event used by some th_notify functions to wake up the main
  130. * thread. */
  131. struct event th_notify;
  132. /** A function used to wake up the main thread from another thread. */
  133. int (*th_notify_fn)(struct event_base *base);
  134.  
  135. /** Saved seed for weak random number generator. Some backends use
  136. * this to produce fairness among sockets. Protected by th_base_lock. */
  137. struct evutil_weakrand_state weakrand_seed;
  138.  
  139. /** List of event_onces that have not yet fired. */
  140. LIST_HEAD(once_event_list, event_once) once_events;
  141.  
  142. };

struct event_base结构体在event-internal.h文件中定义。

二、初始化函数

1、event_base_new函数

  1. struct event_base *
  2. event_base_new(void)
  3. {
  4. struct event_base *base = NULL;
  5. struct event_config *cfg = event_config_new();
  6. if (cfg) {
  7. base = event_base_new_with_config(cfg);
  8. event_config_free(cfg);
  9. }
  10. return base;
  11. }

(1)调用event_config_new函数分配一个struct event_config结构体。
(2)如果分配成功,就调用event_base_new_with_config(cfg)分配一个struct event_base对象指针,然后将该指针返回。

总结:所以event_base_new还是调用了event_base_new_with_config函数。所以下面接着来看event_base_new_with_config函数。

2、event_base_new_with_config函数

  1. struct event_base *
  2. event_base_new_with_config(const struct event_config *cfg)
  3. {
  4. int i;
  5. struct event_base *base;
  6. int should_check_environment;
  7.  
  8. #ifndef EVENT__DISABLE_DEBUG_MODE
  9. event_debug_mode_too_late = ;
  10. #endif
  11.  
  12. if ((base = mm_calloc(, sizeof(struct event_base))) == NULL) {
  13. event_warn("%s: calloc", __func__);
  14. return NULL;
  15. }
  16.  
  17. if (cfg)
  18. base->flags = cfg->flags;
  19.  
  20. should_check_environment =
  21. !(cfg && (cfg->flags & EVENT_BASE_FLAG_IGNORE_ENV));
  22.  
  23. {
  24. struct timeval tmp;
  25. int precise_time =
  26. cfg && (cfg->flags & EVENT_BASE_FLAG_PRECISE_TIMER);
  27. int flags;
  28. if (should_check_environment && !precise_time) {
  29. precise_time = evutil_getenv_("EVENT_PRECISE_TIMER") != NULL;
  30. base->flags |= EVENT_BASE_FLAG_PRECISE_TIMER;
  31. }
  32. flags = precise_time ? EV_MONOT_PRECISE : ;
  33. evutil_configure_monotonic_time_(&base->monotonic_timer, flags);
  34.  
  35. gettime(base, &tmp);
  36. }
  37.  
  38. min_heap_ctor_(&base->timeheap);
  39.  
  40. base->sig.ev_signal_pair[] = -;
  41. base->sig.ev_signal_pair[] = -;
  42. base->th_notify_fd[] = -;
  43. base->th_notify_fd[] = -;
  44.  
  45. TAILQ_INIT(&base->active_later_queue);
  46.  
  47. evmap_io_initmap_(&base->io);
  48. evmap_signal_initmap_(&base->sigmap);
  49. event_changelist_init_(&base->changelist);
  50.  
  51. base->evbase = NULL;
  52.  
  53. if (cfg) {
  54. memcpy(&base->max_dispatch_time,
  55. &cfg->max_dispatch_interval, sizeof(struct timeval));
  56. base->limit_callbacks_after_prio =
  57. cfg->limit_callbacks_after_prio;
  58. } else {
  59. base->max_dispatch_time.tv_sec = -;
  60. base->limit_callbacks_after_prio = ;
  61. }
  62. if (cfg && cfg->max_dispatch_callbacks >= ) {
  63. base->max_dispatch_callbacks = cfg->max_dispatch_callbacks;
  64. } else {
  65. base->max_dispatch_callbacks = INT_MAX;
  66. }
  67. if (base->max_dispatch_callbacks == INT_MAX &&
  68. base->max_dispatch_time.tv_sec == -)
  69. base->limit_callbacks_after_prio = INT_MAX;
  70.  
  71. for (i = ; eventops[i] && !base->evbase; i++) {
  72. if (cfg != NULL) {
  73. /* determine if this backend should be avoided */
  74. if (event_config_is_avoided_method(cfg,
  75. eventops[i]->name))
  76. continue;
  77. if ((eventops[i]->features & cfg->require_features)
  78. != cfg->require_features)
  79. continue;
  80. }
  81.  
  82. /* also obey the environment variables */
  83. if (should_check_environment &&
  84. event_is_method_disabled(eventops[i]->name))
  85. continue;
  86.  
  87. base->evsel = eventops[i];
  88.  
  89. base->evbase = base->evsel->init(base);
  90. }
  91.  
  92. if (base->evbase == NULL) {
  93. event_warnx("%s: no event mechanism available",
  94. __func__);
  95. base->evsel = NULL;
  96. event_base_free(base);
  97. return NULL;
  98. }
  99.  
  100. if (evutil_getenv_("EVENT_SHOW_METHOD"))
  101. event_msgx("libevent using: %s", base->evsel->name);
  102.  
  103. /* allocate a single active event queue */
  104. if (event_base_priority_init(base, ) < ) {
  105. event_base_free(base);
  106. return NULL;
  107. }
  108.  
  109. /* prepare for threading */
  110.  
  111. #if !defined(EVENT__DISABLE_THREAD_SUPPORT) && !defined(EVENT__DISABLE_DEBUG_MODE)
  112. event_debug_created_threadable_ctx_ = ;
  113. #endif
  114.  
  115. #ifndef EVENT__DISABLE_THREAD_SUPPORT
  116. if (EVTHREAD_LOCKING_ENABLED() &&
  117. (!cfg || !(cfg->flags & EVENT_BASE_FLAG_NOLOCK))) {
  118. int r;
  119. EVTHREAD_ALLOC_LOCK(base->th_base_lock, );
  120. EVTHREAD_ALLOC_COND(base->current_event_cond);
  121. r = evthread_make_base_notifiable(base);
  122. if (r<) {
  123. event_warnx("%s: Unable to make base notifiable.", __func__);
  124. event_base_free(base);
  125. return NULL;
  126. }
  127. }
  128. #endif
  129.  
  130. #ifdef _WIN32
  131. if (cfg && (cfg->flags & EVENT_BASE_FLAG_STARTUP_IOCP))
  132. event_base_start_iocp_(base, cfg->n_cpus_hint);
  133. #endif
  134.  
  135. return (base);
  136. }

(1)调用mm_calloc函数分配一块大小为sizeof(struct event_base)的内存空间。

(2)如果形参cfg不为NULL,则将base.flags赋值为cfg->flags。

(3)第71-90行设置了实际使用的后端机制,for循环从遍历数组eventops,直到找到一个可用的后端为止,可以看一下eventops。

  1. #ifdef EVENT__HAVE_EVENT_PORTS
  2. extern const struct eventop evportops;
  3. #endif
  4. #ifdef EVENT__HAVE_SELECT
  5. extern const struct eventop selectops;
  6. #endif
  7. #ifdef EVENT__HAVE_POLL
  8. extern const struct eventop pollops;
  9. #endif
  10. #ifdef EVENT__HAVE_EPOLL
  11. extern const struct eventop epollops;
  12. #endif
  13. #ifdef EVENT__HAVE_WORKING_KQUEUE
  14. extern const struct eventop kqops;
  15. #endif
  16. #ifdef EVENT__HAVE_DEVPOLL
  17. extern const struct eventop devpollops;
  18. #endif
  19. #ifdef _WIN32
  20. extern const struct eventop win32ops;
  21. #endif
  22.  
  23. /* Array of backends in order of preference. */
  24. static const struct eventop *eventops[] = {
  25. #ifdef EVENT__HAVE_EVENT_PORTS
  26. &evportops,
  27. #endif
  28. #ifdef EVENT__HAVE_WORKING_KQUEUE
  29. &kqops,
  30. #endif
  31. #ifdef EVENT__HAVE_EPOLL
  32. &epollops,
  33. #endif
  34. #ifdef EVENT__HAVE_DEVPOLL
  35. &devpollops,
  36. #endif
  37. #ifdef EVENT__HAVE_POLL
  38. &pollops,
  39. #endif
  40. #ifdef EVENT__HAVE_SELECT
  41. &selectops,
  42. #endif
  43. #ifdef _WIN32
  44. &win32ops,
  45. #endif
  46. NULL
  47. };

从代码中可以看到,根据宏定义来决定某些后端机制是否存在,这样就可以找到运行机子上支持的一个可用的后端机制,而且需要注意,epool、pool、select的顺序,所以如果支持epoll就不会选择poll,如果支持poll就不会选择select,select机制是最后的选择。

  1. /** Structure to define the backend of a given event_base. */
  2. struct eventop {
  3. /** The name of this backend. */
  4. const char *name;
  5. /** Function to set up an event_base to use this backend. It should
  6. * create a new structure holding whatever information is needed to
  7. * run the backend, and return it. The returned pointer will get
  8. * stored by event_init into the event_base.evbase field. On failure,
  9. * this function should return NULL. */
  10. void *(*init)(struct event_base *);
  11. /** Enable reading/writing on a given fd or signal. 'events' will be
  12. * the events that we're trying to enable: one or more of EV_READ,
  13. * EV_WRITE, EV_SIGNAL, and EV_ET. 'old' will be those events that
  14. * were enabled on this fd previously. 'fdinfo' will be a structure
  15. * associated with the fd by the evmap; its size is defined by the
  16. * fdinfo field below. It will be set to 0 the first time the fd is
  17. * added. The function should return 0 on success and -1 on error.
  18. */
  19. int (*add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
  20. /** As "add", except 'events' contains the events we mean to disable. */
  21. int (*del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
  22. /** Function to implement the core of an event loop. It must see which
  23. added events are ready, and cause event_active to be called for each
  24. active event (usually via event_io_active or such). It should
  25. return 0 on success and -1 on error.
  26. */
  27. int (*dispatch)(struct event_base *, struct timeval *);
  28. /** Function to clean up and free our data from the event_base. */
  29. void (*dealloc)(struct event_base *);
  30. /** Flag: set if we need to reinitialize the event base after we fork.
  31. */
  32. int need_reinit;
  33. /** Bit-array of supported event_method_features that this backend can
  34. * provide. */
  35. enum event_method_feature features;
  36. /** Length of the extra information we should record for each fd that
  37. has one or more active events. This information is recorded
  38. as part of the evmap entry for each fd, and passed as an argument
  39. to the add and del functions above.
  40. */
  41. size_t fdinfo_len;
  42. };

struct eventop结构定义了后端机制的一个公共接口,至于每个后端是如何将自己的函数封装成符合这个接口的,我下面会逐个分析。

(4)然后调用init函数来初始化event_base对象。init函数的具体实现根据不同的后端机制会有所不同。

3、event_new函数

  1. struct event *
  2. event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg)
  3. {
  4. struct event *ev;
  5. ev = mm_malloc(sizeof(struct event));
  6. if (ev == NULL)
  7. return (NULL);
  8. if (event_assign(ev, base, fd, events, cb, arg) < ) {
  9. mm_free(ev);
  10. return (NULL);
  11. }
  12.  
  13. return (ev);
  14. }

(1)调用mm_malloc函数分配一块大小为sizeof(struct event)的内存空间。
(2)event_new的实现类似于event_base_new函数类似,分配好空间之后,调用了event_assign函数来填充结构体。

4、event_assign函数

  1. int
  2. event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*callback)(evutil_socket_t, short, void *), void *arg)
  3. {
  4. if (!base)
  5. base = current_base;
  6. if (arg == &event_self_cbarg_ptr_)
  7. arg = ev;
  8.  
  9. event_debug_assert_not_added_(ev);
  10.  
  11. ev->ev_base = base;
  12.  
  13. ev->ev_callback = callback;
  14. ev->ev_arg = arg;
  15. ev->ev_fd = fd;
  16. ev->ev_events = events;
  17. ev->ev_res = ;
  18. ev->ev_flags = EVLIST_INIT;
  19. ev->ev_ncalls = ;
  20. ev->ev_pncalls = NULL;
  21.  
  22. if (events & EV_SIGNAL) {
  23. if ((events & (EV_READ|EV_WRITE|EV_CLOSED)) != ) {
  24. event_warnx("%s: EV_SIGNAL is not compatible with "
  25. "EV_READ, EV_WRITE or EV_CLOSED", __func__);
  26. return -;
  27. }
  28. ev->ev_closure = EV_CLOSURE_EVENT_SIGNAL;
  29. } else {
  30. if (events & EV_PERSIST) {
  31. evutil_timerclear(&ev->ev_io_timeout);
  32. ev->ev_closure = EV_CLOSURE_EVENT_PERSIST;
  33. } else {
  34. ev->ev_closure = EV_CLOSURE_EVENT;
  35. }
  36. }
  37.  
  38. min_heap_elem_init_(ev);
  39.  
  40. if (base != NULL) {
  41. /* by default, we put new events into the middle priority */
  42. ev->ev_pri = base->nactivequeues / ;
  43. }
  44.  
  45. event_debug_note_setup_(ev);
  46.  
  47. return ;
  48. }

(1)event_assign函数的主要操作是给形参struct event *ev的成员赋值,包括ev->ev_base、ev->ev_callback、ev->ev_arg、ev->ev_fd、ev->ev_events等

总结:event_new、event_assign函数会把传递进来的struct event_base* base保存在获取到的strut event结构体内部。

5、event_add函数

  1. int
  2. event_add(struct event *ev, const struct timeval *tv)
  3. {
  4. int res;
  5.  
  6. if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
  7. event_warnx("%s: event has no event_base set.", __func__);
  8. return -;
  9. }
  10.  
  11. EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
  12.  
  13. res = event_add_nolock_(ev, tv, );
  14.  
  15. EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
  16.  
  17. return (res);
  18. }

(1)event_add函数调用了event_add_nolock_函数进行实际的操作。

6、event_add_nolock_函数

  1. /* Implementation function to add an event. Works just like event_add,
  2. * except: 1) it requires that we have the lock. 2) if tv_is_absolute is set,
  3. * we treat tv as an absolute time, not as an interval to add to the current
  4. * time */
  5. int
  6. event_add_nolock_(struct event *ev, const struct timeval *tv,
  7. int tv_is_absolute)
  8. {
  9. struct event_base *base = ev->ev_base;
  10. int res = ;
  11. int notify = ;
  12.  
  13. EVENT_BASE_ASSERT_LOCKED(base);
  14. event_debug_assert_is_setup_(ev);
  15.  
  16. event_debug((
  17. "event_add: event: %p (fd "EV_SOCK_FMT"), %s%s%s%scall %p",
  18. ev,
  19. EV_SOCK_ARG(ev->ev_fd),
  20. ev->ev_events & EV_READ ? "EV_READ " : " ",
  21. ev->ev_events & EV_WRITE ? "EV_WRITE " : " ",
  22. ev->ev_events & EV_CLOSED ? "EV_CLOSED " : " ",
  23. tv ? "EV_TIMEOUT " : " ",
  24. ev->ev_callback));
  25.  
  26. EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL));
  27.  
  28. if (ev->ev_flags & EVLIST_FINALIZING) {
  29. /* XXXX debug */
  30. return (-);
  31. }
  32.  
  33. /*
  34. * prepare for timeout insertion further below, if we get a
  35. * failure on any step, we should not change any state.
  36. */
  37. if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) {
  38. if (min_heap_reserve_(&base->timeheap,
  39. + min_heap_size_(&base->timeheap)) == -)
  40. return (-); /* ENOMEM == errno */
  41. }
  42.  
  43. /* If the main thread is currently executing a signal event's
  44. * callback, and we are not the main thread, then we want to wait
  45. * until the callback is done before we mess with the event, or else
  46. * we can race on ev_ncalls and ev_pncalls below. */
  47. #ifndef EVENT__DISABLE_THREAD_SUPPORT
  48. if (base->current_event == event_to_event_callback(ev) &&
  49. (ev->ev_events & EV_SIGNAL)
  50. && !EVBASE_IN_THREAD(base)) {
  51. ++base->current_event_waiters;
  52. EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
  53. }
  54. #endif
  55.  
  56. if ((ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL)) &&
  57. !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) {
  58. if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED))
  59. res = evmap_io_add_(base, ev->ev_fd, ev);
  60. else if (ev->ev_events & EV_SIGNAL)
  61. res = evmap_signal_add_(base, (int)ev->ev_fd, ev);
  62. if (res != -)
  63. event_queue_insert_inserted(base, ev);
  64. if (res == ) {
  65. /* evmap says we need to notify the main thread. */
  66. notify = ;
  67. res = ;
  68. }
  69. }
  70.  
  71. /*
  72. * we should change the timeout state only if the previous event
  73. * addition succeeded.
  74. */
  75. if (res != - && tv != NULL) {
  76. struct timeval now;
  77. int common_timeout;
  78. #ifdef USE_REINSERT_TIMEOUT
  79. int was_common;
  80. int old_timeout_idx;
  81. #endif
  82.  
  83. /*
  84. * for persistent timeout events, we remember the
  85. * timeout value and re-add the event.
  86. *
  87. * If tv_is_absolute, this was already set.
  88. */
  89. if (ev->ev_closure == EV_CLOSURE_EVENT_PERSIST && !tv_is_absolute)
  90. ev->ev_io_timeout = *tv;
  91.  
  92. #ifndef USE_REINSERT_TIMEOUT
  93. if (ev->ev_flags & EVLIST_TIMEOUT) {
  94. event_queue_remove_timeout(base, ev);
  95. }
  96. #endif
  97.  
  98. /* Check if it is active due to a timeout. Rescheduling
  99. * this timeout before the callback can be executed
  100. * removes it from the active list. */
  101. if ((ev->ev_flags & EVLIST_ACTIVE) &&
  102. (ev->ev_res & EV_TIMEOUT)) {
  103. if (ev->ev_events & EV_SIGNAL) {
  104. /* See if we are just active executing
  105. * this event in a loop
  106. */
  107. if (ev->ev_ncalls && ev->ev_pncalls) {
  108. /* Abort loop */
  109. *ev->ev_pncalls = ;
  110. }
  111. }
  112.  
  113. event_queue_remove_active(base, event_to_event_callback(ev));
  114. }
  115.  
  116. gettime(base, &now);
  117.  
  118. common_timeout = is_common_timeout(tv, base);
  119. #ifdef USE_REINSERT_TIMEOUT
  120. was_common = is_common_timeout(&ev->ev_timeout, base);
  121. old_timeout_idx = COMMON_TIMEOUT_IDX(&ev->ev_timeout);
  122. #endif
  123.  
  124. if (tv_is_absolute) {
  125. ev->ev_timeout = *tv;
  126. } else if (common_timeout) {
  127. struct timeval tmp = *tv;
  128. tmp.tv_usec &= MICROSECONDS_MASK;
  129. evutil_timeradd(&now, &tmp, &ev->ev_timeout);
  130. ev->ev_timeout.tv_usec |=
  131. (tv->tv_usec & ~MICROSECONDS_MASK);
  132. } else {
  133. evutil_timeradd(&now, tv, &ev->ev_timeout);
  134. }
  135.  
  136. event_debug((
  137. "event_add: event %p, timeout in %d seconds %d useconds, call %p",
  138. ev, (int)tv->tv_sec, (int)tv->tv_usec, ev->ev_callback));
  139.  
  140. #ifdef USE_REINSERT_TIMEOUT
  141. event_queue_reinsert_timeout(base, ev, was_common, common_timeout, old_timeout_idx);
  142. #else
  143. event_queue_insert_timeout(base, ev);
  144. #endif
  145.  
  146. if (common_timeout) {
  147. struct common_timeout_list *ctl =
  148. get_common_timeout_list(base, &ev->ev_timeout);
  149. if (ev == TAILQ_FIRST(&ctl->events)) {
  150. common_timeout_schedule(ctl, &now, ev);
  151. }
  152. } else {
  153. struct event* top = NULL;
  154. /* See if the earliest timeout is now earlier than it
  155. * was before: if so, we will need to tell the main
  156. * thread to wake up earlier than it would otherwise.
  157. * We double check the timeout of the top element to
  158. * handle time distortions due to system suspension.
  159. */
  160. if (min_heap_elt_is_top_(ev))
  161. notify = ;
  162. else if ((top = min_heap_top_(&base->timeheap)) != NULL &&
  163. evutil_timercmp(&top->ev_timeout, &now, <))
  164. notify = ;
  165. }
  166. }
  167.  
  168. /* if we are not in the right thread, we need to wake up the loop */
  169. if (res != - && notify && EVBASE_NEED_NOTIFY(base))
  170. evthread_notify_base(base);
  171.  
  172. event_debug_note_add_(ev);
  173.  
  174. return (res);
  175. }

(1)

三、event_base_dispatch函数

1、event_base_dispatch函数

  1. int
  2. event_base_dispatch(struct event_base *event_base)
  3. {
  4. return (event_base_loop(event_base, ));
  5. }

(1)可以看到,event_base_dispatch函数间接调用了 event_base_loop函数

2、event_base_loop函数

  1. int
  2. event_base_loop(struct event_base *base, int flags)
  3. {
  4. const struct eventop *evsel = base->evsel;
  5. struct timeval tv;
  6. struct timeval *tv_p;
  7. int res, done, retval = ;
  8.  
  9. /* Grab the lock. We will release it inside evsel.dispatch, and again
  10. * as we invoke user callbacks. */
  11. EVBASE_ACQUIRE_LOCK(base, th_base_lock);
  12.  
  13. if (base->running_loop) {
  14. event_warnx("%s: reentrant invocation. Only one event_base_loop"
  15. " can run on each event_base at once.", __func__);
  16. EVBASE_RELEASE_LOCK(base, th_base_lock);
  17. return -;
  18. }
  19.  
  20. base->running_loop = ;
  21.  
  22. clear_time_cache(base);
  23.  
  24. if (base->sig.ev_signal_added && base->sig.ev_n_signals_added)
  25. evsig_set_base_(base);
  26.  
  27. done = ;
  28.  
  29. #ifndef EVENT__DISABLE_THREAD_SUPPORT
  30. base->th_owner_id = EVTHREAD_GET_ID();
  31. #endif
  32.  
  33. base->event_gotterm = base->event_break = ;
  34.  
  35. while (!done) {
  36. base->event_continue = ;
  37. base->n_deferreds_queued = ;
  38.  
  39. /* Terminate the loop if we have been asked to */
  40. if (base->event_gotterm) {
  41. break;
  42. }
  43.  
  44. if (base->event_break) {
  45. break;
  46. }
  47.  
  48. tv_p = &tv;
  49. if (!N_ACTIVE_CALLBACKS(base) && !(flags & EVLOOP_NONBLOCK)) {
  50. timeout_next(base, &tv_p);
  51. } else {
  52. /*
  53. * if we have active events, we just poll new events
  54. * without waiting.
  55. */
  56. evutil_timerclear(&tv);
  57. }
  58.  
  59. /* If we have no events, we just exit */
  60. if (==(flags&EVLOOP_NO_EXIT_ON_EMPTY) &&
  61. !event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) {
  62. event_debug(("%s: no events registered.", __func__));
  63. retval = ;
  64. goto done;
  65. }
  66.  
  67. event_queue_make_later_events_active(base);
  68.  
  69. clear_time_cache(base);
  70.  
  71. res = evsel->dispatch(base, tv_p);
  72.  
  73. if (res == -) {
  74. event_debug(("%s: dispatch returned unsuccessfully.",
  75. __func__));
  76. retval = -;
  77. goto done;
  78. }
  79.  
  80. update_time_cache(base);
  81.  
  82. timeout_process(base);
  83.  
  84. if (N_ACTIVE_CALLBACKS(base)) {
  85. int n = event_process_active(base);
  86. if ((flags & EVLOOP_ONCE)
  87. && N_ACTIVE_CALLBACKS(base) ==
  88. && n != )
  89. done = ;
  90. } else if (flags & EVLOOP_NONBLOCK)
  91. done = ;
  92. }
  93. event_debug(("%s: asked to terminate loop.", __func__));
  94.  
  95. done:
  96. clear_time_cache(base);
  97. base->running_loop = ;
  98.  
  99. EVBASE_RELEASE_LOCK(base, th_base_lock);
  100.  
  101. return (retval);
  102. }

(1)event_base_loop函数的主要逻辑是就一个死循环,在循环中不断的调用由不同多路分发机制提供的后端接口。71行。

(2)调用后端接口返回后,调用event_process_active函数处理激活的事件。

3、event_process_active函数

  1. /*
  2. * Active events are stored in priority queues. Lower priorities are always
  3. * process before higher priorities. Low priority events can starve high
  4. * priority ones.
  5. */
  6.  
  7. static int
  8. event_process_active(struct event_base *base)
  9. {
  10. /* Caller must hold th_base_lock */
  11. struct evcallback_list *activeq = NULL;
  12. int i, c = ;
  13. const struct timeval *endtime;
  14. struct timeval tv;
  15. const int maxcb = base->max_dispatch_callbacks;
  16. const int limit_after_prio = base->limit_callbacks_after_prio;
  17. if (base->max_dispatch_time.tv_sec >= ) {
  18. update_time_cache(base);
  19. gettime(base, &tv);
  20. evutil_timeradd(&base->max_dispatch_time, &tv, &tv);
  21. endtime = &tv;
  22. } else {
  23. endtime = NULL;
  24. }
  25.  
  26. for (i = ; i < base->nactivequeues; ++i) {
  27. if (TAILQ_FIRST(&base->activequeues[i]) != NULL) {
  28. base->event_running_priority = i;
  29. activeq = &base->activequeues[i];
  30. if (i < limit_after_prio)
  31. c = event_process_active_single_queue(base, activeq,
  32. INT_MAX, NULL);
  33. else
  34. c = event_process_active_single_queue(base, activeq,
  35. maxcb, endtime);
  36. if (c < ) {
  37. goto done;
  38. } else if (c > )
  39. break; /* Processed a real event; do not
  40. * consider lower-priority events */
  41. /* If we get here, all of the events we processed
  42. * were internal. Continue. */
  43. }
  44. }
  45.  
  46. done:
  47. base->event_running_priority = -;
  48.  
  49. return c;
  50. }

(1)第26-44行,循环遍历激活的事件,然后调用event_process_active_single_queue函数。

4、event_process_active_single_queue函数

  1. /*
  2. Helper for event_process_active to process all the events in a single queue,
  3. releasing the lock as we go. This function requires that the lock be held
  4. when it's invoked. Returns -1 if we get a signal or an event_break that
  5. means we should stop processing any active events now. Otherwise returns
  6. the number of non-internal event_callbacks that we processed.
  7. */
  8. static int
  9. event_process_active_single_queue(struct event_base *base,
  10. struct evcallback_list *activeq,
  11. int max_to_process, const struct timeval *endtime)
  12. {
  13. struct event_callback *evcb;
  14. int count = ;
  15.  
  16. EVUTIL_ASSERT(activeq != NULL);
  17.  
  18. for (evcb = TAILQ_FIRST(activeq); evcb; evcb = TAILQ_FIRST(activeq)) {
  19. struct event *ev=NULL;
  20. if (evcb->evcb_flags & EVLIST_INIT) {
  21. ev = event_callback_to_event(evcb);
  22.  
  23. if (ev->ev_events & EV_PERSIST || ev->ev_flags & EVLIST_FINALIZING)
  24. event_queue_remove_active(base, evcb);
  25. else
  26. event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
  27. event_debug((
  28. "event_process_active: event: %p, %s%s%scall %p",
  29. ev,
  30. ev->ev_res & EV_READ ? "EV_READ " : " ",
  31. ev->ev_res & EV_WRITE ? "EV_WRITE " : " ",
  32. ev->ev_res & EV_CLOSED ? "EV_CLOSED " : " ",
  33. ev->ev_callback));
  34. } else {
  35. event_queue_remove_active(base, evcb);
  36. event_debug(("event_process_active: event_callback %p, "
  37. "closure %d, call %p",
  38. evcb, evcb->evcb_closure, evcb->evcb_cb_union.evcb_callback));
  39. }
  40.  
  41. if (!(evcb->evcb_flags & EVLIST_INTERNAL))
  42. ++count;
  43.  
  44. base->current_event = evcb;
  45. #ifndef EVENT__DISABLE_THREAD_SUPPORT
  46. base->current_event_waiters = ;
  47. #endif
  48.  
  49. switch (evcb->evcb_closure) {
  50. case EV_CLOSURE_EVENT_SIGNAL:
  51. EVUTIL_ASSERT(ev != NULL);
  52. event_signal_closure(base, ev);
  53. break;
  54. case EV_CLOSURE_EVENT_PERSIST:
  55. EVUTIL_ASSERT(ev != NULL);
  56. event_persist_closure(base, ev);
  57. break;
  58. case EV_CLOSURE_EVENT: {
  59. void (*evcb_callback)(evutil_socket_t, short, void *);
  60. EVUTIL_ASSERT(ev != NULL);
  61. evcb_callback = *ev->ev_callback;
  62. EVBASE_RELEASE_LOCK(base, th_base_lock);
  63. evcb_callback(ev->ev_fd, ev->ev_res, ev->ev_arg);
  64. }
  65. break;
  66. case EV_CLOSURE_CB_SELF: {
  67. void (*evcb_selfcb)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_selfcb;
  68. EVBASE_RELEASE_LOCK(base, th_base_lock);
  69. evcb_selfcb(evcb, evcb->evcb_arg);
  70. }
  71. break;
  72. case EV_CLOSURE_EVENT_FINALIZE:
  73. case EV_CLOSURE_EVENT_FINALIZE_FREE: {
  74. void (*evcb_evfinalize)(struct event *, void *);
  75. int evcb_closure = evcb->evcb_closure;
  76. EVUTIL_ASSERT(ev != NULL);
  77. base->current_event = NULL;
  78. evcb_evfinalize = ev->ev_evcallback.evcb_cb_union.evcb_evfinalize;
  79. EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING));
  80. EVBASE_RELEASE_LOCK(base, th_base_lock);
  81. evcb_evfinalize(ev, ev->ev_arg);
  82. event_debug_note_teardown_(ev);
  83. if (evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE)
  84. mm_free(ev);
  85. }
  86. break;
  87. case EV_CLOSURE_CB_FINALIZE: {
  88. void (*evcb_cbfinalize)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_cbfinalize;
  89. base->current_event = NULL;
  90. EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING));
  91. EVBASE_RELEASE_LOCK(base, th_base_lock);
  92. evcb_cbfinalize(evcb, evcb->evcb_arg);
  93. }
  94. break;
  95. default:
  96. EVUTIL_ASSERT();
  97. }
  98.  
  99. EVBASE_ACQUIRE_LOCK(base, th_base_lock);
  100. base->current_event = NULL;
  101. #ifndef EVENT__DISABLE_THREAD_SUPPORT
  102. if (base->current_event_waiters) {
  103. base->current_event_waiters = ;
  104. EVTHREAD_COND_BROADCAST(base->current_event_cond);
  105. }
  106. #endif
  107.  
  108. if (base->event_break)
  109. return -;
  110. if (count >= max_to_process)
  111. return count;
  112. if (count && endtime) {
  113. struct timeval now;
  114. update_time_cache(base);
  115. gettime(base, &now);
  116. if (evutil_timercmp(&now, endtime, >=))
  117. return count;
  118. }
  119. if (base->event_continue)
  120. break;
  121. }
  122. return count;
  123. }

(1)

libevent源码分析(一)的更多相关文章

  1. 【转】libevent源码分析

    libevent源码分析 转自:http://www.cnblogs.com/hustcat/archive/2010/08/31/1814022.html 这两天没事,看了一下Memcached和l ...

  2. Libevent源码分析 (1) hello-world

    Libevent源码分析 (1) hello-world ⑨月份接触了久闻大名的libevent,当时想读读源码,可是由于事情比较多一直没有时间,现在手头的东西基本告一段落了,我准备读读libeven ...

  3. Libevent源码分析系列【转】

    转自:https://www.cnblogs.com/zxiner/p/6919021.html 1.使用libevent库     源码那么多,该怎么分析从哪分析呢?一个好的方法就是先用起来,会用了 ...

  4. Libevent源码分析系列

    1.使用libevent库     源码那么多,该怎么分析从哪分析呢?一个好的方法就是先用起来,会用了,然后去看底层相应的源码,这样比较有条理,自上向下掌握.下面用libevent库写个程序,每隔1秒 ...

  5. libevent源码分析

    这两天没事,看了一下Memcached和libevent的源码,做个小总结. 1.入门 1.1.概述Libevent是一个用于开发可扩展性网络服务器的基于事件驱动(event-driven)模型的网络 ...

  6. libevent源码分析二--timeout事件响应

    libevent不仅支持io事件,同时还支持timeout事件与signal事件,这篇文件将分析libevent是如何组织timeout事件以及如何响应timeout事件. 1.  min_heap ...

  7. libevent源码分析一--io事件响应

    这篇文章将分析libevent如何组织io事件,如何捕捉事件的发生并进行相应的响应.这里不会详细分析event与event_base的细节,仅描述io事件如何存储与如何响应. 1.  select l ...

  8. Libevent源码分析—event_init()

    下面开始看初始化event_base结构的相关函数.相关源码位于event.c event_init() 首先调用event_init()初始化event_base结构体 struct event_b ...

  9. Libevent源码分析—event, event_base

    event和event_base是libevent的两个核心结构体,分别是反应堆模式中的Event和Reactor.源码分别位于event.h和event-internal.h中 1.event: s ...

  10. Libevent源码分析—event_add()

    接下来就是将已经初始化的event注册到libevent的事件链表上,通过event_add()来实现,源码位于event.c中. event_add() 这个函数主要完成了下面几件事: 1.将eve ...

随机推荐

  1. WOFF mime类型

    WOFF fonts,国外网站很多调用了.woff字体文件,IIS默认不支持,所以会报错404,只需要添加扩展MIME类型mime类型是:application/x-font-woff.

  2. vs2013打开 2010项目时: 请确认 <Import> 声明中的路径正确,且磁盘上存在该文件

    错误原因:原来2010项目中使用的路径是 v11.0,但是我的 C:\Program Files (x86)\MSBuild\12.0\ 是 12.0,所以该成 12.0 解决办法:将项目文件(.cs ...

  3. SQL语句判断是否为今天或昨天

    方法一 select * from AAA where to_char(a,'yyyymmdd') = to_char(sysdate,'yyyymmdd'); select * from AAA w ...

  4. mybatis动态SQL语句

    一 if标签 ? 1 2 3 4 5 6 <select id=" getStudentListLikeName " parameterType="StudentE ...

  5. mysql时间格式化,按时间段查询MYSQL语句

    描述:有一个会员表,有个birthday字段,值为'YYYY-MM-DD'格式,现在要查询一个时间段内过生日的会员,比如'06-03'到'07-08'这个时间段内所有过生日的会员. SQL语句: Se ...

  6. 平方和和立方和_hdu2007

    #include <stdio.h>int main(){ int a, b, m , n, t;  while( scanf("%d %d", &a, &am ...

  7. BizTalk动手实验(八)消息路由

    1 课程简介 通过本课程熟悉BizTalk消息由的机制 2 准备工作 1. 熟悉XML.XML Schema.XSLT等相关XML开发技术 2. 新建BizTalk空项目 3 演示 3.1 动态消息路 ...

  8. BizTalk开发系列(十一) 在Orchestration中执行Pipeline

    由于开发需要有时要在流程中执行Pipeline.比如从DB的某个字段中取消息的字符串并在流程中构造消息.该需要通过pipeline进行升级 属性字段,验证消息等处理.BizTalk架构已经开放了此接口 ...

  9. 用session实现简单的购物

    package cn.itcast.shopping; import java.io.IOException; import java.io.PrintWriter; import java.util ...

  10. Nosql学习笔记

    1.利用Query查询,Query操作只搜索主键属性值,并支持对键属性值使用部分比较运算符,以优化搜索过程. * 查询结果始终按范围键排序.如果范围键的数据类型是数字,则会按数字顺序返回结果:否则,会 ...