summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-10-25 12:38:13 -0700
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-01-15 03:29:29 +0100
commitf927efd77a8e75e330b3592996277ad6e6eb6a8d (patch)
treeb7226a2eb337446a9a256b28fae469d9847b5b3b /src
parentdbfa6518890f4e6c4af6da92df45803ecfe0309a (diff)
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 <robin+qt@viroteck.net> Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/kernel/qeventdispatcher_unix.cpp37
-rw-r--r--src/corelib/kernel/qeventdispatcher_unix_p.h3
2 files changed, 36 insertions, 4 deletions
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 <stdio.h>
#include <stdlib.h>
+#ifndef QT_NO_EVENTFD
+# include <sys/eventfd.h>
+#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