aboutsummaryrefslogtreecommitdiff
path: root/src/platform
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-10-19 15:16:25 -0700
committerGarrett D'Amore <garrett@damore.org>2017-10-19 17:56:49 -0700
commit4e668fdd5b5da0d46f97d835249dbe5f0ea319a7 (patch)
tree0aaad8a672024b3a510763150b167320be6f1f5b /src/platform
parentd7e39a2423212a31c5ef62dcb0b7a5b4bf9f34df (diff)
downloadnng-4e668fdd5b5da0d46f97d835249dbe5f0ea319a7.tar.gz
nng-4e668fdd5b5da0d46f97d835249dbe5f0ea319a7.tar.bz2
nng-4e668fdd5b5da0d46f97d835249dbe5f0ea319a7.zip
fixes #84 Consider using msec for durations
There is now a public nng_duration type. We have also updated the zerotier work to work with the signed int64_t's that the latst ZeroTier dev branch is using.
Diffstat (limited to 'src/platform')
-rw-r--r--src/platform/posix/posix_clock.c51
-rw-r--r--src/platform/posix/posix_thread.c4
-rw-r--r--src/platform/windows/win_clock.c11
-rw-r--r--src/platform/windows/win_thread.c3
4 files changed, 30 insertions, 39 deletions
diff --git a/src/platform/posix/posix_clock.c b/src/platform/posix/posix_clock.c
index 3e46f787..289638ca 100644
--- a/src/platform/posix/posix_clock.c
+++ b/src/platform/posix/posix_clock.c
@@ -1,5 +1,6 @@
//
-// Copyright 2016 Garrett D'Amore <garrett@damore.org>
+// Copyright 2017 Garrett D'Amore <garrett@damore.org>
+// Copyright 2017 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
@@ -23,28 +24,28 @@ nni_time
nni_plat_clock(void)
{
struct timespec ts;
- nni_time usec;
+ nni_time msec;
if (clock_gettime(NNG_USE_CLOCKID, &ts) != 0) {
- /* This should never ever occur. */
+ // This should never ever occur.
nni_panic("clock_gettime failed: %s", strerror(errno));
}
- usec = ts.tv_sec;
- usec *= 1000000;
- usec += (ts.tv_nsec / 1000);
- return (usec);
+ msec = ts.tv_sec;
+ msec *= 1000;
+ msec += (ts.tv_nsec / 1000000);
+ return (msec);
}
void
-nni_plat_usleep(nni_duration usec)
+nni_plat_sleep(nni_duration ms)
{
struct timespec ts;
- ts.tv_sec = usec / 1000000;
- ts.tv_nsec = (usec % 1000000) * 1000;
+ ts.tv_sec = ms / 1000;
+ ts.tv_nsec = (ms % 1000) * 1000000;
- /* Do this in a loop, so that interrupts don't actually wake us. */
+ // Do this in a loop, so that interrupts don't actually wake us.
while (ts.tv_sec || ts.tv_nsec) {
if (nanosleep(&ts, &ts) == 0) {
break;
@@ -67,10 +68,10 @@ nni_plat_usleep(nni_duration usec)
#include <pthread.h>
#include <sys/time.h>
-nni_time
+static nni_time
nni_plat_clock(void)
{
- nni_time usec;
+ nni_time ms;
struct timeval tv;
@@ -78,14 +79,14 @@ nni_plat_clock(void)
nni_panic("gettimeofday failed: %s", strerror(errno));
}
- usec = tv.tv_sec;
- usec *= 1000000;
- usec += tv.tv_usec;
- return (usec);
+ ms = tv.tv_sec;
+ msec *= 1000;
+ msec += (tv.tv_usec / 1000);
+ return (msec);
}
void
-nni_plat_usleep(nni_duration usec)
+nni_plat_sleep(nni_duration ms)
{
// So probably there is no nanosleep. We could in theory use
// pthread condition variables, but that means doing memory
@@ -93,8 +94,7 @@ nni_plat_usleep(nni_duration usec)
// 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 we can use poll() instead.
struct pollfd pfd;
nni_time now;
nni_time expire;
@@ -105,16 +105,11 @@ nni_plat_usleep(nni_duration usec)
pfd.fd = -1;
pfd.events = 0;
- now = nni_clock();
- expire = now + usec;
+ now = nni_plat_clock(); // XXX: until nni_plat_clock returns ms.
+ expire = now + ms;
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.
- (void) poll(&pfd, 0, (int) ((expire - now) / 1000));
+ (void) poll(&pfd, 0, (int) (expire - now));
now = nni_clock();
}
}
diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c
index 4278a057..115768dc 100644
--- a/src/platform/posix/posix_thread.c
+++ b/src/platform/posix/posix_thread.c
@@ -354,8 +354,8 @@ nni_plat_cv_until(nni_plat_cv *cv, nni_time until)
NNI_ASSERT(cv->mtx->owner == pthread_self());
// Our caller has already guaranteed a sane value for until.
- ts.tv_sec = until / 1000000;
- ts.tv_nsec = (until % 1000000) * 1000;
+ ts.tv_sec = until / 1000;
+ ts.tv_nsec = (until % 1000) * 1000000;
if (cv->fallback) {
rv = nni_plat_cv_until_fallback(cv, &ts);
diff --git a/src/platform/windows/win_clock.c b/src/platform/windows/win_clock.c
index 949b2dfc..613af4be 100644
--- a/src/platform/windows/win_clock.c
+++ b/src/platform/windows/win_clock.c
@@ -1,5 +1,6 @@
//
-// Copyright 2016 Garrett D'Amore <garrett@damore.org>
+// Copyright 2017 Garrett D'Amore <garrett@damore.org>
+// Copyright 2017 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
@@ -15,18 +16,14 @@ nni_time
nni_plat_clock(void)
{
// We are limited by the system clock, but that is ok.
- return (GetTickCount64() * 1000);
+ return (GetTickCount64());
}
void
-nni_plat_usleep(nni_duration dur)
+nni_plat_sleep(nni_duration dur)
{
uint64_t exp;
- // Convert duration to msec, rounding up.
- dur += 999;
- dur /= 1000;
-
exp = (uint64_t) GetTickCount64() + dur;
// Sleep() would be our preferred API, if it didn't have a nasty
diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c
index 41ba721c..0d9e7387 100644
--- a/src/platform/windows/win_thread.c
+++ b/src/platform/windows/win_thread.c
@@ -91,8 +91,7 @@ nni_plat_cv_until(nni_plat_cv *cv, nni_time until)
if (now > until) {
msec = 0;
} else {
- // times are in usec, but win32 wants millis
- msec = (DWORD)(((until - now) + 999) / 1000);
+ msec = (DWORD)(until - now);
}
ok = SleepConditionVariableSRW(&cv->cv, cv->srl, msec, 0);