aboutsummaryrefslogtreecommitdiff
path: root/src/platform/posix/posix_clock.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2016-12-22 13:22:18 -0800
committerGarrett D'Amore <garrett@damore.org>2016-12-22 13:22:18 -0800
commitee969ad99dc1e07e1c38876223e7aed13463b121 (patch)
tree40bde325d041532661e7dcc3441185aca7701e53 /src/platform/posix/posix_clock.c
parent6c1325a2b17548a4249d26a846bc32b95b7d747d (diff)
downloadnng-ee969ad99dc1e07e1c38876223e7aed13463b121.tar.gz
nng-ee969ad99dc1e07e1c38876223e7aed13463b121.tar.bz2
nng-ee969ad99dc1e07e1c38876223e7aed13463b121.zip
Synchronization enhancements - inproc & msgqueue. Absolute waits...
Diffstat (limited to 'src/platform/posix/posix_clock.c')
-rw-r--r--src/platform/posix/posix_clock.c88
1 files changed, 38 insertions, 50 deletions
diff --git a/src/platform/posix/posix_clock.c b/src/platform/posix/posix_clock.c
index 83f0b151..48a1e09b 100644
--- a/src/platform/posix/posix_clock.c
+++ b/src/platform/posix/posix_clock.c
@@ -1,15 +1,13 @@
-/*
- * Copyright 2016 Garrett D'Amore <garrett@damore.org>
- *
- * This software is supplied under the terms of the MIT License, a
- * copy of which should be located in the distribution where this
- * file was obtained (LICENSE.txt). A copy of the license may also be
- * found online at https://opensource.org/licenses/MIT.
- */
-
-/*
- * POSIX clock stuff.
- */
+//
+// Copyright 2016 Garrett D'Amore <garrett@damore.org>
+//
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
+
+// POSIX clock stuff.
#include "core/nng_impl.h"
#ifdef PLATFORM_POSIX_CLOCK
@@ -20,9 +18,7 @@
#ifndef NNG_USE_GETTIMEOFDAY
-/*
- * Use POSIX realtime stuff.
- */
+// Use POSIX realtime stuff
uint64_t
nni_clock(void)
{
@@ -56,18 +52,16 @@ nni_usleep(uint64_t usec)
}
-#else /* NNG_USE_GETTIMEOFDAY */
+#else // NNG_USE_GETTIMEOFDAY
-/*
- * If you're here, its because you don't have a modern clock_gettime with
- * monotonic clocks, or the necessary pthread_condattr_settclock(). In
- * this case, you should be advised that *bad* things can happen if your
- * system clock changes time while programs using this library are running.
- * (Basically, timeouts can take longer or shorter, leading to either hangs
- * or apparent spurious errors. Eventually it should all sort itself out,
- * but if you change the clock by a large amount you might wonder what the
- * heck is happening until it does.)
- */
+// If you're here, its because you don't have a modern clock_gettime with
+// monotonic clocks, or the necessary pthread_condattr_settclock(). In
+// this case, you should be advised that *bad* things can happen if your
+// system clock changes time while programs using this library are running.
+// (Basically, timeouts can take longer or shorter, leading to either hangs
+// or apparent spurious errors. Eventually it should all sort itself out,
+// but if you change the clock by a large amount you might wonder what the
+// heck is happening until it does.)
#include <pthread.h>
#include <sys/time.h>
@@ -94,25 +88,21 @@ nni_clock(void)
void
nni_usleep(uint64_t usec)
{
- /*
- * So probably there is no nanosleep. We could in theory use
- * pthread condition variables, but that means doing memory
- * allocation, or forcing the use of pthreads where the platform
- * might be preferring the use of another threading package.
- * Additionally, use of pthreads means that we cannot use
- * relative times in a clock_settime safe manner.
- * So we can use poll() instead, which is rather coarse, but
- * pretty much guaranteed to work.
- */
+ // So probably there is no nanosleep. We could in theory use
+ // pthread condition variables, but that means doing memory
+ // allocation, or forcing the use of pthreads where the platform
+ // might be preferring the use of another threading package.
+ // Additionally, use of pthreads means that we cannot use
+ // relative times in a clock_settime safe manner.
+ // So we can use poll() instead, which is rather coarse, but
+ // pretty much guaranteed to work.
struct pollfd pfd;
uint64_t now;
uint64_t expire;
- /*
- * Possibly we could pass NULL instead of pfd, but passing a valid
- * pointer ensures that if the system dereferences the pointer it
- * won't come back with EFAULT.
- */
+ // Possibly we could pass NULL instead of pfd, but passing a valid
+ // pointer ensures that if the system dereferences the pointer it
+ // won't come back with EFAULT.
pfd.fd = -1;
pfd.events = 0;
@@ -120,19 +110,17 @@ nni_usleep(uint64_t usec)
expire = now + usec;
while (now < expire) {
- /*
- * In theory we could round up to a whole number of msec,
- * but under the covers poll already does some rounding up,
- * and the loop above guarantees that we will not bail out
- * early. So this gives us a better chance to avoid adding
- * nearly an extra unneeded millisecond to the wait.
- */
+ // In theory we could round up to a whole number of msec,
+ // but under the covers poll already does some rounding up,
+ // and the loop above guarantees that we will not bail out
+ // early. So this gives us a better chance to avoid adding
+ // nearly an extra unneeded millisecond to the wait.
(void) poll(&pfd, 0, (int) ((expire - now) / 1000));
now = nni_clock();
}
}
-#endif /* NNG_USE_GETTIMEOFDAY */
+#endif // NNG_USE_GETTIMEOFDAY
-#endif /* PLATFORM_POSIX_CLOCK */
+#endif // PLATFORM_POSIX_CLOCK