diff options
Diffstat (limited to 'src/platform')
26 files changed, 537 insertions, 746 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. diff --git a/src/platform/windows/win_clock.c b/src/platform/windows/win_clock.c index 9064b5e2..5a874a71 100644 --- a/src/platform/windows/win_clock.c +++ b/src/platform/windows/win_clock.c @@ -15,16 +15,14 @@ nni_time nni_plat_clock(void) { // We are limited by the system clock, but that is ok. - return (GetTickCount64()*1000); + return (GetTickCount64() * 1000); } - void nni_plat_usleep(nni_duration dur) { uint64_t exp; - // Convert duration to msec, rounding up. dur += 999; dur /= 1000; @@ -43,10 +41,9 @@ nni_plat_usleep(nni_duration dur) } } - #else // Suppress empty symbols warnings in ranlib. int nni_win_clock_not_used = 0; -#endif // PLATFORM_WINDOWS +#endif // PLATFORM_WINDOWS diff --git a/src/platform/windows/win_debug.c b/src/platform/windows/win_debug.c index 43db0505..bc3d5bfa 100644 --- a/src/platform/windows/win_debug.c +++ b/src/platform/windows/win_debug.c @@ -12,8 +12,8 @@ #ifdef PLATFORM_WINDOWS #include <errno.h> -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> void @@ -22,14 +22,12 @@ nni_plat_abort(void) abort(); } - void nni_plat_println(const char *message) { (void) fprintf(stderr, "%s\n", message); } - const char * nni_plat_strerror(int errnum) { @@ -39,25 +37,16 @@ nni_plat_strerror(int errnum) return (strerror(errnum)); } - // Win32 has its own error codes, but these ones it shares with POSIX. static struct { - int sys_err; - int nng_err; -} -nni_plat_errnos[] = { - { ENOENT, NNG_ENOENT }, - { EINTR, NNG_EINTR }, - { EINVAL, NNG_EINVAL }, - { ENOMEM, NNG_ENOMEM }, - { EACCES, NNG_EPERM }, - { EAGAIN, NNG_EAGAIN }, - { EBADF, NNG_ECLOSED }, - { EBUSY, NNG_EBUSY }, - { ENAMETOOLONG, NNG_EINVAL }, - { EPERM, NNG_EPERM }, - { EPIPE, NNG_ECLOSED }, - { 0, 0 } // must be last + int sys_err; + int nng_err; +} nni_plat_errnos[] = { + { ENOENT, NNG_ENOENT }, { EINTR, NNG_EINTR }, { EINVAL, NNG_EINVAL }, + { ENOMEM, NNG_ENOMEM }, { EACCES, NNG_EPERM }, { EAGAIN, NNG_EAGAIN }, + { EBADF, NNG_ECLOSED }, { EBUSY, NNG_EBUSY }, + { ENAMETOOLONG, NNG_EINVAL }, { EPERM, NNG_EPERM }, + { EPIPE, NNG_ECLOSED }, { 0, 0 } // must be last }; int @@ -77,15 +66,14 @@ nni_plat_errno(int errnum) return (NNG_ESYSERR + errnum); } - // Windows has infinite numbers of error codes it seems. We only bother // with the ones that are relevant to us (we think). Note that there is // no overlap between errnos and GetLastError values. static struct { - int win_err; - int nng_err; -} -nni_win_errnos[] = { + int win_err; + int nng_err; +} nni_win_errnos[] = { + // clang-format off { ERROR_FILE_NOT_FOUND, NNG_ENOENT }, { ERROR_ACCESS_DENIED, NNG_EPERM }, { ERROR_INVALID_HANDLE, NNG_ECLOSED }, @@ -106,6 +94,7 @@ nni_win_errnos[] = { { WAIT_TIMEOUT, NNG_ETIMEDOUT }, // Must be Last!! { 0, 0 }, + // clang-format on }; // This converts a Windows API error (from GetLastError()) to an @@ -127,7 +116,6 @@ nni_win_error(int errnum) return (NNG_ESYSERR + errnum); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_impl.h b/src/platform/windows/win_impl.h index 6ce38250..234acc35 100644 --- a/src/platform/windows/win_impl.h +++ b/src/platform/windows/win_impl.h @@ -16,10 +16,10 @@ #define WIN32_LEAN_AND_MEAN #endif -#include <windows.h> -#include <winsock2.h> #include <mswsock.h> #include <process.h> +#include <windows.h> +#include <winsock2.h> #include <ws2tcpip.h> #include "core/list.h" @@ -27,7 +27,7 @@ // These types are provided for here, to permit them to be directly inlined // elsewhere. -typedef struct nni_win_event nni_win_event; +typedef struct nni_win_event nni_win_event; // nni_win_event is used with io completion ports. This allows us to get // to a specific completion callback without requiring the poller (in the @@ -35,50 +35,50 @@ typedef struct nni_win_event nni_win_event; // this to pass back status and counts to the routine, which may not be // conveyed in the OVERLAPPED directly. struct nni_win_event { - OVERLAPPED olpd; - HANDLE h; - void * ptr; - nni_cb cb; - nni_list aios; + OVERLAPPED olpd; + HANDLE h; + void * ptr; + nni_cb cb; + nni_list aios; }; struct nni_plat_thr { - void (*func)(void *); - void * arg; - HANDLE handle; + void (*func)(void *); + void * arg; + HANDLE handle; }; struct nni_plat_mtx { SRWLOCK srl; - DWORD owner; - int init; + DWORD owner; + int init; }; struct nni_plat_cv { - CONDITION_VARIABLE cv; - PSRWLOCK srl; + CONDITION_VARIABLE cv; + PSRWLOCK srl; }; extern int nni_win_error(int); extern int nni_winsock_error(int); -extern int nni_win_event_init(nni_win_event *, nni_cb, void *, HANDLE); -extern void nni_win_event_fini(nni_win_event *); -extern int nni_win_event_reset(nni_win_event *); +extern int nni_win_event_init(nni_win_event *, nni_cb, void *, HANDLE); +extern void nni_win_event_fini(nni_win_event *); +extern int nni_win_event_reset(nni_win_event *); extern OVERLAPPED *nni_win_event_overlapped(nni_win_event *); -extern void nni_win_event_cancel(nni_win_event *); +extern void nni_win_event_cancel(nni_win_event *); extern int nni_win_iocp_register(HANDLE); -extern int nni_win_iocp_sysinit(void); +extern int nni_win_iocp_sysinit(void); extern void nni_win_iocp_sysfini(void); -extern int nni_win_ipc_sysinit(void); +extern int nni_win_ipc_sysinit(void); extern void nni_win_ipc_sysfini(void); -extern int nni_win_resolv_sysinit(void); +extern int nni_win_resolv_sysinit(void); extern void nni_win_resolv_sysfini(void); -#endif // PLATFORM_WINDOWS +#endif // PLATFORM_WINDOWS -#endif // PLATFORM_WIN_IMPL_H +#endif // PLATFORM_WIN_IMPL_H diff --git a/src/platform/windows/win_iocp.c b/src/platform/windows/win_iocp.c index 381da64f..7244b360 100644 --- a/src/platform/windows/win_iocp.c +++ b/src/platform/windows/win_iocp.c @@ -11,7 +11,7 @@ #ifdef PLATFORM_WINDOWS -#define NNI_WIN_IOCP_NTHREADS 4 +#define NNI_WIN_IOCP_NTHREADS 4 #include <stdio.h> // Windows IO Completion Port support. We basically creaet a single @@ -19,33 +19,33 @@ // to the port on an as needed basis. We use a single IO completion // port for pretty much everything. -static HANDLE nni_win_global_iocp = NULL; +static HANDLE nni_win_global_iocp = NULL; static nni_thr nni_win_iocp_thrs[NNI_WIN_IOCP_NTHREADS]; static nni_mtx nni_win_iocp_mtx; static void nni_win_iocp_handler(void *arg) { - HANDLE iocp; - DWORD cnt; - ULONG_PTR key; - OVERLAPPED *olpd; + HANDLE iocp; + DWORD cnt; + ULONG_PTR key; + OVERLAPPED * olpd; nni_win_event *evt; - int status; - BOOL rv; + int status; + BOOL rv; NNI_ARG_UNUSED(arg); iocp = nni_win_global_iocp; for (;;) { - key = 0; - olpd = NULL; + key = 0; + olpd = NULL; status = 0; - cnt = 0; + cnt = 0; - rv = GetQueuedCompletionStatus(iocp, &cnt, &key, &olpd, - INFINITE); + rv = GetQueuedCompletionStatus( + iocp, &cnt, &key, &olpd, INFINITE); if (rv == FALSE) { if (olpd == NULL) { @@ -62,7 +62,6 @@ nni_win_iocp_handler(void *arg) } } - int nni_win_iocp_register(HANDLE h) { @@ -72,23 +71,21 @@ nni_win_iocp_register(HANDLE h) return (0); } - int nni_win_event_init(nni_win_event *evt, nni_cb cb, void *ptr, HANDLE h) { - ZeroMemory(&evt->olpd, sizeof (evt->olpd)); + ZeroMemory(&evt->olpd, sizeof(evt->olpd)); evt->olpd.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (evt->olpd.hEvent == NULL) { return (nni_win_error(GetLastError())); } nni_aio_list_init(&evt->aios); evt->ptr = ptr; - evt->cb = cb; - evt->h = h; + evt->cb = cb; + evt->h = h; return (0); } - void nni_win_event_fini(nni_win_event *evt) { @@ -98,7 +95,6 @@ nni_win_event_fini(nni_win_event *evt) } } - int nni_win_event_reset(nni_win_event *evt) { @@ -108,18 +104,16 @@ nni_win_event_reset(nni_win_event *evt) return (0); } - OVERLAPPED * nni_win_event_overlapped(nni_win_event *evt) { return (&evt->olpd); } - void nni_win_event_cancel(nni_win_event *evt) { - int rv; + int rv; DWORD cnt; // Try to cancel the event... @@ -146,23 +140,22 @@ nni_win_event_cancel(nni_win_event *evt) GetOverlappedResult(evt->h, &evt->olpd, &cnt, TRUE); } - int nni_win_iocp_sysinit(void) { HANDLE h; - int i; - int rv; + int i; + int rv; - h = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, - NNI_WIN_IOCP_NTHREADS); + h = CreateIoCompletionPort( + INVALID_HANDLE_VALUE, NULL, 0, NNI_WIN_IOCP_NTHREADS); if (h == NULL) { return (nni_win_error(GetLastError())); } nni_win_global_iocp = h; for (i = 0; i < NNI_WIN_IOCP_NTHREADS; i++) { - rv = nni_thr_init(&nni_win_iocp_thrs[i], nni_win_iocp_handler, - NULL); + rv = nni_thr_init( + &nni_win_iocp_thrs[i], nni_win_iocp_handler, NULL); if (rv != 0) { goto fail; } @@ -186,11 +179,10 @@ fail: return (rv); } - void nni_win_iocp_sysfini(void) { - int i; + int i; HANDLE h; if ((h = nni_win_global_iocp) != NULL) { @@ -203,7 +195,6 @@ nni_win_iocp_sysfini(void) nni_mtx_fini(&nni_win_iocp_mtx); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_ipc.c b/src/platform/windows/win_ipc.c index f6601f93..237ded78 100644 --- a/src/platform/windows/win_ipc.c +++ b/src/platform/windows/win_ipc.c @@ -20,31 +20,30 @@ static void nni_win_ipc_send_start(nni_plat_ipc_pipe *); static void nni_win_ipc_recv_start(nni_plat_ipc_pipe *); struct nni_plat_ipc_pipe { - HANDLE p; - nni_win_event recv_evt; - nni_win_event send_evt; - nni_mtx mtx; - nni_list readq; - nni_list writeq; + HANDLE p; + nni_win_event recv_evt; + nni_win_event send_evt; + nni_mtx mtx; + nni_list readq; + nni_list writeq; }; struct nni_plat_ipc_ep { - char path[256]; - int mode; - int started; - nni_list aios; - HANDLE p; // accept side only - nni_win_event acc_evt; // accept side only - nni_mtx mtx; // accept side only - nni_list_node node; // conn side uses this + char path[256]; + int mode; + int started; + nni_list aios; + HANDLE p; // accept side only + nni_win_event acc_evt; // accept side only + nni_mtx mtx; // accept side only + nni_list_node node; // conn side uses this }; - static int nni_win_ipc_pipe_init(nni_plat_ipc_pipe **pipep, HANDLE p) { nni_plat_ipc_pipe *pipe; - int rv; + int rv; if ((pipe = NNI_ALLOC_STRUCT(pipe)) == NULL) { return (NNG_ENOMEM); @@ -71,7 +70,6 @@ nni_win_ipc_pipe_init(nni_plat_ipc_pipe **pipep, HANDLE p) return (0); } - static void nni_win_ipc_send_cancel(nni_aio *aio) { @@ -83,15 +81,14 @@ nni_win_ipc_send_cancel(nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_win_ipc_send_finish(nni_plat_ipc_pipe *pipe) { - nni_win_event *evt = &pipe->send_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - int rv = 0; - nni_aio *aio; - DWORD cnt; + nni_win_event *evt = &pipe->send_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + int rv = 0; + nni_aio * aio; + DWORD cnt; if (GetOverlappedResult(pipe->p, olpd, &cnt, TRUE) == FALSE) { rv = nni_win_error(GetLastError()); @@ -110,7 +107,7 @@ nni_win_ipc_send_finish(nni_plat_ipc_pipe *pipe) if (aio->a_iov[0].iov_len == 0) { int i; 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]; } aio->a_niov--; } @@ -127,16 +124,15 @@ nni_win_ipc_send_finish(nni_plat_ipc_pipe *pipe) nni_aio_finish(aio, rv, aio->a_count); } - static void nni_win_ipc_send_start(nni_plat_ipc_pipe *pipe) { - void *buf; - DWORD len; - int rv; - nni_win_event *evt = &pipe->send_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - nni_aio *aio = nni_list_first(&pipe->writeq); + void * buf; + DWORD len; + int rv; + nni_win_event *evt = &pipe->send_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + nni_aio * aio = nni_list_first(&pipe->writeq); NNI_ASSERT(aio != NULL); NNI_ASSERT(aio->a_niov > 0); @@ -157,8 +153,8 @@ nni_win_ipc_send_start(nni_plat_ipc_pipe *pipe) // scrambling the data anyway. Note that Windows named pipes do // not appear to support scatter/gather, so we have to process // each element in turn. - buf = aio->a_iov[0].iov_buf; - len = (DWORD) aio->a_iov[0].iov_len; + buf = aio->a_iov[0].iov_buf; + len = (DWORD) aio->a_iov[0].iov_len; olpd = nni_win_event_overlapped(evt); // We limit ourselves to writing 16MB at a time. Named Pipes @@ -190,7 +186,6 @@ fail: nni_aio_finish(aio, rv, aio->a_count); } - static void nni_win_ipc_send_cb(void *arg) { @@ -201,12 +196,11 @@ nni_win_ipc_send_cb(void *arg) nni_mtx_unlock(&pipe->mtx); } - void nni_plat_ipc_pipe_send(nni_plat_ipc_pipe *pipe, nni_aio *aio) { nni_win_event *evt = &pipe->send_evt; - int rv; + int rv; nni_mtx_lock(&pipe->mtx); if ((rv = nni_aio_start(aio, nni_win_ipc_send_cancel, pipe)) != 0) { @@ -229,7 +223,6 @@ nni_plat_ipc_pipe_send(nni_plat_ipc_pipe *pipe, nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_win_ipc_recv_cancel(nni_aio *aio) { @@ -241,15 +234,14 @@ nni_win_ipc_recv_cancel(nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_win_ipc_recv_finish(nni_plat_ipc_pipe *pipe) { - nni_win_event *evt = &pipe->recv_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - int rv = 0; - nni_aio *aio; - DWORD cnt; + nni_win_event *evt = &pipe->recv_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + int rv = 0; + nni_aio * aio; + DWORD cnt; if (GetOverlappedResult(pipe->p, olpd, &cnt, TRUE) == FALSE) { rv = nni_win_error(GetLastError()); @@ -268,7 +260,7 @@ nni_win_ipc_recv_finish(nni_plat_ipc_pipe *pipe) if (aio->a_iov[0].iov_len == 0) { int i; 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]; } aio->a_niov--; } @@ -285,16 +277,15 @@ nni_win_ipc_recv_finish(nni_plat_ipc_pipe *pipe) nni_aio_finish(aio, rv, aio->a_count); } - static void nni_win_ipc_recv_start(nni_plat_ipc_pipe *pipe) { - void *buf; - DWORD len; - int rv; - nni_win_event *evt = &pipe->recv_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - nni_aio *aio = nni_list_first(&pipe->readq); + void * buf; + DWORD len; + int rv; + nni_win_event *evt = &pipe->recv_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + nni_aio * aio = nni_list_first(&pipe->readq); NNI_ASSERT(aio != NULL); NNI_ASSERT(aio->a_niov > 0); @@ -315,8 +306,8 @@ nni_win_ipc_recv_start(nni_plat_ipc_pipe *pipe) // scrambling the data anyway. Note that Windows named pipes do // not appear to support scatter/gather, so we have to process // each element in turn. - buf = aio->a_iov[0].iov_buf; - len = (DWORD) aio->a_iov[0].iov_len; + buf = aio->a_iov[0].iov_buf; + len = (DWORD) aio->a_iov[0].iov_len; olpd = nni_win_event_overlapped(evt); // We limit ourselves to writing 16MB at a time. Named Pipes @@ -348,7 +339,6 @@ fail: nni_aio_finish(aio, rv, 0); } - static void nni_win_ipc_recv_cb(void *arg) { @@ -359,12 +349,11 @@ nni_win_ipc_recv_cb(void *arg) nni_mtx_unlock(&pipe->mtx); } - void nni_plat_ipc_pipe_recv(nni_plat_ipc_pipe *pipe, nni_aio *aio) { nni_win_event *evt = &pipe->send_evt; - int rv; + int rv; nni_mtx_lock(&pipe->mtx); if ((rv = nni_aio_start(aio, nni_win_ipc_recv_cancel, pipe)) != 0) { @@ -386,7 +375,6 @@ nni_plat_ipc_pipe_recv(nni_plat_ipc_pipe *pipe, nni_aio *aio) nni_win_ipc_recv_start(pipe); } - void nni_plat_ipc_pipe_close(nni_plat_ipc_pipe *pipe) { @@ -400,7 +388,6 @@ nni_plat_ipc_pipe_close(nni_plat_ipc_pipe *pipe) nni_mtx_unlock(&pipe->mtx); } - void nni_plat_ipc_pipe_fini(nni_plat_ipc_pipe *pipe) { @@ -412,13 +399,12 @@ nni_plat_ipc_pipe_fini(nni_plat_ipc_pipe *pipe) NNI_FREE_STRUCT(pipe); } - int nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) { - const char *path; + const char * path; nni_plat_ipc_ep *ep; - int rv; + int rv; if (strncmp(url, "ipc://", strlen("ipc://")) != 0) { return (NNG_EADDRINVAL); @@ -427,7 +413,7 @@ nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { return (NNG_ENOMEM); } - ZeroMemory(ep, sizeof (ep)); + ZeroMemory(ep, sizeof(ep)); if ((rv = nni_mtx_init(&ep->mtx)) != 0) { NNI_FREE_STRUCT(ep); return (rv); @@ -437,17 +423,16 @@ nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) NNI_LIST_NODE_INIT(&ep->node); nni_aio_list_init(&ep->aios); - (void) snprintf(ep->path, sizeof (ep->path), "\\\\.\\pipe\\%s", path); + (void) snprintf(ep->path, sizeof(ep->path), "\\\\.\\pipe\\%s", path); *epp = ep; return (0); } - int nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) { - int rv; + int rv; HANDLE p; nni_mtx_lock(&ep->mtx); @@ -463,12 +448,11 @@ nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) // We create the first named pipe, and we make sure that it is // properly ours. p = CreateNamedPipeA(ep->path, - PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | - FILE_FLAG_FIRST_PIPE_INSTANCE, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | - PIPE_REJECT_REMOTE_CLIENTS, - PIPE_UNLIMITED_INSTANCES, - 4096, 4096, 0, NULL); + PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | + FILE_FLAG_FIRST_PIPE_INSTANCE, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | + PIPE_REJECT_REMOTE_CLIENTS, + PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, NULL); if (p == INVALID_HANDLE_VALUE) { if ((rv = GetLastError()) == ERROR_ACCESS_DENIED) { rv = NNG_EADDRINUSE; @@ -486,7 +470,7 @@ nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) goto failed; } - ep->p = p; + ep->p = p; ep->started = 1; nni_mtx_unlock(&ep->mtx); return (0); @@ -501,16 +485,15 @@ failed: return (rv); } - static void nni_win_ipc_acc_finish(nni_plat_ipc_ep *ep) { - nni_win_event *evt = &ep->acc_evt; - DWORD nbytes; - int rv; + nni_win_event * evt = &ep->acc_evt; + DWORD nbytes; + int rv; nni_plat_ipc_pipe *pipe; - nni_aio *aio; - HANDLE newp, oldp; + nni_aio * aio; + HANDLE newp, oldp; // Note: This should be called with the ep lock held, and only when // the ConnectNamedPipe has finished. @@ -541,17 +524,16 @@ nni_win_ipc_acc_finish(nni_plat_ipc_ep *ep) } newp = CreateNamedPipeA(ep->path, - PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | - PIPE_REJECT_REMOTE_CLIENTS, - PIPE_UNLIMITED_INSTANCES, - 4096, 4096, 0, NULL); + PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | + PIPE_REJECT_REMOTE_CLIENTS, + PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, NULL); if (newp == INVALID_HANDLE_VALUE) { rv = nni_win_error(GetLastError()); DisconnectNamedPipe(ep->p); return; } - oldp = ep->p; + oldp = ep->p; ep->p = newp; if ((rv = nni_win_ipc_pipe_init(&pipe, oldp)) != 0) { @@ -564,7 +546,6 @@ nni_win_ipc_acc_finish(nni_plat_ipc_ep *ep) nni_aio_finish(aio, 0, 0); } - static void nni_win_ipc_acc_cb(void *arg) { @@ -575,7 +556,6 @@ nni_win_ipc_acc_cb(void *arg) nni_mtx_unlock(&ep->mtx); } - static void nni_win_ipc_acc_cancel(nni_aio *aio) { @@ -587,11 +567,10 @@ nni_win_ipc_acc_cancel(nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - void nni_plat_ipc_ep_accept(nni_plat_ipc_ep *ep, nni_aio *aio) { - int rv; + int rv; nni_win_event *evt = &ep->acc_evt; nni_mtx_lock(&ep->mtx); @@ -628,34 +607,32 @@ nni_plat_ipc_ep_accept(nni_plat_ipc_ep *ep, nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - // So Windows IPC is a bit different on the client side. There is no // support for asynchronous connection, but we can fake it with a single // thread that runs to establish the connection. That thread will run // keep looping, sleeping for 10 ms between attempts. It performs non-blocking // attempts to connect. -typedef struct nni_win_ipc_conn_work nni_win_ipc_conn_work; +typedef struct nni_win_ipc_conn_work nni_win_ipc_conn_work; struct nni_win_ipc_conn_work { - nni_list waiters; - nni_list workers; - nni_mtx mtx; - nni_cv cv; - nni_thr thr; - int exit; + nni_list waiters; + nni_list workers; + nni_mtx mtx; + nni_cv cv; + nni_thr thr; + int exit; }; static nni_win_ipc_conn_work nni_win_ipc_connecter; - static void nni_win_ipc_conn_thr(void *arg) { nni_win_ipc_conn_work *w = arg; - nni_plat_ipc_ep *ep; - nni_plat_ipc_pipe *pipe; - nni_aio *aio; - HANDLE p; - int rv; + nni_plat_ipc_ep * ep; + nni_plat_ipc_pipe * pipe; + nni_aio * aio; + HANDLE p; + int rv; nni_mtx_lock(&w->mtx); for (;;) { @@ -674,10 +651,9 @@ nni_win_ipc_conn_thr(void *arg) continue; } nni_list_remove(&ep->aios, aio); - p = CreateFileA(ep->path, - GENERIC_READ | GENERIC_WRITE, - 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, - NULL); + p = CreateFileA(ep->path, GENERIC_READ | GENERIC_WRITE, + 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, + NULL); if (p == INVALID_HANDLE_VALUE) { switch ((rv = GetLastError())) { @@ -686,12 +662,12 @@ nni_win_ipc_conn_thr(void *arg) nni_list_prepend(&ep->aios, aio); break; case ERROR_FILE_NOT_FOUND: - nni_aio_finish(aio, NNG_ECONNREFUSED, - 0); + nni_aio_finish( + aio, NNG_ECONNREFUSED, 0); break; default: - nni_aio_finish(aio, nni_win_error(rv), - 0); + nni_aio_finish( + aio, nni_win_error(rv), 0); break; } } else { @@ -723,12 +699,11 @@ nni_win_ipc_conn_thr(void *arg) nni_mtx_unlock(&w->mtx); } - static void nni_win_ipc_conn_cancel(nni_aio *aio) { - nni_win_ipc_conn_work *w = &nni_win_ipc_connecter; - nni_plat_ipc_ep *ep = aio->a_prov_data; + nni_win_ipc_conn_work *w = &nni_win_ipc_connecter; + nni_plat_ipc_ep * ep = aio->a_prov_data; nni_mtx_lock(&w->mtx); nni_aio_list_remove(aio); @@ -738,7 +713,6 @@ nni_win_ipc_conn_cancel(nni_aio *aio) nni_mtx_unlock(&w->mtx); } - void nni_plat_ipc_ep_connect(nni_plat_ipc_ep *ep, nni_aio *aio) { @@ -762,7 +736,6 @@ nni_plat_ipc_ep_connect(nni_plat_ipc_ep *ep, nni_aio *aio) nni_mtx_unlock(&w->mtx); } - void nni_plat_ipc_ep_fini(nni_plat_ipc_ep *ep) { @@ -776,12 +749,11 @@ nni_plat_ipc_ep_fini(nni_plat_ipc_ep *ep) NNI_FREE_STRUCT(ep); } - void nni_plat_ipc_ep_close(nni_plat_ipc_ep *ep) { nni_win_ipc_conn_work *w = &nni_win_ipc_connecter; - nni_aio *aio; + nni_aio * aio; switch (ep->mode) { case NNI_EP_MODE_DIAL: @@ -811,11 +783,10 @@ nni_plat_ipc_ep_close(nni_plat_ipc_ep *ep) } } - int nni_win_ipc_sysinit(void) { - int rv; + int rv; nni_win_ipc_conn_work *worker = &nni_win_ipc_connecter; NNI_LIST_INIT(&worker->workers, nni_plat_ipc_ep, node); @@ -835,7 +806,6 @@ nni_win_ipc_sysinit(void) return (0); } - void nni_win_ipc_sysfini(void) { @@ -850,7 +820,6 @@ nni_win_ipc_sysfini(void) nni_mtx_fini(&worker->mtx); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_net.c b/src/platform/windows/win_net.c index 2a77d97f..19491d02 100644 --- a/src/platform/windows/win_net.c +++ b/src/platform/windows/win_net.c @@ -14,36 +14,33 @@ #include <stdio.h> static LPFN_CONNECTEX nni_win_connectex; -static LPFN_ACCEPTEX nni_win_acceptex; - +static LPFN_ACCEPTEX nni_win_acceptex; struct nni_plat_tcp_pipe { - SOCKET s; - nni_win_event recv_evt; - nni_win_event send_evt; - WSAOVERLAPPED recv_olpd; - WSAOVERLAPPED send_olpd; + SOCKET s; + nni_win_event recv_evt; + nni_win_event send_evt; + WSAOVERLAPPED recv_olpd; + WSAOVERLAPPED send_olpd; }; - struct nni_plat_tcp_ep { - SOCKET s; - nni_win_event evt; - WSAOVERLAPPED olpd; + SOCKET s; + nni_win_event evt; + WSAOVERLAPPED olpd; // We have to lookup some function pointers using ioctls. Winsock, // gotta love it. - LPFN_CONNECTEX connectex; - LPFN_ACCEPTEX acceptex; + LPFN_CONNECTEX connectex; + LPFN_ACCEPTEX acceptex; }; - // Windows has infinite numbers of error codes it seems. static struct { - int wsa_err; - int nng_err; -} -nni_plat_wsa_errnos[] = { + int wsa_err; + int nng_err; +} nni_plat_wsa_errnos[] = { + // clang-format off { WSA_INVALID_HANDLE, NNG_ECLOSED }, { WSA_NOT_ENOUGH_MEMORY, NNG_ENOMEM }, { WSA_INVALID_PARAMETER, NNG_EINVAL }, @@ -106,6 +103,7 @@ nni_plat_wsa_errnos[] = { // Eliding all the QoS related errors. // Must be Last!! { 0, 0 }, + // clang-format on }; diff --git a/src/platform/windows/win_pipe.c b/src/platform/windows/win_pipe.c index 22886faa..ed6149c0 100644 --- a/src/platform/windows/win_pipe.c +++ b/src/platform/windows/win_pipe.c @@ -24,17 +24,17 @@ nni_plat_pipe_open(int *wfdp, int *rfdp) SOCKET wfd = INVALID_SOCKET; struct sockaddr_in addr; - socklen_t alen; - int one; - ULONG yes; - int rv; + socklen_t alen; + int one; + ULONG yes; + int rv; - ZeroMemory(&addr, sizeof (addr)); + ZeroMemory(&addr, sizeof(addr)); // Restrict our bind to the loopback address. We bind to an // ephemeral port. - addr.sin_family = AF_INET; - addr.sin_port = 0; + addr.sin_family = AF_INET; + addr.sin_port = 0; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); afd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); @@ -42,15 +42,14 @@ nni_plat_pipe_open(int *wfdp, int *rfdp) goto fail; } - // Make sure we have exclusive address use... one = 1; - if (setsockopt(afd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, - (char *) (&one), sizeof (one)) != 0) { + if (setsockopt(afd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char *) (&one), + sizeof(one)) != 0) { goto fail; } - alen = sizeof (addr); + alen = sizeof(addr); if (bind(afd, (struct sockaddr *) &addr, alen) != 0) { goto fail; } @@ -110,7 +109,6 @@ fail: return (rv); } - void nni_plat_pipe_raise(int wfd) { @@ -119,7 +117,6 @@ nni_plat_pipe_raise(int wfd) send((SOCKET) wfd, &c, 1, 0); } - void nni_plat_pipe_clear(int rfd) { @@ -128,13 +125,12 @@ nni_plat_pipe_clear(int rfd) for (;;) { // Completely drain the pipe, but don't wait. This coalesces // events somewhat. - if (recv((SOCKET) rfd, buf, sizeof (buf), 0) <= 0) { + if (recv((SOCKET) rfd, buf, sizeof(buf), 0) <= 0) { return; } } } - void nni_plat_pipe_close(int wfd, int rfd) { @@ -142,7 +138,6 @@ nni_plat_pipe_close(int wfd, int rfd) closesocket((SOCKET) rfd); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_rand.c b/src/platform/windows/win_rand.c index a1e381d8..38c86572 100644 --- a/src/platform/windows/win_rand.c +++ b/src/platform/windows/win_rand.c @@ -21,18 +21,17 @@ nni_plat_seed_prng(void *buf, size_t bufsz) // The rand_s routine uses RtlGenRandom to get high quality // pseudo random numbers (i.e. numbers that should be good enough // for use with crypto keying.) - while (bufsz > sizeof (val)) { + while (bufsz > sizeof(val)) { rand_s(&val); - memcpy(buf, &val, sizeof (val)); - buf = (((char *) buf) + sizeof (val)); - bufsz -= sizeof (val); + memcpy(buf, &val, sizeof(val)); + buf = (((char *) buf) + sizeof(val)); + bufsz -= sizeof(val); } } - #else // Suppress empty symbols warnings in ranlib. int nni_win_rand_not_used = 0; -#endif // PLATFORM_WINDOWS +#endif // PLATFORM_WINDOWS diff --git a/src/platform/windows/win_resolv.c b/src/platform/windows/win_resolv.c index 3d37d546..44d00c34 100644 --- a/src/platform/windows/win_resolv.c +++ b/src/platform/windows/win_resolv.c @@ -7,7 +7,6 @@ // found online at https://opensource.org/licenses/MIT. // - #include "core/nng_impl.h" #ifdef PLATFORM_WINDOWS @@ -23,25 +22,23 @@ // changed with this define. #ifndef NNG_WIN_RESOLV_CONCURRENCY -#define NNG_WIN_RESOLV_CONCURRENCY 4 +#define NNG_WIN_RESOLV_CONCURRENCY 4 #endif - static nni_taskq *nni_win_resolv_tq = NULL; -static nni_mtx nni_win_resolv_mtx; +static nni_mtx nni_win_resolv_mtx; -typedef struct nni_win_resolv_item nni_win_resolv_item; +typedef struct nni_win_resolv_item nni_win_resolv_item; struct nni_win_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_win_resolv_finish(nni_win_resolv_item *item, int rv) { @@ -52,7 +49,6 @@ nni_win_resolv_finish(nni_win_resolv_item *item, int rv) NNI_FREE_STRUCT(item); } - static void nni_win_resolv_cancel(nni_aio *aio) { @@ -69,7 +65,6 @@ nni_win_resolv_cancel(nni_aio *aio) NNI_FREE_STRUCT(item); } - static int nni_win_gai_errno(int rv) { @@ -97,16 +92,15 @@ nni_win_gai_errno(int rv) } } - static void nni_win_resolv_task(void *arg) { nni_win_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; @@ -116,13 +110,13 @@ nni_win_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; } hints.ai_flags |= AI_ADDRCONFIG; hints.ai_protocol = item->proto; - hints.ai_family = item->family; + hints.ai_family = item->family; if (item->family == AF_INET6) { hints.ai_flags |= AI_V4MAPPED; } @@ -152,27 +146,27 @@ nni_win_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++; @@ -200,13 +194,12 @@ nni_win_resolv_task(void *arg) nni_mtx_unlock(&nni_win_resolv_mtx); } - static void -nni_win_resolv_ip(const char *host, const char *serv, int passive, - int family, int proto, nni_aio *aio) +nni_win_resolv_ip(const char *host, const char *serv, int passive, int family, + int proto, nni_aio *aio) { nni_win_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); @@ -231,10 +224,10 @@ nni_win_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_win_resolv_mtx); // If we were stopped, we're done... @@ -251,15 +244,13 @@ nni_win_resolv_ip(const char *host, const char *serv, int passive, nni_mtx_unlock(&nni_win_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_win_resolv_ip(host, serv, passive, family, IPPROTO_TCP, aio); } - int nni_win_resolv_sysinit(void) { @@ -275,7 +266,6 @@ nni_win_resolv_sysinit(void) return (0); } - void nni_win_resolv_sysfini(void) { @@ -286,7 +276,6 @@ nni_win_resolv_sysfini(void) nni_mtx_fini(&nni_win_resolv_mtx); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index bc2d7a0c..693516bd 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -22,7 +22,6 @@ nni_alloc(size_t sz) return (v); } - void nni_free(void *b, size_t z) { @@ -30,7 +29,6 @@ nni_free(void *b, size_t z) HeapFree(GetProcessHeap(), 0, b); } - int nni_plat_mtx_init(nni_plat_mtx *mtx) { @@ -39,28 +37,24 @@ nni_plat_mtx_init(nni_plat_mtx *mtx) return (0); } - void nni_plat_mtx_fini(nni_plat_mtx *mtx) { mtx->init = 0; } - void nni_plat_mtx_lock(nni_plat_mtx *mtx) { AcquireSRWLockExclusive(&mtx->srl); } - void nni_plat_mtx_unlock(nni_plat_mtx *mtx) { ReleaseSRWLockExclusive(&mtx->srl); } - int nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx) { @@ -69,49 +63,43 @@ nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx) return (0); } - void nni_plat_cv_wake(nni_plat_cv *cv) { WakeAllConditionVariable(&cv->cv); } - void nni_plat_cv_wait(nni_plat_cv *cv) { (void) SleepConditionVariableSRW(&cv->cv, cv->srl, INFINITE, 0); } - int nni_plat_cv_until(nni_plat_cv *cv, nni_time until) { nni_time now; - DWORD msec; - BOOL ok; + DWORD msec; + BOOL ok; now = nni_plat_clock(); if (now > until) { msec = 0; } else { // times are in usec, but win32 wants millis - msec = (DWORD) (((until - now) + 999)/1000); + msec = (DWORD)(((until - now) + 999) / 1000); } ok = SleepConditionVariableSRW(&cv->cv, cv->srl, msec, 0); return (ok ? 0 : NNG_ETIMEDOUT); } - void nni_plat_cv_fini(nni_plat_cv *cv) { } - -static unsigned int __stdcall -nni_plat_thr_main(void *arg) +static unsigned int __stdcall nni_plat_thr_main(void *arg) { nni_plat_thr *thr = arg; @@ -119,25 +107,22 @@ nni_plat_thr_main(void *arg) return (0); } - int nni_plat_thr_init(nni_plat_thr *thr, void (*fn)(void *), void *arg) { thr->func = fn; - thr->arg = arg; + thr->arg = arg; // We could probably even go down to 8k... but crypto for some // protocols might get bigger than this. 1MB is waaay too big. - thr->handle = (HANDLE) _beginthreadex(NULL, 16384, - nni_plat_thr_main, thr, STACK_SIZE_PARAM_IS_A_RESERVATION, - NULL); + thr->handle = (HANDLE) _beginthreadex(NULL, 16384, nni_plat_thr_main, + thr, STACK_SIZE_PARAM_IS_A_RESERVATION, NULL); if (thr->handle == NULL) { - return (NNG_ENOMEM); // Best guess... + return (NNG_ENOMEM); // Best guess... } return (0); } - void nni_plat_thr_fini(nni_plat_thr *thr) { @@ -149,23 +134,22 @@ nni_plat_thr_fini(nni_plat_thr *thr) } } - int nni_plat_init(int (*helper)(void)) { - static LONG inited = 0; - int rv; + static LONG inited = 0; + int rv; static SRWLOCK lock = SRWLOCK_INIT; if (inited) { - return (0); // fast path + return (0); // fast path } AcquireSRWLockExclusive(&lock); if (!inited) { WSADATA data; - WORD ver; + WORD ver; ver = MAKEWORD(2, 2); if (WSAStartup(MAKEWORD(2, 2), &data) != 0) { if ((LOBYTE(data.wVersion) != 2) || @@ -195,7 +179,6 @@ out: return (rv); } - void nni_plat_fini(void) { @@ -205,7 +188,6 @@ nni_plat_fini(void) WSACleanup(); } - #else // Suppress empty symbols warnings in ranlib. |
