diff options
| author | Garrett D'Amore <garrett@damore.org> | 2016-12-14 18:53:10 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2016-12-14 18:53:10 -0800 |
| commit | 3583d5e407476b8836228c0abc52c400d74aba61 (patch) | |
| tree | 113b6510b9f3d4f7eaffd331ecbd173724327c96 /src | |
| parent | e6476c5e4eac773aed4b76f16d3e76e7956c2468 (diff) | |
| download | nng-3583d5e407476b8836228c0abc52c400d74aba61.tar.gz nng-3583d5e407476b8836228c0abc52c400d74aba61.tar.bz2 nng-3583d5e407476b8836228c0abc52c400d74aba61.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 17 | ||||
| -rw-r--r-- | src/core/init.c | 44 | ||||
| -rw-r--r-- | src/core/init.h | 41 | ||||
| -rw-r--r-- | src/core/nng_impl.h | 1 | ||||
| -rw-r--r-- | src/core/panic.c | 17 | ||||
| -rw-r--r-- | src/core/panic.h | 9 | ||||
| -rw-r--r-- | src/core/platform.c | 2 | ||||
| -rw-r--r-- | src/core/platform.h | 73 | ||||
| -rw-r--r-- | src/core/snprintf.c | 6 | ||||
| -rw-r--r-- | src/core/snprintf.h | 1 | ||||
| -rw-r--r-- | src/core/socket.c | 4 | ||||
| -rw-r--r-- | src/nng.c | 20 | ||||
| -rw-r--r-- | src/platform/posix/posix_alloc.c (renamed from src/platform/posix/posix_alloc.h) | 8 | ||||
| -rw-r--r-- | src/platform/posix/posix_clock.c (renamed from src/platform/posix/posix_clock.h) | 5 | ||||
| -rw-r--r-- | src/platform/posix/posix_debug.c (renamed from src/platform/posix/posix_debug.h) | 22 | ||||
| -rw-r--r-- | src/platform/posix/posix_impl.h | 13 | ||||
| -rw-r--r-- | src/platform/posix/posix_synch.c (renamed from src/platform/posix/posix_synch.h) | 6 | ||||
| -rw-r--r-- | src/platform/posix/posix_thread.c (renamed from src/platform/posix/posix_thread.h) | 21 | ||||
| -rw-r--r-- | src/platform/posix/posix_vsnprintf.c (renamed from src/platform/posix/posix_vsnprintf.h) | 8 |
19 files changed, 237 insertions, 81 deletions
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 <garrett@damore.org> + * + * 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 <stdlib.h> +#include <stdio.h> + +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 <garrett@damore.org> + * + * 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 */ } @@ -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.h b/src/platform/posix/posix_alloc.c index 8451cc9c..b47a95b2 100644 --- a/src/platform/posix/posix_alloc.h +++ b/src/platform/posix/posix_alloc.c @@ -27,6 +27,12 @@ * get multiple symbols defined. */ +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_ALLOC + +#include <stdlib.h> + /* * POSIX memory allocation. This is pretty much standard C. */ @@ -43,3 +49,5 @@ 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_clock.h b/src/platform/posix/posix_clock.c index 07fc603d..c772263c 100644 --- a/src/platform/posix/posix_clock.h +++ b/src/platform/posix/posix_clock.c @@ -30,6 +30,9 @@ /* * POSIX clock stuff. */ +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_CLOCK #include <time.h> #include <errno.h> @@ -147,3 +150,5 @@ nni_usleep(uint64_t usec) } #endif /* NNG_USE_GETTIMEOFDAY */ + +#endif /* PLATFORM_POSIX_CLOCK */ diff --git a/src/platform/posix/posix_debug.h b/src/platform/posix/posix_debug.c index d25a6c11..6b2d875a 100644 --- a/src/platform/posix/posix_debug.h +++ b/src/platform/posix/posix_debug.c @@ -20,29 +20,23 @@ * IN THE SOFTWARE. */ +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_DEBUG + #include <stdlib.h> #include <stdio.h> -void (*debug_out)(const char *line); - void -nni_abort(void) +nni_plat_abort(void) { abort(); } void -nni_debug_out(const char *message) +nni_plat_println(const char *message) { - if (debug_out != NULL) { - debug_out(message); - } else { - (void) fprintf(stderr, "%s\n", message); - } + (void) fprintf(stderr, "%s\n", message); } -void -nni_set_debug_out(void (*out)(const char *)) -{ - debug_out = out; -} +#endif /* PLATFORM_POSIX_DEBUG */
\ No newline at end of file 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.h b/src/platform/posix/posix_synch.c index d3e44411..cb8b30dd 100644 --- a/src/platform/posix/posix_synch.h +++ b/src/platform/posix/posix_synch.c @@ -32,6 +32,10 @@ * pthreads. */ +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_SYNCH + #include <pthread.h> #include <time.h> @@ -235,3 +239,5 @@ nni_cond_timedwait(nni_cond_t c, uint64_t usec) } return (0); } + +#endif
\ No newline at end of file diff --git a/src/platform/posix/posix_thread.h b/src/platform/posix/posix_thread.c index 27ad888f..a41c7e7c 100644 --- a/src/platform/posix/posix_thread.h +++ b/src/platform/posix/posix_thread.c @@ -31,6 +31,10 @@ * POSIX threads. */ +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_THREAD + #include <pthread.h> #include <time.h> #include <string.h> @@ -66,7 +70,7 @@ nni_thread_create(nni_thread_t *tp, void (*fn)(void *), void *arg) thr->func = fn; thr->arg = arg; - if ((rv = pthread_create(&thr->tid, thr, thrfunc, arg)) != 0) { + if ((rv = pthread_create(&thr->tid, NULL, thrfunc, thr)) != 0) { nni_free(thr, sizeof (*thr)); return (NNG_ENOMEM); } @@ -91,8 +95,9 @@ atfork_child(void) } int -nni_platform_init(void) +nni_plat_init(int (*helper)(void)) { + int rv; if (plat_fork) { nni_panic("nng is fork-reentrant safe"); } @@ -108,14 +113,18 @@ nni_platform_init(void) pthread_mutex_unlock(&plat_lock); return (NNG_ENOMEM); } - plat_init = 1; + if ((rv = helper()) == 0) { + plat_init = 1; + } pthread_mutex_unlock(&plat_lock); - return (0); + return (rv); } void -nni_platform_fini(void) +nni_plat_fini(void) { /* XXX: NOTHING *YET* */ -}
\ No newline at end of file +} + +#endif
\ No newline at end of file diff --git a/src/platform/posix/posix_vsnprintf.h b/src/platform/posix/posix_vsnprintf.c index 8270d255..42367b4c 100644 --- a/src/platform/posix/posix_vsnprintf.h +++ b/src/platform/posix/posix_vsnprintf.c @@ -20,11 +20,17 @@ * IN THE SOFTWARE. */ +#include "core/nng_impl.h" + +#ifdef PLATFORM_POSIX_VSNPRINTF + #include <stdarg.h> #include <stdio.h> void -nni_vsnprintf(char *dst, size_t sz, const char *fmt, va_list va) +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 |
