#ifdef COMPILE_FOR_LINUX_AND_FREEBSD_AND_CYGWIN_AND_OPENBSD
struct timespec tn;
- // can't use CLOCK_MONOTONIC_RAW as it's not implemented in OpenWrt
- // CLOCK_REALTIME because PTP uses it.
+#ifdef CLOCK_MONOTONIC_RAW
+ clock_gettime(CLOCK_MONOTONIC_RAW, &tn);
+#else
clock_gettime(CLOCK_MONOTONIC, &tn);
+#endif
uint64_t tnnsec = tn.tv_sec;
tnnsec = tnnsec * 1000000000;
uint64_t tnjnsec = tn.tv_nsec;
// all these clock things are now in macOS now since 10.13 (September 2017). Must update...
uint64_t get_monotonic_raw_time_in_ns() {
- // CLOCK_MONOTONIC_RAW in FreeBSD etc, monotonic in MacOSX
+ // CLOCK_MONOTONIC_RAW/CLOCK_MONOTONIC in Linux/FreeBSD etc, monotonic in MacOSX
uint64_t time_now_ns;
#ifdef COMPILE_FOR_LINUX_AND_FREEBSD_AND_CYGWIN_AND_OPENBSD
struct timespec tn;
+#ifdef CLOCK_MONOTONIC_RAW
clock_gettime(CLOCK_MONOTONIC_RAW, &tn);
+#else
+ clock_gettime(CLOCK_MONOTONIC, &tn);
+#endif
uint64_t tnnsec = tn.tv_sec;
tnnsec = tnnsec * 1000000000;
uint64_t tnjnsec = tn.tv_nsec;
uint64_t time_now_ns;
struct timespec tn;
- // can't use CLOCK_MONOTONIC_RAW as it's not implemented in OpenWrt
- // CLOCK_REALTIME because PTP uses it.
- clock_gettime(CLOCK_REALTIME, &tn);
+#ifdef CLOCK_MONOTONIC_RAW
+ clock_gettime(CLOCK_MONOTONIC_RAW, &tn);
+#else
+ clock_gettime(CLOCK_MONOTONIC, &tn);
+#endif
uint64_t tnnsec = tn.tv_sec;
tnnsec = tnnsec * 1000000000;
uint64_t tnjnsec = tn.tv_nsec;
#endif
uint64_t get_absolute_time_in_ns() {
- // CLOCK_MONOTONIC_RAW in FreeBSD etc, monotonic in MacOSX
+ // CLOCK_MONOTONIC_RAW/CLOCK_MONOTONIC in Linux/FreeBSD etc, monotonic in MacOSX
uint64_t time_now_ns;
#ifdef COMPILE_FOR_LINUX_AND_FREEBSD_AND_CYGWIN_AND_OPENBSD
struct timespec tn;
+#ifdef CLOCK_MONOTONIC_RAW
clock_gettime(CLOCK_MONOTONIC_RAW, &tn);
+#else
+ clock_gettime(CLOCK_MONOTONIC, &tn);
+#endif
uint64_t tnnsec = tn.tv_sec;
tnnsec = tnnsec * 1000000000;
uint64_t tnjnsec = tn.tv_nsec;
int oldState;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
-
struct timespec timeoutTime;
uint64_t wait_until_time = dally_time * 1000; // to nanoseconds
uint64_t start_time = get_realtime_in_ns(); // this is from CLOCK_REALTIME
return pthread_mutex_lock(mutex);
int oldState;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
- uint64_t time_at_start = get_absolute_time_in_ns();
- char dstring[1000];
- memset(dstring, 0, sizeof(dstring));
- snprintf(dstring, sizeof(dstring), "%s:%d", filename, line);
- if (debuglevel != 0)
- _debug(filename, line, 3, "mutex_lock \"%s\" at \"%s\".", mutexname, dstring); // only if you really ask for it!
+ if (debuglevel != 0)
+ _debug(filename, line, 3, "mutex_lock \"%s\".", mutexname); // only if you really ask for it!
int result = sps_pthread_mutex_timedlock(mutex, dally_time);
if (result == ETIMEDOUT) {
+ _debug(filename, line, debuglevel,
+ "mutex_lock \"%s\" failed to lock after %f ms. Now trying an untimed lock...",
+ mutexname, dally_time * 1E-3);
result = pthread_mutex_lock(mutex);
- uint64_t time_delay = get_absolute_time_in_ns() - time_at_start;
_debug(filename, line, debuglevel,
- "mutex_lock \"%s\" expected max wait: %" PRId64 " ns, actual wait: %u microseconds.",
- mutexname, (1.0 * dally_time) / 1000000, 0.000000001 * time_delay);
+ "Untimed lock exited with an error code of %u", result);
}
pthread_setcancelstate(oldState, NULL);
return result;