summaryrefslogtreecommitdiff
path: root/src/common/compat_pthreads.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2013-09-22 22:08:41 -0400
committerNick Mathewson <nickm@torproject.org>2015-01-14 10:47:39 -0500
commite865248156a8512d756be003118de446d29611d1 (patch)
treea19cf3d0fe7bd1bb700309f138b2480b6ed596af /src/common/compat_pthreads.c
parentc2f0d52b7fb937f3f66ef8b2b74b0fdf239b0e9b (diff)
downloadtor-e865248156a8512d756be003118de446d29611d1.tar.gz
tor-e865248156a8512d756be003118de446d29611d1.zip
Add a timeout to tor_cond_wait; add tor_cond impl from libevent
The windows code may need some tweaks for it to compile; I've not tested it yet.
Diffstat (limited to 'src/common/compat_pthreads.c')
-rw-r--r--src/common/compat_pthreads.c24
1 files changed, 17 insertions, 7 deletions
diff --git a/src/common/compat_pthreads.c b/src/common/compat_pthreads.c
index 276b2443c4..0e5d33a659 100644
--- a/src/common/compat_pthreads.c
+++ b/src/common/compat_pthreads.c
@@ -148,10 +148,6 @@ tor_get_thread_id(void)
/* Conditions. */
-/** Cross-platform condition implementation. */
-struct tor_cond_t {
- pthread_cond_t cond;
-};
/** Return a newly allocated condition, with nobody waiting on it. */
tor_cond_t *
tor_cond_new(void)
@@ -177,11 +173,25 @@ tor_cond_free(tor_cond_t *cond)
}
/** Wait until one of the tor_cond_signal functions is called on <b>cond</b>.
* All waiters on the condition must wait holding the same <b>mutex</b>.
- * Returns 0 on success, negative on failure. */
+ * Returns 0 on success, -1 on failure, 1 on timeout. */
int
-tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex)
+tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv)
{
- return pthread_cond_wait(&cond->cond, &mutex->mutex) ? -1 : 0;
+ if (tv == NULL) {
+ return pthread_cond_wait(&cond->cond, &mutex->mutex) ? -1 : 0;
+ } else {
+ struct timespec ts;
+ int r;
+ ts.tv_sec = tv->tv_sec;
+ ts.tv_nsec = tv->tv_usec * 1000;
+ r = pthread_cond_timedwait(&cond->cond, &mutex->mutex, &ts);
+ if (r == 0)
+ return 0;
+ else if (r == ETIMEDOUT)
+ return 1;
+ else
+ return -1;
+ }
}
/** Wake up one of the waiters on <b>cond</b>. */
void