diff options
| author | Garrett D'Amore <garrett@damore.org> | 2016-12-22 15:23:21 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2016-12-22 15:23:21 -0800 |
| commit | 934c1316ae47754a2e368c65228c3cbfe552680f (patch) | |
| tree | e81c4d2854df83e3d908c9269dd35c0600fa6acb /src/platform/posix | |
| parent | ee969ad99dc1e07e1c38876223e7aed13463b121 (diff) | |
| download | nng-934c1316ae47754a2e368c65228c3cbfe552680f.tar.gz nng-934c1316ae47754a2e368c65228c3cbfe552680f.tar.bz2 nng-934c1316ae47754a2e368c65228c3cbfe552680f.zip | |
Inline locks (fewer allocs), simpler absolute times for wakeups. nn_sock_recv.
Diffstat (limited to 'src/platform/posix')
| -rw-r--r-- | src/platform/posix/posix_clock.c | 16 | ||||
| -rw-r--r-- | src/platform/posix/posix_impl.h | 2 | ||||
| -rw-r--r-- | src/platform/posix/posix_synch.c | 109 | ||||
| -rw-r--r-- | src/platform/posix/posix_thread.c | 48 |
4 files changed, 33 insertions, 142 deletions
diff --git a/src/platform/posix/posix_clock.c b/src/platform/posix/posix_clock.c index 48a1e09b..c4206ebe 100644 --- a/src/platform/posix/posix_clock.c +++ b/src/platform/posix/posix_clock.c @@ -19,11 +19,11 @@ #ifndef NNG_USE_GETTIMEOFDAY // Use POSIX realtime stuff -uint64_t +nni_time nni_clock(void) { struct timespec ts; - uint64_t usec; + nni_time usec; if (clock_gettime(NNG_USE_CLOCKID, &ts) != 0) { /* This should never ever occur. */ @@ -38,7 +38,7 @@ nni_clock(void) void -nni_usleep(uint64_t usec) +nni_usleep(nni_duration usec) { struct timespec ts; @@ -67,10 +67,10 @@ nni_usleep(uint64_t usec) #include <sys/time.h> #include <poll.h> -uint64_t +nni_time nni_clock(void) { - uint64_t usec; + nni_time usec; struct timeval tv; @@ -86,7 +86,7 @@ nni_clock(void) void -nni_usleep(uint64_t usec) +nni_usleep(nni_duration usec) { // So probably there is no nanosleep. We could in theory use // pthread condition variables, but that means doing memory @@ -97,8 +97,8 @@ nni_usleep(uint64_t usec) // 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; + nni_time now; + nni_time expire; // Possibly we could pass NULL instead of pfd, but passing a valid // pointer ensures that if the system dereferences the pointer it diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index 4f93b101..0fa15b43 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -42,4 +42,4 @@ struct nni_cond { }; #endif -#endif // PLATFORM_POSIX_IMPL_H
\ No newline at end of file +#endif // PLATFORM_POSIX_IMPL_H diff --git a/src/platform/posix/posix_synch.c b/src/platform/posix/posix_synch.c index c0a2721c..3d91ee3d 100644 --- a/src/platform/posix/posix_synch.c +++ b/src/platform/posix/posix_synch.c @@ -42,54 +42,6 @@ nni_mutex_fini(nni_mutex *mp) } -// XXX: REMOVE THIS FUNCTION -int -nni_mutex_create(nni_mutex_t *mp) -{ - struct nni_mutex *m; - pthread_mutexattr_t attr; - int rv; - - if ((m = nni_alloc(sizeof (*m))) == NULL) { - return (NNG_ENOMEM); - } - - // We ask for more error checking... - if (pthread_mutexattr_init(&attr) != 0) { - nni_free(m, sizeof (*m)); - return (NNG_ENOMEM); - } - - if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0) { - nni_panic("pthread_mutexattr_settype failed"); - } - - rv = pthread_mutex_init(&m->mx, &attr); - - if (pthread_mutexattr_destroy(&attr) != 0) { - nni_panic("pthread_mutexattr_destroy failed"); - } - - if (rv != 0) { - nni_free(m, sizeof (*m)); - return (NNG_ENOMEM); - } - *mp = m; - return (0); -} - - -// XXX: REMOVE THIS FUNCTION -void -nni_mutex_destroy(nni_mutex_t m) -{ - if (pthread_mutex_destroy(&m->mx) != 0) { - nni_panic("pthread_mutex_destroy failed"); - } - nni_free(m, sizeof (*m)); -} - - void nni_mutex_enter(nni_mutex *m) { @@ -140,55 +92,6 @@ nni_cond_fini(nni_cond *c) } -// XXXX: REMOVE THIS FUNCTION -static int -nni_cond_attr(pthread_condattr_t **attrpp) -{ - *attrpp = NULL; - return (0); -} - - -// XXX: REMOVE THIS FUNCTION -int -nni_cond_create(nni_cond **cvp, nni_mutex *mx) -{ - /* - * By preference, we use a CLOCK_MONOTONIC version of condition - * variables, which insulates us from changes to the system time. - */ - struct nni_cond *c; - pthread_condattr_t *attrp; - int rv; - - if ((rv = nni_cond_attr(&attrp)) != 0) { - return (rv); - } - if ((c = nni_alloc(sizeof (*c))) == NULL) { - return (NNG_ENOMEM); - } - c->mx = &mx->mx; - if (pthread_cond_init(&c->cv, attrp) != 0) { - /* In theory could be EAGAIN, but handle like ENOMEM */ - nni_free(c, sizeof (*c)); - return (NNG_ENOMEM); - } - *cvp = c; - return (0); -} - - -// XXX: REMOVE THIS FUNCTION -void -nni_cond_destroy(nni_cond *c) -{ - if (pthread_cond_destroy(&c->cv) != 0) { - nni_panic("pthread_cond_destroy failed"); - } - nni_free(c, sizeof (*c)); -} - - void nni_cond_signal(nni_cond *c) { @@ -237,16 +140,4 @@ nni_cond_waituntil(nni_cond *c, uint64_t usec) return (0); } - -int -nni_cond_timedwait(nni_cond *c, int usec) -{ - if (usec < 0) { - nni_cond_wait(c); - return (0); - } - return (nni_cond_waituntil(c, ((uint64_t) usec) + nni_clock())); -} - - #endif diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c index 79f69762..80f3b96f 100644 --- a/src/platform/posix/posix_thread.c +++ b/src/platform/posix/posix_thread.c @@ -23,14 +23,14 @@ struct nni_thread { void (*func)(void *); }; -static pthread_mutex_t plat_lock = PTHREAD_MUTEX_INITIALIZER; -static int plat_init = 0; -static int plat_fork = 0; +static pthread_mutex_t nni_plat_lock = PTHREAD_MUTEX_INITIALIZER; +static int nni_plat_inited = 0; +static int nni_plat_forked = 0; static void * nni_thrfunc(void *arg) { - nni_thread_t thr = arg; + nni_thread *thr = arg; thr->func(thr->arg); return (NULL); @@ -38,9 +38,9 @@ nni_thrfunc(void *arg) int -nni_thread_create(nni_thread_t *tp, void (*fn)(void *), void *arg) +nni_thread_create(nni_thread **tp, void (*fn)(void *), void *arg) { - nni_thread_t thr; + nni_thread *thr; int rv; if ((thr = nni_alloc(sizeof (*thr))) == NULL) { @@ -59,7 +59,7 @@ nni_thread_create(nni_thread_t *tp, void (*fn)(void *), void *arg) void -nni_thread_reap(nni_thread_t thr) +nni_thread_reap(nni_thread * thr) { int rv; @@ -73,7 +73,7 @@ nni_thread_reap(nni_thread_t thr) void nni_atfork_child(void) { - plat_fork = 1; + nni_plat_forked = 1; } @@ -85,48 +85,48 @@ nni_plat_init(int (*helper)(void)) { int rv; - if (plat_fork) { + if (nni_plat_forked) { nni_panic("nng is fork-reentrant safe"); } - if (plat_init) { + if (nni_plat_inited) { return (0); // fast path } - pthread_mutex_lock(&plat_lock); - if (plat_init) { // check again under the lock to be sure - pthread_mutex_unlock(&plat_lock); + pthread_mutex_lock(&nni_plat_lock); + if (nni_plat_inited) { // check again under the lock to be sure + pthread_mutex_unlock(&nni_plat_lock); return (0); } if (pthread_condattr_init(&nni_condattr) != 0) { - pthread_mutex_unlock(&plat_lock); + pthread_mutex_unlock(&nni_plat_lock); return (NNG_ENOMEM); } #if !defined(NNG_USE_GETTIMEOFDAY) && NNG_USE_CLOCKID != CLOCK_REALTIME if (pthread_condattr_setclock(&nni_condattr, NNG_USE_CLOCKID) != 0) { - pthread_mutex_unlock(&plat_lock); + pthread_mutex_unlock(&nni_plat_lock); return (NNG_ENOMEM); } #endif if (pthread_mutexattr_init(&nni_mutexattr) != 0) { - pthread_mutex_unlock(&plat_lock); + pthread_mutex_unlock(&nni_plat_lock); return (NNG_ENOMEM); } if (pthread_mutexattr_settype(&nni_mutexattr, PTHREAD_MUTEX_ERRORCHECK) != 0) { - pthread_mutex_unlock(&plat_lock); + pthread_mutex_unlock(&nni_plat_lock); return (NNG_ENOMEM); } if (pthread_atfork(NULL, NULL, nni_atfork_child) != 0) { - pthread_mutex_unlock(&plat_lock); + pthread_mutex_unlock(&nni_plat_lock); return (NNG_ENOMEM); } if ((rv = helper()) == 0) { - plat_init = 1; + nni_plat_inited = 1; } - pthread_mutex_unlock(&plat_lock); + pthread_mutex_unlock(&nni_plat_lock); return (rv); } @@ -135,13 +135,13 @@ nni_plat_init(int (*helper)(void)) void nni_plat_fini(void) { - pthread_mutex_lock(&plat_lock); - if (plat_init) { + pthread_mutex_lock(&nni_plat_lock); + if (nni_plat_inited) { pthread_mutexattr_destroy(&nni_mutexattr); pthread_condattr_destroy(&nni_condattr); - plat_init = 0; + nni_plat_inited = 0; } - pthread_mutex_unlock(&plat_lock); + pthread_mutex_unlock(&nni_plat_lock); } |
