From 6c1325a2b17548a4249d26a846bc32b95b7d747d Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Thu, 22 Dec 2016 02:19:18 -0800 Subject: Start of work to inline mutexes and condition variables. --- src/platform/posix/posix_impl.h | 9 ++++++--- src/platform/posix/posix_synch.c | 21 +++++++++++---------- src/platform/posix/posix_thread.c | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 16 deletions(-) (limited to 'src/platform') diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index 0a3151f3..a7b15edc 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -22,14 +22,17 @@ // Define types that this platform uses. #ifdef PLATFORM_POSIX_SYNCH + +#include + struct nni_mutex { pthread_mutex_t mx; -} +}; -struct nni_condvar { +struct nni_cond { pthread_cond_t cv; pthread_mutex_t * mx; -} +}; #endif #endif // PLATFORM_POSIX_IMPL_H \ No newline at end of file diff --git a/src/platform/posix/posix_synch.c b/src/platform/posix/posix_synch.c index 13147573..a3129f62 100644 --- a/src/platform/posix/posix_synch.c +++ b/src/platform/posix/posix_synch.c @@ -25,12 +25,15 @@ #include #include +#include + +extern pthread_condattr_t nni_condattr; +extern pthread_mutexattr_t nni_mutexattr; int nni_mutex_init(nni_mutex *mp) { - // pthrad_mutex_attr_t attr; - if (pthread_mutex_init(&mp->mx, NULL) != NULL) { + if (pthread_mutex_init(&mp->mx, &nni_mutexattr) != 0) { return (NNG_ENOMEM); } return (0); @@ -42,7 +45,7 @@ nni_mutex_fini(nni_mutex *mp) { int rv; - if ((rv = pthread_mutex_destroy(&mp-- > mx)) != 0) { + if ((rv = pthread_mutex_destroy(&mp->mx)) != 0) { nni_panic("pthread_mutex_destroy failed: %s", strerror(rv)); } } @@ -136,12 +139,10 @@ nni_cond_attr(pthread_condattr_t **attrpp) static int init = 0; int rv; - /* - * For efficiency's sake, we try to reuse the same attr for the - * life of the library. This avoids many reallocations. Technically - * this means that we will leak the attr on exit(), but this is - * preferable to constantly allocating and reallocating it. - */ + // For efficiency's sake, we try to reuse the same attr for the + // life of the library. This avoids many reallocations. Technically + // this means that we will leak the attr on exit(), but this is + // preferable to constantly allocating and reallocating it. if (init) { *attrpp = &attr; return (0); @@ -178,7 +179,7 @@ nni_cond_create(nni_cond_t *cvp, nni_mutex_t mx) pthread_condattr_t *attrp; int rv; - if ((rv = cond_attr(&attrp)) != 0) { + if ((rv = nni_cond_attr(&attrp)) != 0) { return (rv); } if ((c = nni_alloc(sizeof (*c))) == NULL) { diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c index 8928974c..1bbe6e28 100644 --- a/src/platform/posix/posix_thread.c +++ b/src/platform/posix/posix_thread.c @@ -71,12 +71,15 @@ nni_thread_reap(nni_thread_t thr) void -atfork_child(void) +nni_atfork_child(void) { plat_fork = 1; } +pthread_condattr_t nni_condattr; +pthread_mutexattr_t nni_mutexattr; + int nni_plat_init(int (*helper)(void)) { @@ -93,7 +96,30 @@ nni_plat_init(int (*helper)(void)) pthread_mutex_unlock(&plat_lock); return (0); } - if (pthread_atfork(NULL, NULL, atfork_child) != 0) { + if (pthread_condattr_init(&nni_condattr) != 0) { + pthread_mutex_unlock(&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); + return (NNG_ENOMEM); + } +#endif + + if (pthread_mutexattr_init(&nni_mutexattr) != 0) { + pthread_mutex_unlock(&plat_lock); + return (NNG_ENOMEM); + } + + if (pthread_mutexattr_settype(&nni_mutexattr, + PTHREAD_MUTEX_ERRORCHECK) != 0) { + pthread_mutex_unlock(&plat_lock); + return (NNG_ENOMEM); + } + + + if (pthread_atfork(NULL, NULL, nni_atfork_child) != 0) { pthread_mutex_unlock(&plat_lock); return (NNG_ENOMEM); } @@ -109,7 +135,13 @@ nni_plat_init(int (*helper)(void)) void nni_plat_fini(void) { - // XXX: NOTHING *YET* + pthread_mutex_lock(&plat_lock); + if (plat_init) { + pthread_mutexattr_destroy(&nni_mutexattr); + pthread_condattr_destroy(&nni_condattr); + plat_init = 0; + } + pthread_mutex_unlock(&plat_lock); } -- cgit v1.2.3-70-g09d2