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/posix_thread.c | |
| 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/posix_thread.c')
| -rw-r--r-- | src/platform/posix/posix_thread.c | 48 |
1 files changed, 24 insertions, 24 deletions
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); } |
