您的位置:首页 > 其它

3.7.3 event_enable_read和event_enable_write:挂载回调函数

2016-04-07 09:54 176 查看
这两个函数将指定文件描述符的read和write事件相应的回调函数挂载到EVENT_FDTABLE结构体的callback字段中,下面以event_enable_read为例来介绍:

/util/event.c
729 void   event_enable_read(int fd, EVENT_NOTIFY_RDWR_FN callback, void *context)
730 {
731    const char *myname = "event_enable_read";
732    EVENT_FDTABLE *fdp;
733    int     err;
734
735    if (EVENT_INIT_NEEDED())
736        event_init();


735-736没有运行event_init的话运行event_init。

737
738    /*
739     * Sanity checks.
740     */
741    if (fd < 0 || fd >= event_fdlimit)
742        msg_panic("%s: bad file descriptor: %d", myname, fd);


741-742 验证fd合规性,小于0或超过event_fdlimit限制程序退出。postfix的代码中遍布着“Sanitychecks”。

743
744    if (msg_verbose > 2)
745        msg_info("%s: fd %d", myname, fd);
746
747    if (fd >= event_fdslots)
748        event_extend(fd);


747-748 如果fd大于现有EVENT_FDTABLE数组个数,对其扩容。event_extend使用myremalloc函数对EVENT_FDTABLE数组扩容。新数组的个数按如下原则设置:如果现有的event_fdslots数目大于要监听的fd数目的一半,则将新的event_fdslots数目设置为原数目两倍,否则将新的event_fdslots数目数目增加EVENT_ALLOC_INCR。

749
750    /*
751     * Disallow mixed (i.e. read and write) requests on the same descriptor.
752     */
753    if (EVENT_MASK_ISSET(fd, &event_wmask))
754        msg_panic("%s: fd %d: read/write I/O request", myname, fd);


753-754 该fd不能在写文件描述符组中。同样,对于event_enable_write,则fd不能在读文件描述符组中。

755
756    /*
757     * Postfix 2.4 allows multiple event_enable_read() calls on the same
758     * descriptor without requiring event_disable_readwrite() calls between
759     * them. With kernel-based filters (kqueue, /dev/poll, epoll) it's
760     * wasteful to make system calls when we change only application
761     * call-back information. It has a noticeable effect on smtp-source
762     * performance.
763     */
764    if (EVENT_MASK_ISSET(fd, &event_rmask) == 0) {
765        EVENT_MASK_SET(fd, &event_xmask);
766        EVENT_MASK_SET(fd, &event_rmask);
767         if (event_max_fd < fd)
768            event_max_fd = fd;


764-768 如果文件描述符不在event_rmask中,将其加入到event_rmask和event_xmask中,视情况重置event_max_fd。

769 #if (EVENTS_STYLE != EVENTS_STYLE_SELECT)
770        EVENT_REG_ADD_READ(err, fd);
771        if (err < 0)
772             msg_fatal("%s: %s: %m",myname, EVENT_REG_ADD_TEXT);
773 #endif
774    }
775    fdp = event_fdtable + fd;
776    if (fdp->callback != callback || fdp->context != context) {
777        fdp->callback = callback;
778        fdp->context = context;
779    }


775-778 将EVENT_FDTABLE数组中EVENT_FDTABLE成员的回调函数字段设置为相应的回调函数,上下文结构体字段设置为相应的上下文结构体。EVENT_FDTABLE结构体按fd索引。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: