From 3583d5e407476b8836228c0abc52c400d74aba61 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Wed, 14 Dec 2016 18:53:10 -0800 Subject: More robust platform definition support. The idea is that someday it will be possible to just concatenate the entire set of source files into a single giant source file, for systems that want to work this way. As a result, the build system now compiles every file, although some of them will not have any definitions. --- src/platform/posix/posix_alloc.c | 53 ++++++++ src/platform/posix/posix_alloc.h | 45 ------- src/platform/posix/posix_clock.c | 154 ++++++++++++++++++++++ src/platform/posix/posix_clock.h | 149 --------------------- src/platform/posix/posix_debug.c | 42 ++++++ src/platform/posix/posix_debug.h | 48 ------- src/platform/posix/posix_impl.h | 13 +- src/platform/posix/posix_synch.c | 243 +++++++++++++++++++++++++++++++++++ src/platform/posix/posix_synch.h | 237 ---------------------------------- src/platform/posix/posix_thread.c | 130 +++++++++++++++++++ src/platform/posix/posix_thread.h | 121 ----------------- src/platform/posix/posix_vsnprintf.c | 36 ++++++ src/platform/posix/posix_vsnprintf.h | 30 ----- 13 files changed, 665 insertions(+), 636 deletions(-) create mode 100644 src/platform/posix/posix_alloc.c delete mode 100644 src/platform/posix/posix_alloc.h create mode 100644 src/platform/posix/posix_clock.c delete mode 100644 src/platform/posix/posix_clock.h create mode 100644 src/platform/posix/posix_debug.c delete mode 100644 src/platform/posix/posix_debug.h create mode 100644 src/platform/posix/posix_synch.c delete mode 100644 src/platform/posix/posix_synch.h create mode 100644 src/platform/posix/posix_thread.c delete mode 100644 src/platform/posix/posix_thread.h create mode 100644 src/platform/posix/posix_vsnprintf.c delete mode 100644 src/platform/posix/posix_vsnprintf.h (limited to 'src/platform') diff --git a/src/platform/posix/posix_alloc.c b/src/platform/posix/posix_alloc.c new file mode 100644 index 00000000..b47a95b2 --- /dev/null +++ b/src/platform/posix/posix_alloc.c @@ -0,0 +1,53 @@ +/* + * Copyright 2016 Garrett D'Amore + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +/* + * This is more of a direct #include of a .c rather than .h file. + * But having it be a .h makes compiler rules work out properly. Do + * not include this more than once into your program, or you will + * get multiple symbols defined. + */ + +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_ALLOC + +#include + +/* + * POSIX memory allocation. This is pretty much standard C. + */ + +void * +nni_alloc(size_t size) +{ + return (malloc(size)); +} + +void +nni_free(void *ptr, size_t size) +{ + NNI_ARG_UNUSED(size); + free(ptr); +} + +#endif \ No newline at end of file diff --git a/src/platform/posix/posix_alloc.h b/src/platform/posix/posix_alloc.h deleted file mode 100644 index 8451cc9c..00000000 --- a/src/platform/posix/posix_alloc.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2016 Garrett D'Amore - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom - * the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -/* - * This is more of a direct #include of a .c rather than .h file. - * But having it be a .h makes compiler rules work out properly. Do - * not include this more than once into your program, or you will - * get multiple symbols defined. - */ - -/* - * POSIX memory allocation. This is pretty much standard C. - */ - -void * -nni_alloc(size_t size) -{ - return (malloc(size)); -} - -void -nni_free(void *ptr, size_t size) -{ - NNI_ARG_UNUSED(size); - free(ptr); -} diff --git a/src/platform/posix/posix_clock.c b/src/platform/posix/posix_clock.c new file mode 100644 index 00000000..c772263c --- /dev/null +++ b/src/platform/posix/posix_clock.c @@ -0,0 +1,154 @@ +/* + * Copyright 2016 Garrett D'Amore + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +/* + * This is more of a direct #include of a .c rather than .h file. + * But having it be a .h makes compiler rules work out properly. Do + * not include this more than once into your program, or you will + * get multiple symbols defined. + */ + +/* + * POSIX clock stuff. + */ +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_CLOCK + +#include +#include +#include + +#ifndef NNG_USE_GETTIMEOFDAY + +/* + * Use POSIX realtime stuff. + */ + +uint64_t +nni_clock(void) +{ + struct timespec ts; + uint64_t usec; + + if (clock_gettime(NNG_USE_CLOCKID, &ts) != 0) { + /* 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); +} + + +void +nni_usleep(uint64_t usec) +{ + struct timespec ts; + + ts.tv_sec = usec / 1000000; + ts.tv_nsec = (usec % 1000000) * 1000; + + /* Do this in a loop, so that interrupts don't actually wake us. */ + while (ts.tv_sec || ts.tv_nsec) { + (void) nanosleep(&ts, &ts); + } +} + +#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.) + */ + +#include +#include + +uint64_t +nni_clock(void) +{ + uint64_t usec; + + struct timeval tv; + if (gettimeofday(&tv, NULL) != 0) { + nni_panic("gettimeofday failed: %s", strerror(errno)); + } + + usec = tv.tv_sec; + usec *= 1000000; + usec += tv.tv_usec; + return (usec); +} + +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. + */ + 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. + */ + pfd.fd = -1; + pfd.events = 0; + + now = nni_clock(); + 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. + */ + (void) poll(&pfd, 0, (int)((expire - now) / 1000)); + now = nni_clock(); + } +} + +#endif /* NNG_USE_GETTIMEOFDAY */ + +#endif /* PLATFORM_POSIX_CLOCK */ diff --git a/src/platform/posix/posix_clock.h b/src/platform/posix/posix_clock.h deleted file mode 100644 index 07fc603d..00000000 --- a/src/platform/posix/posix_clock.h +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright 2016 Garrett D'Amore - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom - * the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -/* - * This is more of a direct #include of a .c rather than .h file. - * But having it be a .h makes compiler rules work out properly. Do - * not include this more than once into your program, or you will - * get multiple symbols defined. - */ - -/* - * POSIX clock stuff. - */ - -#include -#include -#include - -#ifndef NNG_USE_GETTIMEOFDAY - -/* - * Use POSIX realtime stuff. - */ - -uint64_t -nni_clock(void) -{ - struct timespec ts; - uint64_t usec; - - if (clock_gettime(NNG_USE_CLOCKID, &ts) != 0) { - /* 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); -} - - -void -nni_usleep(uint64_t usec) -{ - struct timespec ts; - - ts.tv_sec = usec / 1000000; - ts.tv_nsec = (usec % 1000000) * 1000; - - /* Do this in a loop, so that interrupts don't actually wake us. */ - while (ts.tv_sec || ts.tv_nsec) { - (void) nanosleep(&ts, &ts); - } -} - -#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.) - */ - -#include -#include - -uint64_t -nni_clock(void) -{ - uint64_t usec; - - struct timeval tv; - if (gettimeofday(&tv, NULL) != 0) { - nni_panic("gettimeofday failed: %s", strerror(errno)); - } - - usec = tv.tv_sec; - usec *= 1000000; - usec += tv.tv_usec; - return (usec); -} - -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. - */ - 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. - */ - pfd.fd = -1; - pfd.events = 0; - - now = nni_clock(); - 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. - */ - (void) poll(&pfd, 0, (int)((expire - now) / 1000)); - now = nni_clock(); - } -} - -#endif /* NNG_USE_GETTIMEOFDAY */ diff --git a/src/platform/posix/posix_debug.c b/src/platform/posix/posix_debug.c new file mode 100644 index 00000000..6b2d875a --- /dev/null +++ b/src/platform/posix/posix_debug.c @@ -0,0 +1,42 @@ +/* + * Copyright 2016 Garrett D'Amore + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_DEBUG + +#include +#include + +void +nni_plat_abort(void) +{ + abort(); +} + +void +nni_plat_println(const char *message) +{ + (void) fprintf(stderr, "%s\n", message); +} + +#endif /* PLATFORM_POSIX_DEBUG */ \ No newline at end of file diff --git a/src/platform/posix/posix_debug.h b/src/platform/posix/posix_debug.h deleted file mode 100644 index d25a6c11..00000000 --- a/src/platform/posix/posix_debug.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016 Garrett D'Amore - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom - * the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#include -#include - -void (*debug_out)(const char *line); - -void -nni_abort(void) -{ - abort(); -} - -void -nni_debug_out(const char *message) -{ - if (debug_out != NULL) { - debug_out(message); - } else { - (void) fprintf(stderr, "%s\n", message); - } -} - -void -nni_set_debug_out(void (*out)(const char *)) -{ - debug_out = out; -} diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index 60a1d663..48b6e99b 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -31,11 +31,12 @@ */ #ifdef PLATFORM_POSIX +#define PLATFORM_POSIX_ALLOC +#define PLATFORM_POSIX_DEBUG +#define PLATFORM_POSIX_CLOCK +#define PLATFORM_POSIX_SYNCH +#define PLATFORM_POSIX_THREAD +#define PLATFORM_POSIX_VSNPRINTF + #include "platform/posix/posix_config.h" -#include "platform/posix/posix_debug.h" -#include "platform/posix/posix_alloc.h" -#include "platform/posix/posix_clock.h" -#include "platform/posix/posix_synch.h" -#include "platform/posix/posix_thread.h" -#include "platform/posix/posix_vsnprintf.h" #endif diff --git a/src/platform/posix/posix_synch.c b/src/platform/posix/posix_synch.c new file mode 100644 index 00000000..cb8b30dd --- /dev/null +++ b/src/platform/posix/posix_synch.c @@ -0,0 +1,243 @@ +/* + * Copyright 2016 Garrett D'Amore + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +/* + * This is more of a direct #include of a .c rather than .h file. + * But having it be a .h makes compiler rules work out properly. Do + * not include this more than once into your program, or you will + * get multiple symbols defined. + */ + +/* + * POSIX synchronization (mutexes and condition variables). This uses + * pthreads. + */ + +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_SYNCH + +#include +#include + +struct nni_mutex { + pthread_mutex_t mx; +}; + +struct nni_cond { + pthread_cond_t cv; + pthread_mutex_t *mx; +}; + +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); +} + +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_t m) +{ + if (pthread_mutex_lock(&m->mx) != 0) { + nni_panic("pthread_mutex_lock failed"); + } +} + +void +nni_mutex_exit(nni_mutex_t m) +{ + if (pthread_mutex_unlock(&m->mx) != 0) { + nni_panic("pthread_mutex_unlock failed"); + } +} + +int +nni_mutex_tryenter(nni_mutex_t m) +{ + if (pthread_mutex_trylock(&m->mx) != 0) { + return (NNG_EBUSY); + } + return (0); +} + +int +cond_attr(pthread_condattr_t **attrpp) +{ +#if defined(NNG_USE_GETTIMEOFDAY) || NNG_USE_CLOCKID == CLOCK_REALTIME + *attrpp = NULL; + return (0); +#else + /* In order to make this fast, avoid reinitializing attrs. */ + static pthread_condattr_t attr; + static pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER; + 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. + */ + if (init) { + *attrpp = &attr; + return (0); + } + + (void) pthread_mutex_lock(&mx); + while (!init) { + if ((rv = pthread_condattr_init(&attr)) != 0) { + (void) pthread_mutex_unlock(&mx); + return (NNG_ENOMEM); + } + rv = pthread_condattr_setclock(&attr, NNG_USE_CLOCKID); + if (rv != 0) { + nni_panic("condattr_setclock: %s", strerror(rv)); + } + init = 1; + } + (void) pthread_mutex_unlock(&mx); + *attrpp = &attr; + return (0); +#endif +} + +int +nni_cond_create(nni_cond_t *cvp, nni_mutex_t 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 = 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); +} + +void +nni_cond_destroy(nni_cond_t 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_t c) +{ + if (pthread_cond_signal(&c->cv) != 0) { + nni_panic("pthread_cond_signal failed"); + } +} + +void +nni_cond_broadcast(nni_cond_t c) +{ + if (pthread_cond_broadcast(&c->cv) != 0) { + nni_panic("pthread_cond_broadcast failed"); + } +} + +void +nni_cond_wait(nni_cond_t c) +{ + if (pthread_cond_wait(&c->cv, c->mx) != 0) { + nni_panic("pthread_cond_wait failed"); + } +} + +int +nni_cond_timedwait(nni_cond_t c, uint64_t usec) +{ + struct timespec ts; + int rv; + + usec += nni_clock(); + + ts.tv_sec = usec / 1000000; + ts.tv_nsec = (usec % 10000) * 1000; + + rv = pthread_cond_timedwait(&c->cv, c->mx, &ts); + + if (rv == ETIMEDOUT) { + return (NNG_ETIMEDOUT); + } else if (rv != 0) { + nni_panic("pthread_cond_timedwait returned %d", rv); + } + return (0); +} + +#endif \ No newline at end of file diff --git a/src/platform/posix/posix_synch.h b/src/platform/posix/posix_synch.h deleted file mode 100644 index d3e44411..00000000 --- a/src/platform/posix/posix_synch.h +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Copyright 2016 Garrett D'Amore - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom - * the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -/* - * This is more of a direct #include of a .c rather than .h file. - * But having it be a .h makes compiler rules work out properly. Do - * not include this more than once into your program, or you will - * get multiple symbols defined. - */ - -/* - * POSIX synchronization (mutexes and condition variables). This uses - * pthreads. - */ - -#include -#include - -struct nni_mutex { - pthread_mutex_t mx; -}; - -struct nni_cond { - pthread_cond_t cv; - pthread_mutex_t *mx; -}; - -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); -} - -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_t m) -{ - if (pthread_mutex_lock(&m->mx) != 0) { - nni_panic("pthread_mutex_lock failed"); - } -} - -void -nni_mutex_exit(nni_mutex_t m) -{ - if (pthread_mutex_unlock(&m->mx) != 0) { - nni_panic("pthread_mutex_unlock failed"); - } -} - -int -nni_mutex_tryenter(nni_mutex_t m) -{ - if (pthread_mutex_trylock(&m->mx) != 0) { - return (NNG_EBUSY); - } - return (0); -} - -int -cond_attr(pthread_condattr_t **attrpp) -{ -#if defined(NNG_USE_GETTIMEOFDAY) || NNG_USE_CLOCKID == CLOCK_REALTIME - *attrpp = NULL; - return (0); -#else - /* In order to make this fast, avoid reinitializing attrs. */ - static pthread_condattr_t attr; - static pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER; - 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. - */ - if (init) { - *attrpp = &attr; - return (0); - } - - (void) pthread_mutex_lock(&mx); - while (!init) { - if ((rv = pthread_condattr_init(&attr)) != 0) { - (void) pthread_mutex_unlock(&mx); - return (NNG_ENOMEM); - } - rv = pthread_condattr_setclock(&attr, NNG_USE_CLOCKID); - if (rv != 0) { - nni_panic("condattr_setclock: %s", strerror(rv)); - } - init = 1; - } - (void) pthread_mutex_unlock(&mx); - *attrpp = &attr; - return (0); -#endif -} - -int -nni_cond_create(nni_cond_t *cvp, nni_mutex_t 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 = 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); -} - -void -nni_cond_destroy(nni_cond_t 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_t c) -{ - if (pthread_cond_signal(&c->cv) != 0) { - nni_panic("pthread_cond_signal failed"); - } -} - -void -nni_cond_broadcast(nni_cond_t c) -{ - if (pthread_cond_broadcast(&c->cv) != 0) { - nni_panic("pthread_cond_broadcast failed"); - } -} - -void -nni_cond_wait(nni_cond_t c) -{ - if (pthread_cond_wait(&c->cv, c->mx) != 0) { - nni_panic("pthread_cond_wait failed"); - } -} - -int -nni_cond_timedwait(nni_cond_t c, uint64_t usec) -{ - struct timespec ts; - int rv; - - usec += nni_clock(); - - ts.tv_sec = usec / 1000000; - ts.tv_nsec = (usec % 10000) * 1000; - - rv = pthread_cond_timedwait(&c->cv, c->mx, &ts); - - if (rv == ETIMEDOUT) { - return (NNG_ETIMEDOUT); - } else if (rv != 0) { - nni_panic("pthread_cond_timedwait returned %d", rv); - } - return (0); -} diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c new file mode 100644 index 00000000..a41c7e7c --- /dev/null +++ b/src/platform/posix/posix_thread.c @@ -0,0 +1,130 @@ +/* + * Copyright 2016 Garrett D'Amore + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +/* + * This is more of a direct #include of a .c rather than .h file. + * But having it be a .h makes compiler rules work out properly. Do + * not include this more than once into your program, or you will + * get multiple symbols defined. + */ + +/* + * POSIX threads. + */ + +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_THREAD + +#include +#include +#include + +struct nni_thread { + pthread_t tid; + void *arg; + void (*func)(void *); +}; + +static pthread_mutex_t plat_lock = PTHREAD_MUTEX_INITIALIZER; +static int plat_init = 0; +static int plat_fork = 0; + +static void * +thrfunc(void *arg) +{ + nni_thread_t thr = arg; + + thr->func(thr->arg); + return (NULL); +} + +int +nni_thread_create(nni_thread_t *tp, void (*fn)(void *), void *arg) +{ + nni_thread_t thr; + int rv; + + if ((thr = nni_alloc(sizeof (*thr))) == NULL) { + return (NNG_ENOMEM); + } + thr->func = fn; + thr->arg = arg; + + if ((rv = pthread_create(&thr->tid, NULL, thrfunc, thr)) != 0) { + nni_free(thr, sizeof (*thr)); + return (NNG_ENOMEM); + } + *tp = thr; + return (0); +} + +void +nni_thread_reap(nni_thread_t thr) +{ + int rv; + if ((rv = pthread_join(thr->tid, NULL)) != 0) { + nni_panic("pthread_thread: %s", strerror(errno)); + } + nni_free(thr, sizeof (*thr)); +} + +void +atfork_child(void) +{ + plat_fork = 1; +} + +int +nni_plat_init(int (*helper)(void)) +{ + int rv; + if (plat_fork) { + nni_panic("nng is fork-reentrant safe"); + } + if (plat_init) { + 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); + return (0); + } + if (pthread_atfork(NULL, NULL, atfork_child) != 0) { + pthread_mutex_unlock(&plat_lock); + return (NNG_ENOMEM); + } + if ((rv = helper()) == 0) { + plat_init = 1; + } + pthread_mutex_unlock(&plat_lock); + + return (rv); +} + +void +nni_plat_fini(void) +{ + /* XXX: NOTHING *YET* */ +} + +#endif \ No newline at end of file diff --git a/src/platform/posix/posix_thread.h b/src/platform/posix/posix_thread.h deleted file mode 100644 index 27ad888f..00000000 --- a/src/platform/posix/posix_thread.h +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright 2016 Garrett D'Amore - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom - * the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -/* - * This is more of a direct #include of a .c rather than .h file. - * But having it be a .h makes compiler rules work out properly. Do - * not include this more than once into your program, or you will - * get multiple symbols defined. - */ - -/* - * POSIX threads. - */ - -#include -#include -#include - -struct nni_thread { - pthread_t tid; - void *arg; - void (*func)(void *); -}; - -static pthread_mutex_t plat_lock = PTHREAD_MUTEX_INITIALIZER; -static int plat_init = 0; -static int plat_fork = 0; - -static void * -thrfunc(void *arg) -{ - nni_thread_t thr = arg; - - thr->func(thr->arg); - return (NULL); -} - -int -nni_thread_create(nni_thread_t *tp, void (*fn)(void *), void *arg) -{ - nni_thread_t thr; - int rv; - - if ((thr = nni_alloc(sizeof (*thr))) == NULL) { - return (NNG_ENOMEM); - } - thr->func = fn; - thr->arg = arg; - - if ((rv = pthread_create(&thr->tid, thr, thrfunc, arg)) != 0) { - nni_free(thr, sizeof (*thr)); - return (NNG_ENOMEM); - } - *tp = thr; - return (0); -} - -void -nni_thread_reap(nni_thread_t thr) -{ - int rv; - if ((rv = pthread_join(thr->tid, NULL)) != 0) { - nni_panic("pthread_thread: %s", strerror(errno)); - } - nni_free(thr, sizeof (*thr)); -} - -void -atfork_child(void) -{ - plat_fork = 1; -} - -int -nni_platform_init(void) -{ - if (plat_fork) { - nni_panic("nng is fork-reentrant safe"); - } - if (plat_init) { - 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); - return (0); - } - if (pthread_atfork(NULL, NULL, atfork_child) != 0) { - pthread_mutex_unlock(&plat_lock); - return (NNG_ENOMEM); - } - plat_init = 1; - pthread_mutex_unlock(&plat_lock); - - return (0); -} - -void -nni_platform_fini(void) -{ - /* XXX: NOTHING *YET* */ -} \ No newline at end of file diff --git a/src/platform/posix/posix_vsnprintf.c b/src/platform/posix/posix_vsnprintf.c new file mode 100644 index 00000000..42367b4c --- /dev/null +++ b/src/platform/posix/posix_vsnprintf.c @@ -0,0 +1,36 @@ +/* + * Copyright 2016 Garrett D'Amore + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_VSNPRINTF + +#include +#include + +void +nni_plat_vsnprintf(char *dst, size_t sz, const char *fmt, va_list va) +{ + (void) vsnprintf(dst, sz, fmt, va); +} + +#endif \ No newline at end of file diff --git a/src/platform/posix/posix_vsnprintf.h b/src/platform/posix/posix_vsnprintf.h deleted file mode 100644 index 8270d255..00000000 --- a/src/platform/posix/posix_vsnprintf.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2016 Garrett D'Amore - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom - * the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#include -#include - -void -nni_vsnprintf(char *dst, size_t sz, const char *fmt, va_list va) -{ - (void) vsnprintf(dst, sz, fmt, va); -} -- cgit v1.2.3-70-g09d2