diff options
| author | Garrett D'Amore <garrett@damore.org> | 2017-07-10 15:02:38 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2017-07-10 15:02:38 -0700 |
| commit | 795aebbee77bb74d8792df96dfe1aa79ec9548fc (patch) | |
| tree | 58c16424c16b9e71cebdceaee4507ab6608f80da /src/platform/posix | |
| parent | de90f97167d2df6739db47b2c6aad85f06250270 (diff) | |
| download | nng-795aebbee77bb74d8792df96dfe1aa79ec9548fc.tar.gz nng-795aebbee77bb74d8792df96dfe1aa79ec9548fc.tar.bz2 nng-795aebbee77bb74d8792df96dfe1aa79ec9548fc.zip | |
Give up on uncrustify; switch to clang-format.
Diffstat (limited to 'src/platform/posix')
| -rw-r--r-- | src/platform/posix/posix_aio.h | 15 | ||||
| -rw-r--r-- | src/platform/posix/posix_alloc.c | 2 | ||||
| -rw-r--r-- | src/platform/posix/posix_clock.c | 26 | ||||
| -rw-r--r-- | src/platform/posix/posix_config.h | 14 | ||||
| -rw-r--r-- | src/platform/posix/posix_debug.c | 15 | ||||
| -rw-r--r-- | src/platform/posix/posix_epdesc.c | 117 | ||||
| -rw-r--r-- | src/platform/posix/posix_impl.h | 23 | ||||
| -rw-r--r-- | src/platform/posix/posix_ipc.c | 56 | ||||
| -rw-r--r-- | src/platform/posix/posix_net.c | 62 | ||||
| -rw-r--r-- | src/platform/posix/posix_pipe.c | 24 | ||||
| -rw-r--r-- | src/platform/posix/posix_pipedesc.c | 79 | ||||
| -rw-r--r-- | src/platform/posix/posix_pollq.h | 30 | ||||
| -rw-r--r-- | src/platform/posix/posix_pollq_poll.c | 104 | ||||
| -rw-r--r-- | src/platform/posix/posix_rand.c | 25 | ||||
| -rw-r--r-- | src/platform/posix/posix_resolv_gai.c | 84 | ||||
| -rw-r--r-- | src/platform/posix/posix_thread.c | 55 |
16 files changed, 307 insertions, 424 deletions
diff --git a/src/platform/posix/posix_aio.h b/src/platform/posix/posix_aio.h index 13559c08..60594152 100644 --- a/src/platform/posix/posix_aio.h +++ b/src/platform/posix/posix_aio.h @@ -18,26 +18,25 @@ #include "core/nng_impl.h" -typedef struct nni_posix_pollq nni_posix_pollq; +typedef struct nni_posix_pollq nni_posix_pollq; +typedef struct nni_posix_pipedesc nni_posix_pipedesc; +typedef struct nni_posix_epdesc nni_posix_epdesc; -typedef struct nni_posix_pipedesc nni_posix_pipedesc; -typedef struct nni_posix_epdesc nni_posix_epdesc; - -extern int nni_posix_pipedesc_init(nni_posix_pipedesc **, int); +extern int nni_posix_pipedesc_init(nni_posix_pipedesc **, int); extern void nni_posix_pipedesc_fini(nni_posix_pipedesc *); extern void nni_posix_pipedesc_recv(nni_posix_pipedesc *, nni_aio *); extern void nni_posix_pipedesc_send(nni_posix_pipedesc *, nni_aio *); extern void nni_posix_pipedesc_close(nni_posix_pipedesc *); -extern int nni_posix_epdesc_init(nni_posix_epdesc **, const char *); +extern int nni_posix_epdesc_init(nni_posix_epdesc **, const char *); extern const char *nni_posix_epdesc_url(nni_posix_epdesc *); -extern void nni_posix_epdesc_set_local(nni_posix_epdesc *, void *, int); +extern void nni_posix_epdesc_set_local(nni_posix_epdesc *, void *, int); extern void nni_posix_epdesc_set_remote(nni_posix_epdesc *, void *, int); extern void nni_posix_epdesc_fini(nni_posix_epdesc *); extern void nni_posix_epdesc_close(nni_posix_epdesc *); extern void nni_posix_epdesc_connect(nni_posix_epdesc *, nni_aio *); -extern int nni_posix_epdesc_listen(nni_posix_epdesc *); +extern int nni_posix_epdesc_listen(nni_posix_epdesc *); extern void nni_posix_epdesc_accept(nni_posix_epdesc *, nni_aio *); #endif // PLATFORM_POSIX_AIO_H diff --git a/src/platform/posix/posix_alloc.c b/src/platform/posix/posix_alloc.c index aef5bbfc..a3cd29b9 100644 --- a/src/platform/posix/posix_alloc.c +++ b/src/platform/posix/posix_alloc.c @@ -20,7 +20,6 @@ nni_alloc(size_t sz) return (calloc(1, sz)); } - void nni_free(void *ptr, size_t size) { @@ -28,7 +27,6 @@ nni_free(void *ptr, size_t size) free(ptr); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_clock.c b/src/platform/posix/posix_clock.c index c87fb890..93e0978c 100644 --- a/src/platform/posix/posix_clock.c +++ b/src/platform/posix/posix_clock.c @@ -12,9 +12,9 @@ #ifdef PLATFORM_POSIX_CLOCK -#include <time.h> #include <errno.h> #include <string.h> +#include <time.h> #ifndef NNG_USE_GETTIMEOFDAY @@ -23,7 +23,7 @@ nni_time nni_plat_clock(void) { struct timespec ts; - nni_time usec; + nni_time usec; if (clock_gettime(NNG_USE_CLOCKID, &ts) != 0) { /* This should never ever occur. */ @@ -36,13 +36,12 @@ nni_plat_clock(void) return (usec); } - void nni_plat_usleep(nni_duration usec) { struct timespec ts; - ts.tv_sec = usec / 1000000; + ts.tv_sec = usec / 1000000; ts.tv_nsec = (usec % 1000000) * 1000; /* Do this in a loop, so that interrupts don't actually wake us. */ @@ -53,8 +52,7 @@ nni_plat_usleep(nni_duration usec) } } - -#else // NNG_USE_GETTIMEOFDAY +#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 @@ -65,9 +63,9 @@ nni_plat_usleep(nni_duration usec) // but if you change the clock by a large amount you might wonder what the // heck is happening until it does.) +#include <poll.h> #include <pthread.h> #include <sys/time.h> -#include <poll.h> nni_time nni_plat_clock(void) @@ -86,7 +84,6 @@ nni_plat_clock(void) return (usec); } - void nni_plat_usleep(nni_duration usec) { @@ -99,16 +96,16 @@ nni_plat_usleep(nni_duration usec) // So we can use poll() instead, which is rather coarse, but // pretty much guaranteed to work. struct pollfd pfd; - nni_time now; - nni_time expire; + nni_time now; + nni_time 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.fd = -1; pfd.events = 0; - now = nni_clock(); + now = nni_clock(); expire = now + usec; while (now < expire) { @@ -122,12 +119,11 @@ nni_plat_usleep(nni_duration usec) } } - -#endif // NNG_USE_GETTIMEOFDAY +#endif // NNG_USE_GETTIMEOFDAY #else // Suppress empty symbols warnings in ranlib. int nni_posix_clock_not_used = 0; -#endif // PLATFORM_POSIX_CLOCK +#endif // PLATFORM_POSIX_CLOCK diff --git a/src/platform/posix/posix_config.h b/src/platform/posix/posix_config.h index 7e82a050..8ed6387c 100644 --- a/src/platform/posix/posix_config.h +++ b/src/platform/posix/posix_config.h @@ -49,18 +49,18 @@ // exists but has somehow very very different semantics. We don't know // of any such concerns.) This won't be used if any of the other options // are defined and work. -#define NNG_USE_DEVURANDOM 1 +#define NNG_USE_DEVURANDOM 1 -#define NNG_USE_CLOCKID CLOCK_REALTIME +#define NNG_USE_CLOCKID CLOCK_REALTIME #ifndef CLOCK_REALTIME #define NNG_USE_GETTIMEOFDAY #elif !defined(NNG_USE_CLOCKID) -#define NNG_USE_CLOCKID CLOCK_MONOTONIC +#define NNG_USE_CLOCKID CLOCK_MONOTONIC #else -#define NNG_USE_CLOCKID CLOCK_REALTIME -#endif // CLOCK_REALTIME +#define NNG_USE_CLOCKID CLOCK_REALTIME +#endif // CLOCK_REALTIME -#define NNG_USE_POSIX_POLLQ_POLL 1 -#define NNG_USE_POSIX_RESOLV_GAI 1 +#define NNG_USE_POSIX_POLLQ_POLL 1 +#define NNG_USE_POSIX_RESOLV_GAI 1 #endif // PLATFORM_POSIX diff --git a/src/platform/posix/posix_debug.c b/src/platform/posix/posix_debug.c index b9321ea5..647d93ff 100644 --- a/src/platform/posix/posix_debug.c +++ b/src/platform/posix/posix_debug.c @@ -12,8 +12,8 @@ #ifdef PLATFORM_POSIX_DEBUG #include <errno.h> -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> void @@ -22,7 +22,6 @@ nni_plat_abort(void) abort(); } - void nni_plat_println(const char *message) { @@ -30,7 +29,6 @@ nni_plat_println(const char *message) fputc('\n', stderr); } - const char * nni_plat_strerror(int errnum) { @@ -40,7 +38,6 @@ nni_plat_strerror(int errnum) return (strerror(errnum)); } - // There are of course other errors than these, but these are the ones // that we might reasonably expect and want to handle "cleanly". Most of // the others should be handled by the system error code. Note that EFAULT @@ -50,10 +47,10 @@ nni_plat_strerror(int errnum) // error codes should exist, and be distinct positive numbers. (EWOULDBLOCK // and EAGAIN are permitted to have the same value.) static struct { - int posix_err; - int nng_err; -} -nni_plat_errnos[] = { + int posix_err; + int nng_err; +} nni_plat_errnos[] = { + // clang-format off { EINTR, NNG_EINTR }, { EINVAL, NNG_EINVAL }, { ENOMEM, NNG_ENOMEM }, @@ -86,6 +83,7 @@ nni_plat_errnos[] = { { EWOULDBLOCK, NNG_EAGAIN }, // must be last { 0, 0 }, + // clang-format on }; int @@ -108,7 +106,6 @@ nni_plat_errno(int errnum) return (NNG_ESYSERR + errnum); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_epdesc.c b/src/platform/posix/posix_epdesc.c index 77662ddd..a6de29e1 100644 --- a/src/platform/posix/posix_epdesc.c +++ b/src/platform/posix/posix_epdesc.c @@ -14,38 +14,37 @@ #include "platform/posix/posix_pollq.h" #include <errno.h> +#include <fcntl.h> +#include <netdb.h> +#include <poll.h> #include <stdlib.h> #include <string.h> -#include <sys/types.h> #include <sys/socket.h> +#include <sys/types.h> #include <sys/uio.h> #include <sys/un.h> -#include <fcntl.h> #include <unistd.h> -#include <poll.h> -#include <netdb.h> -#ifdef SOCK_CLOEXEC -#define NNI_STREAM_SOCKTYPE (SOCK_STREAM | SOCK_CLOEXEC) +#ifdef SOCK_CLOEXEC +#define NNI_STREAM_SOCKTYPE (SOCK_STREAM | SOCK_CLOEXEC) #else -#define NNI_STREAM_SOCKTYPE SOCK_STREAM +#define NNI_STREAM_SOCKTYPE SOCK_STREAM #endif struct nni_posix_epdesc { - int fd; - nni_list connectq; - nni_list acceptq; - nni_posix_pollq_node node; - nni_posix_pollq * pq; + int fd; + nni_list connectq; + nni_list acceptq; + nni_posix_pollq_node node; + nni_posix_pollq * pq; struct sockaddr_storage locaddr; struct sockaddr_storage remaddr; - socklen_t loclen; - socklen_t remlen; - const char * url; - nni_mtx mtx; + socklen_t loclen; + socklen_t remlen; + const char * url; + nni_mtx mtx; }; - static void nni_posix_epdesc_cancel(nni_aio *aio) { @@ -56,11 +55,10 @@ nni_posix_epdesc_cancel(nni_aio *aio) nni_mtx_unlock(&ed->mtx); } - static void nni_posix_epdesc_finish(nni_aio *aio, int rv, int newfd) { - nni_posix_epdesc *ed = aio->a_prov_data; + nni_posix_epdesc * ed = aio->a_prov_data; nni_posix_pipedesc *pd; // acceptq or connectq. @@ -78,13 +76,12 @@ nni_posix_epdesc_finish(nni_aio *aio, int rv, int newfd) nni_aio_finish(aio, rv, 0); } - static void nni_posix_epdesc_doconnect(nni_posix_epdesc *ed) { - nni_aio *aio; + nni_aio * aio; socklen_t sz; - int rv; + int rv; // Note that normally there will only be a single connect AIO... // A socket that is here will have *initiated* with a connect() @@ -94,7 +91,7 @@ nni_posix_epdesc_doconnect(nni_posix_epdesc *ed) // status of the connection attempt... while ((aio = nni_list_first(&ed->connectq)) != NULL) { rv = -1; - sz = sizeof (rv); + sz = sizeof(rv); if (getsockopt(ed->fd, SOL_SOCKET, SO_ERROR, &rv, &sz) < 0) { rv = errno; } @@ -121,24 +118,22 @@ nni_posix_epdesc_doconnect(nni_posix_epdesc *ed) } } - static void nni_posix_epdesc_doaccept(nni_posix_epdesc *ed) { - nni_aio *aio; - int newfd; + nni_aio * aio; + int newfd; struct sockaddr_storage ss; - socklen_t slen; + socklen_t slen; while ((aio = nni_list_first(&ed->acceptq)) != NULL) { - // We could argue that knowing the remote peer address would - // be nice. But frankly if someone wants it, they can just - // do getpeername(). +// We could argue that knowing the remote peer address would +// be nice. But frankly if someone wants it, they can just +// do getpeername(). #ifdef NNG_USE_ACCEPT4 newfd = accept4(ed->fd, NULL, NULL, SOCK_CLOEXEC); - if ((newfd < 0) && - ((errno == ENOSYS) || (errno == ENOTSUP))) { + if ((newfd < 0) && ((errno == ENOSYS) || (errno == ENOTSUP))) { newfd = accept(ed->fd, NULL, NULL); } #else @@ -172,13 +167,12 @@ nni_posix_epdesc_doaccept(nni_posix_epdesc *ed) } } - static void nni_posix_epdesc_doerror(nni_posix_epdesc *ed) { - nni_aio *aio; - int rv = 1; - socklen_t sz = sizeof (rv); + nni_aio * aio; + int rv = 1; + socklen_t sz = sizeof(rv); if (getsockopt(ed->fd, SOL_SOCKET, SO_ERROR, &rv, &sz) < 0) { rv = errno; @@ -196,11 +190,10 @@ nni_posix_epdesc_doerror(nni_posix_epdesc *ed) } } - static void nni_posix_epdesc_doclose(nni_posix_epdesc *ed) { - nni_aio *aio; + nni_aio * aio; struct sockaddr_un *sun; if (ed->fd != -1) { @@ -220,7 +213,6 @@ nni_posix_epdesc_doclose(nni_posix_epdesc *ed) } } - static void nni_posix_epdesc_cb(void *arg) { @@ -234,14 +226,14 @@ nni_posix_epdesc_cb(void *arg) if (ed->node.revents & POLLOUT) { nni_posix_epdesc_doconnect(ed); } - if (ed->node.revents & (POLLERR|POLLHUP)) { + if (ed->node.revents & (POLLERR | POLLHUP)) { nni_posix_epdesc_doerror(ed); } if (ed->node.revents & POLLNVAL) { nni_posix_epdesc_doclose(ed); } ed->node.revents = 0; - ed->node.events = 0; + ed->node.events = 0; if (!nni_list_empty(&ed->connectq)) { ed->node.events |= POLLOUT; @@ -252,7 +244,6 @@ nni_posix_epdesc_cb(void *arg) nni_mtx_unlock(&ed->mtx); } - void nni_posix_epdesc_close(nni_posix_epdesc *ed) { @@ -263,16 +254,15 @@ nni_posix_epdesc_close(nni_posix_epdesc *ed) nni_mtx_unlock(&ed->mtx); } - static int nni_posix_epdesc_parseaddr(char *pair, char **hostp, uint16_t *portp) { char *host, *port, *end; - char c; - int val; + char c; + int val; if (pair[0] == '[') { - host = pair+1; + host = pair + 1; // IP address enclosed ... for IPv6 usually. if ((end = strchr(host, ']')) == NULL) { return (NNG_EADDRINVAL); @@ -315,18 +305,17 @@ nni_posix_epdesc_parseaddr(char *pair, char **hostp, uint16_t *portp) return (0); } - int nni_posix_epdesc_listen(nni_posix_epdesc *ed) { - int len; + int len; struct sockaddr_storage *ss; - int rv; - int fd; + int rv; + int fd; nni_mtx_lock(&ed->mtx); - ss = &ed->locaddr; + ss = &ed->locaddr; len = ed->loclen; if ((fd = socket(ss->ss_family, NNI_STREAM_SOCKTYPE, 0)) < 0) { @@ -352,13 +341,12 @@ nni_posix_epdesc_listen(nni_posix_epdesc *ed) (void) fcntl(fd, F_SETFL, O_NONBLOCK); - ed->fd = fd; + ed->fd = fd; ed->node.fd = fd; nni_mtx_unlock(&ed->mtx); return (0); } - void nni_posix_epdesc_accept(nni_posix_epdesc *ed, nni_aio *aio) { @@ -394,7 +382,6 @@ nni_posix_epdesc_accept(nni_posix_epdesc *ed, nni_aio *aio) nni_mtx_unlock(&ed->mtx); } - void nni_posix_epdesc_connect(nni_posix_epdesc *ed, nni_aio *aio) { @@ -469,12 +456,11 @@ nni_posix_epdesc_connect(nni_posix_epdesc *ed, nni_aio *aio) nni_mtx_unlock(&ed->mtx); } - int nni_posix_epdesc_init(nni_posix_epdesc **edp, const char *url) { nni_posix_epdesc *ed; - int rv; + int rv; if ((ed = NNI_ALLOC_STRUCT(ed)) == NULL) { return (NNG_ENOMEM); @@ -490,12 +476,12 @@ nni_posix_epdesc_init(nni_posix_epdesc **edp, const char *url) // one. For now we just have a global pollq. Note that by tying // the ed to a single pollq we may get some kind of cache warmth. - ed->pq = nni_posix_pollq_get((int) nni_random()); - ed->fd = -1; + ed->pq = nni_posix_pollq_get((int) nni_random()); + ed->fd = -1; ed->node.index = 0; - ed->node.cb = nni_posix_epdesc_cb; - ed->node.data = ed; - ed->url = url; + ed->node.cb = nni_posix_epdesc_cb; + ed->node.data = ed; + ed->url = url; nni_aio_list_init(&ed->connectq); nni_aio_list_init(&ed->acceptq); @@ -504,18 +490,16 @@ nni_posix_epdesc_init(nni_posix_epdesc **edp, const char *url) return (0); } - const char * nni_posix_epdesc_url(nni_posix_epdesc *ed) { return (ed->url); } - void nni_posix_epdesc_set_local(nni_posix_epdesc *ed, void *sa, int len) { - if ((len < 0) || (len > sizeof (struct sockaddr_storage))) { + if ((len < 0) || (len > sizeof(struct sockaddr_storage))) { return; } nni_mtx_lock(&ed->mtx); @@ -524,11 +508,10 @@ nni_posix_epdesc_set_local(nni_posix_epdesc *ed, void *sa, int len) nni_mtx_unlock(&ed->mtx); } - void nni_posix_epdesc_set_remote(nni_posix_epdesc *ed, void *sa, int len) { - if ((len < 0) || (len > sizeof (struct sockaddr_storage))) { + if ((len < 0) || (len > sizeof(struct sockaddr_storage))) { return; } nni_mtx_lock(&ed->mtx); @@ -537,7 +520,6 @@ nni_posix_epdesc_set_remote(nni_posix_epdesc *ed, void *sa, int len) nni_mtx_unlock(&ed->mtx); } - void nni_posix_epdesc_fini(nni_posix_epdesc *ed) { @@ -548,7 +530,6 @@ nni_posix_epdesc_fini(nni_posix_epdesc *ed) NNI_FREE_STRUCT(ed); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index 407adf74..72842076 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -16,7 +16,7 @@ // when using PLATFORM_POSIX_CLOCK, your condition variable timeouts need // to use the same base clock values. Normally these should be used // together. Almost everything depends on PLATFORM_POSIX_DEBUG. -#ifdef PLATFORM_POSIX +#ifdef PLATFORM_POSIX #define PLATFORM_POSIX_ALLOC #define PLATFORM_POSIX_DEBUG #define PLATFORM_POSIX_CLOCK @@ -40,7 +40,7 @@ extern int nni_plat_errno(int); // Define types that this platform uses. #ifdef PLATFORM_POSIX_THREAD -extern int nni_plat_devnull; // open descriptor on /dev/null +extern int nni_plat_devnull; // open descriptor on /dev/null #include <pthread.h> @@ -48,28 +48,27 @@ extern int nni_plat_devnull; // open descriptor on /dev/null // elsewhere. struct nni_plat_mtx { - int init; - pthread_t owner; + int init; + pthread_t owner; pthread_mutex_t mtx; }; struct nni_plat_thr { - pthread_t tid; - void (*func)(void *); - void * arg; + pthread_t tid; + void (*func)(void *); + void *arg; }; struct nni_plat_cv { - pthread_cond_t cv; - nni_plat_mtx * mtx; + pthread_cond_t cv; + nni_plat_mtx * mtx; }; #endif - -extern int nni_posix_pollq_sysinit(void); +extern int nni_posix_pollq_sysinit(void); extern void nni_posix_pollq_sysfini(void); -extern int nni_posix_resolv_sysinit(void); +extern int nni_posix_resolv_sysinit(void); extern void nni_posix_resolv_sysfini(void); #endif // PLATFORM_POSIX_IMPL_H diff --git a/src/platform/posix/posix_ipc.c b/src/platform/posix/posix_ipc.c index 706ef15f..a362de6d 100644 --- a/src/platform/posix/posix_ipc.c +++ b/src/platform/posix/posix_ipc.c @@ -13,24 +13,23 @@ #include "platform/posix/posix_aio.h" #include <errno.h> -#include <stdlib.h> +#include <fcntl.h> +#include <netdb.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> -#include <sys/types.h> #include <sys/socket.h> +#include <sys/types.h> #include <sys/uio.h> #include <sys/un.h> -#include <fcntl.h> #include <unistd.h> -#include <netdb.h> -#ifdef SOCK_CLOEXEC -#define NNI_STREAM_SOCKTYPE (SOCK_STREAM | SOCK_CLOEXEC) +#ifdef SOCK_CLOEXEC +#define NNI_STREAM_SOCKTYPE (SOCK_STREAM | SOCK_CLOEXEC) #else -#define NNI_STREAM_SOCKTYPE SOCK_STREAM +#define NNI_STREAM_SOCKTYPE SOCK_STREAM #endif - // Solaris/SunOS systems define this, which collides with our symbol // names. Just undefine it now. #ifdef sun @@ -43,32 +42,32 @@ int nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) { - nni_posix_epdesc *ed; - int rv; + nni_posix_epdesc * ed; + int rv; struct sockaddr_un sun; - const char *path; + const char * path; if (strncmp(url, "ipc://", strlen("ipc://")) != 0) { return (NNG_EADDRINVAL); } - path = url + strlen("ipc://"); // skip the prefix. + path = url + strlen("ipc://"); // skip the prefix. // prepare the sockaddr_un sun.sun_family = AF_UNIX; - if (strlen(url) >= sizeof (sun.sun_path)) { + if (strlen(url) >= sizeof(sun.sun_path)) { return (NNG_EADDRINVAL); } - snprintf(sun.sun_path, sizeof (sun.sun_path), "%s", path); + snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path); if ((rv = nni_posix_epdesc_init(&ed, url)) != 0) { return (rv); } switch (mode) { case NNI_EP_MODE_DIAL: - nni_posix_epdesc_set_remote(ed, &sun, sizeof (sun)); + nni_posix_epdesc_set_remote(ed, &sun, sizeof(sun)); break; case NNI_EP_MODE_LISTEN: - nni_posix_epdesc_set_local(ed, &sun, sizeof (sun)); + nni_posix_epdesc_set_local(ed, &sun, sizeof(sun)); break; default: nni_posix_epdesc_fini(ed); @@ -79,21 +78,18 @@ nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) return (0); } - void nni_plat_ipc_ep_fini(nni_plat_ipc_ep *ep) { nni_posix_epdesc_fini((void *) ep); } - void nni_plat_ipc_ep_close(nni_plat_ipc_ep *ep) { nni_posix_epdesc_close((void *) ep); } - // UNIX DOMAIN SOCKETS -- these have names in the file namespace. // We are going to check to see if there was a name already there. // If there was, and nothing is listening (ECONNREFUSED), then we @@ -102,12 +98,12 @@ nni_plat_ipc_ep_close(nni_plat_ipc_ep *ep) static int nni_plat_ipc_remove_stale(const char *path) { - int fd; - int rv; + int fd; + int rv; struct sockaddr_un sun; sun.sun_family = AF_UNIX; - snprintf(sun.sun_path, sizeof (sun.sun_path), "%s", path); + snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path); if ((fd = socket(AF_UNIX, NNI_STREAM_SOCKTYPE, 0)) < 0) { return (nni_plat_errno(errno)); @@ -119,7 +115,7 @@ nni_plat_ipc_remove_stale(const char *path) // then the cleanup will fail. As this is supposed to be an // exceptional case, don't worry. (void) fcntl(fd, F_SETFL, O_NONBLOCK); - if (connect(fd, (void *) &sun, sizeof (sun)) < 0) { + if (connect(fd, (void *) &sun, sizeof(sun)) < 0) { if (errno == ECONNREFUSED) { (void) unlink(path); } @@ -128,14 +124,13 @@ nni_plat_ipc_remove_stale(const char *path) return (0); } - int nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) { - const char *path; - nni_posix_epdesc *ed = (void *) ep; + const char * path; + nni_posix_epdesc * ed = (void *) ep; struct sockaddr_un sun; - int rv; + int rv; path = nni_posix_epdesc_url(ed); path += strlen("ipc://"); @@ -146,49 +141,42 @@ nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) return (nni_posix_epdesc_listen(ed)); } - void nni_plat_ipc_ep_connect(nni_plat_ipc_ep *ep, nni_aio *aio) { nni_posix_epdesc_connect((void *) ep, aio); } - void nni_plat_ipc_ep_accept(nni_plat_ipc_ep *ep, nni_aio *aio) { nni_posix_epdesc_accept((void *) ep, aio); } - void nni_plat_ipc_pipe_fini(nni_plat_ipc_pipe *p) { nni_posix_pipedesc_fini((void *) p); } - void nni_plat_ipc_pipe_close(nni_plat_ipc_pipe *p) { nni_posix_pipedesc_close((void *) p); } - void nni_plat_ipc_pipe_send(nni_plat_ipc_pipe *p, nni_aio *aio) { nni_posix_pipedesc_send((void *) p, aio); } - void nni_plat_ipc_pipe_recv(nni_plat_ipc_pipe *p, nni_aio *aio) { nni_posix_pipedesc_recv((void *) p, aio); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_net.c b/src/platform/posix/posix_net.c index 26ec3632..78e1e6ee 100644 --- a/src/platform/posix/posix_net.c +++ b/src/platform/posix/posix_net.c @@ -12,72 +12,70 @@ #ifdef PLATFORM_POSIX_NET #include "platform/posix/posix_aio.h" +#include <arpa/inet.h> #include <errno.h> +#include <fcntl.h> +#include <netinet/in.h> +#include <netinet/tcp.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sys/types.h> #include <sys/socket.h> +#include <sys/types.h> #include <sys/uio.h> -#include <netinet/in.h> -#include <netinet/tcp.h> -#include <arpa/inet.h> -#include <fcntl.h> #include <unistd.h> static int nni_posix_tcp_addr(struct sockaddr_storage *ss, const nni_sockaddr *sa) { - struct sockaddr_in *sin; + struct sockaddr_in * sin; struct sockaddr_in6 *sin6; switch (sa->s_un.s_family) { case NNG_AF_INET: sin = (void *) ss; - memset(sin, 0, sizeof (*sin)); - sin->sin_family = PF_INET; - sin->sin_port = sa->s_un.s_in.sa_port; + memset(sin, 0, sizeof(*sin)); + sin->sin_family = PF_INET; + sin->sin_port = sa->s_un.s_in.sa_port; sin->sin_addr.s_addr = sa->s_un.s_in.sa_addr; - return (sizeof (*sin)); - + return (sizeof(*sin)); case NNG_AF_INET6: sin6 = (void *) ss; - memset(sin6, 0, sizeof (*sin6)); -#ifdef SIN6_LEN - sin6->sin6_len = sizeof (*sin6); + memset(sin6, 0, sizeof(*sin6)); +#ifdef SIN6_LEN + sin6->sin6_len = sizeof(*sin6); #endif sin6->sin6_family = PF_INET6; - sin6->sin6_port = sa->s_un.s_in6.sa_port; + sin6->sin6_port = sa->s_un.s_in6.sa_port; memcpy(sin6->sin6_addr.s6_addr, sa->s_un.s_in6.sa_addr, 16); - return (sizeof (*sin6)); + return (sizeof(*sin6)); } return (-1); } - extern int nni_tcp_parse_url(char *, char **, char **, char **, char **); int nni_plat_tcp_ep_init(nni_plat_tcp_ep **epp, const char *url, int mode) { - nni_posix_epdesc *ed; - char buf[NNG_MAXADDRLEN]; - int rv; - char *lhost, *rhost; - char *lserv, *rserv; - char *sep; + nni_posix_epdesc * ed; + char buf[NNG_MAXADDRLEN]; + int rv; + char * lhost, *rhost; + char * lserv, *rserv; + char * sep; struct sockaddr_storage ss; - int len; - int passive; - nni_aio aio; + int len; + int passive; + nni_aio aio; if ((rv = nni_posix_epdesc_init(&ed, url)) != 0) { return (rv); } // Make a local copy. - snprintf(buf, sizeof (buf), "%s", url); + snprintf(buf, sizeof(buf), "%s", url); nni_aio_init(&aio, NULL, NULL); if (mode == NNI_EP_MODE_DIAL) { @@ -138,70 +136,60 @@ done: return (rv); } - void nni_plat_tcp_ep_fini(nni_plat_tcp_ep *ep) { nni_posix_epdesc_fini((void *) ep); } - void nni_plat_tcp_ep_close(nni_plat_tcp_ep *ep) { nni_posix_epdesc_close((void *) ep); } - int nni_plat_tcp_ep_listen(nni_plat_tcp_ep *ep) { return (nni_posix_epdesc_listen((void *) ep)); } - void nni_plat_tcp_ep_connect(nni_plat_tcp_ep *ep, nni_aio *aio) { return (nni_posix_epdesc_connect((void *) ep, aio)); } - void nni_plat_tcp_ep_accept(nni_plat_tcp_ep *ep, nni_aio *aio) { return (nni_posix_epdesc_accept((void *) ep, aio)); } - void nni_plat_tcp_pipe_fini(nni_plat_tcp_pipe *p) { nni_posix_pipedesc_fini((void *) p); } - void nni_plat_tcp_pipe_close(nni_plat_tcp_pipe *p) { nni_posix_pipedesc_close((void *) p); } - void nni_plat_tcp_pipe_send(nni_plat_tcp_pipe *p, nni_aio *aio) { nni_posix_pipedesc_send((void *) p, aio); } - void nni_plat_tcp_pipe_recv(nni_plat_tcp_pipe *p, nni_aio *aio) { nni_posix_pipedesc_recv((void *) p, aio); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_pipe.c b/src/platform/posix/posix_pipe.c index c86022ef..78415d26 100644 --- a/src/platform/posix/posix_pipe.c +++ b/src/platform/posix/posix_pipe.c @@ -21,14 +21,14 @@ // Linux eventfd. This is lighter weight than pipes, and has better semantics // to boot. This is far better than say epoll(). -#include <sys/eventfd.h> #include <fcntl.h> +#include <sys/eventfd.h> #include <unistd.h> #ifdef EFD_CLOEXEC -#define NNI_EVENTFD_FLAGS EFD_CLOEXEC +#define NNI_EVENTFD_FLAGS EFD_CLOEXEC #else -#define NNI_EVENTFD_FLAGS 0 +#define NNI_EVENTFD_FLAGS 0 #endif int @@ -46,25 +46,22 @@ nni_plat_pipe_open(int *wfd, int *rfd) return (0); } - void nni_plat_pipe_raise(int wfd) { uint64_t one = 1; - (void) write(wfd, &one, sizeof (one)); + (void) write(wfd, &one, sizeof(one)); } - void nni_plat_pipe_clear(int rfd) { uint64_t val; - (void) read(rfd, &val, sizeof (val)); + (void) read(rfd, &val, sizeof(val)); } - void nni_plat_pipe_close(int wfd, int rfd) { @@ -72,11 +69,10 @@ nni_plat_pipe_close(int wfd, int rfd) (void) close(wfd); } - #else // NNG_USE_EVENTFD -#include <unistd.h> #include <fcntl.h> +#include <unistd.h> int nni_plat_pipe_open(int *wfd, int *rfd) @@ -97,7 +93,6 @@ nni_plat_pipe_open(int *wfd, int *rfd) return (0); } - void nni_plat_pipe_raise(int wfd) { @@ -106,23 +101,21 @@ nni_plat_pipe_raise(int wfd) (void) write(wfd, &c, 1); } - void nni_plat_pipe_clear(int rfd) { char buf[32]; - int rv; + int rv; for (;;) { // Completely drain the pipe, but don't wait. This coalesces // events somewhat. - if (read(rfd, buf, sizeof (buf)) <= 0) { + if (read(rfd, buf, sizeof(buf)) <= 0) { return; } } } - void nni_plat_pipe_close(int wfd, int rfd) { @@ -130,7 +123,6 @@ nni_plat_pipe_close(int wfd, int rfd) close(rfd); } - #endif // NNG_USE_EVENTFD #else diff --git a/src/platform/posix/posix_pipedesc.c b/src/platform/posix/posix_pipedesc.c index c509643e..faab91ad 100644 --- a/src/platform/posix/posix_pipedesc.c +++ b/src/platform/posix/posix_pipedesc.c @@ -14,29 +14,27 @@ #include "platform/posix/posix_pollq.h" #include <errno.h> +#include <fcntl.h> +#include <poll.h> #include <stdlib.h> #include <string.h> -#include <sys/types.h> #include <sys/socket.h> +#include <sys/types.h> #include <sys/uio.h> -#include <fcntl.h> #include <unistd.h> -#include <poll.h> - // nni_posix_pipedesc is a descriptor kept one per transport pipe (i.e. open // file descriptor for TCP socket, etc.) This contains the list of pending // aios for that underlying socket, as well as the socket itself. struct nni_posix_pipedesc { - nni_posix_pollq * pq; - int fd; - nni_list readq; - nni_list writeq; - nni_posix_pollq_node node; - nni_mtx mtx; + nni_posix_pollq * pq; + int fd; + nni_list readq; + nni_list writeq; + nni_posix_pollq_node node; + nni_mtx mtx; }; - static void nni_posix_pipedesc_finish(nni_aio *aio, int rv) { @@ -44,24 +42,23 @@ nni_posix_pipedesc_finish(nni_aio *aio, int rv) nni_aio_finish(aio, rv, aio->a_count); } - static void nni_posix_pipedesc_dowrite(nni_posix_pipedesc *pd) { - int n; - int rv; - int i; - struct iovec iovec[4]; + int n; + int rv; + int i; + struct iovec iovec[4]; struct iovec *iovp; - nni_aio *aio; + nni_aio * aio; while ((aio = nni_list_first(&pd->writeq)) != NULL) { for (i = 0; i < aio->a_niov; i++) { - iovec[i].iov_len = aio->a_iov[i].iov_len; + iovec[i].iov_len = aio->a_iov[i].iov_len; iovec[i].iov_base = aio->a_iov[i].iov_buf; } iovp = &iovec[0]; - rv = 0; + rv = 0; n = writev(pd->fd, iovp, aio->a_niov); if (n < 0) { @@ -92,7 +89,7 @@ nni_posix_pipedesc_dowrite(nni_posix_pipedesc *pd) // remaininng ones up, and decrement count handled. n -= aio->a_iov[0].iov_len; for (i = 1; i < aio->a_niov; i++) { - aio->a_iov[i-1] = aio->a_iov[i]; + aio->a_iov[i - 1] = aio->a_iov[i]; } NNI_ASSERT(aio->a_niov > 0); aio->a_niov--; @@ -106,24 +103,23 @@ nni_posix_pipedesc_dowrite(nni_posix_pipedesc *pd) } } - static void nni_posix_pipedesc_doread(nni_posix_pipedesc *pd) { - int n; - int rv; - int i; - struct iovec iovec[4]; + int n; + int rv; + int i; + struct iovec iovec[4]; struct iovec *iovp; - nni_aio *aio; + nni_aio * aio; while ((aio = nni_list_first(&pd->readq)) != NULL) { for (i = 0; i < aio->a_niov; i++) { - iovec[i].iov_len = aio->a_iov[i].iov_len; + iovec[i].iov_len = aio->a_iov[i].iov_len; iovec[i].iov_base = aio->a_iov[i].iov_buf; } iovp = &iovec[0]; - rv = 0; + rv = 0; n = readv(pd->fd, iovp, aio->a_niov); if (n < 0) { @@ -160,7 +156,7 @@ nni_posix_pipedesc_doread(nni_posix_pipedesc *pd) // remaininng ones up, and decrement count handled. n -= aio->a_iov[0].iov_len; for (i = 1; i < aio->a_niov; i++) { - aio->a_iov[i-1] = aio->a_iov[i]; + aio->a_iov[i - 1] = aio->a_iov[i]; } NNI_ASSERT(aio->a_niov > 0); aio->a_niov--; @@ -174,7 +170,6 @@ nni_posix_pipedesc_doread(nni_posix_pipedesc *pd) } } - static void nni_posix_pipedesc_doclose(nni_posix_pipedesc *pd) { @@ -194,7 +189,6 @@ nni_posix_pipedesc_doclose(nni_posix_pipedesc *pd) } } - static void nni_posix_pipedesc_cb(void *arg) { @@ -207,12 +201,12 @@ nni_posix_pipedesc_cb(void *arg) if (pd->node.revents & POLLOUT) { nni_posix_pipedesc_dowrite(pd); } - if (pd->node.revents & (POLLHUP|POLLERR|POLLNVAL)) { + if (pd->node.revents & (POLLHUP | POLLERR | POLLNVAL)) { nni_posix_pipedesc_doclose(pd); } pd->node.revents = 0; - pd->node.events = 0; + pd->node.events = 0; if (!nni_list_empty(&pd->writeq)) { pd->node.events |= POLLOUT; @@ -228,7 +222,6 @@ nni_posix_pipedesc_cb(void *arg) nni_mtx_unlock(&pd->mtx); } - void nni_posix_pipedesc_close(nni_posix_pipedesc *pd) { @@ -239,7 +232,6 @@ nni_posix_pipedesc_close(nni_posix_pipedesc *pd) nni_mtx_unlock(&pd->mtx); } - static void nni_posix_pipedesc_cancel(nni_aio *aio) { @@ -250,7 +242,6 @@ nni_posix_pipedesc_cancel(nni_aio *aio) nni_mtx_unlock(&pd->mtx); } - void nni_posix_pipedesc_recv(nni_posix_pipedesc *pd, nni_aio *aio) { @@ -280,7 +271,6 @@ nni_posix_pipedesc_recv(nni_posix_pipedesc *pd, nni_aio *aio) nni_mtx_unlock(&pd->mtx); } - void nni_posix_pipedesc_send(nni_posix_pipedesc *pd, nni_aio *aio) { @@ -310,17 +300,16 @@ nni_posix_pipedesc_send(nni_posix_pipedesc *pd, nni_aio *aio) nni_mtx_unlock(&pd->mtx); } - int nni_posix_pipedesc_init(nni_posix_pipedesc **pdp, int fd) { nni_posix_pipedesc *pd; - int rv; + int rv; if ((pd = NNI_ALLOC_STRUCT(pd)) == NULL) { return (NNG_ENOMEM); } - memset(pd, 0, sizeof (*pd)); + memset(pd, 0, sizeof(*pd)); // We could randomly choose a different pollq, or for efficiencies // sake we could take a modulo of the file desc number to choose @@ -331,10 +320,10 @@ nni_posix_pipedesc_init(nni_posix_pipedesc **pdp, int fd) NNI_FREE_STRUCT(pd); return (rv); } - pd->pq = nni_posix_pollq_get(fd); - pd->fd = fd; - pd->node.fd = fd; - pd->node.cb = nni_posix_pipedesc_cb; + pd->pq = nni_posix_pollq_get(fd); + pd->fd = fd; + pd->node.fd = fd; + pd->node.cb = nni_posix_pipedesc_cb; pd->node.data = pd; (void) fcntl(pd->fd, F_SETFL, O_NONBLOCK); @@ -346,7 +335,6 @@ nni_posix_pipedesc_init(nni_posix_pipedesc **pdp, int fd) return (0); } - void nni_posix_pipedesc_fini(nni_posix_pipedesc *pd) { @@ -358,7 +346,6 @@ nni_posix_pipedesc_fini(nni_posix_pipedesc *pd) NNI_FREE_STRUCT(pd); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_pollq.h b/src/platform/posix/posix_pollq.h index 224bf765..e42b120d 100644 --- a/src/platform/posix/posix_pollq.h +++ b/src/platform/posix/posix_pollq.h @@ -21,27 +21,27 @@ #include "core/nng_impl.h" #include <poll.h> -typedef struct nni_posix_pollq_node nni_posix_pollq_node; -typedef struct nni_posix_pollq nni_posix_pollq; +typedef struct nni_posix_pollq_node nni_posix_pollq_node; +typedef struct nni_posix_pollq nni_posix_pollq; struct nni_posix_pollq_node { - nni_posix_pollq * pq; // associated pollq - nni_list_node node; // linkage into the pollq list - int index; // used by the poller impl - int armed; // used by the poller impl - int fd; // file descriptor to poll - int events; // events to watch for - int revents; // events received - void * data; // user data - nni_cb cb; // user callback on event + nni_posix_pollq *pq; // associated pollq + nni_list_node node; // linkage into the pollq list + int index; // used by the poller impl + int armed; // used by the poller impl + int fd; // file descriptor to poll + int events; // events to watch for + int revents; // events received + void * data; // user data + nni_cb cb; // user callback on event }; extern nni_posix_pollq *nni_posix_pollq_get(int); -extern int nni_posix_pollq_submit(nni_posix_pollq *, nni_posix_pollq_node *); +extern int nni_posix_pollq_submit(nni_posix_pollq *, nni_posix_pollq_node *); extern void nni_posix_pollq_cancel(nni_posix_pollq *, nni_posix_pollq_node *); -extern int nni_posix_pollq_sysinit(void); +extern int nni_posix_pollq_sysinit(void); extern void nni_posix_pollq_sysfini(void); -#endif // PLATFORM_POSIX +#endif // PLATFORM_POSIX -#endif // PLATFORM_POSIX_POLLQ_H +#endif // PLATFORM_POSIX_POLLQ_H diff --git a/src/platform/posix/posix_pollq_poll.c b/src/platform/posix/posix_pollq_poll.c index cd2eea7b..fe1359ec 100644 --- a/src/platform/posix/posix_pollq_poll.c +++ b/src/platform/posix/posix_pollq_poll.c @@ -13,15 +13,14 @@ #ifdef NNG_USE_POSIX_POLLQ_POLL #include <errno.h> +#include <fcntl.h> +#include <poll.h> #include <stdlib.h> #include <string.h> -#include <sys/types.h> #include <sys/socket.h> +#include <sys/types.h> #include <sys/uio.h> -#include <fcntl.h> #include <unistd.h> -#include <poll.h> - // POSIX AIO using poll(). We use a single poll thread to perform // I/O operations for the entire system. This isn't entirely scalable, @@ -33,33 +32,31 @@ // nni_posix_pollq is a work structure used by the poller thread, that keeps // track of all the underlying pipe handles and so forth being used by poll(). struct nni_posix_pollq { - nni_mtx mtx; - nni_cv cv; - struct pollfd * fds; - struct pollfd * newfds; - int nfds; - int nnewfds; - int wakewfd; // write side of waker pipe - int wakerfd; // read side of waker pipe - int close; // request for worker to exit - int started; - nni_thr thr; // worker thread - nni_list nodes; // poll list - nni_list notify; // notify list - int nnodes; // num of nodes in nodes list - int cancel; // waiters for cancellation - int inpoll; // poller asleep in poll - - nni_posix_pollq_node * active; // active node (in callback) + nni_mtx mtx; + nni_cv cv; + struct pollfd *fds; + struct pollfd *newfds; + int nfds; + int nnewfds; + int wakewfd; // write side of waker pipe + int wakerfd; // read side of waker pipe + int close; // request for worker to exit + int started; + nni_thr thr; // worker thread + nni_list nodes; // poll list + nni_list notify; // notify list + int nnodes; // num of nodes in nodes list + int cancel; // waiters for cancellation + int inpoll; // poller asleep in poll + + nni_posix_pollq_node *active; // active node (in callback) }; - - static int nni_posix_pollq_poll_grow(nni_posix_pollq *pq) { - int grow = pq->nnodes + 2; // one for us, one for waker - int noldfds; + int grow = pq->nnodes + 2; // one for us, one for waker + int noldfds; struct pollfd *oldfds; struct pollfd *newfds; @@ -72,34 +69,32 @@ nni_posix_pollq_poll_grow(nni_posix_pollq *pq) // Maybe we are adding a *lot* of pipes at once, and have to grow // multiple times before the poller gets scheduled. In that case // toss the old array before we finish. - oldfds = pq->newfds; + oldfds = pq->newfds; noldfds = pq->nnewfds; - if ((newfds = nni_alloc(grow * sizeof (struct pollfd))) == NULL) { + if ((newfds = nni_alloc(grow * sizeof(struct pollfd))) == NULL) { return (NNG_ENOMEM); } - - pq->newfds = newfds; + pq->newfds = newfds; pq->nnewfds = grow; if (noldfds != 0) { - nni_free(oldfds, noldfds * sizeof (struct pollfd)); + nni_free(oldfds, noldfds * sizeof(struct pollfd)); } return (0); } - static void nni_posix_poll_thr(void *arg) { - nni_posix_pollq *pollq = arg; + nni_posix_pollq * pollq = arg; nni_posix_pollq_node *node, *nextnode; nni_mtx_lock(&pollq->mtx); for (;;) { - int rv; - int nfds; + int rv; + int nfds; struct pollfd *fds; if (pollq->close) { @@ -109,28 +104,28 @@ nni_posix_poll_thr(void *arg) if (pollq->newfds != NULL) { // We have "grown" by the caller. Free up the old // space, and start using the new. - nni_free(pollq->fds, - pollq->nfds * sizeof (struct pollfd)); - pollq->fds = pollq->newfds; - pollq->nfds = pollq->nnewfds; + nni_free( + pollq->fds, pollq->nfds * sizeof(struct pollfd)); + pollq->fds = pollq->newfds; + pollq->nfds = pollq->nnewfds; pollq->newfds = NULL; } - fds = pollq->fds; + fds = pollq->fds; nfds = 0; // The waker pipe is set up so that we will be woken // when it is written (this allows us to be signaled). - fds[nfds].fd = pollq->wakerfd; - fds[nfds].events = POLLIN; + fds[nfds].fd = pollq->wakerfd; + fds[nfds].events = POLLIN; fds[nfds].revents = 0; nfds++; // Set up the poll list. NNI_LIST_FOREACH (&pollq->nodes, node) { - fds[nfds].fd = node->fd; - fds[nfds].events = node->armed; + fds[nfds].fd = node->fd; + fds[nfds].events = node->armed; fds[nfds].revents = 0; - node->index = nfds; + node->index = nfds; nfds++; } @@ -150,7 +145,6 @@ nni_posix_poll_thr(void *arg) continue; } - // If the waker pipe was signaled, read from it. if (fds[0].revents & POLLIN) { NNI_ASSERT(fds[0].fd == pollq->wakerfd); @@ -183,7 +177,7 @@ nni_posix_poll_thr(void *arg) } // Clear the index for the next time around. - node->index = 0; + node->index = 0; node->revents = fds[index].revents; // Now we move this node to the callback list. @@ -216,7 +210,6 @@ nni_posix_poll_thr(void *arg) nni_mtx_unlock(&pollq->mtx); } - void nni_posix_pollq_cancel(nni_posix_pollq *pq, nni_posix_pollq_node *node) { @@ -238,7 +231,6 @@ nni_posix_pollq_cancel(nni_posix_pollq *pq, nni_posix_pollq_node *node) nni_mtx_unlock(&pq->mtx); } - int nni_posix_pollq_submit(nni_posix_pollq *pq, nni_posix_pollq_node *node) { @@ -278,13 +270,12 @@ nni_posix_pollq_submit(nni_posix_pollq *pq, nni_posix_pollq_node *node) return (0); } - static void nni_posix_pollq_fini(nni_posix_pollq *pq) { if (pq->started) { nni_mtx_lock(&pq->mtx); - pq->close = 1; + pq->close = 1; pq->started = 0; nni_plat_pipe_raise(pq->wakewfd); @@ -298,12 +289,11 @@ nni_posix_pollq_fini(nni_posix_pollq *pq) nni_plat_pipe_close(pq->wakewfd, pq->wakerfd); pq->wakewfd = pq->wakerfd = -1; } - nni_free(pq->newfds, pq->nnewfds * sizeof (struct pollfd)); - nni_free(pq->fds, pq->nfds * sizeof (struct pollfd)); + nni_free(pq->newfds, pq->nnewfds * sizeof(struct pollfd)); + nni_free(pq->fds, pq->nfds * sizeof(struct pollfd)); nni_mtx_fini(&pq->mtx); } - static int nni_posix_pollq_init(nni_posix_pollq *pq) { @@ -313,7 +303,7 @@ nni_posix_pollq_init(nni_posix_pollq *pq) NNI_LIST_INIT(&pq->notify, nni_posix_pollq_node, node); pq->wakewfd = -1; pq->wakerfd = -1; - pq->close = 0; + pq->close = 0; if (((rv = nni_mtx_init(&pq->mtx)) != 0) || ((rv = nni_cv_init(&pq->cv, &pq->mtx)) != 0) || @@ -328,7 +318,6 @@ nni_posix_pollq_init(nni_posix_pollq *pq) return (0); } - // We use a single pollq for the entire system, which means only a single // thread is polling. This may be somewhat less than optimally efficient, // and it may be worth investigating having multiple threads to improve @@ -344,7 +333,6 @@ nni_posix_pollq_get(int fd) return (&nni_posix_global_pollq); } - int nni_posix_pollq_sysinit(void) { @@ -354,14 +342,12 @@ nni_posix_pollq_sysinit(void) return (rv); } - void nni_posix_pollq_sysfini(void) { nni_posix_pollq_fini(&nni_posix_global_pollq); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_rand.c b/src/platform/posix/posix_rand.c index 11d166bd..4322e491 100644 --- a/src/platform/posix/posix_rand.c +++ b/src/platform/posix/posix_rand.c @@ -12,14 +12,14 @@ #ifdef PLATFORM_POSIX_RANDOM -#include <time.h> #include <errno.h> +#include <fcntl.h> #include <stdlib.h> +#include <string.h> #include <sys/types.h> -#include <unistd.h> #include <sys/utsname.h> -#include <fcntl.h> -#include <string.h> +#include <time.h> +#include <unistd.h> #ifdef NNG_USE_GETRANDOM #include <linux/random.h> @@ -34,17 +34,17 @@ // really really you want to have more data than this available, especially // for cryptographic applications. struct nni_plat_prng_x { - nni_time now; - pid_t pid; - uid_t uid; - struct utsname uts; + nni_time now; + pid_t pid; + uid_t uid; + struct utsname uts; }; void nni_plat_seed_prng(void *buf, size_t bufsz) { struct nni_plat_prng_x x; - int i; + int i; memset(buf, 0, bufsz); @@ -75,21 +75,20 @@ nni_plat_seed_prng(void *buf, size_t bufsz) // limited of systems, we have at least *some* level of randomness. // The mixing is done in a way to avoid diminishing entropy we may // have already collected. - memset(&x, 0, sizeof (x)); // satisfy valgrind + memset(&x, 0, sizeof(x)); // satisfy valgrind x.now = nni_clock(); x.pid = getpid(); x.uid = getuid(); uname(&x.uts); - for (i = 0; (i < bufsz) && (i < sizeof (x)); i++) { + for (i = 0; (i < bufsz) && (i < sizeof(x)); i++) { ((uint8_t *) buf)[i] ^= ((uint8_t *) &x)[i]; } } - #else // Suppress empty symbols warnings in ranlib. int nni_posix_rand_not_used = 0; -#endif // PLATFORM_POSIX_RANDOM +#endif // PLATFORM_POSIX_RANDOM diff --git a/src/platform/posix/posix_resolv_gai.c b/src/platform/posix/posix_resolv_gai.c index 4283e9f6..cc27b213 100644 --- a/src/platform/posix/posix_resolv_gai.c +++ b/src/platform/posix/posix_resolv_gai.c @@ -7,24 +7,22 @@ // found online at https://opensource.org/licenses/MIT. // - #include "core/nng_impl.h" #ifdef NNG_USE_POSIX_RESOLV_GAI #include "platform/posix/posix_aio.h" #include <errno.h> +#include <netdb.h> +#include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sys/types.h> #include <sys/socket.h> +#include <sys/types.h> #include <sys/uio.h> #include <sys/un.h> #include <unistd.h> -#include <netdb.h> -#include <netinet/in.h> - // We use a single resolver taskq - but we allocate a few threads // for it to ensure that names can be looked up concurrently. This isn't @@ -35,25 +33,23 @@ // thread-safe getaddrinfo(). In that case they should set this to 1. #ifndef NNG_POSIX_RESOLV_CONCURRENCY -#define NNG_POSIX_RESOLV_CONCURRENCY 4 +#define NNG_POSIX_RESOLV_CONCURRENCY 4 #endif - static nni_taskq *nni_posix_resolv_tq = NULL; -static nni_mtx nni_posix_resolv_mtx; +static nni_mtx nni_posix_resolv_mtx; -typedef struct nni_posix_resolv_item nni_posix_resolv_item; +typedef struct nni_posix_resolv_item nni_posix_resolv_item; struct nni_posix_resolv_item { - int family; - int passive; - const char * name; - const char * serv; - int proto; - nni_aio * aio; - nni_taskq_ent tqe; + int family; + int passive; + const char * name; + const char * serv; + int proto; + nni_aio * aio; + nni_taskq_ent tqe; }; - static void nni_posix_resolv_finish(nni_posix_resolv_item *item, int rv) { @@ -64,7 +60,6 @@ nni_posix_resolv_finish(nni_posix_resolv_item *item, int rv) NNI_FREE_STRUCT(item); } - static void nni_posix_resolv_cancel(nni_aio *aio) { @@ -81,7 +76,6 @@ nni_posix_resolv_cancel(nni_aio *aio) NNI_FREE_STRUCT(item); } - static int nni_posix_gai_errno(int rv) { @@ -110,16 +104,15 @@ nni_posix_gai_errno(int rv) } } - static void nni_posix_resolv_task(void *arg) { nni_posix_resolv_item *item = arg; - nni_aio *aio = item->aio; - struct addrinfo hints; - struct addrinfo *results; - struct addrinfo *probe; - int i, rv; + nni_aio * aio = item->aio; + struct addrinfo hints; + struct addrinfo * results; + struct addrinfo * probe; + int i, rv; results = NULL; @@ -129,7 +122,7 @@ nni_posix_resolv_task(void *arg) case AF_UNSPEC: // We treat these all as IP addresses. The service and the // host part are split. - memset(&hints, 0, sizeof (hints)); + memset(&hints, 0, sizeof(hints)); if (item->passive) { hints.ai_flags |= AI_PASSIVE; } @@ -137,11 +130,11 @@ nni_posix_resolv_task(void *arg) hints.ai_flags |= AI_ADDRCONFIG; #endif hints.ai_protocol = item->proto; - hints.ai_family = item->family; + hints.ai_family = item->family; if (item->family == AF_INET6) { - // We prefer to have v4mapped addresses if a remote - // v4 address isn't avaiable. And we prefer to only - // do this if we actually support v6. +// We prefer to have v4mapped addresses if a remote +// v4 address isn't avaiable. And we prefer to only +// do this if we actually support v6. #if defined(AI_V4MAPPED_CFG) hints.ai_flags |= AI_V4MAPPED_CFG; #elif defined(AI_V4MAPPED) @@ -174,27 +167,27 @@ nni_posix_resolv_task(void *arg) aio->a_addrs = NNI_ALLOC_STRUCTS(aio->a_addrs, aio->a_naddrs); if (aio->a_addrs == NULL) { aio->a_naddrs = 0; - rv = NNG_ENOMEM; + rv = NNG_ENOMEM; break; } i = 0; for (probe = results; probe != NULL; probe = probe->ai_next) { - struct sockaddr_in *sin; + struct sockaddr_in * sin; struct sockaddr_in6 *sin6; - nng_sockaddr *sa = &aio->a_addrs[i]; + nng_sockaddr * sa = &aio->a_addrs[i]; switch (probe->ai_addr->sa_family) { case AF_INET: sin = (void *) probe->ai_addr; sa->s_un.s_in.sa_family = NNG_AF_INET; - sa->s_un.s_in.sa_port = sin->sin_port; - sa->s_un.s_in.sa_addr = sin->sin_addr.s_addr; + sa->s_un.s_in.sa_port = sin->sin_port; + sa->s_un.s_in.sa_addr = sin->sin_addr.s_addr; i++; break; case AF_INET6: sin6 = (void *) probe->ai_addr; sa->s_un.s_in6.sa_family = NNG_AF_INET6; - sa->s_un.s_in6.sa_port = sin6->sin6_port; + sa->s_un.s_in6.sa_port = sin6->sin6_port; memcpy(sa->s_un.s_in6.sa_addr, sin6->sin6_addr.s6_addr, 16); i++; @@ -222,13 +215,12 @@ nni_posix_resolv_task(void *arg) nni_mtx_unlock(&nni_posix_resolv_mtx); } - static void nni_posix_resolv_ip(const char *host, const char *serv, int passive, int family, int proto, nni_aio *aio) { nni_posix_resolv_item *item; - int rv; + int rv; if ((aio->a_naddrs != 0) && (aio->a_addrs != NULL)) { NNI_FREE_STRUCTS(aio->a_addrs, aio->a_naddrs); @@ -253,10 +245,10 @@ nni_posix_resolv_ip(const char *host, const char *serv, int passive, } // NB: host and serv must remain valid until this is completed. item->passive = passive; - item->name = host; - item->serv = serv; - item->proto = proto; - item->aio = aio; + item->name = host; + item->serv = serv; + item->proto = proto; + item->aio = aio; nni_mtx_lock(&nni_posix_resolv_mtx); // If we were stopped, we're done... @@ -273,15 +265,13 @@ nni_posix_resolv_ip(const char *host, const char *serv, int passive, nni_mtx_unlock(&nni_posix_resolv_mtx); } - void -nni_plat_tcp_resolv(const char *host, const char *serv, int family, - int passive, nni_aio *aio) +nni_plat_tcp_resolv( + const char *host, const char *serv, int family, int passive, nni_aio *aio) { nni_posix_resolv_ip(host, serv, passive, family, IPPROTO_TCP, aio); } - int nni_posix_resolv_sysinit(void) { @@ -297,7 +287,6 @@ nni_posix_resolv_sysinit(void) return (0); } - void nni_posix_resolv_sysfini(void) { @@ -308,7 +297,6 @@ nni_posix_resolv_sysfini(void) nni_mtx_fini(&nni_posix_resolv_mtx); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c index a2f24f8a..45bbfcd3 100644 --- a/src/platform/posix/posix_thread.c +++ b/src/platform/posix/posix_thread.c @@ -14,21 +14,21 @@ #ifdef PLATFORM_POSIX_THREAD #include <errno.h> +#include <fcntl.h> #include <pthread.h> -#include <time.h> -#include <string.h> +#include <signal.h> #include <stdlib.h> +#include <string.h> #include <sys/types.h> +#include <time.h> #include <unistd.h> -#include <fcntl.h> -#include <signal.h> -static pthread_mutex_t nni_plat_lock = PTHREAD_MUTEX_INITIALIZER; -static int nni_plat_inited = 0; -static int nni_plat_forked = 0; +static pthread_mutex_t nni_plat_lock = PTHREAD_MUTEX_INITIALIZER; +static int nni_plat_inited = 0; +static int nni_plat_forked = 0; -pthread_condattr_t nni_cvattr; -pthread_mutexattr_t nni_mxattr; +pthread_condattr_t nni_cvattr; +pthread_mutexattr_t nni_mxattr; static pthread_attr_t nni_pthread_attr; // We open a /dev/null file descriptor so that we can dup2() it to @@ -55,7 +55,6 @@ nni_plat_mtx_init(nni_plat_mtx *mtx) return (0); } - void nni_plat_mtx_fini(nni_plat_mtx *mtx) { @@ -72,7 +71,6 @@ nni_plat_mtx_fini(nni_plat_mtx *mtx) mtx->init = 0; } - void nni_plat_mtx_lock(nni_plat_mtx *mtx) { @@ -84,7 +82,6 @@ nni_plat_mtx_lock(nni_plat_mtx *mtx) mtx->owner = pthread_self(); } - void nni_plat_mtx_unlock(nni_plat_mtx *mtx) { @@ -97,7 +94,6 @@ nni_plat_mtx_unlock(nni_plat_mtx *mtx) } } - int nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx) { @@ -117,7 +113,6 @@ nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx) return (0); } - void nni_plat_cv_wake(nni_plat_cv *cv) { @@ -128,7 +123,6 @@ nni_plat_cv_wake(nni_plat_cv *cv) } } - void nni_plat_cv_wait(nni_plat_cv *cv) { @@ -141,20 +135,19 @@ nni_plat_cv_wait(nni_plat_cv *cv) cv->mtx->owner = pthread_self(); } - int nni_plat_cv_until(nni_plat_cv *cv, nni_time until) { struct timespec ts; - int rv; + int rv; NNI_ASSERT(cv->mtx->owner == pthread_self()); // Our caller has already guaranteed a sane value for until. - ts.tv_sec = until / 1000000; + ts.tv_sec = until / 1000000; ts.tv_nsec = (until % 1000000) * 1000; - rv = pthread_cond_timedwait(&cv->cv, &cv->mtx->mtx, &ts); + rv = pthread_cond_timedwait(&cv->cv, &cv->mtx->mtx, &ts); cv->mtx->owner = pthread_self(); if (rv == ETIMEDOUT) { return (NNG_ETIMEDOUT); @@ -164,7 +157,6 @@ nni_plat_cv_until(nni_plat_cv *cv, nni_time until) return (0); } - void nni_plat_cv_fini(nni_plat_cv *cv) { @@ -179,12 +171,11 @@ nni_plat_cv_fini(nni_plat_cv *cv) cv->mtx = NULL; } - static void * nni_plat_thr_main(void *arg) { nni_plat_thr *thr = arg; - sigset_t set; + sigset_t set; // Suppress (block) SIGPIPE for this thread. sigemptyset(&set); @@ -195,26 +186,24 @@ nni_plat_thr_main(void *arg) return (NULL); } - int nni_plat_thr_init(nni_plat_thr *thr, void (*fn)(void *), void *arg) { int rv; thr->func = fn; - thr->arg = arg; + thr->arg = arg; // POSIX wants functions to return a void *, but we don't care. - rv = pthread_create(&thr->tid, &nni_pthread_attr, - nni_plat_thr_main, thr); + rv = pthread_create( + &thr->tid, &nni_pthread_attr, nni_plat_thr_main, thr); if (rv != 0) { - //nni_printf("pthread_create: %s", strerror(rv)); + // nni_printf("pthread_create: %s", strerror(rv)); return (NNG_ENOMEM); } return (0); } - void nni_plat_thr_fini(nni_plat_thr *thr) { @@ -225,14 +214,12 @@ nni_plat_thr_fini(nni_plat_thr *thr) } } - void nni_atfork_child(void) { nni_plat_forked = 1; } - int nni_plat_init(int (*helper)(void)) { @@ -243,14 +230,14 @@ nni_plat_init(int (*helper)(void)) nni_panic("nng is not fork-reentrant safe"); } if (nni_plat_inited) { - return (0); // fast path + return (0); // fast path } if ((devnull = open("/dev/null", O_RDONLY)) < 0) { return (nni_plat_errno(errno)); } pthread_mutex_lock(&nni_plat_lock); - if (nni_plat_inited) { // check again under the lock to be sure + if (nni_plat_inited) { // check again under the lock to be sure pthread_mutex_unlock(&nni_plat_lock); close(devnull); return (0); @@ -337,7 +324,6 @@ nni_plat_init(int (*helper)(void)) return (rv); } - void nni_plat_fini(void) { @@ -349,12 +335,11 @@ nni_plat_fini(void) pthread_attr_destroy(&nni_pthread_attr); (void) close(nni_plat_devnull); nni_plat_devnull = -1; - nni_plat_inited = 0; + nni_plat_inited = 0; } pthread_mutex_unlock(&nni_plat_lock); } - #else // Suppress empty symbols warnings in ranlib. |
