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/CMakeLists.txt | 17 ++- src/core/init.c | 44 +++++++ src/core/init.h | 41 ++++++ src/core/nng_impl.h | 1 + src/core/panic.c | 17 ++- src/core/panic.h | 9 ++ src/core/platform.c | 2 + src/core/platform.h | 73 ++++++----- src/core/snprintf.c | 6 + src/core/snprintf.h | 1 + src/core/socket.c | 4 +- src/nng.c | 20 ++- 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 ----- 25 files changed, 846 insertions(+), 690 deletions(-) create mode 100644 src/core/init.c create mode 100644 src/core/init.h 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') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4d28cb34..b4367365 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,6 +28,8 @@ set (NNG_SOURCES nng.h core/defs.h + core/init.c + core/init.h core/list.c core/list.h core/message.c @@ -50,14 +52,15 @@ set (NNG_SOURCES core/transport.c core/transport.h - platform/posix/posix_alloc.h - platform/posix/posix_clock.h - platform/posix/posix_config.h - platform/posix/posix_debug.h platform/posix/posix_impl.h - platform/posix/posix_synch.h - platform/posix/posix_thread.h - platform/posix/posix_vsnprintf.h + platform/posix/posix_config.h + + platform/posix/posix_alloc.c + platform/posix/posix_clock.c + platform/posix/posix_debug.c + platform/posix/posix_synch.c + platform/posix/posix_thread.c + platform/posix/posix_vsnprintf.c protocol/pair/pair.c diff --git a/src/core/init.c b/src/core/init.c new file mode 100644 index 00000000..4738a585 --- /dev/null +++ b/src/core/init.c @@ -0,0 +1,44 @@ +/* + * 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" +#include +#include + +static int +init_helper(void) +{ + /* This would be where we would initialize transports, etc. */ + return (0); +} + +int +nni_init(void) +{ + return (nni_plat_init(init_helper)); +} + +void +nni_fini(void) +{ + nni_plat_fini(); +} diff --git a/src/core/init.h b/src/core/init.h new file mode 100644 index 00000000..3a9221e4 --- /dev/null +++ b/src/core/init.h @@ -0,0 +1,41 @@ +/* + * 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. + */ + +#ifndef CORE_INIT_H +#define CORE_INIT_H + +#include "core/nng_impl.h" + +/* + * nni_init is called each time the user enters the library. It ensures that + * the library is initlialized properly, and also deals with checks such as + * whether the process has forked since last initialization. + */ +extern int nni_init(void); + +/* + * nni_fini tears everything down. In the future it may be used to ensure + * that all resources used by the library are released back to the system. + */ +extern void nni_fini(void); + +#endif /* CORE_INIT_H */ \ No newline at end of file diff --git a/src/core/nng_impl.h b/src/core/nng_impl.h index 4b62cfee..4d982113 100644 --- a/src/core/nng_impl.h +++ b/src/core/nng_impl.h @@ -40,6 +40,7 @@ #include "core/defs.h" #include "core/list.h" +#include "core/init.h" #include "core/message.h" #include "core/msgqueue.h" #include "core/panic.h" diff --git a/src/core/panic.c b/src/core/panic.c index 52dcb8c8..43c25beb 100644 --- a/src/core/panic.c +++ b/src/core/panic.c @@ -50,7 +50,7 @@ show_backtrace(void) return; } for (i = 1; i < nframes; i++) { - nni_debug_out(lines[i]); + nni_println(lines[i]); } } #endif @@ -75,10 +75,17 @@ nni_panic(const char *fmt, ...) (void) nni_vsnprintf(buf, sizeof (buf), fbuf, va); va_end(va); - nni_debug_out(buf); - nni_debug_out("This message is indicative of a BUG."); - nni_debug_out("Report this at http://github.com/nanomsg/nanomsg"); + nni_println(buf); + nni_println("This message is indicative of a BUG."); + nni_println("Report this at http://github.com/nanomsg/nanomsg"); show_backtrace(); - nni_abort(); + nni_plat_abort(); +} + +void +nni_println(const char *msg) +{ + /* TODO: support redirection of this later. */ + nni_plat_println(msg); } diff --git a/src/core/panic.h b/src/core/panic.h index 6deef964..2dc65f8c 100644 --- a/src/core/panic.h +++ b/src/core/panic.h @@ -34,4 +34,13 @@ */ extern void nni_panic(const char *, ...); +/* + * nni_println is used to print output to a debug console. This should only + * be used in the most dire of circumstances -- such as during an assertion + * failure that is going to cause the program to crash. After the string is + * emitted, a new line character is emitted, so the string should not + * include one. + */ +extern void nni_println(const char *); + #endif /* CORE_PANIC_H */ diff --git a/src/core/platform.c b/src/core/platform.c index d96ebe70..dbdf3a7c 100644 --- a/src/core/platform.c +++ b/src/core/platform.c @@ -26,8 +26,10 @@ #include "core/nng_impl.h" +#if 0 #if defined(PLATFORM_POSIX) #include "platform/posix/posix_impl.h" #else #error "unknown platform" #endif +#endif diff --git a/src/core/platform.h b/src/core/platform.h index f7af7c30..230055c3 100644 --- a/src/core/platform.h +++ b/src/core/platform.h @@ -56,32 +56,23 @@ */ /* - * nni_abort crashes the system; it should do whatever is appropriate + * nni_plat_abort crashes the system; it should do whatever is appropriate * for abnormal programs on the platform, such as calling abort(). */ -void nni_abort(void); +extern void nni_plat_abort(void); /* - * nni_vnsprintf is exactly like its POSIX counterpart. + * nni_plat_vnsprintf is exactly like its POSIX counterpart. * Some platforms (Windows!) need a special version of this. */ -void nni_vsnprintf(char *, size_t, const char *, va_list); +extern void nni_plat_vsnprintf(char *, size_t, const char *, va_list); /* - * nni_debug_output is used to emit debug messages. Typically this is used + * nni_plat_println is used to emit debug messages. Typically this is used * during core debugging, or to emit panic messages. Message content will * not contain newlines, but the output will add them. */ -void nni_debug_out(const char *); - -/* - * nni_set_debug_output is used to redirect debug output; for example an - * application could replace the default output routine with one that sends - * it's output to syslog. If NULL is specified, then a default handler - * used instead. The handler should add any newlines to the output as - * required. The default handler writes to standard error. - */ -void nni_set_debug_out(void (*)(const char *)); +extern void nni_plat_println(const char *); /* * nni_alloc allocates memory. In most cases this can just be malloc(). @@ -89,7 +80,7 @@ void nni_set_debug_out(void (*)(const char *)); * possible to use a slab allocator or somesuch. It is permissible for this * to return NULL if memory cannot be allocated. */ -void *nni_alloc(size_t); +extern void *nni_alloc(size_t); /* * nni_free frees memory allocated with nni_alloc. It takes a size because @@ -99,7 +90,7 @@ void *nni_alloc(size_t); * This routine does nothing if supplied with a NULL pointer and zero size. * Most implementations can just call free() here. */ -void nni_free(void *, size_t); +extern void nni_free(void *, size_t); typedef struct nni_mutex *nni_mutex_t; typedef struct nni_cond *nni_cond_t; @@ -107,32 +98,32 @@ typedef struct nni_cond *nni_cond_t; /* * Mutex handling. */ -int nni_mutex_create(nni_mutex_t *); -void nni_mutex_destroy(nni_mutex_t); -void nni_mutex_enter(nni_mutex_t); -void nni_mutex_exit(nni_mutex_t); -int nni_mutex_tryenter(nni_mutex_t); +extern int nni_mutex_create(nni_mutex_t *); +extern void nni_mutex_destroy(nni_mutex_t); +extern void nni_mutex_enter(nni_mutex_t); +extern void nni_mutex_exit(nni_mutex_t); +extern int nni_mutex_tryenter(nni_mutex_t); -int nni_cond_create(nni_cond_t *, nni_mutex_t); -void nni_cond_destroy(nni_cond_t); +extern int nni_cond_create(nni_cond_t *, nni_mutex_t); +extern void nni_cond_destroy(nni_cond_t); /* * nni_cond_broadcast wakes all waiters on the condition. This should be * called with the lock held. */ -void nni_cond_broadcast(nni_cond_t); +extern void nni_cond_broadcast(nni_cond_t); /* * nni_cond_signal wakes a signal waiter. */ -void nni_cond_signal(nni_cond_t); +extern void nni_cond_signal(nni_cond_t); /* * nni_condwait waits for a wake up on the condition variable. The * associated lock is atomically released and reacquired upon wake up. * Callers can be spuriously woken. The associated lock must be held. */ -void nni_cond_wait(nni_cond_t); +extern void nni_cond_wait(nni_cond_t); /* * nni_cond_timedwait waits for a wakeup on the condition variable, just @@ -143,33 +134,33 @@ void nni_cond_wait(nni_cond_t); * can be NNG_ETIMEDOUT. Note that it is permissible to wait for longer * than the timeout based on the resolution of your system clock. */ -int nni_cond_timedwait(nni_cond_t, uint64_t); +extern int nni_cond_timedwait(nni_cond_t, uint64_t); typedef struct nni_thread *nni_thread_t; /* * nni_thread_creates a thread that runs the given function. The thread * receives a single argument. */ -int nni_thread_create(nni_thread_t *, void (*fn)(void *), void *); +extern int nni_thread_create(nni_thread_t *, void (*fn)(void *), void *); /* * nni_thread_reap waits for the thread to exit, and then releases any * resources associated with the thread. After this returns, it * is an error to reference the thread in any further way. */ -void nni_thread_reap(nni_thread_t); +extern void nni_thread_reap(nni_thread_t); /* * nn_clock returns a number of microseconds since some arbitrary time * in the past. The values returned by nni_clock may be used with * nni_cond_timedwait. */ -uint64_t nni_clock(void); +extern uint64_t nni_clock(void); /* * nni_usleep sleeps for the specified number of microseconds (at least). */ -void nni_usleep(uint64_t); +extern void nni_usleep(uint64_t); /* * nni_platform_init is called to allow the platform the chance to @@ -178,15 +169,27 @@ void nni_usleep(uint64_t); * may be called at any point thereafter. It is permitted to return * an error if some critical failure inializing the platform occurs, * but once this succeeds, all future calls must succeed as well, unless - * nni_fini has been called. + * nni_plat_fini has been called. + * + * The function argument should be called if the platform has not initialized + * (i.e. exactly once please), and its result passed back to the caller. */ -int nni_platform_init(void); +extern int nni_plat_init(int (*)(void)); /* * nni_platform_fini is called to clean up resources. It is intended to * be called as the last thing executed in the library, and no other functions * will be called until nni_platform_init is called. */ -void nni_platform_fini(void); +extern void nni_plat_fini(void); + +/* + * Actual platforms we support. + */ +#if defined(PLATFORM_POSIX) +#include "platform/posix/posix_impl.h" +#else +#error "unknown platform" +#endif #endif /* CORE_PLATFORM_H */ diff --git a/src/core/snprintf.c b/src/core/snprintf.c index 574d537a..004916a2 100644 --- a/src/core/snprintf.c +++ b/src/core/snprintf.c @@ -34,3 +34,9 @@ nni_snprintf(char *dst, size_t sz, const char *fmt, ...) nni_vsnprintf(dst, sz, fmt, va); va_end(va); } + +void +nni_vsnprintf(char *dst, size_t sz, const char *fmt, va_list va) +{ + nni_plat_vsnprintf(dst, sz, fmt, va); +} diff --git a/src/core/snprintf.h b/src/core/snprintf.h index b98b6ee7..12d7f207 100644 --- a/src/core/snprintf.h +++ b/src/core/snprintf.h @@ -31,5 +31,6 @@ * from the platform. */ extern void nni_snprintf(char *, size_t, const char *, ...); +extern void nni_vsnprintf(char *, size_t, const char *, va_list); #endif /* CORE_SNPRINTF_H */ diff --git a/src/core/socket.c b/src/core/socket.c index f5904823..094f9ce0 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -110,8 +110,8 @@ nni_socket_close(nni_socket_t sock) */ /* XXX: close remaining pipes */ - while ((pipe = nni_list_first(&sock->nn_pipes)) != NULL) { - nni_list_remove(&sock->nn_pipes, pipe); + while ((pipe = nni_list_first(&sock->s_pipes)) != NULL) { + nni_list_remove(&sock->s_pipes, pipe); /* XXX: call nni_pipe_close, then nni_pipe_destroy */ } diff --git a/src/nng.c b/src/nng.c index 123c69bd..9b10c3b1 100644 --- a/src/nng.c +++ b/src/nng.c @@ -36,21 +36,27 @@ int nng_socket_create(nng_socket_t *s, uint16_t proto) { - nni_platform_init(); + int rv; + if ((rv = nni_init()) != 0) { + return (rv); + } return (nni_socket_create(s, proto)); } int nng_socket_close(nng_socket_t s) { - nni_platform_init(); + int rv; + if ((rv = nni_init()) != 0) { + return (rv); + } return (nni_socket_close(s)); } uint16_t nng_socket_protocol(nng_socket_t s) { - nni_platform_init(); + nni_init(); return (nni_socket_protocol(s)); } @@ -61,6 +67,7 @@ nng_socket_protocol(nng_socket_t s) const char * nng_strerror(int num) { + nni_init(); switch (num) { case 0: return ("Hunky dory"); /* what did you expect? */ @@ -96,13 +103,16 @@ nng_strerror(int num) int nng_msg_alloc(nng_msg_t *msgp, size_t size) { - nni_platform_init(); + int rv; + if ((rv = nni_init()) != 0) { + return (rv); + } return (nni_msg_alloc(msgp, size)); } void nng_msg_free(nng_msg_t msg) { - nni_platform_init(); + nni_init(); return (nni_msg_free(msg)); } \ No newline at end of file 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