| 633 | } |
| 634 | |
| 635 | extern "C" void ProcessEventCore(aeEventLoop *eventLoop, aeFileEvent *fe, int mask, int fd) |
| 636 | { |
| 637 | #define LOCK_IF_NECESSARY(fe, tsmask) \ |
| 638 | std::unique_lock<decltype(g_lock)> ulock(g_lock, std::defer_lock); \ |
| 639 | if (!(fe->mask & tsmask)) { \ |
| 640 | g_forkLock.releaseRead(); \ |
| 641 | ulock.lock(); \ |
| 642 | g_forkLock.acquireRead(); \ |
| 643 | } |
| 644 | |
| 645 | int fired = 0; /* Number of events fired for current fd. */ |
| 646 | |
| 647 | /* Normally we execute the readable event first, and the writable |
| 648 | * event laster. This is useful as sometimes we may be able |
| 649 | * to serve the reply of a query immediately after processing the |
| 650 | * query. |
| 651 | * |
| 652 | * However if AE_BARRIER is set in the mask, our application is |
| 653 | * asking us to do the reverse: never fire the writable event |
| 654 | * after the readable. In such a case, we invert the calls. |
| 655 | * This is useful when, for instance, we want to do things |
| 656 | * in the beforeSleep() hook, like fsynching a file to disk, |
| 657 | * before replying to a client. */ |
| 658 | int invert = fe->mask & AE_BARRIER; |
| 659 | |
| 660 | /* Note the "fe->mask & mask & ..." code: maybe an already |
| 661 | * processed event removed an element that fired and we still |
| 662 | * didn't processed, so we check if the event is still valid. |
| 663 | * |
| 664 | * Fire the readable event if the call sequence is not |
| 665 | * inverted. */ |
| 666 | if (!invert && fe->mask & mask & AE_READABLE) { |
| 667 | LOCK_IF_NECESSARY(fe, AE_READ_THREADSAFE); |
| 668 | fe->rfileProc(eventLoop,fd,fe->clientData,mask | (fe->mask & AE_READ_THREADSAFE)); |
| 669 | fired++; |
| 670 | fe = &eventLoop->events[fd]; /* Refresh in case of resize. */ |
| 671 | } |
| 672 | |
| 673 | /* Fire the writable event. */ |
| 674 | if (fe->mask & mask & AE_WRITABLE) { |
| 675 | if (!fired || fe->wfileProc != fe->rfileProc) { |
| 676 | LOCK_IF_NECESSARY(fe, AE_WRITE_THREADSAFE); |
| 677 | fe->wfileProc(eventLoop,fd,fe->clientData,mask | (fe->mask & AE_WRITE_THREADSAFE)); |
| 678 | fired++; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | /* If we have to invert the call, fire the readable event now |
| 683 | * after the writable one. */ |
| 684 | if (invert && fe->mask & mask & AE_READABLE) { |
| 685 | if (!fired || fe->wfileProc != fe->rfileProc) { |
| 686 | LOCK_IF_NECESSARY(fe, AE_READ_THREADSAFE); |
| 687 | fe->rfileProc(eventLoop,fd,fe->clientData,mask | (fe->mask & AE_READ_THREADSAFE)); |
| 688 | fired++; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | #undef LOCK_IF_NECESSARY |