Skip to content

Commit

Permalink
Use tinycthread on all platforms
Browse files Browse the repository at this point in the history
This also changes the function names in tinycthread so that they don't
conflict with (and inadverantly link to) functions with the same name
from C11-style threads.h.
  • Loading branch information
wch committed Jan 3, 2019
1 parent 2fb214b commit 772622d
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 226 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
*.so
*.dll
inst/doc
src/Makevars
45 changes: 0 additions & 45 deletions configure

This file was deleted.

6 changes: 2 additions & 4 deletions src/Makevars.in → src/Makevars
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
PKG_CPPFLAGS = -pthread @cppflags@
PKG_LIBS = -pthread @libs@
PKG_CPPFLAGS = -pthread
PKG_LIBS = -pthread

# Uncomment to enable thread assertions
# PKG_CPPFLAGS += -DDEBUG_THREAD -UNDEBUG

$(SHLIB): @libs@
7 changes: 0 additions & 7 deletions src/Makevars.win

This file was deleted.

16 changes: 0 additions & 16 deletions src/c11threads.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/callback_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void testCallbackOrdering() {
}
}

CallbackRegistry::CallbackRegistry() : mutex(mtx_recursive), condvar(mutex) {
CallbackRegistry::CallbackRegistry() : mutex(tct_mtx_recursive), condvar(mutex) {
}

void CallbackRegistry::add(Rcpp::Function func, double secs) {
Expand Down
2 changes: 1 addition & 1 deletion src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// See the Makevars file to see how to compile with various debugging settings.

#if defined(DEBUG_THREAD)
#include "c11threads.h"
#include "tinycthread.h"

extern thrd_t __main_thread__;
extern thrd_t __background_thread__;
Expand Down
2 changes: 1 addition & 1 deletion src/later_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int dummy_pipe_in, dummy_pipe_out;
bool hot = false;
// This mutex protects reading/writing of `hot` and of reading from/writing to
// the pipe.
Mutex m(mtx_plain);
Mutex m(tct_mtx_plain);

// The buffer we're using for the pipe. This doesn't have to be large,
// in theory it only ever holds zero or one byte.
Expand Down
38 changes: 19 additions & 19 deletions src/threadutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_signed.hpp>

#include "c11threads.h"
#include "tinycthread.h"
#include "timeconv.h"

class ConditionVariable;

class Mutex : boost::noncopyable {
friend class ConditionVariable;
mtx_t _m;
tct_mtx_t _m;

public:
// type must be one of:
Expand All @@ -28,34 +28,34 @@ class Mutex : boost::noncopyable {
//
// (although mtx_timed seems not to be actually implemented)
Mutex(int type) {
if (mtx_init(&_m, type) != thrd_success) {
if (tct_mtx_init(&_m, type) != tct_thrd_success) {
throw std::runtime_error("Mutex creation failed");
}
}

virtual ~Mutex() {
mtx_destroy(&_m);
tct_mtx_destroy(&_m);
}

void lock() {
if (mtx_lock(&_m) != thrd_success) {
if (tct_mtx_lock(&_m) != tct_thrd_success) {
throw std::runtime_error("Mutex failed to lock");
}
}

bool tryLock() {
int res = mtx_trylock(&_m);
if (res == thrd_success) {
int res = tct_mtx_trylock(&_m);
if (res == tct_thrd_success) {
return true;
} else if (res == thrd_busy) {
} else if (res == tct_thrd_busy) {
return false;
} else {
throw std::runtime_error("Mutex failed to trylock");
}
}

void unlock() {
if (mtx_unlock(&_m) != thrd_success) {
if (tct_mtx_unlock(&_m) != tct_thrd_success) {
throw std::runtime_error("Mutex failed to unlock");
}
}
Expand All @@ -75,8 +75,8 @@ class Guard : boost::noncopyable {
};

class ConditionVariable : boost::noncopyable {
mtx_t* _m;
cnd_t _c;
tct_mtx_t* _m;
tct_cnd_t _c;

public:
ConditionVariable(Mutex& mutex) : _m(&mutex._m) {
Expand All @@ -89,28 +89,28 @@ class ConditionVariable : boost::noncopyable {
if (!boost::is_signed<time_t>::value)
throw std::runtime_error("Signed time_t type expected");

if (cnd_init(&_c) != thrd_success)
if (tct_cnd_init(&_c) != tct_thrd_success)
throw std::runtime_error("Condition variable failed to initialize");
}

virtual ~ConditionVariable() {
cnd_destroy(&_c);
tct_cnd_destroy(&_c);
}

// Unblocks one thread (if any are waiting)
void signal() {
if (cnd_signal(&_c) != thrd_success)
if (tct_cnd_signal(&_c) != tct_thrd_success)
throw std::runtime_error("Condition variable failed to signal");
}

// Unblocks all waiting threads
void broadcast() {
if (cnd_broadcast(&_c) != thrd_success)
if (tct_cnd_broadcast(&_c) != tct_thrd_success)
throw std::runtime_error("Condition variable failed to broadcast");
}

void wait() {
if (cnd_wait(&_c, _m) != thrd_success)
if (tct_cnd_wait(&_c, _m) != tct_thrd_success)
throw std::runtime_error("Condition variable failed to wait");
}

Expand All @@ -122,10 +122,10 @@ class ConditionVariable : boost::noncopyable {

ts = addSeconds(ts, timeoutSecs);

int res = cnd_timedwait(&_c, _m, &ts);
if (res == thrd_success) {
int res = tct_cnd_timedwait(&_c, _m, &ts);
if (res == tct_thrd_success) {
return true;
} else if (res == thrd_timedout) {
} else if (res == tct_thrd_timedout) {
return false;
} else {
throw std::runtime_error("Condition variable failed to timedwait");
Expand Down
2 changes: 1 addition & 1 deletion src/timeconv.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// tinycthread.h to provide timespec. Whether tinycthread
// defines timespec or not, we want it to be consistent for
// anyone who uses these functions.
#include "c11threads.h"
#include "tinycthread.h"

inline timespec timevalToTimespec(const timeval& tv) {
timespec ts;
Expand Down
Loading

0 comments on commit 772622d

Please sign in to comment.