From f927efd77a8e75e330b3592996277ad6e6eb6a8d Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 25 Oct 2012 12:38:13 -0700 Subject: Add support for Linux eventfd(7) in the UNIX event loop eventfd(7) uses less resources than a pipe, as it only needs to store a single 64-bit integer, as opposed to a full buffer. It was introduced first on Linux version 2.6.22 and glibc 2.7. However, both the configure-time test and the runtime usage require the use of EFD_CLOEXEC for thread-safety, so this code will be enabled only for Linux 2.6.27 and up as well as glibc 2.9 and up. Change-Id: Ic7e10b28d7b1d4ca24be614ed84055c4429a68e4 Reviewed-by: Robin Burchell Reviewed-by: Oswald Buddenhagen --- src/corelib/kernel/qeventdispatcher_unix.cpp | 37 +++++++++++++++++++++++++--- src/corelib/kernel/qeventdispatcher_unix_p.h | 3 +++ 2 files changed, 36 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/corelib/kernel/qeventdispatcher_unix.cpp b/src/corelib/kernel/qeventdispatcher_unix.cpp index 995f5101cb..6408147567 100644 --- a/src/corelib/kernel/qeventdispatcher_unix.cpp +++ b/src/corelib/kernel/qeventdispatcher_unix.cpp @@ -56,6 +56,10 @@ #include #include +#ifndef QT_NO_EVENTFD +# include +#endif + // VxWorks doesn't correctly set the _POSIX_... options #if defined(Q_OS_VXWORKS) # if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK <= 0) @@ -127,6 +131,12 @@ QEventDispatcherUNIXPrivate::QEventDispatcherUNIXPrivate() } } #else +# ifndef QT_NO_EVENTFD + thread_pipe[0] = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); + if (thread_pipe[0] != -1) + thread_pipe[1] = -1; + else // fall through the next "if" +# endif if (qt_safe_pipe(thread_pipe, O_NONBLOCK) == -1) { perror("QEventDispatcherUNIXPrivate(): Unable to create thread pipe"); pipefail = true; @@ -155,7 +165,8 @@ QEventDispatcherUNIXPrivate::~QEventDispatcherUNIXPrivate() #else // cleanup the common parts of the event loop close(thread_pipe[0]); - close(thread_pipe[1]); + if (thread_pipe[1] != -1) + close(thread_pipe[1]); #endif // cleanup timers @@ -279,9 +290,18 @@ int QEventDispatcherUNIXPrivate::processThreadWakeUp(int nsel) ::read(thread_pipe[0], c, sizeof(c)); ::ioctl(thread_pipe[0], FIOFLUSH, 0); #else - char c[16]; - while (::read(thread_pipe[0], c, sizeof(c)) > 0) - ; +# ifndef QT_NO_EVENTFD + if (thread_pipe[1] == -1) { + // eventfd + eventfd_t value; + eventfd_read(thread_pipe[0], &value); + } else +# endif + { + char c[16]; + while (::read(thread_pipe[0], c, sizeof(c)) > 0) { + } + } #endif if (!wakeUps.testAndSetRelease(1, 0)) { // hopefully, this is dead code @@ -630,6 +650,15 @@ void QEventDispatcherUNIX::wakeUp() { Q_D(QEventDispatcherUNIX); if (d->wakeUps.testAndSetAcquire(0, 1)) { +#ifndef QT_NO_EVENTFD + if (d->thread_pipe[1] == -1) { + // eventfd + eventfd_t value = 1; + int ret; + EINTR_LOOP(ret, eventfd_write(d->thread_pipe[0], value)); + return; + } +#endif char c = 0; qt_safe_write( d->thread_pipe[1], &c, 1 ); } diff --git a/src/corelib/kernel/qeventdispatcher_unix_p.h b/src/corelib/kernel/qeventdispatcher_unix_p.h index 98ea19ced8..e96acf65af 100644 --- a/src/corelib/kernel/qeventdispatcher_unix_p.h +++ b/src/corelib/kernel/qeventdispatcher_unix_p.h @@ -148,6 +148,9 @@ public: virtual int processThreadWakeUp(int nsel); bool mainThread; + + // note for eventfd(7) support: + // if thread_pipe[1] is -1, then eventfd(7) is in use and is stored in thread_pipe[0] int thread_pipe[2]; // highest fd for all socket notifiers -- cgit v1.2.3