aboutsummaryrefslogtreecommitdiffstats
path: root/src/thread/PosixCond.hxx
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-04-17 01:33:07 +0200
committerMax Kellermann <max@duempel.org>2013-04-17 01:33:07 +0200
commitc5c43c4541f4434b4c5a5bc5914d6bc08f75ea94 (patch)
tree1705020ab31359e019b89c75cab740dbfd5f9511 /src/thread/PosixCond.hxx
parent0954f580fa62ce52bb63739e7624f8b3f97546a6 (diff)
downloadmpd-c5c43c4541f4434b4c5a5bc5914d6bc08f75ea94.tar.gz
mpd-c5c43c4541f4434b4c5a5bc5914d6bc08f75ea94.tar.xz
mpd-c5c43c4541f4434b4c5a5bc5914d6bc08f75ea94.zip
thread/Cond: add method timed_wait()
Diffstat (limited to 'src/thread/PosixCond.hxx')
-rw-r--r--src/thread/PosixCond.hxx13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/thread/PosixCond.hxx b/src/thread/PosixCond.hxx
index acdc05edc..6f98d3ad0 100644
--- a/src/thread/PosixCond.hxx
+++ b/src/thread/PosixCond.hxx
@@ -32,6 +32,8 @@
#include "PosixMutex.hxx"
+#include <sys/time.h>
+
/**
* Low-level wrapper for a pthread_cond_t.
*/
@@ -55,6 +57,17 @@ public:
void wait(PosixMutex &mutex) {
pthread_cond_wait(&cond, &mutex.mutex);
}
+
+ bool timed_wait(PosixMutex &mutex, unsigned timeout_ms) {
+ struct timeval now;
+ gettimeofday(&now, nullptr);
+
+ struct timespec ts;
+ ts.tv_sec = now.tv_sec + timeout_ms / 1000;
+ ts.tv_nsec = (now.tv_usec + (timeout_ms % 1000) * 1000) * 1000;
+
+ return pthread_cond_timedwait(&cond, &mutex.mutex, &ts) == 0;
+ }
};
#endif