summaryrefslogtreecommitdiff
path: root/src/common/compat_pthreads.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/compat_pthreads.c')
-rw-r--r--src/common/compat_pthreads.c56
1 files changed, 34 insertions, 22 deletions
diff --git a/src/common/compat_pthreads.c b/src/common/compat_pthreads.c
index 962b5fc0e4..c1ae66c1d2 100644
--- a/src/common/compat_pthreads.c
+++ b/src/common/compat_pthreads.c
@@ -10,8 +10,6 @@
* functions.
*/
-#define _GNU_SOURCE
-
#include "orconfig.h"
#include <pthread.h>
#include <signal.h>
@@ -21,11 +19,6 @@
#include "torlog.h"
#include "util.h"
-#ifdef __APPLE__
-#undef CLOCK_MONOTONIC
-#undef HAVE_CLOCK_GETTIME
-#endif
-
/** Wraps a void (*)(void*) function and its argument so we can
* invoke them in a way pthreads would expect.
*/
@@ -109,11 +102,13 @@ void
tor_mutex_init(tor_mutex_t *mutex)
{
if (PREDICT_UNLIKELY(!threads_initialized))
- tor_threads_init();
+ tor_threads_init(); // LCOV_EXCL_LINE
const int err = pthread_mutex_init(&mutex->mutex, &attr_recursive);
if (PREDICT_UNLIKELY(err)) {
+ // LCOV_EXCL_START
log_err(LD_GENERAL, "Error %d creating a mutex.", err);
- tor_fragile_assert();
+ tor_assert_unreached();
+ // LCOV_EXCL_STOP
}
}
@@ -123,12 +118,14 @@ void
tor_mutex_init_nonrecursive(tor_mutex_t *mutex)
{
int err;
- if (PREDICT_UNLIKELY(!threads_initialized))
- tor_threads_init();
+ if (!threads_initialized)
+ tor_threads_init(); // LCOV_EXCL_LINE
err = pthread_mutex_init(&mutex->mutex, NULL);
if (PREDICT_UNLIKELY(err)) {
+ // LCOV_EXCL_START
log_err(LD_GENERAL, "Error %d creating a mutex.", err);
- tor_fragile_assert();
+ tor_assert_unreached();
+ // LCOV_EXCL_STOP
}
}
@@ -140,8 +137,10 @@ tor_mutex_acquire(tor_mutex_t *m)
tor_assert(m);
err = pthread_mutex_lock(&m->mutex);
if (PREDICT_UNLIKELY(err)) {
+ // LCOV_EXCL_START
log_err(LD_GENERAL, "Error %d locking a mutex.", err);
- tor_fragile_assert();
+ tor_assert_unreached();
+ // LCOV_EXCL_STOP
}
}
/** Release the lock <b>m</b> so another thread can have it. */
@@ -152,8 +151,10 @@ tor_mutex_release(tor_mutex_t *m)
tor_assert(m);
err = pthread_mutex_unlock(&m->mutex);
if (PREDICT_UNLIKELY(err)) {
+ // LCOV_EXCL_START
log_err(LD_GENERAL, "Error %d unlocking a mutex.", err);
- tor_fragile_assert();
+ tor_assert_unreached();
+ // LCOV_EXCL_STOP
}
}
/** Clean up the mutex <b>m</b> so that it no longer uses any system
@@ -166,8 +167,10 @@ tor_mutex_uninit(tor_mutex_t *m)
tor_assert(m);
err = pthread_mutex_destroy(&m->mutex);
if (PREDICT_UNLIKELY(err)) {
+ // LCOV_EXCL_START
log_err(LD_GENERAL, "Error %d destroying a mutex.", err);
- tor_fragile_assert();
+ tor_assert_unreached();
+ // LCOV_EXCL_STOP
}
}
/** Return an integer representing this thread. */
@@ -197,14 +200,21 @@ tor_cond_init(tor_cond_t *cond)
return -1;
}
-#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) \
- && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK)
+#if defined(HAVE_CLOCK_GETTIME)
+#if defined(CLOCK_MONOTONIC) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK)
/* Use monotonic time so when we timedwait() on it, any clock adjustment
* won't affect the timeout value. */
if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC)) {
return -1;
}
-#endif
+#define USE_COND_CLOCK CLOCK_MONOTONIC
+#else /* !defined HAVE_PTHREAD_CONDATTR_SETCLOCK */
+ /* On OSX Sierra, there is no pthread_condattr_setclock, so we are stuck
+ * with the realtime clock.
+ */
+#define USE_COND_CLOCK CLOCK_REALTIME
+#endif /* which clock to use */
+#endif /* HAVE_CLOCK_GETTIME */
if (pthread_cond_init(&cond->cond, &condattr)) {
return -1;
}
@@ -217,8 +227,10 @@ void
tor_cond_uninit(tor_cond_t *cond)
{
if (pthread_cond_destroy(&cond->cond)) {
+ // LCOV_EXCL_START
log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno));
return;
+ // LCOV_EXCL_STOP
}
}
/** Wait until one of the tor_cond_signal functions is called on <b>cond</b>.
@@ -239,7 +251,7 @@ tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv)
/* EINTR should be impossible according to POSIX, but POSIX, like the
* Pirate's Code, is apparently treated "more like what you'd call
* guidelines than actual rules." */
- continue;
+ continue; // LCOV_EXCL_LINE
}
return r ? -1 : 0;
}
@@ -247,12 +259,12 @@ tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv)
struct timeval tvnow, tvsum;
struct timespec ts;
while (1) {
-#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
- if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
+#if defined(HAVE_CLOCK_GETTIME) && defined(USE_COND_CLOCK)
+ if (clock_gettime(USE_COND_CLOCK, &ts) < 0) {
return -1;
}
tvnow.tv_sec = ts.tv_sec;
- tvnow.tv_usec = ts.tv_nsec / 1000;
+ tvnow.tv_usec = (int)(ts.tv_nsec / 1000);
timeradd(tv, &tvnow, &tvsum);
#else
if (gettimeofday(&tvnow, NULL) < 0)