Process every pending time event, then every pending file event * (that may be registered by time event callbacks just processed). * Without special flags the function sleeps until some file event * fires, or when the next time event occurs (if any). * * If flags is 0, the function does nothing and returns. * if flags has AE_ALL_EVENTS set, all the kind of events are processed. * if flags h
| 708 | * |
| 709 | * The function returns the number of events processed. */ |
| 710 | int aeProcessEvents(aeEventLoop *eventLoop, int flags) |
| 711 | { |
| 712 | serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop); |
| 713 | int processed = 0, numevents; |
| 714 | |
| 715 | /* Nothing to do? return ASAP */ |
| 716 | if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return 0; |
| 717 | |
| 718 | /* Note that we want to call select() even if there are no |
| 719 | * file events to process as long as we want to process time |
| 720 | * events, in order to sleep until the next time event is ready |
| 721 | * to fire. */ |
| 722 | if (eventLoop->maxfd != -1 || |
| 723 | ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) { |
| 724 | int j; |
| 725 | struct timeval tv, *tvp; |
| 726 | int64_t usUntilTimer = -1; |
| 727 | |
| 728 | if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT)) |
| 729 | usUntilTimer = usUntilEarliestTimer(eventLoop); |
| 730 | |
| 731 | if (usUntilTimer >= 0) { |
| 732 | tv.tv_sec = usUntilTimer / 1000000; |
| 733 | tv.tv_usec = usUntilTimer % 1000000; |
| 734 | tvp = &tv; |
| 735 | } else { |
| 736 | /* If we have to check for events but need to return |
| 737 | * ASAP because of AE_DONT_WAIT we need to set the timeout |
| 738 | * to zero */ |
| 739 | if (flags & AE_DONT_WAIT) { |
| 740 | tv.tv_sec = tv.tv_usec = 0; |
| 741 | tvp = &tv; |
| 742 | } else { |
| 743 | /* Otherwise we can block */ |
| 744 | tvp = NULL; /* wait forever */ |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | if (eventLoop->beforesleep != NULL && flags & AE_CALL_BEFORE_SLEEP) { |
| 749 | std::unique_lock<decltype(g_lock)> ulock(g_lock, std::defer_lock); |
| 750 | if (!(eventLoop->beforesleepFlags & AE_SLEEP_THREADSAFE)) { |
| 751 | g_forkLock.releaseRead(); |
| 752 | ulock.lock(); |
| 753 | g_forkLock.acquireRead(); |
| 754 | } |
| 755 | eventLoop->beforesleep(eventLoop); |
| 756 | } |
| 757 | |
| 758 | if (eventLoop->flags & AE_DONT_WAIT) { |
| 759 | tv.tv_sec = tv.tv_usec = 0; |
| 760 | tvp = &tv; |
| 761 | } |
| 762 | |
| 763 | /* Call the multiplexing API, will return only on timeout or when |
| 764 | * some event fires. */ |
| 765 | numevents = aeApiPoll(eventLoop, tvp); |
| 766 | |
| 767 | /* After sleep callback. */ |
no test coverage detected