Skip to content

Commit

Permalink
Merge pull request #54 from r-lib/joe/bugfix/emulated-timespec
Browse files Browse the repository at this point in the history
Fix build warning on macOS 10.11
  • Loading branch information
wch authored May 1, 2018
2 parents 0d35ef9 + 55aa347 commit f9b5766
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Fixed [issue #48](https://github.com/r-lib/later/issues/48): Occasional timedwait errors from later::run_now. Thanks, @vnijs! [PR #49](https://github.com/r-lib/later/pull/49)

* Fixed a build warning on OS X 10.11 and earlier. [PR #54](https://github.com/r-lib/later/pull/54)

## later 0.7.1

* Fixed [issue #39](https://github.com/r-lib/later/issues/39): Calling the C++ function `later::later()` from a different thread could cause an R GC event to occur on that thread, leading to memory corruption. [PR #40](https://github.com/r-lib/later/pull/40)
Expand Down
2 changes: 0 additions & 2 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
// See the Makevars file to see how to compile with various debugging settings.

#if defined(DEBUG_THREAD)
extern "C" {
#include "tinycthread.h"
}

extern thrd_t __main_thread__;
extern thrd_t __background_thread__;
Expand Down
11 changes: 8 additions & 3 deletions src/threadutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

#include <stdexcept>
#include <sys/time.h>
extern "C" {
#include "tinycthread.h"
}
#include <boost/noncopyable.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_signed.hpp>

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

#ifndef CLOCK_REALTIME
Expand Down Expand Up @@ -89,8 +88,14 @@ class ConditionVariable : boost::noncopyable {

public:
ConditionVariable(Mutex& mutex) : _m(&mutex._m) {
// If time_t isn't integral, our addSeconds logic needs to change,
// as it relies on casting to time_t being a truncation.
if (!boost::is_integral<time_t>::value)
throw std::runtime_error("Integral time_t type expected");
// If time_t isn't signed, our addSeconds logic can't handle
// negative values for secs.
if (!boost::is_signed<time_t>::value)
throw std::runtime_error("Signed time_t type expected");

if (cnd_init(&_c) != thrd_success)
throw std::runtime_error("Condition variable failed to initialize");
Expand Down
5 changes: 5 additions & 0 deletions src/timeconv.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include <sys/time.h>
// Some platforms (Win32, previously some Mac versions) use
// tinycthread.h to provide timespec. Whether tinycthread
// defines timespec or not, we want it to be consistent for
// anyone who uses these functions.
#include "tinycthread.h"

inline timespec timevalToTimespec(const timeval& tv) {
timespec ts;
Expand Down
22 changes: 19 additions & 3 deletions src/tinycthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ freely, subject to the following restrictions:
#ifndef _TINYCTHREAD_H_
#define _TINYCTHREAD_H_

#ifdef __cplusplus
extern "C" {
#endif

// jcheng 2017-11-03: _XOPEN_SOURCE 600 is necessary to prevent Solaris headers
// from complaining about the combination of C99 and _XOPEN_SOURCE <= 500. The
// error message starts with:
Expand Down Expand Up @@ -112,15 +116,23 @@ freely, subject to the following restrictions:
#endif
#endif

/* Workaround for missing clock_gettime (most Windows compilers, and macOS < 10.12 afaik) */
#if defined(_TTHREAD_WIN32_) || (defined(__APPLE__) && !defined(CLOCK_MONOTONIC))
#define _TTHREAD_EMULATE_CLOCK_GETTIME_
// jcheng 2018-04-30: This was in the _TTHREAD_EMULATE_CLOCK_GETTIME_
// block previously, but all macOS versions provide timespec, and
// overwriting it with _ttherad_timespec caused compilation errors
// when trying to call POSIX functions that expected the regular
// timespec.
#if defined(_TTHREAD_WIN32_)
/* Emulate struct timespec */
struct _ttherad_timespec {
time_t tv_sec;
long tv_nsec;
};
#define timespec _ttherad_timespec
#endif

/* Workaround for missing clock_gettime (most Windows compilers, and macOS < 10.12 afaik) */
#if defined(_TTHREAD_WIN32_) || (defined(__APPLE__) && !defined(CLOCK_MONOTONIC))
#define _TTHREAD_EMULATE_CLOCK_GETTIME_

/* Emulate clockid_t */
typedef int _tthread_clockid_t;
Expand Down Expand Up @@ -442,5 +454,9 @@ void *tss_get(tss_t key);
int tss_set(tss_t key, void *val);


#ifdef __cplusplus
}
#endif

#endif /* _TINYTHREAD_H_ */

0 comments on commit f9b5766

Please sign in to comment.