搜尋

首頁  >  問答  >  主體

php - libevent裡event_* 函式使用了那種事件模型?

知道libevent支援 select/poll/epoll時間模型。 今天突然想到 封裝了libevent底層運算的 event_* 系列函式是使用了那種事件模型呢

世界只因有你世界只因有你2843 天前529

全部回覆(1)我來回復

  • 滿天的星座

    滿天的星座2017-05-16 13:08:51

    <event.c>
    
    #ifdef HAVE_EVENT_PORTS
    extern const struct eventop evportops;
    #endif
    #ifdef HAVE_SELECT
    extern const struct eventop selectops;
    #endif
    #ifdef HAVE_POLL
    extern const struct eventop pollops;
    #endif
    #ifdef HAVE_EPOLL
    extern const struct eventop epollops;
    #endif
    #ifdef HAVE_WORKING_KQUEUE
    extern const struct eventop kqops;
    #endif
    #ifdef HAVE_DEVPOLL
    extern const struct eventop devpollops;
    #endif
    #ifdef WIN32
    extern const struct eventop win32ops;
    #endif
    
    /* In order of preference */
    static const struct eventop *eventops[] = {
    #ifdef HAVE_EVENT_PORTS
        &evportops,
    #endif
    #ifdef HAVE_WORKING_KQUEUE
        &kqops,
    #endif
    #ifdef HAVE_EPOLL
        &epollops,
    #endif
    #ifdef HAVE_DEVPOLL
        &devpollops,
    #endif
    #ifdef HAVE_POLL
        &pollops,
    #endif
    #ifdef HAVE_SELECT
        &selectops,
    #endif
    #ifdef WIN32
        &win32ops,
    #endif
        NULL
    };
    

    依照上面的順序,取第一個os支援的介面,如下初始化的過程:

    <event.c>
    
    struct event_base *
    event_base_new(void)
    {
    ...    
        base->evbase = NULL;
        for (i = 0; eventops[i] && !base->evbase; i++) {
            base->evsel = eventops[i];
    
            base->evbase = base->evsel->init(base);
        }
    
        if (base->evbase == NULL)
            event_errx(1, "%s: no event mechanism available", __func__);
    
        ...
    }

    下面是epoll的eventop封裝

    epoll.c
    
    const struct eventop epollops = {
        "epoll",
        epoll_init,
        epoll_add,
        epoll_del,
        epoll_dispatch,
        epoll_dealloc,
        1 /* need reinit */
    };
    

    回覆
    0
  • 取消回覆