static int evsig_add(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p) { struct evsig_info *sig = &base->sig; (void)p; EVUTIL_ASSERT(evsignal >= 0 && evsignal < NSIG); /* catch signals if they happen quickly */ EVSIGBASE_LOCK(); if (evsig_base != base && evsig_base_n_signals_added) { event_warnx("Added a signal to event base %p with signals " "already added to event_base %p. Only one can have " "signals at a time with the %s backend. The base with " "the most recently added signal or the most recent " "event_base_loop() call gets preference; do " "not rely on this behavior in future Libevent versions.", base, evsig_base, base->evsel->name); } evsig_base = base; evsig_base_n_signals_added = ++sig->ev_n_signals_added; evsig_base_fd = base->sig.ev_signal_pair[1]; EVSIGBASE_UNLOCK(); event_debug(("%s: %d: changing signal handler", __func__, (int)evsignal)); if (evsig_set_handler_(base, (int)evsignal, evsig_handler) == -1) { goto err; } if (!sig->ev_signal_added) { if (event_add(&sig->ev_signal, NULL)) goto err; sig->ev_signal_added = 1; } return (0); err: EVSIGBASE_LOCK(); --evsig_base_n_signals_added; --sig->ev_n_signals_added; EVSIGBASE_UNLOCK(); return (-1); }
void evsig_set_base_(struct event_base *base) { EVSIGBASE_LOCK(); evsig_base = base; evsig_base_n_signals_added = base->sig.ev_n_signals_added; evsig_base_fd = base->sig.ev_signal_pair[1]; EVSIGBASE_UNLOCK(); }
void evsig_set_base(struct event_base *base) { EVSIGBASE_LOCK(); evsig_base = base; evsig_base_n_signals_added = base->sig.ev_n_signals_added; evsig_base_fd = base->sig.ev_signal_pair[0];//0用来信号处理函数发送给base,1已用base接收信号 EVSIGBASE_UNLOCK(); }
static int evsig_del(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p) { EVUTIL_ASSERT(evsignal >= 0 && evsignal < NSIG); event_debug(("%s: %d: restoring signal handler", __func__, evsignal)); EVSIGBASE_LOCK(); --evsig_base_n_signals_added; --base->sig.ev_n_signals_added; EVSIGBASE_UNLOCK(); return (evsig_restore_handler_(base, (int)evsignal)); }
void evsig_dealloc(struct event_base *base) { int i = 0; if (base->sig.ev_signal_added) { event_del(&base->sig.ev_signal);//删除信号事件 base->sig.ev_signal_added = 0; } /* debug event is created in evsig_init/event_assign even when * ev_signal_added == 0, so unassign is required */ event_debug_unassign(&base->sig.ev_signal); //恢复所有的信号处理函数 for (i = 0; i < NSIG; ++i) { if (i < base->sig.sh_old_max && base->sig.sh_old[i] != NULL) _evsig_restore_handler(base, i); } EVSIGBASE_LOCK(); if (base == evsig_base) { evsig_base = NULL; evsig_base_n_signals_added = 0; evsig_base_fd = -1; } EVSIGBASE_UNLOCK(); //关闭socketpair描述府 if (base->sig.ev_signal_pair[0] != -1) { evutil_closesocket(base->sig.ev_signal_pair[0]); base->sig.ev_signal_pair[0] = -1; } if (base->sig.ev_signal_pair[1] != -1) { evutil_closesocket(base->sig.ev_signal_pair[1]); base->sig.ev_signal_pair[1] = -1; } base->sig.sh_old_max = 0; /* per index frees are handled in evsig_del() */ if (base->sig.sh_old) { mm_free(base->sig.sh_old); base->sig.sh_old = NULL; } }
void evsig_dealloc(struct event_base *base) { int i = 0; if (base->sig.ev_signal_added) { event_del(&base->sig.ev_signal); event_debug_unassign(&base->sig.ev_signal); base->sig.ev_signal_added = 0; } for (i = 0; i < NSIG; ++i) { if (i < base->sig.sh_old_max && base->sig.sh_old[i] != NULL) _evsig_restore_handler(base, i); } EVSIGBASE_LOCK(); if (base == evsig_base) { evsig_base = NULL; evsig_base_n_signals_added = 0; evsig_base_fd = -1; } EVSIGBASE_UNLOCK(); if (base->sig.ev_signal_pair[0] != -1) { evutil_closesocket(base->sig.ev_signal_pair[0]); base->sig.ev_signal_pair[0] = -1; } if (base->sig.ev_signal_pair[1] != -1) { evutil_closesocket(base->sig.ev_signal_pair[1]); base->sig.ev_signal_pair[1] = -1; } base->sig.sh_old_max = 0; /* per index frees are handled in evsig_del() */ if (base->sig.sh_old) { mm_free(base->sig.sh_old); base->sig.sh_old = NULL; } }