summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qthread_unix.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-08-12 12:54:11 +0200
committerQt by Nokia <qt-info@nokia.com>2012-08-25 05:41:14 +0200
commitcaa22ff8ad9ee85dadf1620a9be24c1d555c1973 (patch)
tree3a569e86e367b222e4cc82697dd4a73d859c86d3 /src/corelib/thread/qthread_unix.cpp
parent88c7c35b21df302f3d3124ce26e79ec6429d3bde (diff)
Use nanosleep instead of pthread_cond_timedwait for thread sleeping
There's a comment saying nanosleep's availability is questionable, but the information of what systems don't have that is now lost in time. It's quite likely that they were older, Unix systems we no longer support anyway. nanosleep comes from POSIX.1b-1993, which is merged into POSIX.1-2001, so chances are that it's supported almost everywhere where Qt runs (except for Windows anyway). Change-Id: I4fd18f8715c43a42429000f3b3d2c3b7343f94b4 Reviewed-by: Lars Knoll <lars.knoll@nokia.com> Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
Diffstat (limited to 'src/corelib/thread/qthread_unix.cpp')
-rw-r--r--src/corelib/thread/qthread_unix.cpp48
1 files changed, 8 insertions, 40 deletions
diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp
index f472dee56e..f6df15f7a7 100644
--- a/src/corelib/thread/qthread_unix.cpp
+++ b/src/corelib/thread/qthread_unix.cpp
@@ -454,59 +454,27 @@ void QThread::yieldCurrentThread()
sched_yield();
}
-/*
- \internal
- helper function to do thread sleeps, since usleep()/nanosleep()
- aren't reliable enough (in terms of behavior and availability)
-*/
-static void thread_sleep(struct timespec *ti)
+static timespec makeTimespec(time_t secs, long nsecs)
{
- pthread_mutex_t mtx;
- pthread_cond_t cnd;
-
- pthread_mutex_init(&mtx, 0);
- pthread_cond_init(&cnd, 0);
-
- pthread_mutex_lock(&mtx);
- (void) pthread_cond_timedwait(&cnd, &mtx, ti);
- pthread_mutex_unlock(&mtx);
-
- pthread_cond_destroy(&cnd);
- pthread_mutex_destroy(&mtx);
+ struct timespec ts;
+ ts.tv_sec = secs;
+ ts.tv_nsec = nsecs;
+ return ts;
}
void QThread::sleep(unsigned long secs)
{
- struct timeval tv;
- gettimeofday(&tv, 0);
- struct timespec ti;
- ti.tv_sec = tv.tv_sec + secs;
- ti.tv_nsec = (tv.tv_usec * 1000);
- thread_sleep(&ti);
+ qt_nanosleep(makeTimespec(secs, 0));
}
void QThread::msleep(unsigned long msecs)
{
- struct timeval tv;
- gettimeofday(&tv, 0);
- struct timespec ti;
-
- ti.tv_nsec = (tv.tv_usec + (msecs % 1000) * 1000) * 1000;
- ti.tv_sec = tv.tv_sec + (msecs / 1000) + (ti.tv_nsec / 1000000000);
- ti.tv_nsec %= 1000000000;
- thread_sleep(&ti);
+ qt_nanosleep(makeTimespec(msecs / 1000, msecs % 1000 * 1000 * 1000));
}
void QThread::usleep(unsigned long usecs)
{
- struct timeval tv;
- gettimeofday(&tv, 0);
- struct timespec ti;
-
- ti.tv_nsec = (tv.tv_usec + (usecs % 1000000)) * 1000;
- ti.tv_sec = tv.tv_sec + (usecs / 1000000) + (ti.tv_nsec / 1000000000);
- ti.tv_nsec %= 1000000000;
- thread_sleep(&ti);
+ qt_nanosleep(makeTimespec(usecs / 1000 / 1000, usecs % (1000*1000) * 1000));
}
#ifdef QT_HAS_THREAD_PRIORITY_SCHEDULING