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/core | |
| 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/core')
41 files changed, 844 insertions, 1066 deletions
diff --git a/src/core/aio.c b/src/core/aio.c index c7d2b0b0..b69671b0 100644 --- a/src/core/aio.c +++ b/src/core/aio.c @@ -7,13 +7,13 @@ // found online at https://opensource.org/licenses/MIT. // -#include <string.h> #include "core/nng_impl.h" +#include <string.h> -#define NNI_AIO_WAKE (1<<0) -#define NNI_AIO_DONE (1<<1) -#define NNI_AIO_FINI (1<<2) -#define NNI_AIO_STOP (1<<3) +#define NNI_AIO_WAKE (1 << 0) +#define NNI_AIO_DONE (1 << 1) +#define NNI_AIO_FINI (1 << 2) +#define NNI_AIO_STOP (1 << 3) int nni_aio_init(nni_aio *aio, nni_cb cb, void *arg) @@ -21,10 +21,10 @@ nni_aio_init(nni_aio *aio, nni_cb cb, void *arg) int rv; if (cb == NULL) { - cb = (nni_cb) nni_aio_wake; + cb = (nni_cb) nni_aio_wake; arg = aio; } - memset(aio, 0, sizeof (*aio)); + memset(aio, 0, sizeof(*aio)); if ((rv = nni_mtx_init(&aio->a_lk)) != 0) { return (rv); } @@ -32,16 +32,15 @@ nni_aio_init(nni_aio *aio, nni_cb cb, void *arg) nni_mtx_fini(&aio->a_lk); return (rv); } - aio->a_cb = cb; - aio->a_cbarg = arg; + aio->a_cb = cb; + aio->a_cbarg = arg; aio->a_expire = NNI_TIME_NEVER; - aio->a_flags = 0; + aio->a_flags = 0; nni_taskq_ent_init(&aio->a_tqe, cb, arg); return (0); } - void nni_aio_fini(nni_aio *aio) { @@ -72,7 +71,6 @@ nni_aio_fini(nni_aio *aio) } } - int nni_aio_result(nni_aio *aio) { @@ -80,21 +78,19 @@ nni_aio_result(nni_aio *aio) nni_mtx_lock(&aio->a_lk); rv = aio->a_result; - if (aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP)) { + if (aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP)) { rv = NNG_ECANCELED; } nni_mtx_unlock(&aio->a_lk); return (rv); } - size_t nni_aio_count(nni_aio *aio) { return (aio->a_count); } - void nni_aio_wake(nni_aio *aio) { @@ -104,18 +100,16 @@ nni_aio_wake(nni_aio *aio) nni_mtx_unlock(&aio->a_lk); } - void nni_aio_wait(nni_aio *aio) { nni_mtx_lock(&aio->a_lk); - while ((aio->a_flags & (NNI_AIO_WAKE|NNI_AIO_FINI)) == 0) { + while ((aio->a_flags & (NNI_AIO_WAKE | NNI_AIO_FINI)) == 0) { nni_cv_wait(&aio->a_cv); } nni_mtx_unlock(&aio->a_lk); } - int nni_aio_start(nni_aio *aio, void (*cancel)(nni_aio *), void *data) { @@ -123,28 +117,27 @@ nni_aio_start(nni_aio *aio, void (*cancel)(nni_aio *), void *data) NNI_ASSERT(aio->a_prov_cancel == NULL); nni_mtx_lock(&aio->a_lk); - aio->a_flags &= ~(NNI_AIO_DONE|NNI_AIO_WAKE); - if (aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP)) { + aio->a_flags &= ~(NNI_AIO_DONE | NNI_AIO_WAKE); + if (aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP)) { // We should not reschedule anything at this point. nni_mtx_unlock(&aio->a_lk); return (NNG_ECANCELED); } - aio->a_result = 0; - aio->a_count = 0; + aio->a_result = 0; + aio->a_count = 0; aio->a_prov_cancel = cancel; - aio->a_prov_data = data; + aio->a_prov_data = data; nni_mtx_unlock(&aio->a_lk); return (0); } - void nni_aio_stop(nni_aio *aio) { void (*cancelfn)(nni_aio *); nni_mtx_lock(&aio->a_lk); - aio->a_flags |= NNI_AIO_DONE|NNI_AIO_STOP; + aio->a_flags |= NNI_AIO_DONE | NNI_AIO_STOP; cancelfn = aio->a_prov_cancel; nni_mtx_unlock(&aio->a_lk); @@ -154,7 +147,7 @@ nni_aio_stop(nni_aio *aio) } nni_mtx_lock(&aio->a_lk); - aio->a_prov_data = NULL; + aio->a_prov_data = NULL; aio->a_prov_cancel = NULL; nni_cv_wake(&aio->a_cv); nni_mtx_unlock(&aio->a_lk); @@ -164,7 +157,6 @@ nni_aio_stop(nni_aio *aio) nni_taskq_cancel(NULL, &aio->a_tqe); } - void nni_aio_cancel(nni_aio *aio) { @@ -179,7 +171,7 @@ nni_aio_cancel(nni_aio *aio) } aio->a_flags |= NNI_AIO_DONE; aio->a_result = NNG_ECANCELED; - cancelfn = aio->a_prov_cancel; + cancelfn = aio->a_prov_cancel; nni_mtx_unlock(&aio->a_lk); // This unregisters the AIO from the provider. @@ -189,16 +181,15 @@ nni_aio_cancel(nni_aio *aio) nni_mtx_lock(&aio->a_lk); // These should have already been cleared by the cancel function. - aio->a_prov_data = NULL; + aio->a_prov_data = NULL; aio->a_prov_cancel = NULL; - if (!(aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP))) { + if (!(aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP))) { nni_taskq_dispatch(NULL, &aio->a_tqe); } nni_mtx_unlock(&aio->a_lk); } - // I/O provider related functions. void @@ -211,25 +202,23 @@ nni_aio_finish(nni_aio *aio, int result, size_t count) return; } aio->a_flags |= NNI_AIO_DONE; - aio->a_result = result; - aio->a_count = count; + aio->a_result = result; + aio->a_count = count; aio->a_prov_cancel = NULL; - aio->a_prov_data = NULL; + aio->a_prov_data = NULL; - if (!(aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP))) { + if (!(aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP))) { nni_taskq_dispatch(NULL, &aio->a_tqe); } nni_mtx_unlock(&aio->a_lk); } - void nni_aio_list_init(nni_list *list) { NNI_LIST_INIT(list, nni_aio, a_prov_node); } - void nni_aio_list_append(nni_list *list, nni_aio *aio) { @@ -237,14 +226,12 @@ nni_aio_list_append(nni_list *list, nni_aio *aio) nni_list_append(list, aio); } - void nni_aio_list_remove(nni_aio *aio) { nni_list_node_remove(&aio->a_prov_node); } - int nni_aio_list_active(nni_aio *aio) { diff --git a/src/core/aio.h b/src/core/aio.h index ee2b084e..1b1f58e1 100644 --- a/src/core/aio.h +++ b/src/core/aio.h @@ -15,41 +15,41 @@ #include "core/taskq.h" #include "core/thread.h" -typedef struct nni_aio_ops nni_aio_ops; +typedef struct nni_aio_ops nni_aio_ops; // An nni_aio is an async I/O handle. struct nni_aio { - int a_result; // Result code (nng_errno) - size_t a_count; // Bytes transferred (I/O only) - nni_cb a_cb; // User specified callback. - void * a_cbarg; // Callback argument. - nni_time a_expire; + int a_result; // Result code (nng_errno) + size_t a_count; // Bytes transferred (I/O only) + nni_cb a_cb; // User specified callback. + void * a_cbarg; // Callback argument. + nni_time a_expire; // These fields are private to the aio framework. - nni_mtx a_lk; - nni_cv a_cv; - unsigned a_flags; - nni_taskq_ent a_tqe; + nni_mtx a_lk; + nni_cv a_cv; + unsigned a_flags; + nni_taskq_ent a_tqe; // Read/write operations. - nni_iov a_iov[4]; - int a_niov; + nni_iov a_iov[4]; + int a_niov; // Message operations. - nni_msg * a_msg; + nni_msg *a_msg; // Connect/accept operations. - void * a_endpt; // opaque endpoint handle - void * a_pipe; // opaque pipe handle + void *a_endpt; // opaque endpoint handle + void *a_pipe; // opaque pipe handle // Resolver operations. - nni_sockaddr * a_addrs; - int a_naddrs; + nni_sockaddr *a_addrs; + int a_naddrs; // Provider-use fields. - void (*a_prov_cancel)(nni_aio *); - void * a_prov_data; - nni_list_node a_prov_node; + void (*a_prov_cancel)(nni_aio *); + void * a_prov_data; + nni_list_node a_prov_node; }; // nni_aio_init initializes an aio object. The callback is called with @@ -95,14 +95,14 @@ extern void nni_aio_wait(nni_aio *); extern void nni_aio_list_init(nni_list *); extern void nni_aio_list_append(nni_list *, nni_aio *); extern void nni_aio_list_remove(nni_aio *); -extern int nni_aio_list_active(nni_aio *); +extern int nni_aio_list_active(nni_aio *); // nni_aio_finish is called by the provider when an operation is complete. // The provider gives the result code (0 for success, an NNG errno otherwise), // and the amount of data transferred (if any). extern void nni_aio_finish(nni_aio *, int, size_t); -extern int nni_aio_start(nni_aio *, void (*)(nni_aio *), void *); +extern int nni_aio_start(nni_aio *, void (*)(nni_aio *), void *); extern void nni_aio_stop(nni_aio *); #endif // CORE_AIO_H diff --git a/src/core/clock.c b/src/core/clock.c index 3e36358a..31678f67 100644 --- a/src/core/clock.c +++ b/src/core/clock.c @@ -15,7 +15,6 @@ nni_clock(void) return (nni_plat_clock()); } - void nni_usleep(nni_duration usec) { diff --git a/src/core/defs.h b/src/core/defs.h index 5db68c1c..98ff5b97 100644 --- a/src/core/defs.h +++ b/src/core/defs.h @@ -15,118 +15,115 @@ // C compilers may get unhappy when named arguments are not used. While // there are things like __attribute__((unused)) which are arguably // superior, support for such are not universal. -#define NNI_ARG_UNUSED(x) ((void) x); +#define NNI_ARG_UNUSED(x) ((void) x); #define NNI_ASSERT(x) \ if (!(x)) \ - nni_panic("%s: %d: assert err: %s", __FILE__, __LINE__, # x) + nni_panic("%s: %d: assert err: %s", __FILE__, __LINE__, #x) // These types are common but have names shared with user space. -typedef struct nng_msg nni_msg; -typedef struct nng_sockaddr nni_sockaddr; -typedef struct nng_event nni_event; -typedef struct nng_notify nni_notify; +typedef struct nng_msg nni_msg; +typedef struct nng_sockaddr nni_sockaddr; +typedef struct nng_event nni_event; +typedef struct nng_notify nni_notify; // These are our own names. -typedef struct nni_socket nni_sock; -typedef struct nni_ep nni_ep; -typedef struct nni_pipe nni_pipe; -typedef struct nni_tran nni_tran; -typedef struct nni_tran_ep nni_tran_ep; -typedef struct nni_tran_pipe nni_tran_pipe; - -typedef struct nni_proto_sock_ops nni_proto_sock_ops; -typedef struct nni_proto_pipe_ops nni_proto_pipe_ops; -typedef struct nni_proto nni_proto; - -typedef struct nni_mtx nni_mtx; -typedef struct nni_cv nni_cv; -typedef struct nni_idhash nni_idhash; -typedef struct nni_thr nni_thr; +typedef struct nni_socket nni_sock; +typedef struct nni_ep nni_ep; +typedef struct nni_pipe nni_pipe; +typedef struct nni_tran nni_tran; +typedef struct nni_tran_ep nni_tran_ep; +typedef struct nni_tran_pipe nni_tran_pipe; + +typedef struct nni_proto_sock_ops nni_proto_sock_ops; +typedef struct nni_proto_pipe_ops nni_proto_pipe_ops; +typedef struct nni_proto nni_proto; + +typedef struct nni_mtx nni_mtx; +typedef struct nni_cv nni_cv; +typedef struct nni_idhash nni_idhash; +typedef struct nni_thr nni_thr; typedef void (*nni_thr_func)(void *); -typedef int nni_signal; // Wakeup channel. -typedef uint64_t nni_time; // Abs. time (usec). -typedef int64_t nni_duration; // Rel. time (usec). +typedef int nni_signal; // Wakeup channel. +typedef uint64_t nni_time; // Abs. time (usec). +typedef int64_t nni_duration; // Rel. time (usec). -typedef struct nni_aio nni_aio; +typedef struct nni_aio nni_aio; typedef void (*nni_cb)(void *); // Used by transports for scatter gather I/O. typedef struct { - void * iov_buf; - size_t iov_len; + void * iov_buf; + size_t iov_len; } nni_iov; // Notify descriptor. typedef struct { - int sn_wfd; // written to in order to flag an event - int sn_rfd; // read from in order to clear an event - int sn_init; + int sn_wfd; // written to in order to flag an event + int sn_rfd; // read from in order to clear an event + int sn_init; } nni_notifyfd; // Some default timing things. -#define NNI_TIME_NEVER ((nni_time) -1) -#define NNI_TIME_ZERO ((nni_time) 0) -#define NNI_SECOND (1000000) +#define NNI_TIME_NEVER ((nni_time) -1) +#define NNI_TIME_ZERO ((nni_time) 0) +#define NNI_SECOND (1000000) // Structure allocation conveniences. -#define NNI_ALLOC_STRUCT(s) nni_alloc(sizeof (*s)) -#define NNI_FREE_STRUCT(s) nni_free((s), sizeof (*s)) -#define NNI_ALLOC_STRUCTS(s, n) nni_alloc(sizeof (*s) * n) -#define NNI_FREE_STRUCTS(s, n) nni_free(s, sizeof (*s) * n) - -#define NNI_PUT16(ptr, u) \ - do { \ - (ptr)[0] = (uint8_t) (((uint16_t) (u)) >> 8); \ - (ptr)[1] = (uint8_t) ((uint16_t) (u)); \ - } \ - while (0) - -#define NNI_PUT32(ptr, u) \ - do { \ - (ptr)[0] = (uint8_t) (((uint32_t) (u)) >> 24); \ - (ptr)[1] = (uint8_t) (((uint32_t) (u)) >> 16); \ - (ptr)[2] = (uint8_t) (((uint32_t) (u)) >> 8); \ - (ptr)[3] = (uint8_t) ((uint32_t) (u)); \ - } \ - while (0) - -#define NNI_PUT64(ptr, u) \ - do { \ - (ptr)[0] = (uint8_t) (((uint64_t) (u)) >> 56); \ - (ptr)[1] = (uint8_t) (((uint64_t) (u)) >> 48); \ - (ptr)[2] = (uint8_t) (((uint64_t) (u)) >> 40); \ - (ptr)[3] = (uint8_t) (((uint64_t) (u)) >> 32); \ - (ptr)[4] = (uint8_t) (((uint64_t) (u)) >> 24); \ - (ptr)[5] = (uint8_t) (((uint64_t) (u)) >> 16); \ - (ptr)[6] = (uint8_t) (((uint64_t) (u)) >> 8); \ - (ptr)[7] = (uint8_t) ((uint64_t) (u)); \ - } \ - while (0) - -#define NNI_GET16(ptr, v) \ - v = (((uint32_t) ((uint8_t) (ptr)[0])) << 8) + \ - (((uint32_t) (uint8_t) (ptr)[1])) - -#define NNI_GET32(ptr, v) \ - v = (((uint32_t) ((uint8_t) (ptr)[0])) << 24) + \ - (((uint32_t) ((uint8_t) (ptr)[1])) << 16) + \ - (((uint32_t) ((uint8_t) (ptr)[2])) << 8) + \ - (((uint32_t) (uint8_t) (ptr)[3])) - -#define NNI_GET64(ptr, v) \ - v = (((uint64_t) ((uint8_t) (ptr)[0])) << 56) + \ - (((uint64_t) ((uint8_t) (ptr)[1])) << 48) + \ - (((uint64_t) ((uint8_t) (ptr)[2])) << 40) + \ - (((uint64_t) ((uint8_t) (ptr)[3])) << 32) + \ - (((uint64_t) ((uint8_t) (ptr)[4])) << 24) + \ - (((uint64_t) ((uint8_t) (ptr)[5])) << 16) + \ - (((uint64_t) ((uint8_t) (ptr)[6])) << 8) + \ - (((uint64_t) (uint8_t) (ptr)[7])) +#define NNI_ALLOC_STRUCT(s) nni_alloc(sizeof(*s)) +#define NNI_FREE_STRUCT(s) nni_free((s), sizeof(*s)) +#define NNI_ALLOC_STRUCTS(s, n) nni_alloc(sizeof(*s) * n) +#define NNI_FREE_STRUCTS(s, n) nni_free(s, sizeof(*s) * n) + +#define NNI_PUT16(ptr, u) \ + do { \ + (ptr)[0] = (uint8_t)(((uint16_t)(u)) >> 8); \ + (ptr)[1] = (uint8_t)((uint16_t)(u)); \ + } while (0) + +#define NNI_PUT32(ptr, u) \ + do { \ + (ptr)[0] = (uint8_t)(((uint32_t)(u)) >> 24); \ + (ptr)[1] = (uint8_t)(((uint32_t)(u)) >> 16); \ + (ptr)[2] = (uint8_t)(((uint32_t)(u)) >> 8); \ + (ptr)[3] = (uint8_t)((uint32_t)(u)); \ + } while (0) + +#define NNI_PUT64(ptr, u) \ + do { \ + (ptr)[0] = (uint8_t)(((uint64_t)(u)) >> 56); \ + (ptr)[1] = (uint8_t)(((uint64_t)(u)) >> 48); \ + (ptr)[2] = (uint8_t)(((uint64_t)(u)) >> 40); \ + (ptr)[3] = (uint8_t)(((uint64_t)(u)) >> 32); \ + (ptr)[4] = (uint8_t)(((uint64_t)(u)) >> 24); \ + (ptr)[5] = (uint8_t)(((uint64_t)(u)) >> 16); \ + (ptr)[6] = (uint8_t)(((uint64_t)(u)) >> 8); \ + (ptr)[7] = (uint8_t)((uint64_t)(u)); \ + } while (0) + +#define NNI_GET16(ptr, v) \ + v = (((uint32_t)((uint8_t)(ptr)[0])) << 8) + \ + (((uint32_t)(uint8_t)(ptr)[1])) + +#define NNI_GET32(ptr, v) \ + v = (((uint32_t)((uint8_t)(ptr)[0])) << 24) + \ + (((uint32_t)((uint8_t)(ptr)[1])) << 16) + \ + (((uint32_t)((uint8_t)(ptr)[2])) << 8) + \ + (((uint32_t)(uint8_t)(ptr)[3])) + +#define NNI_GET64(ptr, v) \ + v = (((uint64_t)((uint8_t)(ptr)[0])) << 56) + \ + (((uint64_t)((uint8_t)(ptr)[1])) << 48) + \ + (((uint64_t)((uint8_t)(ptr)[2])) << 40) + \ + (((uint64_t)((uint8_t)(ptr)[3])) << 32) + \ + (((uint64_t)((uint8_t)(ptr)[4])) << 24) + \ + (((uint64_t)((uint8_t)(ptr)[5])) << 16) + \ + (((uint64_t)((uint8_t)(ptr)[6])) << 8) + \ + (((uint64_t)(uint8_t)(ptr)[7])) // A few assorted other items. -#define NNI_FLAG_IPV4ONLY 1 +#define NNI_FLAG_IPV4ONLY 1 -#endif // CORE_DEFS_H +#endif // CORE_DEFS_H diff --git a/src/core/device.c b/src/core/device.c index 7d0454bd..cd0c8dfc 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -12,18 +12,18 @@ #include <string.h> struct nni_device_pair { - nni_thr thrs[2]; - nni_sock * socks[2]; - int err[2]; + nni_thr thrs[2]; + nni_sock *socks[2]; + int err[2]; }; -typedef struct nni_device_pair nni_device_pair; +typedef struct nni_device_pair nni_device_pair; static int nni_device_loop(nni_sock *from, nni_sock *to) { nni_msg *msg; - int rv = 0; + int rv = 0; for (;;) { // Take messages sock[0], and send to sock[1]. @@ -40,7 +40,6 @@ nni_device_loop(nni_sock *from, nni_sock *to) return (rv); } - static void nni_device_fwd(void *p) { @@ -51,7 +50,6 @@ nni_device_fwd(void *p) nni_sock_shutdown(pair->socks[1]); } - static void nni_device_rev(void *p) { @@ -62,14 +60,13 @@ nni_device_rev(void *p) nni_sock_shutdown(pair->socks[1]); } - int nni_device(nni_sock *sock1, nni_sock *sock2) { nni_device_pair pair; - int rv; + int rv; - memset(&pair, 0, sizeof (pair)); + memset(&pair, 0, sizeof(pair)); pair.socks[0] = sock1; pair.socks[1] = sock2; @@ -105,8 +102,7 @@ nni_device(nni_sock *sock1, nni_sock *sock2) } // If the sockets are the same, then its a simple one way forwarder, // and we don't need two workers (but would be harmless if we did it). - if ((sock1 != sock2) && - ((sock2->s_flags & NNI_PROTO_FLAG_RCV) != 0) && + if ((sock1 != sock2) && ((sock2->s_flags & NNI_PROTO_FLAG_RCV) != 0) && ((sock1->s_flags & NNI_PROTO_FLAG_SND) != 0)) { nni_thr_run(&pair.thrs[1]); } diff --git a/src/core/endpt.c b/src/core/endpt.c index ddfde49e..0582b92b 100644 --- a/src/core/endpt.c +++ b/src/core/endpt.c @@ -9,16 +9,15 @@ #include "core/nng_impl.h" -#include <string.h> -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> +#include <string.h> // Functionality related to end points. static nni_objhash *nni_eps = NULL; -static void *nni_ep_ctor(uint32_t); -static void nni_ep_dtor(void *); - +static void * nni_ep_ctor(uint32_t); +static void nni_ep_dtor(void *); int nni_ep_sys_init(void) @@ -32,7 +31,6 @@ nni_ep_sys_init(void) return (rv); } - void nni_ep_sys_fini(void) { @@ -40,11 +38,10 @@ nni_ep_sys_fini(void) nni_eps = NULL; } - int nni_ep_find(nni_ep **epp, uint32_t id) { - int rv; + int rv; nni_ep *ep; if ((rv = nni_init()) != 0) { @@ -68,7 +65,6 @@ nni_ep_find(nni_ep **epp, uint32_t id) return (0); } - void nni_ep_hold(nni_ep *ep) { @@ -78,35 +74,32 @@ nni_ep_hold(nni_ep *ep) NNI_ASSERT(rv == 0); } - void nni_ep_rele(nni_ep *ep) { nni_objhash_unref(nni_eps, ep->ep_id); } - uint32_t nni_ep_id(nni_ep *ep) { return (ep->ep_id); } - static void * nni_ep_ctor(uint32_t id) { nni_ep *ep; - int rv; + int rv; if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { return (NULL); } ep->ep_closed = 0; - ep->ep_bound = 0; - ep->ep_pipe = NULL; - ep->ep_id = id; - ep->ep_data = NULL; + ep->ep_bound = 0; + ep->ep_pipe = NULL; + ep->ep_id = id; + ep->ep_data = NULL; NNI_LIST_NODE_INIT(&ep->ep_node); @@ -126,7 +119,6 @@ nni_ep_ctor(uint32_t id) return (ep); } - static void nni_ep_dtor(void *ptr) { @@ -140,14 +132,13 @@ nni_ep_dtor(void *ptr) NNI_FREE_STRUCT(ep); } - int nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr, int mode) { nni_tran *tran; - nni_ep *ep; - int rv; - uint32_t id; + nni_ep * ep; + int rv; + uint32_t id; if ((tran = nni_tran_find(addr)) == NULL) { return (NNG_ENOTSUP); @@ -165,14 +156,13 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr, int mode) ep->ep_mode = mode; // Could safely use strcpy here, but this avoids discussion. - (void) snprintf(ep->ep_addr, sizeof (ep->ep_addr), "%s", addr); + (void) snprintf(ep->ep_addr, sizeof(ep->ep_addr), "%s", addr); // Make a copy of the endpoint operations. This allows us to // modify them (to override NULLs for example), and avoids an extra // dereference on hot paths. ep->ep_ops = *tran->tran_ep; - if ((rv = ep->ep_ops.ep_init(&ep->ep_data, addr, sock, mode)) != 0) { nni_objhash_unref(nni_eps, id); return (rv); @@ -187,7 +177,6 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr, int mode) return (0); } - void nni_ep_stop(nni_ep *ep) { @@ -200,14 +189,12 @@ nni_ep_stop(nni_ep *ep) nni_mtx_unlock(&ep->ep_mtx); } - void nni_ep_close(nni_ep *ep) { nni_ep_stop(ep); } - void nni_ep_remove(nni_ep *ep) { @@ -236,12 +223,11 @@ nni_ep_remove(nni_ep *ep) nni_objhash_unref(nni_eps, ep->ep_id); } - static int nni_ep_connect_aio(nni_ep *ep, void **pipep) { nni_aio aio; - int rv; + int rv; nni_aio_init(&aio, NULL, NULL); aio.a_endpt = ep->ep_data; @@ -255,12 +241,11 @@ nni_ep_connect_aio(nni_ep *ep, void **pipep) return (rv); } - static int nni_ep_connect_sync(nni_ep *ep) { nni_pipe *pipe; - int rv; + int rv; rv = nni_pipe_create(&pipe, ep, ep->ep_sock, ep->ep_tran); if (rv != 0) { @@ -281,7 +266,6 @@ nni_ep_connect_sync(nni_ep *ep) return (0); } - int nni_ep_pipe_add(nni_ep *ep, nni_pipe *pipe) { @@ -297,7 +281,6 @@ nni_ep_pipe_add(nni_ep *ep, nni_pipe *pipe) return (0); } - void nni_ep_pipe_remove(nni_ep *ep, nni_pipe *pipe) { @@ -316,14 +299,13 @@ nni_ep_pipe_remove(nni_ep *ep, nni_pipe *pipe) nni_mtx_unlock(&ep->ep_mtx); } - // nni_dialer is the thread worker that dials in the background. static void nni_dialer(void *arg) { - nni_ep *ep = arg; - int rv; - nni_time cooldown; + nni_ep * ep = arg; + int rv; + nni_time cooldown; nni_duration maxrtime = 0, nmaxrtime; nni_duration defrtime = 0, ndefrtime; nni_duration rtime; @@ -337,7 +319,7 @@ nni_dialer(void *arg) // Times changed, so reset them. defrtime = ndefrtime; maxrtime = nmaxrtime; - rtime = defrtime; + rtime = defrtime; } nni_mtx_lock(&ep->ep_mtx); @@ -380,7 +362,6 @@ nni_dialer(void *arg) } } - int nni_ep_dial(nni_ep *ep, int flags) { @@ -423,12 +404,11 @@ nni_ep_dial(nni_ep *ep, int flags) return (rv); } - static int nni_ep_accept_aio(nni_ep *ep, void **pipep) { nni_aio aio; - int rv; + int rv; nni_aio_init(&aio, NULL, NULL); aio.a_endpt = ep->ep_data; @@ -442,12 +422,11 @@ nni_ep_accept_aio(nni_ep *ep, void **pipep) return (rv); } - static int nni_ep_accept_sync(nni_ep *ep) { nni_pipe *pipe; - int rv; + int rv; if (ep->ep_closed) { return (NNG_ECLOSED); @@ -468,12 +447,11 @@ nni_ep_accept_sync(nni_ep *ep) return (0); } - static void nni_listener(void *arg) { nni_ep *ep = arg; - int rv; + int rv; for (;;) { nni_time cooldown; @@ -534,7 +512,7 @@ nni_listener(void *arg) default: // Other cases we sleep just a tiny bit to avoid // burning the cpu (e.g. out of files). - cooldown = 1000; // 1 msec + cooldown = 1000; // 1 msec break; } cooldown += nni_clock(); @@ -549,7 +527,6 @@ nni_listener(void *arg) } } - int nni_ep_listen(nni_ep *ep, int flags) { @@ -594,7 +571,6 @@ nni_ep_listen(nni_ep *ep, int flags) return (0); } - void nni_ep_list_init(nni_list *list) { diff --git a/src/core/endpt.h b/src/core/endpt.h index becaa3f4..6ad2172b 100644 --- a/src/core/endpt.h +++ b/src/core/endpt.h @@ -19,41 +19,41 @@ // OUSIDE of the core is STRICTLY VERBOTEN. NO DIRECT ACCESS BY PROTOCOLS // OR TRANSPORTS. struct nni_ep { - nni_tran_ep ep_ops; // transport ops - nni_tran * ep_tran; // transport pointer - void * ep_data; // transport private - uint32_t ep_id; // endpoint id - nni_list_node ep_node; // per socket list - nni_sock * ep_sock; - char ep_addr[NNG_MAXADDRLEN]; - nni_thr ep_thr; - int ep_mode; - int ep_started; - int ep_closed; // full shutdown - int ep_bound; // true if we bound locally - nni_mtx ep_mtx; - nni_cv ep_cv; - nni_pipe * ep_pipe; // Connected pipe (dialers only) - nni_list ep_pipes; + nni_tran_ep ep_ops; // transport ops + nni_tran * ep_tran; // transport pointer + void * ep_data; // transport private + uint32_t ep_id; // endpoint id + nni_list_node ep_node; // per socket list + nni_sock * ep_sock; + char ep_addr[NNG_MAXADDRLEN]; + nni_thr ep_thr; + int ep_mode; + int ep_started; + int ep_closed; // full shutdown + int ep_bound; // true if we bound locally + nni_mtx ep_mtx; + nni_cv ep_cv; + nni_pipe * ep_pipe; // Connected pipe (dialers only) + nni_list ep_pipes; }; -#define NNI_EP_MODE_DIAL 1 -#define NNI_EP_MODE_LISTEN 2 +#define NNI_EP_MODE_DIAL 1 +#define NNI_EP_MODE_LISTEN 2 -extern int nni_ep_sys_init(void); -extern void nni_ep_sys_fini(void); -extern int nni_ep_find(nni_ep **, uint32_t); -extern void nni_ep_hold(nni_ep *); -extern void nni_ep_rele(nni_ep *); +extern int nni_ep_sys_init(void); +extern void nni_ep_sys_fini(void); +extern int nni_ep_find(nni_ep **, uint32_t); +extern void nni_ep_hold(nni_ep *); +extern void nni_ep_rele(nni_ep *); extern uint32_t nni_ep_id(nni_ep *); -extern int nni_ep_create(nni_ep **, nni_sock *, const char *, int); -extern void nni_ep_stop(nni_ep *); -extern void nni_ep_close(nni_ep *); -extern void nni_ep_remove(nni_ep *); -extern int nni_ep_dial(nni_ep *, int); -extern int nni_ep_listen(nni_ep *, int); -extern void nni_ep_list_init(nni_list *); -extern int nni_ep_pipe_add(nni_ep *, nni_pipe *); -extern void nni_ep_pipe_remove(nni_ep *, nni_pipe *); +extern int nni_ep_create(nni_ep **, nni_sock *, const char *, int); +extern void nni_ep_stop(nni_ep *); +extern void nni_ep_close(nni_ep *); +extern void nni_ep_remove(nni_ep *); +extern int nni_ep_dial(nni_ep *, int); +extern int nni_ep_listen(nni_ep *, int); +extern void nni_ep_list_init(nni_list *); +extern int nni_ep_pipe_add(nni_ep *, nni_pipe *); +extern void nni_ep_pipe_remove(nni_ep *, nni_pipe *); #endif // CORE_ENDPT_H diff --git a/src/core/event.c b/src/core/event.c index df9618ca..79910d80 100644 --- a/src/core/event.c +++ b/src/core/event.c @@ -15,13 +15,12 @@ int nni_ev_init(nni_event *event, int type, nni_sock *sock) { - memset(event, 0, sizeof (*event)); + memset(event, 0, sizeof(*event)); event->e_type = type; event->e_sock = sock; return (0); } - void nni_ev_fini(nni_event *event) { diff --git a/src/core/event.h b/src/core/event.h index e43306c9..6d9a9394 100644 --- a/src/core/event.h +++ b/src/core/event.h @@ -14,21 +14,21 @@ #include "core/list.h" struct nng_event { - int e_type; - nni_sock * e_sock; - nni_ep * e_ep; - nni_pipe * e_pipe; + int e_type; + nni_sock *e_sock; + nni_ep * e_ep; + nni_pipe *e_pipe; }; struct nng_notify { nng_notify_func n_func; - void * n_arg; - int n_type; - nni_sock * n_sock; - nni_aio n_aio; + void * n_arg; + int n_type; + nni_sock * n_sock; + nni_aio n_aio; }; -extern int nni_ev_init(nni_event *, int, nni_sock *); +extern int nni_ev_init(nni_event *, int, nni_sock *); extern void nni_ev_fini(nni_event *); #endif // CORE_EVENT_H diff --git a/src/core/idhash.c b/src/core/idhash.c index 4af06b0e..9f3b4b03 100644 --- a/src/core/idhash.c +++ b/src/core/idhash.c @@ -12,59 +12,55 @@ #include <string.h> struct nni_idhash_entry { - uint32_t ihe_key; - uint32_t ihe_skips; - void * ihe_val; + uint32_t ihe_key; + uint32_t ihe_skips; + void * ihe_val; }; - int nni_idhash_init(nni_idhash *h) { h->ih_entries = NULL; - h->ih_count = 0; - h->ih_load = 0; - h->ih_cap = 0; + h->ih_count = 0; + h->ih_load = 0; + h->ih_cap = 0; h->ih_maxload = 0; h->ih_minload = 0; // never shrink below this h->ih_walkers = 0; - h->ih_minval = 0; - h->ih_maxval = 0xffffffff; - h->ih_dynval = 0; + h->ih_minval = 0; + h->ih_maxval = 0xffffffff; + h->ih_dynval = 0; return (0); } - void nni_idhash_fini(nni_idhash *h) { NNI_ASSERT(h->ih_walkers == 0); if (h->ih_entries != NULL) { - nni_free(h->ih_entries, h->ih_cap * sizeof (nni_idhash_entry)); + nni_free(h->ih_entries, h->ih_cap * sizeof(nni_idhash_entry)); h->ih_entries = NULL; h->ih_cap = h->ih_count = 0; h->ih_load = h->ih_minload = h->ih_maxload = 0; } } - void nni_idhash_reclaim(nni_idhash *h) { // Reclaim the buffer if we want, but preserve the limits. if ((h->ih_count == 0) && (h->ih_cap != 0) && (h->ih_walkers == 0)) { - nni_free(h->ih_entries, h->ih_cap * sizeof (nni_idhash_entry)); - h->ih_cap = 0; + nni_free(h->ih_entries, h->ih_cap * sizeof(nni_idhash_entry)); + h->ih_cap = 0; h->ih_entries = NULL; h->ih_minload = 0; h->ih_maxload = 0; } } - void -nni_idhash_set_limits(nni_idhash *h, uint32_t minval, uint32_t maxval, - uint32_t start) +nni_idhash_set_limits( + nni_idhash *h, uint32_t minval, uint32_t maxval, uint32_t start) { h->ih_minval = minval; h->ih_maxval = maxval; @@ -74,11 +70,9 @@ nni_idhash_set_limits(nni_idhash *h, uint32_t minval, uint32_t maxval, NNI_ASSERT(start <= maxval); } - // Inspired by Python dict implementation. This probe will visit every // cell. We always hash consecutively assigned IDs. -#define NNI_IDHASH_NEXTPROBE(h, j) \ - ((((j) * 5) + 1) & (h->ih_cap - 1)) +#define NNI_IDHASH_NEXTPROBE(h, j) ((((j) *5) + 1) & (h->ih_cap - 1)) int nni_idhash_find(nni_idhash *h, uint32_t id, void **valp) @@ -102,21 +96,20 @@ nni_idhash_find(nni_idhash *h, uint32_t id, void **valp) } } - static int nni_hash_resize(nni_idhash *h) { - size_t newsize; - size_t oldsize; + size_t newsize; + size_t oldsize; nni_idhash_entry *newents; nni_idhash_entry *oldents; - uint32_t i; + uint32_t i; if ((h->ih_load < h->ih_maxload) && (h->ih_load >= h->ih_minload)) { // No resize needed. return (0); } - if (h->ih_walkers && (h->ih_load < (h->ih_cap-1))) { + if (h->ih_walkers && (h->ih_load < (h->ih_cap - 1))) { // Don't resize when walkers are running. This way // walk functions can remove hash nodes. return (0); @@ -131,14 +124,14 @@ nni_hash_resize(nni_idhash *h) } oldents = h->ih_entries; - newents = nni_alloc(sizeof (nni_idhash_entry) * newsize); + newents = nni_alloc(sizeof(nni_idhash_entry) * newsize); if (newents == NULL) { return (NNG_ENOMEM); } - memset(newents, 0, sizeof (nni_idhash_entry) * newsize); + memset(newents, 0, sizeof(nni_idhash_entry) * newsize); h->ih_entries = newents; - h->ih_cap = newsize; + h->ih_cap = newsize; if (newsize > 8) { h->ih_minload = newsize / 8; h->ih_maxload = newsize * 2 / 3; @@ -164,17 +157,16 @@ nni_hash_resize(nni_idhash *h) } } if (oldsize != 0) { - nni_free(oldents, sizeof (nni_idhash_entry) * oldsize); + nni_free(oldents, sizeof(nni_idhash_entry) * oldsize); } return (0); } - int nni_idhash_remove(nni_idhash *h, uint32_t id) { - int rv; - void *val; + int rv; + void * val; uint32_t index; // First check that it is in the table. This may double the @@ -212,7 +204,6 @@ nni_idhash_remove(nni_idhash *h, uint32_t id) return (0); } - int nni_idhash_insert(nni_idhash *h, uint32_t id, void *val) { @@ -253,13 +244,12 @@ nni_idhash_insert(nni_idhash *h, uint32_t id, void *val) } } - int nni_idhash_alloc(nni_idhash *h, uint32_t *idp, void *val) { uint32_t id; - void *scrap; - int rv; + void * scrap; + int rv; if (h->ih_count > (h->ih_maxval - h->ih_minval)) { // Really more like ENOSPC.. the table is filled to max. @@ -285,14 +275,12 @@ nni_idhash_alloc(nni_idhash *h, uint32_t *idp, void *val) return (rv); } - size_t nni_idhash_count(nni_idhash *h) { return (h->ih_count); } - int nni_idhash_walk(nni_idhash *h, nni_idhash_walkfn fn, void *arg) { diff --git a/src/core/idhash.h b/src/core/idhash.h index 088eac55..bb3b1581 100644 --- a/src/core/idhash.h +++ b/src/core/idhash.h @@ -22,22 +22,22 @@ // use table sizes that are powers of two. Note that hash items // must be non-NULL. The table is locked. -typedef struct nni_idhash nni_idhash; -typedef struct nni_idhash_entry nni_idhash_entry; +typedef struct nni_idhash nni_idhash; +typedef struct nni_idhash_entry nni_idhash_entry; // The details of the nni_idhash are "private". But they let us inline // this into structures. struct nni_idhash { - size_t ih_cap; - size_t ih_count; - size_t ih_load; - size_t ih_minload; // considers placeholders - size_t ih_maxload; - uint32_t ih_walkers; - uint32_t ih_minval; - uint32_t ih_maxval; - uint32_t ih_dynval; - nni_idhash_entry * ih_entries; + size_t ih_cap; + size_t ih_count; + size_t ih_load; + size_t ih_minload; // considers placeholders + size_t ih_maxload; + uint32_t ih_walkers; + uint32_t ih_minval; + uint32_t ih_maxval; + uint32_t ih_dynval; + nni_idhash_entry *ih_entries; }; // nni_idhash_walkfn is called when walking a hash table. If the @@ -48,17 +48,17 @@ struct nni_idhash { // Note that the walkfn must not attempt to change the hash table. // The user must provide any locking needed. typedef int (*nni_idhash_walkfn)(void *, uint32_t, void *); -extern int nni_idhash_init(nni_idhash *); +extern int nni_idhash_init(nni_idhash *); extern void nni_idhash_fini(nni_idhash *); extern void nni_idhash_reclaim(nni_idhash *); extern void nni_idhash_set_limits(nni_idhash *, uint32_t, uint32_t, uint32_t); -extern int nni_idhash_create(nni_idhash **); +extern int nni_idhash_create(nni_idhash **); extern void nni_idhash_destroy(nni_idhash *); -extern int nni_idhash_find(nni_idhash *, uint32_t, void **); -extern int nni_idhash_remove(nni_idhash *, uint32_t); -extern int nni_idhash_insert(nni_idhash *, uint32_t, void *); -extern int nni_idhash_alloc(nni_idhash *, uint32_t *, void *); +extern int nni_idhash_find(nni_idhash *, uint32_t, void **); +extern int nni_idhash_remove(nni_idhash *, uint32_t); +extern int nni_idhash_insert(nni_idhash *, uint32_t, void *); +extern int nni_idhash_alloc(nni_idhash *, uint32_t *, void *); extern size_t nni_idhash_count(nni_idhash *); -extern int nni_idhash_walk(nni_idhash *, nni_idhash_walkfn, void *); +extern int nni_idhash_walk(nni_idhash *, nni_idhash_walkfn, void *); -#endif // CORE_IDHASH_H +#endif // CORE_IDHASH_H diff --git a/src/core/init.c b/src/core/init.c index 98b187d8..4292146d 100644 --- a/src/core/init.c +++ b/src/core/init.c @@ -8,8 +8,8 @@ // #include "core/nng_impl.h" -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> static int nni_init_helper(void) @@ -53,14 +53,12 @@ nni_init_helper(void) return (0); } - int nni_init(void) { return (nni_plat_init(nni_init_helper)); } - void nni_fini(void) { diff --git a/src/core/list.c b/src/core/list.c index 10ec4ef8..97fe5c30 100644 --- a/src/core/list.c +++ b/src/core/list.c @@ -16,19 +16,17 @@ // Using pointer arithmetic, we can operate as a list of "anything". #define NODE(list, item) \ - (nni_list_node *) (void *) (((char *) item) + list->ll_offset) -#define ITEM(list, node) \ - (void *) (((char *) node) - list->ll_offset) + (nni_list_node *) (void *)(((char *) item) + list->ll_offset) +#define ITEM(list, node) (void *) (((char *) node) - list->ll_offset) void nni_list_init_offset(nni_list *list, size_t offset) { - list->ll_offset = offset; + list->ll_offset = offset; list->ll_head.ln_next = &list->ll_head; list->ll_head.ln_prev = &list->ll_head; } - void * nni_list_first(const nni_list *list) { @@ -40,7 +38,6 @@ nni_list_first(const nni_list *list) return (ITEM(list, node)); } - void * nni_list_last(const nni_list *list) { @@ -52,7 +49,6 @@ nni_list_last(const nni_list *list) return (ITEM(list, node)); } - void nni_list_append(nni_list *list, void *item) { @@ -61,13 +57,12 @@ nni_list_append(nni_list *list, void *item) if ((node->ln_next != NULL) || (node->ln_prev != NULL)) { nni_panic("appending node already on a list or not inited"); } - node->ln_prev = list->ll_head.ln_prev; - node->ln_next = &list->ll_head; + node->ln_prev = list->ll_head.ln_prev; + node->ln_next = &list->ll_head; node->ln_next->ln_prev = node; node->ln_prev->ln_next = node; } - void nni_list_prepend(nni_list *list, void *item) { @@ -76,45 +71,42 @@ nni_list_prepend(nni_list *list, void *item) if ((node->ln_next != NULL) || (node->ln_prev != NULL)) { nni_panic("prepending node already on a list or not inited"); } - node->ln_next = list->ll_head.ln_next; - node->ln_prev = &list->ll_head; + node->ln_next = list->ll_head.ln_next; + node->ln_prev = &list->ll_head; node->ln_next->ln_prev = node; node->ln_prev->ln_next = node; } - void nni_list_insert_before(nni_list *list, void *item, void *before) { - nni_list_node *node = NODE(list, item); + nni_list_node *node = NODE(list, item); nni_list_node *where = NODE(list, before); if ((node->ln_next != NULL) || (node->ln_prev != NULL)) { nni_panic("inserting node already on a list or not inited"); } - node->ln_next = where; - node->ln_prev = where->ln_prev; + node->ln_next = where; + node->ln_prev = where->ln_prev; node->ln_next->ln_prev = node; node->ln_prev->ln_next = node; } - void nni_list_insert_after(nni_list *list, void *item, void *after) { - nni_list_node *node = NODE(list, item); + nni_list_node *node = NODE(list, item); nni_list_node *where = NODE(list, after); if ((node->ln_next != NULL) || (node->ln_prev != NULL)) { nni_panic("inserting node already on a list or not inited"); } - node->ln_prev = where; - node->ln_next = where->ln_next; + node->ln_prev = where; + node->ln_next = where->ln_next; node->ln_next->ln_prev = node; node->ln_prev->ln_next = node; } - void * nni_list_next(const nni_list *list, void *item) { @@ -126,7 +118,6 @@ nni_list_next(const nni_list *list, void *item) return (ITEM(list, node)); } - void * nni_list_prev(const nni_list *list, void *item) { @@ -138,7 +129,6 @@ nni_list_prev(const nni_list *list, void *item) return (ITEM(list, node)); } - void nni_list_remove(nni_list *list, void *item) { @@ -146,11 +136,10 @@ nni_list_remove(nni_list *list, void *item) node->ln_prev->ln_next = node->ln_next; node->ln_next->ln_prev = node->ln_prev; - node->ln_next = NULL; - node->ln_prev = NULL; + node->ln_next = NULL; + node->ln_prev = NULL; } - int nni_list_active(nni_list *list, void *item) { @@ -159,28 +148,25 @@ nni_list_active(nni_list *list, void *item) return (node->ln_next == NULL ? 0 : 1); } - int nni_list_empty(nni_list *list) { return (list->ll_head.ln_next == &list->ll_head); } - int nni_list_node_active(nni_list_node *node) { return (node->ln_next == NULL ? 0 : 1); } - void nni_list_node_remove(nni_list_node *node) { if (node->ln_next != NULL) { node->ln_prev->ln_next = node->ln_next; node->ln_next->ln_prev = node->ln_prev; - node->ln_next = NULL; - node->ln_prev = NULL; + node->ln_next = NULL; + node->ln_prev = NULL; } } diff --git a/src/core/list.h b/src/core/list.h index 9701431e..b0007ec0 100644 --- a/src/core/list.h +++ b/src/core/list.h @@ -15,13 +15,13 @@ // In order to make life easy, we just define the list structures // directly, and let consumers directly inline structures. typedef struct nni_list_node { - struct nni_list_node * ln_next; - struct nni_list_node * ln_prev; + struct nni_list_node *ln_next; + struct nni_list_node *ln_prev; } nni_list_node; typedef struct nni_list { - struct nni_list_node ll_head; - size_t ll_offset; + struct nni_list_node ll_head; + size_t ll_offset; } nni_list; extern void nni_list_init_offset(nni_list *list, size_t offset); @@ -29,24 +29,26 @@ extern void nni_list_init_offset(nni_list *list, size_t offset); #define NNI_LIST_INIT(list, type, field) \ nni_list_init_offset(list, offsetof(type, field)) -#define NNI_LIST_NODE_INIT(node) \ - { (node)->ln_prev = (node)->ln_next = 0; } +#define NNI_LIST_NODE_INIT(node) \ + { \ + (node)->ln_prev = (node)->ln_next = 0; \ + } extern void *nni_list_first(const nni_list *); extern void *nni_list_last(const nni_list *); -extern void nni_list_append(nni_list *, void *); -extern void nni_list_prepend(nni_list *, void *); -extern void nni_list_insert_before(nni_list *, void *, void *); -extern void nni_list_insert_after(nni_list *, void *, void *); +extern void nni_list_append(nni_list *, void *); +extern void nni_list_prepend(nni_list *, void *); +extern void nni_list_insert_before(nni_list *, void *, void *); +extern void nni_list_insert_after(nni_list *, void *, void *); extern void *nni_list_next(const nni_list *, void *); extern void *nni_list_prev(const nni_list *, void *); -extern void nni_list_remove(nni_list *, void *); -extern int nni_list_active(nni_list *, void *); -extern int nni_list_empty(nni_list *); -extern int nni_list_node_active(nni_list_node *); -extern void nni_list_node_remove(nni_list_node *); +extern void nni_list_remove(nni_list *, void *); +extern int nni_list_active(nni_list *, void *); +extern int nni_list_empty(nni_list *); +extern int nni_list_node_active(nni_list_node *); +extern void nni_list_node_remove(nni_list_node *); -#define NNI_LIST_FOREACH(l, it) \ +#define NNI_LIST_FOREACH(l, it) \ for (it = nni_list_first(l); it != NULL; it = nni_list_next(l, it)) -#endif // CORE_LIST_H +#endif // CORE_LIST_H diff --git a/src/core/message.c b/src/core/message.c index 346570c9..4b563ce2 100644 --- a/src/core/message.c +++ b/src/core/message.c @@ -7,9 +7,9 @@ // found online at https://opensource.org/licenses/MIT. // +#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <stdio.h> #include "core/nng_impl.h" @@ -17,35 +17,35 @@ // Message chunk, internal to the message implementation. typedef struct { - size_t ch_cap; // allocated size - size_t ch_len; // length in use - uint8_t * ch_buf; // underlying buffer - uint8_t * ch_ptr; // pointer to actual data + size_t ch_cap; // allocated size + size_t ch_len; // length in use + uint8_t *ch_buf; // underlying buffer + uint8_t *ch_ptr; // pointer to actual data } nni_chunk; // Underlying message structure. struct nng_msg { - nni_chunk m_header; - nni_chunk m_body; - nni_time m_expire; // usec - nni_list m_options; + nni_chunk m_header; + nni_chunk m_body; + nni_time m_expire; // usec + nni_list m_options; }; typedef struct { - int mo_num; - size_t mo_sz; - void * mo_val; - nni_list_node mo_node; + int mo_num; + size_t mo_sz; + void * mo_val; + nni_list_node mo_node; } nni_msgopt; static void nni_chunk_dump(const nni_chunk *chunk, char *prefix) { - size_t i, j; + size_t i, j; uint8_t x; - char buf[128]; + char buf[128]; - (void) snprintf(buf, sizeof (buf), + (void) snprintf(buf, sizeof(buf), " %s (cap %d, len %d, offset %d ptr %p):", prefix, (int) chunk->ch_cap, (int) chunk->ch_len, (int) (chunk->ch_ptr - chunk->ch_buf), chunk->ch_ptr); @@ -59,13 +59,13 @@ nni_chunk_dump(const nni_chunk *chunk, char *prefix) nni_println(buf); j = 0; } - snprintf(buf, sizeof (buf), " %4x: ", (unsigned) i); + snprintf(buf, sizeof(buf), " %4x: ", (unsigned) i); j += strlen(buf); } buf[j++] = ' '; - x = (chunk->ch_ptr[i] >> 4); + x = (chunk->ch_ptr[i] >> 4); buf[j++] = x > 9 ? ('A' + (x - 10)) : '0' + x; - x = (chunk->ch_ptr[i] & 0x0f); + x = (chunk->ch_ptr[i] & 0x0f); buf[j++] = x > 9 ? ('A' + (x - 10)) : '0' + x; } if (j > 0) { @@ -74,20 +74,18 @@ nni_chunk_dump(const nni_chunk *chunk, char *prefix) } } - void nni_msg_dump(const char *banner, const nni_msg *msg) { char buf[128]; - (void) snprintf(buf, sizeof (buf), "--- %s BEGIN ---", banner); + (void) snprintf(buf, sizeof(buf), "--- %s BEGIN ---", banner); nni_println(buf); nni_chunk_dump(&msg->m_header, "HEADER"); nni_chunk_dump(&msg->m_body, "BODY"); nni_println("--- END ---"); } - // nni_chunk_grow increases the underlying space for a chunk. It ensures // that the desired amount of trailing space (including the length) // and headroom (excluding the length) are available. It also copies @@ -101,7 +99,7 @@ nni_msg_dump(const char *banner, const nni_msg *msg) static int nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted) { - size_t headroom = 0; + size_t headroom = 0; uint8_t *newbuf; // We assume that if the pointer is a valid pointer, and inside @@ -115,7 +113,7 @@ nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted) if ((ch->ch_ptr >= ch->ch_buf) && (ch->ch_ptr < (ch->ch_buf + ch->ch_cap))) { - headroom = (size_t) (ch->ch_ptr - ch->ch_buf); + headroom = (size_t)(ch->ch_ptr - ch->ch_buf); if (headwanted < headroom) { headwanted = headroom; // Never shrink this. } @@ -152,7 +150,6 @@ nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted) return (0); } - static void nni_chunk_free(nni_chunk *ch) { @@ -165,7 +162,6 @@ nni_chunk_free(nni_chunk *ch) ch->ch_cap = 0; } - // nni_chunk_trunc truncates bytes from the end of the chunk. static int nni_chunk_trunc(nni_chunk *ch, size_t len) @@ -177,7 +173,6 @@ nni_chunk_trunc(nni_chunk *ch, size_t len) return (0); } - // nni_chunk_trim removes bytes from the beginning of the chunk. static int nni_chunk_trim(nni_chunk *ch, size_t len) @@ -193,7 +188,6 @@ nni_chunk_trim(nni_chunk *ch, size_t len) return (0); } - // nni_chunk_dup allocates storage for a new chunk, and copies // the contents of the source to the destination. The new chunk will // have the same size, headroom, and capacity as the original. @@ -210,7 +204,6 @@ nni_chunk_dup(nni_chunk *dst, const nni_chunk *src) return (0); } - // nni_chunk_append appends the data to the chunk, growing as necessary. // If the data pointer is NULL, then the chunk data region is allocated, // but uninitialized. @@ -235,7 +228,6 @@ nni_chunk_append(nni_chunk *ch, const void *data, size_t len) return (0); } - // nni_chunk_prepend prepends data to the chunk, as efficiently as possible. // If the data pointer is NULL, then no data is actually copied, but the // data region will have "grown" in the beginning, with uninitialized data. @@ -250,7 +242,7 @@ nni_chunk_prepend(nni_chunk *ch, const void *data, size_t len) if ((ch->ch_ptr >= ch->ch_buf) && (ch->ch_ptr < (ch->ch_buf + ch->ch_cap)) && - (len <= (size_t) (ch->ch_ptr - ch->ch_buf))) { + (len <= (size_t)(ch->ch_ptr - ch->ch_buf))) { // There is already enough room at the beginning. ch->ch_ptr -= len; } else if ((ch->ch_len + len) <= ch->ch_cap) { @@ -272,12 +264,11 @@ nni_chunk_prepend(nni_chunk *ch, const void *data, size_t len) return (0); } - int nni_msg_alloc(nni_msg **mp, size_t sz) { nni_msg *m; - int rv; + int rv; if ((m = NNI_ALLOC_STRUCT(m)) == NULL) { return (NNG_ENOMEM); @@ -295,7 +286,7 @@ nni_msg_alloc(nni_msg **mp, size_t sz) // to allow for inlining backtraces, etc. We also allow the // amount of space at the end for the same reason. Large aligned // allocations are unmolested to avoid excessive overallocation. - if ((sz < 1024) || ((sz & (sz-1)) != 0)) { + if ((sz < 1024) || ((sz & (sz - 1)) != 0)) { rv = nni_chunk_grow(&m->m_body, sz + 32, 32); } else { rv = nni_chunk_grow(&m->m_body, sz, 0); @@ -314,22 +305,20 @@ nni_msg_alloc(nni_msg **mp, size_t sz) return (0); } - int nni_msg_dup(nni_msg **dup, const nni_msg *src) { - nni_msg *m; + nni_msg * m; nni_msgopt *mo; nni_msgopt *newmo; - int rv; + int rv; if ((m = NNI_ALLOC_STRUCT(m)) == NULL) { return (NNG_ENOMEM); } - memset(m, 0, sizeof (*m)); + memset(m, 0, sizeof(*m)); NNI_LIST_INIT(&m->m_options, nni_msgopt, mo_node); - if ((rv = nni_chunk_dup(&m->m_header, &src->m_header)) != 0) { NNI_FREE_STRUCT(m); return (rv); @@ -341,13 +330,13 @@ nni_msg_dup(nni_msg **dup, const nni_msg *src) } NNI_LIST_FOREACH (&src->m_options, mo) { - newmo = nni_alloc(sizeof (*newmo) + mo->mo_sz); + newmo = nni_alloc(sizeof(*newmo) + mo->mo_sz); if (newmo == NULL) { nni_msg_free(m); return (NNG_ENOMEM); } - newmo->mo_val = ((char *) newmo + sizeof (*newmo)); - newmo->mo_sz = mo->mo_sz; + newmo->mo_val = ((char *) newmo + sizeof(*newmo)); + newmo->mo_sz = mo->mo_sz; newmo->mo_num = mo->mo_num; memcpy(newmo->mo_val, mo->mo_val, mo->mo_sz); nni_list_append(&m->m_options, newmo); @@ -357,7 +346,6 @@ nni_msg_dup(nni_msg **dup, const nni_msg *src) return (0); } - void nni_msg_free(nni_msg *m) { @@ -368,13 +356,12 @@ nni_msg_free(nni_msg *m) nni_chunk_free(&m->m_body); while ((mo = nni_list_first(&m->m_options)) != NULL) { nni_list_remove(&m->m_options, mo); - nni_free(mo, sizeof (*mo) + mo->mo_sz); + nni_free(mo, sizeof(*mo) + mo->mo_sz); } NNI_FREE_STRUCT(m); } } - int nni_msg_setopt(nni_msg *m, int opt, const void *val, size_t sz) { @@ -392,22 +379,21 @@ nni_msg_setopt(nni_msg *m, int opt, const void *val, size_t sz) break; } } - if ((newmo = nni_alloc(sizeof (*newmo) + sz)) == NULL) { + if ((newmo = nni_alloc(sizeof(*newmo) + sz)) == NULL) { return (NNG_ENOMEM); } - newmo->mo_val = ((char *) newmo + sizeof (*newmo)); - newmo->mo_sz = sz; + newmo->mo_val = ((char *) newmo + sizeof(*newmo)); + newmo->mo_sz = sz; newmo->mo_num = opt; memcpy(newmo->mo_val, val, sz); if (oldmo != NULL) { nni_list_remove(&m->m_options, oldmo); - nni_free(oldmo, sizeof (*oldmo) + oldmo->mo_sz); + nni_free(oldmo, sizeof(*oldmo) + oldmo->mo_sz); } nni_list_append(&m->m_options, newmo); return (0); } - int nni_msg_getopt(nni_msg *m, int opt, void *val, size_t *szp) { @@ -427,7 +413,6 @@ nni_msg_getopt(nni_msg *m, int opt, void *val, size_t *szp) return (NNG_ENOTSUP); } - int nni_msg_realloc(nni_msg *m, size_t sz) { @@ -445,84 +430,72 @@ nni_msg_realloc(nni_msg *m, size_t sz) return (0); } - void * nni_msg_header(nni_msg *m) { return (m->m_header.ch_ptr); } - size_t nni_msg_header_len(nni_msg *m) { return (m->m_header.ch_len); } - void * nni_msg_body(nni_msg *m) { return (m->m_body.ch_ptr); } - size_t nni_msg_len(nni_msg *m) { return (m->m_body.ch_len); } - int nni_msg_append(nni_msg *m, const void *data, size_t len) { return (nni_chunk_append(&m->m_body, data, len)); } - int nni_msg_prepend(nni_msg *m, const void *data, size_t len) { return (nni_chunk_prepend(&m->m_body, data, len)); } - int nni_msg_trim(nni_msg *m, size_t len) { return (nni_chunk_trim(&m->m_body, len)); } - int nni_msg_trunc(nni_msg *m, size_t len) { return (nni_chunk_trunc(&m->m_body, len)); } - int nni_msg_append_header(nni_msg *m, const void *data, size_t len) { return (nni_chunk_append(&m->m_header, data, len)); } - int nni_msg_prepend_header(nni_msg *m, const void *data, size_t len) { return (nni_chunk_prepend(&m->m_header, data, len)); } - int nni_msg_trim_header(nni_msg *m, size_t len) { return (nni_chunk_trim(&m->m_header, len)); } - int nni_msg_trunc_header(nni_msg *m, size_t len) { diff --git a/src/core/message.h b/src/core/message.h index 057c7539..7b71bd5c 100644 --- a/src/core/message.h +++ b/src/core/message.h @@ -12,24 +12,24 @@ // Internally used message API. Again, this is not part of our public API. -extern int nni_msg_alloc(nni_msg **, size_t); -extern void nni_msg_free(nni_msg *); -extern int nni_msg_realloc(nni_msg *, size_t); -extern int nni_msg_dup(nni_msg **, const nni_msg *); -extern void *nni_msg_header(nni_msg *); +extern int nni_msg_alloc(nni_msg **, size_t); +extern void nni_msg_free(nni_msg *); +extern int nni_msg_realloc(nni_msg *, size_t); +extern int nni_msg_dup(nni_msg **, const nni_msg *); +extern void * nni_msg_header(nni_msg *); extern size_t nni_msg_header_len(nni_msg *); -extern void *nni_msg_body(nni_msg *); +extern void * nni_msg_body(nni_msg *); extern size_t nni_msg_len(nni_msg *); -extern int nni_msg_append(nni_msg *, const void *, size_t); -extern int nni_msg_prepend(nni_msg *, const void *, size_t); -extern int nni_msg_append_header(nni_msg *, const void *, size_t); -extern int nni_msg_prepend_header(nni_msg *, const void *, size_t); -extern int nni_msg_trim(nni_msg *, size_t); -extern int nni_msg_trunc(nni_msg *, size_t); -extern int nni_msg_trim_header(nni_msg *, size_t); -extern int nni_msg_trunc_header(nni_msg *, size_t); -extern int nni_msg_setopt(nni_msg *, int, const void *, size_t); -extern int nni_msg_getopt(nni_msg *, int, void *, size_t *); -extern void nni_msg_dump(const char *, const nni_msg *); +extern int nni_msg_append(nni_msg *, const void *, size_t); +extern int nni_msg_prepend(nni_msg *, const void *, size_t); +extern int nni_msg_append_header(nni_msg *, const void *, size_t); +extern int nni_msg_prepend_header(nni_msg *, const void *, size_t); +extern int nni_msg_trim(nni_msg *, size_t); +extern int nni_msg_trunc(nni_msg *, size_t); +extern int nni_msg_trim_header(nni_msg *, size_t); +extern int nni_msg_trunc_header(nni_msg *, size_t); +extern int nni_msg_setopt(nni_msg *, int, const void *, size_t); +extern int nni_msg_getopt(nni_msg *, int, void *, size_t *); +extern void nni_msg_dump(const char *, const nni_msg *); -#endif // CORE_SOCKET_H +#endif // CORE_SOCKET_H diff --git a/src/core/msgqueue.c b/src/core/msgqueue.c index a69af47c..9f7ff7fd 100644 --- a/src/core/msgqueue.c +++ b/src/core/msgqueue.c @@ -15,37 +15,36 @@ // side can close, and they may be closed more than once. struct nni_msgq { - nni_mtx mq_lock; - nni_cv mq_drained; - int mq_cap; - int mq_alloc; // alloc is cap + 2... - int mq_len; - int mq_get; - int mq_put; - int mq_closed; - int mq_puterr; - int mq_geterr; - int mq_draining; - nni_msg ** mq_msgs; - - nni_list mq_aio_putq; - nni_list mq_aio_getq; - nni_list mq_aio_notify_get; - nni_list mq_aio_notify_put; - - nni_timer_node mq_timer; - nni_time mq_expire; + nni_mtx mq_lock; + nni_cv mq_drained; + int mq_cap; + int mq_alloc; // alloc is cap + 2... + int mq_len; + int mq_get; + int mq_put; + int mq_closed; + int mq_puterr; + int mq_geterr; + int mq_draining; + nni_msg **mq_msgs; + + nni_list mq_aio_putq; + nni_list mq_aio_getq; + nni_list mq_aio_notify_get; + nni_list mq_aio_notify_put; + + nni_timer_node mq_timer; + nni_time mq_expire; }; - static void nni_msgq_run_timeout(void *); int nni_msgq_init(nni_msgq **mqp, int cap) { struct nni_msgq *mq; - int rv; - int alloc; + int rv; + int alloc; if (cap < 0) { return (NNG_EINVAL); @@ -72,24 +71,24 @@ nni_msgq_init(nni_msgq **mqp, int cap) if ((rv = nni_cv_init(&mq->mq_drained, &mq->mq_lock)) != 0) { goto fail; } - if ((mq->mq_msgs = nni_alloc(sizeof (nng_msg *) * alloc)) == NULL) { + if ((mq->mq_msgs = nni_alloc(sizeof(nng_msg *) * alloc)) == NULL) { rv = NNG_ENOMEM; goto fail; } nni_timer_init(&mq->mq_timer, nni_msgq_run_timeout, mq); - mq->mq_cap = cap; - mq->mq_alloc = alloc; - mq->mq_len = 0; - mq->mq_get = 0; - mq->mq_put = 0; - mq->mq_closed = 0; - mq->mq_puterr = 0; - mq->mq_geterr = 0; - mq->mq_expire = NNI_TIME_NEVER; + mq->mq_cap = cap; + mq->mq_alloc = alloc; + mq->mq_len = 0; + mq->mq_get = 0; + mq->mq_put = 0; + mq->mq_closed = 0; + mq->mq_puterr = 0; + mq->mq_geterr = 0; + mq->mq_expire = NNI_TIME_NEVER; mq->mq_draining = 0; - *mqp = mq; + *mqp = mq; return (0); @@ -97,13 +96,12 @@ fail: nni_cv_fini(&mq->mq_drained); nni_mtx_fini(&mq->mq_lock); if (mq->mq_msgs != NULL) { - nni_free(mq->mq_msgs, sizeof (nng_msg *) * alloc); + nni_free(mq->mq_msgs, sizeof(nng_msg *) * alloc); } NNI_FREE_STRUCT(mq); return (rv); } - void nni_msgq_fini(nni_msgq *mq) { @@ -127,11 +125,10 @@ nni_msgq_fini(nni_msgq *mq) nni_msg_free(msg); } - nni_free(mq->mq_msgs, mq->mq_alloc * sizeof (nng_msg *)); + nni_free(mq->mq_msgs, mq->mq_alloc * sizeof(nng_msg *)); NNI_FREE_STRUCT(mq); } - static void nni_msgq_finish(nni_aio *aio, int rv) { @@ -139,7 +136,6 @@ nni_msgq_finish(nni_aio *aio, int rv) nni_aio_finish(aio, rv, 0); } - void nni_msgq_set_get_error(nni_msgq *mq, int error) { @@ -159,7 +155,6 @@ nni_msgq_set_get_error(nni_msgq *mq, int error) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_set_put_error(nni_msgq *mq, int error) { @@ -179,7 +174,6 @@ nni_msgq_set_put_error(nni_msgq *mq, int error) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_set_error(nni_msgq *mq, int error) { @@ -205,14 +199,13 @@ nni_msgq_set_error(nni_msgq *mq, int error) nni_mtx_unlock(&mq->mq_lock); } - static void nni_msgq_run_putq(nni_msgq *mq) { nni_aio *waio; nni_aio *raio; nni_msg *msg; - size_t len; + size_t len; while ((waio = nni_list_first(&mq->mq_aio_putq)) != NULL) { msg = waio->a_msg; @@ -248,7 +241,6 @@ nni_msgq_run_putq(nni_msgq *mq) } } - static void nni_msgq_run_getq(nni_msgq *mq) { @@ -272,7 +264,7 @@ nni_msgq_run_getq(nni_msgq *mq) // Nothing queued (unbuffered?), maybe a writer is waiting. if ((waio = nni_list_first(&mq->mq_aio_putq)) != NULL) { - msg = waio->a_msg; + msg = waio->a_msg; waio->a_msg = NULL; raio->a_msg = msg; nni_msgq_finish(raio, 0); @@ -286,7 +278,6 @@ nni_msgq_run_getq(nni_msgq *mq) } } - static void nni_msgq_run_notify(nni_msgq *mq) { @@ -316,7 +307,6 @@ nni_msgq_run_notify(nni_msgq *mq) } } - static void nni_msgq_cancel(nni_aio *aio) { @@ -331,7 +321,6 @@ nni_msgq_cancel(nni_aio *aio) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_aio_notify_put(nni_msgq *mq, nni_aio *aio) { @@ -344,7 +333,6 @@ nni_msgq_aio_notify_put(nni_msgq *mq, nni_aio *aio) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_aio_notify_get(nni_msgq *mq, nni_aio *aio) { @@ -357,7 +345,6 @@ nni_msgq_aio_notify_get(nni_msgq *mq, nni_aio *aio) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_aio_put(nni_msgq *mq, nni_aio *aio) { @@ -391,7 +378,6 @@ nni_msgq_aio_put(nni_msgq *mq, nni_aio *aio) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_aio_get(nni_msgq *mq, nni_aio *aio) { @@ -425,7 +411,6 @@ nni_msgq_aio_get(nni_msgq *mq, nni_aio *aio) nni_mtx_unlock(&mq->mq_lock); } - int nni_msgq_canput(nni_msgq *mq) { @@ -442,7 +427,6 @@ nni_msgq_canput(nni_msgq *mq) return (0); } - int nni_msgq_canget(nni_msgq *mq) { @@ -459,12 +443,11 @@ nni_msgq_canget(nni_msgq *mq) return (0); } - int nni_msgq_tryput(nni_msgq *mq, nni_msg *msg) { nni_aio *raio; - size_t len = nni_msg_len(msg); + size_t len = nni_msg_len(msg); nni_mtx_lock(&mq->mq_lock); if (mq->mq_closed) { @@ -500,16 +483,15 @@ nni_msgq_tryput(nni_msgq *mq, nni_msg *msg) return (NNG_EAGAIN); } - // XXX: Move this to generic AIO timeout... void nni_msgq_run_timeout(void *arg) { nni_msgq *mq = arg; - nni_time now; - nni_time exp; - nni_aio *aio; - nni_aio *naio; + nni_time now; + nni_time exp; + nni_aio * aio; + nni_aio * naio; now = nni_clock(); exp = NNI_TIME_NEVER; @@ -550,12 +532,11 @@ nni_msgq_run_timeout(void *arg) nni_mtx_unlock(&mq->mq_lock); } - int nni_msgq_get_until(nni_msgq *mq, nni_msg **msgp, nni_time expire) { nni_aio aio; - int rv; + int rv; if ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) { return (rv); @@ -564,25 +545,24 @@ nni_msgq_get_until(nni_msgq *mq, nni_msg **msgp, nni_time expire) nni_msgq_aio_get(mq, &aio); nni_aio_wait(&aio); if ((rv = nni_aio_result(&aio)) == 0) { - *msgp = aio.a_msg; + *msgp = aio.a_msg; aio.a_msg = NULL; } nni_aio_fini(&aio); return (rv); } - int nni_msgq_put_until(nni_msgq *mq, nni_msg *msg, nni_time expire) { nni_aio aio; - int rv; + int rv; if ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) { return (rv); } aio.a_expire = expire; - aio.a_msg = msg; + aio.a_msg = msg; nni_msgq_aio_put(mq, &aio); nni_aio_wait(&aio); rv = nni_aio_result(&aio); @@ -590,14 +570,13 @@ nni_msgq_put_until(nni_msgq *mq, nni_msg *msg, nni_time expire) return (rv); } - void nni_msgq_drain(nni_msgq *mq, nni_time expire) { nni_aio *aio; nni_mtx_lock(&mq->mq_lock); - mq->mq_closed = 1; + mq->mq_closed = 1; mq->mq_draining = 1; while ((mq->mq_len > 0) || !nni_list_empty(&mq->mq_aio_putq)) { if (nni_cv_until(&mq->mq_drained, expire) != 0) { @@ -622,7 +601,6 @@ nni_msgq_drain(nni_msgq *mq, nni_time expire) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_close(nni_msgq *mq) { @@ -660,7 +638,6 @@ nni_msgq_close(nni_msgq *mq) nni_mtx_unlock(&mq->mq_lock); } - int nni_msgq_len(nni_msgq *mq) { @@ -672,7 +649,6 @@ nni_msgq_len(nni_msgq *mq) return (rv); } - int nni_msgq_cap(nni_msgq *mq) { @@ -684,23 +660,22 @@ nni_msgq_cap(nni_msgq *mq) return (rv); } - int nni_msgq_resize(nni_msgq *mq, int cap) { - int alloc; - nni_msg *msg; + int alloc; + nni_msg * msg; nni_msg **newq, **oldq; - int oldget; - int oldput; - int oldcap; - int oldlen; - int oldalloc; + int oldget; + int oldput; + int oldcap; + int oldlen; + int oldalloc; alloc = cap + 2; if (alloc > mq->mq_alloc) { - newq = nni_alloc(sizeof (nni_msg *) * alloc); + newq = nni_alloc(sizeof(nni_msg *) * alloc); if (newq == NULL) { return (NNG_ENOMEM); } @@ -726,17 +701,17 @@ nni_msgq_resize(nni_msgq *mq, int cap) goto out; } - oldq = mq->mq_msgs; - oldget = mq->mq_get; - oldput = mq->mq_put; - oldcap = mq->mq_cap; + oldq = mq->mq_msgs; + oldget = mq->mq_get; + oldput = mq->mq_put; + oldcap = mq->mq_cap; oldalloc = mq->mq_alloc; - oldlen = mq->mq_len; + oldlen = mq->mq_len; mq->mq_msgs = newq; mq->mq_len = mq->mq_get = mq->mq_put = 0; - mq->mq_cap = cap; - mq->mq_alloc = alloc; + mq->mq_cap = cap; + mq->mq_alloc = alloc; while (oldlen) { mq->mq_msgs[mq->mq_put++] = oldq[oldget++]; @@ -749,7 +724,7 @@ nni_msgq_resize(nni_msgq *mq, int cap) mq->mq_len++; oldlen--; } - nni_free(oldq, sizeof (nni_msg *) * oldalloc); + nni_free(oldq, sizeof(nni_msg *) * oldalloc); out: // Wake everyone up -- we changed everything. diff --git a/src/core/msgqueue.h b/src/core/msgqueue.h index 485eb9e7..794ac5cd 100644 --- a/src/core/msgqueue.h +++ b/src/core/msgqueue.h @@ -24,7 +24,7 @@ // // Readers & writers in a message queue can be woken either by a timeout // or by a specific signal (arranged by the caller). -typedef struct nni_msgq nni_msgq; +typedef struct nni_msgq nni_msgq; // nni_msgq_init creates a message queue with the given capacity, // which must be a positive number. It returns NNG_EINVAL if the capacity @@ -35,8 +35,8 @@ extern int nni_msgq_init(nni_msgq **, int); // messages that may be in the queue. extern void nni_msgq_fini(nni_msgq *); -extern int nni_msgq_canget(nni_msgq *); -extern int nni_msgq_canput(nni_msgq *); +extern int nni_msgq_canget(nni_msgq *); +extern int nni_msgq_canput(nni_msgq *); extern void nni_msgq_aio_put(nni_msgq *, nni_aio *); extern void nni_msgq_aio_get(nni_msgq *, nni_aio *); extern void nni_msgq_aio_notify_get(nni_msgq *, nni_aio *); @@ -108,4 +108,4 @@ extern int nni_msgq_cap(nni_msgq *mq); // nni_msgq_len returns the number of messages currently in the queue. extern int nni_msgq_len(nni_msgq *mq); -#endif // CORE_MSQUEUE_H +#endif // CORE_MSQUEUE_H diff --git a/src/core/nng_impl.h b/src/core/nng_impl.h index 291b5e28..8b76b036 100644 --- a/src/core/nng_impl.h +++ b/src/core/nng_impl.h @@ -44,9 +44,9 @@ #include "core/transport.h" // These have to come after the others - particularly transport.h +#include "core/endpt.h" #include "core/event.h" #include "core/pipe.h" #include "core/socket.h" -#include "core/endpt.h" -#endif // CORE_NNG_IMPL_H +#endif // CORE_NNG_IMPL_H diff --git a/src/core/objhash.c b/src/core/objhash.c index da4c6012..18c55f83 100644 --- a/src/core/objhash.c +++ b/src/core/objhash.c @@ -7,41 +7,41 @@ // found online at https://opensource.org/licenses/MIT. // -#include "core/nng_impl.h" #include "core/objhash.h" +#include "core/nng_impl.h" #include <string.h> // The details of the nni_objhash are "private". struct nni_objhash { - size_t oh_cap; - size_t oh_count; - size_t oh_load; - size_t oh_minload; // considers placeholders - size_t oh_maxload; - uint32_t oh_minval; - uint32_t oh_maxval; - uint32_t oh_dynval; - nni_mtx oh_lock; - nni_cv oh_cv; - nni_objhash_node * oh_nodes; - nni_objhash_ctor oh_ctor; - nni_objhash_dtor oh_dtor; + size_t oh_cap; + size_t oh_count; + size_t oh_load; + size_t oh_minload; // considers placeholders + size_t oh_maxload; + uint32_t oh_minval; + uint32_t oh_maxval; + uint32_t oh_dynval; + nni_mtx oh_lock; + nni_cv oh_cv; + nni_objhash_node *oh_nodes; + nni_objhash_ctor oh_ctor; + nni_objhash_dtor oh_dtor; }; struct nni_objhash_node { - uint32_t on_id; // the key - uint32_t on_skips; // indicates - uint32_t on_refcnt; // reference count - void * on_val; // pointer to user data + uint32_t on_id; // the key + uint32_t on_skips; // indicates + uint32_t on_refcnt; // reference count + void * on_val; // pointer to user data }; int -nni_objhash_init(nni_objhash **ohp, nni_objhash_ctor ctor, - nni_objhash_dtor dtor) +nni_objhash_init( + nni_objhash **ohp, nni_objhash_ctor ctor, nni_objhash_dtor dtor) { nni_objhash *oh; - int rv; + int rv; if ((ctor == NULL) || (dtor == NULL)) { return (NNG_EINVAL); @@ -62,24 +62,23 @@ nni_objhash_init(nni_objhash **ohp, nni_objhash_ctor ctor, return (rv); } - oh->oh_nodes = NULL; - oh->oh_count = 0; - oh->oh_load = 0; - oh->oh_cap = 0; + oh->oh_nodes = NULL; + oh->oh_count = 0; + oh->oh_load = 0; + oh->oh_cap = 0; oh->oh_maxload = 0; oh->oh_minload = 0; // never shrink below this - oh->oh_minval = 1; - oh->oh_maxval = 0x7fffffff; - oh->oh_dynval = nni_random() % - (oh->oh_maxval - oh->oh_minval) + oh->oh_minval; + oh->oh_minval = 1; + oh->oh_maxval = 0x7fffffff; + oh->oh_dynval = + nni_random() % (oh->oh_maxval - oh->oh_minval) + oh->oh_minval; oh->oh_ctor = ctor; oh->oh_dtor = dtor; - *ohp = oh; + *ohp = oh; return (0); } - void nni_objhash_fini(nni_objhash *oh) { @@ -87,7 +86,7 @@ nni_objhash_fini(nni_objhash *oh) return; } if (oh->oh_nodes != NULL) { - nni_free(oh->oh_nodes, oh->oh_cap * sizeof (nni_objhash_node)); + nni_free(oh->oh_nodes, oh->oh_cap * sizeof(nni_objhash_node)); oh->oh_nodes = NULL; oh->oh_cap = oh->oh_count = 0; oh->oh_load = oh->oh_minload = oh->oh_maxload = 0; @@ -97,19 +96,16 @@ nni_objhash_fini(nni_objhash *oh) NNI_FREE_STRUCT(oh); } - // Inspired by Python dict implementation. This probe will visit every // cell. We always hash consecutively assigned IDs. -#define NNI_OBJHASH_NEXTPROBE(h, j) \ - ((((j) * 5) + 1)& (h->oh_cap - 1)) - +#define NNI_OBJHASH_NEXTPROBE(h, j) ((((j) *5) + 1) & (h->oh_cap - 1)) // nni_objhash_find_node finds the object hash node associated with a given id. // The object hash lock must be held by the caller. static nni_objhash_node * nni_objhash_find_node(nni_objhash *oh, uint32_t id) { - uint32_t index; + uint32_t index; nni_objhash_node *node; if (oh->oh_count == 0) { @@ -131,14 +127,13 @@ nni_objhash_find_node(nni_objhash *oh, uint32_t id) } } - // nni_objhash_find looks up the object, and bumps the reference on it. // The caller should drop the reference when done by calling nni_objhash_unref. int nni_objhash_find(nni_objhash *oh, uint32_t id, void **valp) { nni_objhash_node *node; - int rv; + int rv; nni_mtx_lock(&oh->oh_lock); node = nni_objhash_find_node(oh, id); @@ -156,18 +151,17 @@ nni_objhash_find(nni_objhash *oh, uint32_t id, void **valp) return (rv); } - // Resize the object hash. This is called internally with the lock // for the object hash held. Grow indicates that this is being called // from a function that intends to add data, so extra space is needed. static int nni_objhash_resize(nni_objhash *oh, int grow) { - size_t newsize; - size_t oldsize; + size_t newsize; + size_t oldsize; nni_objhash_node *newnodes; nni_objhash_node *oldnodes; - uint32_t i; + uint32_t i; if ((!grow) && (oh->oh_count == 0) && (oh->oh_cap != 0)) { // Table is empty, and we are unrefing. Lets reclaim the @@ -175,15 +169,16 @@ nni_objhash_resize(nni_objhash *oh, int grow) // fluctuate between one and zero are going to bang on the // allocator a bit. Since such cases should not be very // performance sensitive, this is probably okay. - nni_free(oh->oh_nodes, oh->oh_cap * sizeof (nni_objhash_node)); - oh->oh_cap = 0; - oh->oh_nodes = NULL; + nni_free(oh->oh_nodes, oh->oh_cap * sizeof(nni_objhash_node)); + oh->oh_cap = 0; + oh->oh_nodes = NULL; oh->oh_minload = 0; oh->oh_maxload = 0; return (0); } - if ((oh->oh_load < oh->oh_maxload) && (oh->oh_load >= oh->oh_minload)) { + if ((oh->oh_load < oh->oh_maxload) && + (oh->oh_load >= oh->oh_minload)) { // No resize needed. return (0); } @@ -197,14 +192,14 @@ nni_objhash_resize(nni_objhash *oh, int grow) } oldnodes = oh->oh_nodes; - newnodes = nni_alloc(sizeof (nni_objhash_node) * newsize); + newnodes = nni_alloc(sizeof(nni_objhash_node) * newsize); if (newnodes == NULL) { return (NNG_ENOMEM); } - memset(newnodes, 0, sizeof (nni_objhash_node) * newsize); + memset(newnodes, 0, sizeof(nni_objhash_node) * newsize); oh->oh_nodes = newnodes; - oh->oh_cap = newsize; + oh->oh_cap = newsize; if (newsize > 8) { oh->oh_minload = newsize / 8; oh->oh_maxload = newsize * 2 / 3; @@ -222,7 +217,7 @@ nni_objhash_resize(nni_objhash *oh, int grow) if (newnodes[index].on_val == NULL) { oh->oh_load++; newnodes[index].on_val = oldnodes[i].on_val; - newnodes[index].on_id = oldnodes[i].on_id; + newnodes[index].on_id = oldnodes[i].on_id; newnodes[index].on_refcnt = oldnodes[i].on_refcnt; break; @@ -232,19 +227,18 @@ nni_objhash_resize(nni_objhash *oh, int grow) } } if (oldsize != 0) { - nni_free(oldnodes, sizeof (nni_objhash_node) * oldsize); + nni_free(oldnodes, sizeof(nni_objhash_node) * oldsize); } return (0); } - void nni_objhash_unref(nni_objhash *oh, uint32_t id) { - void *val; - uint32_t index; + void * val; + uint32_t index; nni_objhash_node *node; - nni_objhash_dtor dtor; + nni_objhash_dtor dtor; nni_mtx_lock(&oh->oh_lock); @@ -303,14 +297,13 @@ nni_objhash_unref(nni_objhash *oh, uint32_t id) dtor(val); } - void nni_objhash_unref_wait(nni_objhash *oh, uint32_t id) { - void *val; - uint32_t index; + void * val; + uint32_t index; nni_objhash_node *node; - nni_objhash_dtor dtor; + nni_objhash_dtor dtor; nni_mtx_lock(&oh->oh_lock); @@ -360,7 +353,6 @@ nni_objhash_unref_wait(nni_objhash *oh, uint32_t id) dtor(val); } - // Allocate a new object hash entry. Note that this will execute the // constructor with the object hash lock held. Consequently, code that // runs the constructor must not run for long periods of time, since that @@ -368,8 +360,8 @@ nni_objhash_unref_wait(nni_objhash *oh, uint32_t id) int nni_objhash_alloc(nni_objhash *oh, uint32_t *idp, void **valp) { - uint32_t id; - uint32_t index; + uint32_t id; + uint32_t index; nni_objhash_node *node; nni_mtx_lock(&oh->oh_lock); @@ -418,7 +410,8 @@ nni_objhash_alloc(nni_objhash *oh, uint32_t *idp, void **valp) node->on_val = oh->oh_ctor(id); if (node->on_val == NULL) { - // Constructor failed; walk *again* to undo the skip increments. + // Constructor failed; walk *again* to undo the skip + // increments. node->on_refcnt--; index = id & (oh->oh_cap - 1); for (;;) { @@ -433,7 +426,7 @@ nni_objhash_alloc(nni_objhash *oh, uint32_t *idp, void **valp) } nni_mtx_unlock(&oh->oh_lock); - return (NNG_ENOMEM); // no other return from ctor + return (NNG_ENOMEM); // no other return from ctor } oh->oh_count++; @@ -441,7 +434,7 @@ nni_objhash_alloc(nni_objhash *oh, uint32_t *idp, void **valp) oh->oh_load++; } *valp = node->on_val; - *idp = id; + *idp = id; NNI_ASSERT(node->on_refcnt == 1); @@ -449,7 +442,6 @@ nni_objhash_alloc(nni_objhash *oh, uint32_t *idp, void **valp) return (0); } - size_t nni_objhash_count(nni_objhash *oh) { diff --git a/src/core/objhash.h b/src/core/objhash.h index e234ca3d..97f666b8 100644 --- a/src/core/objhash.h +++ b/src/core/objhash.h @@ -29,8 +29,8 @@ // are powers of two to make the modulo dirt cheap. // -typedef struct nni_objhash nni_objhash; -typedef struct nni_objhash_node nni_objhash_node; +typedef struct nni_objhash nni_objhash; +typedef struct nni_objhash_node nni_objhash_node; // Object constructor function. This is expected to allocate an object. // It takes the generated object ID as an argument, which it can store on @@ -44,14 +44,15 @@ typedef void (*nni_objhash_dtor)(void *); // nni_objhash_init initializes the object hash; the constructor and and // destructor functions are supplied. -extern int nni_objhash_init(nni_objhash **, nni_objhash_ctor, nni_objhash_dtor); +extern int nni_objhash_init( + nni_objhash **, nni_objhash_ctor, nni_objhash_dtor); extern void nni_objhash_fini(nni_objhash *); -extern int nni_objhash_find(nni_objhash *, uint32_t, void **); -extern void nni_objhash_unref(nni_objhash *, uint32_t); -extern void nni_objhash_unref_wait(nni_objhash *, uint32_t); -extern int nni_objhash_alloc(nni_objhash *, uint32_t *, void **); +extern int nni_objhash_find(nni_objhash *, uint32_t, void **); +extern void nni_objhash_unref(nni_objhash *, uint32_t); +extern void nni_objhash_unref_wait(nni_objhash *, uint32_t); +extern int nni_objhash_alloc(nni_objhash *, uint32_t *, void **); extern size_t nni_objhash_count(nni_objhash *); -#endif // CORE_OBJHASH_H +#endif // CORE_OBJHASH_H diff --git a/src/core/options.c b/src/core/options.c index d975c585..6d4afdb4 100644 --- a/src/core/options.c +++ b/src/core/options.c @@ -16,10 +16,10 @@ nni_setopt_duration(nni_duration *ptr, const void *val, size_t size) { nni_duration dur; - if (size != sizeof (*ptr)) { + if (size != sizeof(*ptr)) { return (NNG_EINVAL); } - memcpy(&dur, val, sizeof (dur)); + memcpy(&dur, val, sizeof(dur)); if (dur < -1) { return (NNG_EINVAL); } @@ -27,16 +27,15 @@ nni_setopt_duration(nni_duration *ptr, const void *val, size_t size) return (0); } - int nni_setopt_int(int *ptr, const void *val, size_t size, int minval, int maxval) { int v; - if (size != sizeof (v)) { + if (size != sizeof(v)) { return (NNG_EINVAL); } - memcpy(&v, val, sizeof (v)); + memcpy(&v, val, sizeof(v)); if (v > maxval) { return (NNG_EINVAL); } @@ -47,17 +46,16 @@ nni_setopt_int(int *ptr, const void *val, size_t size, int minval, int maxval) return (0); } - int -nni_setopt_size(size_t *ptr, const void *val, size_t size, size_t minval, - size_t maxval) +nni_setopt_size( + size_t *ptr, const void *val, size_t size, size_t minval, size_t maxval) { size_t v; - if (size != sizeof (v)) { + if (size != sizeof(v)) { return (NNG_EINVAL); } - memcpy(&v, val, sizeof (v)); + memcpy(&v, val, sizeof(v)); if (v > maxval) { return (NNG_EINVAL); } @@ -68,58 +66,54 @@ nni_setopt_size(size_t *ptr, const void *val, size_t size, size_t minval, return (0); } - int nni_getopt_duration(nni_duration *ptr, void *val, size_t *sizep) { - size_t sz = sizeof (*ptr); + size_t sz = sizeof(*ptr); if (sz > *sizep) { sz = *sizep; } - *sizep = sizeof (*ptr); + *sizep = sizeof(*ptr); memcpy(val, ptr, sz); return (0); } - int nni_getopt_int(int *ptr, void *val, size_t *sizep) { - size_t sz = sizeof (*ptr); + size_t sz = sizeof(*ptr); if (sz > *sizep) { sz = *sizep; } - *sizep = sizeof (*ptr); + *sizep = sizeof(*ptr); memcpy(val, ptr, sz); return (0); } - int nni_getopt_size(size_t *ptr, void *val, size_t *sizep) { - size_t sz = sizeof (*ptr); + size_t sz = sizeof(*ptr); if (sz > *sizep) { sz = *sizep; } - *sizep = sizeof (*ptr); + *sizep = sizeof(*ptr); memcpy(val, ptr, sz); return (0); } - int nni_setopt_buf(nni_msgq *mq, const void *val, size_t sz) { int len; - if (sz < sizeof (len)) { + if (sz < sizeof(len)) { return (NNG_EINVAL); } - memcpy(&len, val, sizeof (len)); + memcpy(&len, val, sizeof(len)); if (len < 0) { return (NNG_EINVAL); } @@ -132,7 +126,6 @@ nni_setopt_buf(nni_msgq *mq, const void *val, size_t sz) return (nni_msgq_resize(mq, len)); } - int nni_getopt_buf(nni_msgq *mq, void *val, size_t *sizep) { @@ -140,15 +133,14 @@ nni_getopt_buf(nni_msgq *mq, void *val, size_t *sizep) size_t sz = *sizep; - if (sz > sizeof (len)) { - sz = sizeof (len); + if (sz > sizeof(len)) { + sz = sizeof(len); } memcpy(val, &len, sz); - *sizep = sizeof (len); + *sizep = sizeof(len); return (0); } - static void nni_notifyfd_push(struct nng_event *ev, void *arg) { @@ -159,13 +151,12 @@ nni_notifyfd_push(struct nng_event *ev, void *arg) nni_plat_pipe_raise(fd->sn_wfd); } - int nni_getopt_fd(nni_sock *s, nni_notifyfd *fd, int mask, void *val, size_t *szp) { int rv; - if ((*szp < sizeof (int))) { + if ((*szp < sizeof(int))) { return (NNG_EINVAL); } @@ -186,8 +177,8 @@ nni_getopt_fd(nni_sock *s, nni_notifyfd *fd, int mask, void *val, size_t *szp) // If we already inited this, just give back the same file descriptor. if (fd->sn_init) { - memcpy(val, &fd->sn_rfd, sizeof (int)); - *szp = sizeof (int); + memcpy(val, &fd->sn_rfd, sizeof(int)); + *szp = sizeof(int); return (0); } @@ -200,7 +191,7 @@ nni_getopt_fd(nni_sock *s, nni_notifyfd *fd, int mask, void *val, size_t *szp) return (NNG_ENOMEM); } - *szp = sizeof (int); - memcpy(val, &fd->sn_rfd, sizeof (int)); + *szp = sizeof(int); + memcpy(val, &fd->sn_rfd, sizeof(int)); return (0); } diff --git a/src/core/options.h b/src/core/options.h index ec5cce90..4f958e55 100644 --- a/src/core/options.h +++ b/src/core/options.h @@ -31,8 +31,8 @@ extern int nni_getopt_duration(nni_duration *, void *, size_t *); // maximum values (inclusive). extern int nni_setopt_int(int *, const void *, size_t, int, int); -#define NNI_MAXINT ((int) 2147483647) -#define NNI_MININT ((int) -2147483648) +#define NNI_MAXINT ((int) 2147483647) +#define NNI_MININT ((int) -2147483648) // nni_getopt_int gets an integer. extern int nni_getopt_int(int *, void *, size_t *); @@ -42,8 +42,8 @@ extern int nni_setopt_size(size_t *, const void *, size_t, size_t, size_t); // We limit the maximum size to 4GB. That's intentional because some of the // underlying protocols cannot cope with anything bigger than 32-bits. -#define NNI_MINSZ (0) -#define NNI_MAXSZ ((size_t) 0xffffffff) +#define NNI_MINSZ (0) +#define NNI_MAXSZ ((size_t) 0xffffffff) // nni_getopt_size obtains a size_t option. extern int nni_getopt_size(size_t *, void *, size_t *); @@ -51,4 +51,4 @@ extern int nni_getopt_size(size_t *, void *, size_t *); // nni_getopt_fd obtains a notification file descriptor. extern int nni_getopt_fd(nni_sock *, nni_notifyfd *, int, void *, size_t *); -#endif // CORE_OPTIONS_H +#endif // CORE_OPTIONS_H diff --git a/src/core/panic.c b/src/core/panic.c index b3d64dc3..f3c7cb26 100644 --- a/src/core/panic.c +++ b/src/core/panic.c @@ -7,12 +7,12 @@ // found online at https://opensource.org/licenses/MIT. // -#include <stdlib.h> -#include <string.h> #include <stdarg.h> #include <stdio.h> +#include <stdlib.h> +#include <string.h> -#ifdef NNG_HAVE_BACKTRACE +#ifdef NNG_HAVE_BACKTRACE #include <execinfo.h> #endif @@ -23,12 +23,12 @@ void nni_show_backtrace(void) { #if NNG_HAVE_BACKTRACE - void *frames[50]; - int nframes; + void * frames[50]; + int nframes; char **lines; - int i; + int i; - nframes = backtrace(frames, sizeof (frames) / sizeof (frames[0])); + nframes = backtrace(frames, sizeof(frames) / sizeof(frames[0])); if (nframes > 1) { lines = backtrace_symbols(frames, nframes); if (lines == NULL) { @@ -41,7 +41,6 @@ nni_show_backtrace(void) #endif } - // nni_panic shows a panic message, a possible stack bracktrace, then aborts // the process/program. This should only be called when a condition arises // that should not be possible, e.g. a programming assertion failure. It should @@ -50,16 +49,15 @@ nni_show_backtrace(void) void nni_panic(const char *fmt, ...) { - char buf[128]; - char fbuf[128]; + char buf[128]; + char fbuf[128]; va_list va; va_start(va, fmt); - (void) vsnprintf(fbuf, sizeof (fbuf), fmt, va); + (void) vsnprintf(fbuf, sizeof(fbuf), fmt, va); va_end(va); - - (void) snprintf(buf, sizeof (buf), "panic: %s", fbuf); + (void) snprintf(buf, sizeof(buf), "panic: %s", fbuf); nni_println(buf); nni_println("This message is indicative of a BUG."); @@ -69,7 +67,6 @@ nni_panic(const char *fmt, ...) nni_plat_abort(); } - void nni_println(const char *msg) { diff --git a/src/core/panic.h b/src/core/panic.h index 323d64a1..6af14581 100644 --- a/src/core/panic.h +++ b/src/core/panic.h @@ -26,4 +26,4 @@ extern void nni_panic(const char *, ...); // include one. extern void nni_println(const char *); -#endif // CORE_PANIC_H +#endif // CORE_PANIC_H diff --git a/src/core/pipe.c b/src/core/pipe.c index 9151ede0..702e8eb1 100644 --- a/src/core/pipe.c +++ b/src/core/pipe.c @@ -29,10 +29,10 @@ nni_pipe_ctor(uint32_t id) return (NULL); } - p->p_tran_data = NULL; + p->p_tran_data = NULL; p->p_proto_data = NULL; p->p_proto_dtor = NULL; - p->p_id = id; + p->p_id = id; NNI_LIST_NODE_INIT(&p->p_sock_node); NNI_LIST_NODE_INIT(&p->p_ep_node); @@ -40,7 +40,6 @@ nni_pipe_ctor(uint32_t id) return (p); } - static void nni_pipe_dtor(void *ptr) { @@ -58,7 +57,6 @@ nni_pipe_dtor(void *ptr) NNI_FREE_STRUCT(p); } - int nni_pipe_sys_init(void) { @@ -73,7 +71,6 @@ nni_pipe_sys_init(void) return (0); } - void nni_pipe_sys_fini(void) { @@ -81,7 +78,6 @@ nni_pipe_sys_fini(void) nni_pipes = NULL; } - // nni_pipe_id returns the 32-bit pipe id, which can be used in backtraces. uint32_t nni_pipe_id(nni_pipe *p) @@ -89,21 +85,18 @@ nni_pipe_id(nni_pipe *p) return (p->p_id); } - void nni_pipe_recv(nni_pipe *p, nni_aio *aio) { p->p_tran_ops.p_recv(p->p_tran_data, aio); } - void nni_pipe_send(nni_pipe *p, nni_aio *aio) { p->p_tran_ops.p_send(p->p_tran_data, aio); } - // nni_pipe_close closes the underlying connection. It is expected that // subsequent attempts receive or send (including any waiting receive) will // simply return NNG_ECLOSED. @@ -128,7 +121,6 @@ nni_pipe_close(nni_pipe *p) nni_mtx_unlock(&p->p_mtx); } - // We have to stop asynchronously using a task, because otherwise we can // wind up having a callback from an AIO trying to cancel itself. That // simply will not work. @@ -147,7 +139,6 @@ nni_pipe_remove(nni_pipe *p) nni_objhash_unref(nni_pipes, p->p_id); } - void nni_pipe_stop(nni_pipe *p) { @@ -163,20 +154,18 @@ nni_pipe_stop(nni_pipe *p) nni_taskq_dispatch(NULL, &p->p_reap_tqe); } - uint16_t nni_pipe_peer(nni_pipe *p) { return (p->p_tran_ops.p_peer(p->p_tran_data)); } - static void nni_pipe_start_cb(void *arg) { - nni_pipe *p = arg; - nni_aio *aio = &p->p_start_aio; - int rv; + nni_pipe *p = arg; + nni_aio * aio = &p->p_start_aio; + int rv; nni_mtx_lock(&p->p_mtx); if ((rv = nni_aio_result(aio)) != 0) { @@ -192,13 +181,12 @@ nni_pipe_start_cb(void *arg) } } - int nni_pipe_create(nni_pipe **pp, nni_ep *ep, nni_sock *sock, nni_tran *tran) { nni_pipe *p; - int rv; - uint32_t id; + int rv; + uint32_t id; rv = nni_objhash_alloc(nni_pipes, &id, (void **) &p); if (rv != 0) { @@ -209,7 +197,7 @@ nni_pipe_create(nni_pipe **pp, nni_ep *ep, nni_sock *sock, nni_tran *tran) return (rv); } p->p_sock = sock; - p->p_ep = ep; + p->p_ep = ep; // Make a copy of the transport ops. We can override entry points // and we avoid an extra dereference on hot code paths. @@ -234,7 +222,6 @@ nni_pipe_create(nni_pipe **pp, nni_ep *ep, nni_sock *sock, nni_tran *tran) return (0); } - int nni_pipe_getopt(nni_pipe *p, int opt, void *val, size_t *szp) { @@ -245,7 +232,6 @@ nni_pipe_getopt(nni_pipe *p, int opt, void *val, size_t *szp) return (p->p_tran_ops.p_getopt(p->p_tran_data, opt, val, szp)); } - int nni_pipe_start(nni_pipe *p) { @@ -262,21 +248,18 @@ nni_pipe_start(nni_pipe *p) return (0); } - void * nni_pipe_get_proto_data(nni_pipe *p) { return (p->p_proto_data); } - void nni_pipe_sock_list_init(nni_list *list) { NNI_LIST_INIT(list, nni_pipe, p_sock_node); } - void nni_pipe_ep_list_init(nni_list *list) { diff --git a/src/core/pipe.h b/src/core/pipe.h index f7df8232..3107ffb3 100644 --- a/src/core/pipe.h +++ b/src/core/pipe.h @@ -19,23 +19,23 @@ #include "core/transport.h" struct nni_pipe { - uint32_t p_id; - nni_tran_pipe p_tran_ops; - void * p_tran_data; - void * p_proto_data; - nni_cb p_proto_dtor; - nni_list_node p_sock_node; - nni_list_node p_ep_node; - nni_sock * p_sock; - nni_ep * p_ep; - int p_reap; - int p_stop; - nni_mtx p_mtx; - nni_taskq_ent p_reap_tqe; - nni_aio p_start_aio; + uint32_t p_id; + nni_tran_pipe p_tran_ops; + void * p_tran_data; + void * p_proto_data; + nni_cb p_proto_dtor; + nni_list_node p_sock_node; + nni_list_node p_ep_node; + nni_sock * p_sock; + nni_ep * p_ep; + int p_reap; + int p_stop; + nni_mtx p_mtx; + nni_taskq_ent p_reap_tqe; + nni_aio p_start_aio; }; -extern int nni_pipe_sys_init(void); +extern int nni_pipe_sys_init(void); extern void nni_pipe_sys_fini(void); // AIO @@ -62,7 +62,7 @@ extern int nni_pipe_create(nni_pipe **, nni_ep *, nni_sock *, nni_tran *); extern uint16_t nni_pipe_proto(nni_pipe *); extern uint16_t nni_pipe_peer(nni_pipe *); -extern int nni_pipe_start(nni_pipe *); +extern int nni_pipe_start(nni_pipe *); extern int nni_pipe_getopt(nni_pipe *, int, void *, size_t *sizep); // nni_pipe_get_proto_data gets the protocol private data set with the diff --git a/src/core/platform.h b/src/core/platform.h index 107b180e..b3fc7572 100644 --- a/src/core/platform.h +++ b/src/core/platform.h @@ -76,9 +76,9 @@ extern void *nni_alloc(size_t); // Most implementations can just call free() here. extern void nni_free(void *, size_t); -typedef struct nni_plat_mtx nni_plat_mtx; -typedef struct nni_plat_cv nni_plat_cv; -typedef struct nni_plat_thr nni_plat_thr; +typedef struct nni_plat_mtx nni_plat_mtx; +typedef struct nni_plat_cv nni_plat_cv; +typedef struct nni_plat_thr nni_plat_thr; // // Threading & Synchronization Support @@ -187,7 +187,6 @@ extern int nni_plat_init(int (*)(void)); // will be called until nni_platform_init is called. extern void nni_plat_fini(void); - // nni_plat_lookup_host looks up a hostname in DNS, or the local hosts // file, or whatever. If your platform lacks support for naming, it must // at least cope with converting IP addresses in string form. The final @@ -199,8 +198,8 @@ extern int nni_plat_lookup_host(const char *, nni_sockaddr *, int); // TCP Support. // -typedef struct nni_plat_tcp_ep nni_plat_tcp_ep; -typedef struct nni_plat_tcp_pipe nni_plat_tcp_pipe; +typedef struct nni_plat_tcp_ep nni_plat_tcp_ep; +typedef struct nni_plat_tcp_pipe nni_plat_tcp_pipe; // nni_plat_tcp_ep_init creates a new endpoint associated with the url. extern int nni_plat_tcp_ep_init(nni_plat_tcp_ep **, const char *, int); @@ -247,15 +246,15 @@ extern void nni_plat_tcp_pipe_recv(nni_plat_tcp_pipe *, nni_aio *); // return names of either family. The passive flag indicates that the // name will be used for bind(), otherwise the name will be used with // connect(). The host part may be NULL only if passive is true. -extern void nni_plat_tcp_resolv(const char *, const char *, int, int, - nni_aio *); +extern void nni_plat_tcp_resolv( + const char *, const char *, int, int, nni_aio *); // // IPC (UNIX Domain Sockets & Named Pipes) Support. // -typedef struct nni_plat_ipc_ep nni_plat_ipc_ep; -typedef struct nni_plat_ipc_pipe nni_plat_ipc_pipe; +typedef struct nni_plat_ipc_ep nni_plat_ipc_ep; +typedef struct nni_plat_ipc_pipe nni_plat_ipc_pipe; // nni_plat_ipc_ep_init creates a new endpoint associated with the url. extern int nni_plat_ipc_ep_init(nni_plat_ipc_ep **, const char *, int); diff --git a/src/core/protocol.c b/src/core/protocol.c index 6aa59e84..a60454de 100644 --- a/src/core/protocol.c +++ b/src/core/protocol.c @@ -28,6 +28,7 @@ extern nni_proto nni_surveyor_proto; extern nni_proto nni_respondent_proto; static nni_proto *protocols[] = { + // clang-format off &nni_bus_proto, &nni_pair_proto, &nni_rep_proto, @@ -39,12 +40,13 @@ static nni_proto *protocols[] = { &nni_surveyor_proto, &nni_respondent_proto, NULL + // clang-format on }; nni_proto * nni_proto_find(uint16_t num) { - int i; + int i; nni_proto *p; for (i = 0; (p = protocols[i]) != NULL; i++) { @@ -55,7 +57,6 @@ nni_proto_find(uint16_t num) return (p); } - const char * nni_proto_name(uint16_t num) { @@ -67,12 +68,11 @@ nni_proto_name(uint16_t num) return (p->proto_name); } - uint16_t nni_proto_number(const char *name) { nni_proto *p; - int i; + int i; for (i = 0; (p = protocols[i]) != NULL; i++) { if (strcmp(p->proto_name, name) == 0) { @@ -82,7 +82,6 @@ nni_proto_number(const char *name) return (NNG_PROTO_NONE); } - uint16_t nni_proto_peer(uint16_t num) { diff --git a/src/core/protocol.h b/src/core/protocol.h index 745f5986..3a133469 100644 --- a/src/core/protocol.h +++ b/src/core/protocol.h @@ -24,18 +24,18 @@ struct nni_proto_pipe_ops { // pipe_init creates the protocol-specific per pipe data structure. // The last argument is the per-socket protocol private data. - int (*pipe_init)(void **, nni_pipe *, void *); + int (*pipe_init)(void **, nni_pipe *, void *); // pipe_fini releases any pipe data structures. This is called after // the pipe has been removed from the protocol, and the generic // pipe threads have been stopped. - void (*pipe_fini)(void *); + void (*pipe_fini)(void *); // pipe_start is called to register a pipe with the protocol. The // protocol can reject this, for example if another pipe is already // active on a 1:1 protocol. The protocol may not block during this, // as the socket lock is held. - int (*pipe_start)(void *); + int (*pipe_start)(void *); // pipe_stop is called to unregister a pipe from the protocol. // Threads may still acccess data structures, so the protocol @@ -43,66 +43,66 @@ struct nni_proto_pipe_ops { // lock held, so the protocol may not call back into the socket, and // must not block. This operation must be idempotent, and may // be called even if pipe_start was not. - void (*pipe_stop)(void *); + void (*pipe_stop)(void *); }; struct nni_proto_sock_ops { // sock_initf creates the protocol instance, which will be stored on // the socket. This is run without the sock lock held, and allocates // storage or other resources for the socket. - int (*sock_init)(void **, nni_sock *); + int (*sock_init)(void **, nni_sock *); // sock_fini destroys the protocol instance. This is run without the // socket lock held, and is intended to release resources. It may // block as needed. - void (*sock_fini)(void *); + void (*sock_fini)(void *); // Open the protocol instance. This is run with the lock held, // and intended to allow the protocol to start any asynchronous // processing. - void (*sock_open)(void *); + void (*sock_open)(void *); // Close the protocol instance. This is run with the lock held, // and intended to initiate closure of the socket. For example, // it can signal the socket worker threads to exit. - void (*sock_close)(void *); + void (*sock_close)(void *); // Option manipulation. These may be NULL. - int (*sock_setopt)(void *, int, const void *, size_t); - int (*sock_getopt)(void *, int, void *, size_t *); + int (*sock_setopt)(void *, int, const void *, size_t); + int (*sock_getopt)(void *, int, void *, size_t *); // Receive filter. This may be NULL, but if it isn't, then // messages coming into the system are routed here just before being // delivered to the application. To drop the message, the prtocol // should return NULL, otherwise the message (possibly modified). - nni_msg * (*sock_rfilter)(void *, nni_msg *); + nni_msg *(*sock_rfilter)(void *, nni_msg *); // Send filter. This may be NULL, but if it isn't, then messages // here are filtered just after they come from the application. - nni_msg * (*sock_sfilter)(void *, nni_msg *); + nni_msg *(*sock_sfilter)(void *, nni_msg *); }; struct nni_proto { - uint16_t proto_self; // our 16-bit D - uint16_t proto_peer; // who we peer with (ID) - const char * proto_name; // Our name - uint32_t proto_flags; // Protocol flags - const nni_proto_sock_ops * proto_sock_ops; // Per-socket opeations - const nni_proto_pipe_ops * proto_pipe_ops; // Per-pipe operations. + uint16_t proto_self; // our 16-bit D + uint16_t proto_peer; // who we peer with (ID) + const char * proto_name; // Our name + uint32_t proto_flags; // Protocol flags + const nni_proto_sock_ops *proto_sock_ops; // Per-socket opeations + const nni_proto_pipe_ops *proto_pipe_ops; // Per-pipe operations. }; // These flags determine which operations make sense. We use them so that // we can reject attempts to create notification fds for operations that make // no sense. -#define NNI_PROTO_FLAG_RCV 1 // Protocol can receive -#define NNI_PROTO_FLAG_SND 2 // Protocol can send -#define NNI_PROTO_FLAG_SNDRCV 3 // Protocol can both send & recv +#define NNI_PROTO_FLAG_RCV 1 // Protocol can receive +#define NNI_PROTO_FLAG_SND 2 // Protocol can send +#define NNI_PROTO_FLAG_SNDRCV 3 // Protocol can both send & recv // These functions are not used by protocols, but rather by the socket // core implementation. The lookups can be used by transports as well. -extern nni_proto *nni_proto_find(uint16_t); +extern nni_proto * nni_proto_find(uint16_t); extern const char *nni_proto_name(uint16_t); -extern uint16_t nni_proto_number(const char *); -extern uint16_t nni_proto_peer(uint16_t); +extern uint16_t nni_proto_number(const char *); +extern uint16_t nni_proto_peer(uint16_t); #endif // CORE_PROTOCOL_H diff --git a/src/core/random.c b/src/core/random.c index 00c0bcf7..eb0b4fc2 100644 --- a/src/core/random.c +++ b/src/core/random.c @@ -21,47 +21,46 @@ typedef struct { // the rsl is the actual results, and the randcnt is the length // of the results. - uint32_t randrsl[256]; - uint32_t randcnt; + uint32_t randrsl[256]; + uint32_t randcnt; // lock to protect concurrent access - nni_mtx mx; + nni_mtx mx; // more or less internal state - uint32_t mm[256]; - uint32_t aa; - uint32_t bb; - uint32_t cc; + uint32_t mm[256]; + uint32_t aa; + uint32_t bb; + uint32_t cc; } nni_isaac_ctx; - static void nni_isaac(nni_isaac_ctx *ctx) { register uint32_t i, x, y; - ctx->cc++; // cc incremented once per 256 results - ctx->bb += ctx->cc; // then combined with bb + ctx->cc++; // cc incremented once per 256 results + ctx->bb += ctx->cc; // then combined with bb for (i = 0; i < 256; ++i) { x = ctx->mm[i]; - switch (i%4) { + switch (i % 4) { case 0: - ctx->aa ^= (ctx->aa<<13); + ctx->aa ^= (ctx->aa << 13); break; case 1: - ctx->aa ^= (ctx->aa>>6); + ctx->aa ^= (ctx->aa >> 6); break; case 2: - ctx->aa ^= (ctx->aa<<2); + ctx->aa ^= (ctx->aa << 2); break; case 3: - ctx->aa ^= (ctx->aa>>16); + ctx->aa ^= (ctx->aa >> 16); break; } - ctx->aa += ctx->mm[(i+128)%256]; - ctx->mm[i] = y = ctx->mm[(x>>2)%256] + ctx->aa + ctx->bb; - ctx->randrsl[i] = ctx->bb = ctx->mm[(y>>10)%256] + x; + ctx->aa += ctx->mm[(i + 128) % 256]; + ctx->mm[i] = y = ctx->mm[(x >> 2) % 256] + ctx->aa + ctx->bb; + ctx->randrsl[i] = ctx->bb = ctx->mm[(y >> 10) % 256] + x; // Note that bits 2..9 are chosen from x but 10..17 are chosen // from y. The only important thing here is that 2..9 and @@ -73,83 +72,97 @@ nni_isaac(nni_isaac_ctx *ctx) } } - // if (flag!=0), then use the contents of randrsl[] to initialize mm[]. #define nni_isaac_mix(a, b, c, d, e, f, g, h) \ - { \ - a ^= b<<11; d += a; b += c; \ - b ^= c>>2; e += b; c += d; \ - c ^= d<<8; f += c; d += e; \ - d ^= e>>16; g += d; e += f; \ - e ^= f<<10; h += e; f += g; \ - f ^= g>>4; a += f; g += h; \ - g ^= h<<8; b += g; h += a; \ - h ^= a>>9; c += h; a += b; \ + { \ + a ^= b << 11; \ + d += a; \ + b += c; \ + b ^= c >> 2; \ + e += b; \ + c += d; \ + c ^= d << 8; \ + f += c; \ + d += e; \ + d ^= e >> 16; \ + g += d; \ + e += f; \ + e ^= f << 10; \ + h += e; \ + f += g; \ + f ^= g >> 4; \ + a += f; \ + g += h; \ + g ^= h << 8; \ + b += g; \ + h += a; \ + h ^= a >> 9; \ + c += h; \ + a += b; \ } static void nni_isaac_randinit(nni_isaac_ctx *ctx, int flag) { - int i; + int i; uint32_t a, b, c, d, e, f, g, h; ctx->aa = ctx->bb = ctx->cc = 0; - a = b = c = d = e = f = g = h = 0x9e3779b9; // the golden ratio + a = b = c = d = e = f = g = h = 0x9e3779b9; // the golden ratio - for (i = 0; i < 4; ++i) { // scramble it + for (i = 0; i < 4; ++i) { // scramble it nni_isaac_mix(a, b, c, d, e, f, g, h); } - for (i = 0; i < 256; i += 8) { // fill in mm[] with messy stuff - if (flag) { // use all the information in the seed + for (i = 0; i < 256; i += 8) { // fill in mm[] with messy stuff + if (flag) { // use all the information in the seed a += ctx->randrsl[i]; - b += ctx->randrsl[i+1]; - c += ctx->randrsl[i+2]; - d += ctx->randrsl[i+3]; - e += ctx->randrsl[i+4]; - f += ctx->randrsl[i+5]; - g += ctx->randrsl[i+6]; - h += ctx->randrsl[i+7]; + b += ctx->randrsl[i + 1]; + c += ctx->randrsl[i + 2]; + d += ctx->randrsl[i + 3]; + e += ctx->randrsl[i + 4]; + f += ctx->randrsl[i + 5]; + g += ctx->randrsl[i + 6]; + h += ctx->randrsl[i + 7]; } nni_isaac_mix(a, b, c, d, e, f, g, h); - ctx->mm[i] = a; - ctx->mm[i+1] = b; - ctx->mm[i+2] = c; - ctx->mm[i+3] = d; - ctx->mm[i+4] = e; - ctx->mm[i+5] = f; - ctx->mm[i+6] = g; - ctx->mm[i+7] = h; + ctx->mm[i] = a; + ctx->mm[i + 1] = b; + ctx->mm[i + 2] = c; + ctx->mm[i + 3] = d; + ctx->mm[i + 4] = e; + ctx->mm[i + 5] = f; + ctx->mm[i + 6] = g; + ctx->mm[i + 7] = h; } if (flag) { // do a second pass to make all of the seed affect all of mm for (i = 0; i < 256; i += 8) { a += ctx->mm[i]; - b += ctx->mm[i+1]; - c += ctx->mm[i+2]; - d += ctx->mm[i+3]; - e += ctx->mm[i+4]; - f += ctx->mm[i+5]; - g += ctx->mm[i+6]; - h += ctx->mm[i+7]; + b += ctx->mm[i + 1]; + c += ctx->mm[i + 2]; + d += ctx->mm[i + 3]; + e += ctx->mm[i + 4]; + f += ctx->mm[i + 5]; + g += ctx->mm[i + 6]; + h += ctx->mm[i + 7]; nni_isaac_mix(a, b, c, d, e, f, g, h); - ctx->mm[i] = a; - ctx->mm[i+1] = b; - ctx->mm[i+2] = c; - ctx->mm[i+3] = d; - ctx->mm[i+4] = e; - ctx->mm[i+5] = f; - ctx->mm[i+6] = g; - ctx->mm[i+7] = h; + ctx->mm[i] = a; + ctx->mm[i + 1] = b; + ctx->mm[i + 2] = c; + ctx->mm[i + 3] = d; + ctx->mm[i + 4] = e; + ctx->mm[i + 5] = f; + ctx->mm[i + 6] = g; + ctx->mm[i + 7] = h; } } - nni_isaac(ctx); // fill in the first set of results - ctx->randcnt = 256; // prepare to use the first set of results + nni_isaac(ctx); // fill in the first set of results + ctx->randcnt = 256; // prepare to use the first set of results } - static nni_isaac_ctx nni_random_ctx; int @@ -157,22 +170,21 @@ nni_random_sys_init(void) { // minimally, grab the system clock nni_isaac_ctx *ctx = &nni_random_ctx; - int rv; + int rv; if ((rv = nni_mtx_init(&ctx->mx)) != 0) { return (rv); } - nni_plat_seed_prng(ctx->randrsl, sizeof (ctx->randrsl)); + nni_plat_seed_prng(ctx->randrsl, sizeof(ctx->randrsl)); nni_isaac_randinit(ctx, 1); return (0); } - uint32_t nni_random(void) { - uint32_t rv; + uint32_t rv; nni_isaac_ctx *ctx = &nni_random_ctx; nni_mtx_lock(&ctx->mx); @@ -187,7 +199,6 @@ nni_random(void) return (rv); } - void nni_random_sys_fini(void) { diff --git a/src/core/socket.c b/src/core/socket.c index f7712aa3..c561b719 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -21,7 +21,6 @@ nni_sock_id(nni_sock *s) return (s->s_id); } - // nni_sock_sendq and nni_sock_recvq are called by the protocol to obtain // the upper read and write queues. nni_msgq * @@ -30,18 +29,16 @@ nni_sock_sendq(nni_sock *s) return (s->s_uwq); } - nni_msgq * nni_sock_recvq(nni_sock *s) { return (s->s_urq); } - int nni_sock_find(nni_sock **sockp, uint32_t id) { - int rv; + int rv; nni_sock *sock; if ((rv = nni_init()) != 0) { @@ -64,7 +61,6 @@ nni_sock_find(nni_sock **sockp, uint32_t id) return (0); } - void nni_sock_hold(nni_sock *sock) { @@ -74,18 +70,16 @@ nni_sock_hold(nni_sock *sock) NNI_ASSERT(rv == 0); } - void nni_sock_rele(nni_sock *sock) { nni_objhash_unref(nni_socks, sock->s_id); } - int nni_sock_pipe_ready(nni_sock *sock, nni_pipe *pipe) { - int rv; + int rv; void *pdata = nni_pipe_get_proto_data(pipe); nni_mtx_lock(&sock->s_mx); @@ -112,7 +106,6 @@ nni_sock_pipe_ready(nni_sock *sock, nni_pipe *pipe) return (0); } - void nni_sock_pipe_stop(nni_sock *sock, nni_pipe *pipe) { @@ -136,26 +129,23 @@ nni_sock_pipe_stop(nni_sock *sock, nni_pipe *pipe) nni_mtx_unlock(&sock->s_mx); } - void nni_sock_lock(nni_sock *sock) { nni_mtx_lock(&sock->s_mx); } - void nni_sock_unlock(nni_sock *sock) { nni_mtx_unlock(&sock->s_mx); } - static void nni_sock_cansend_cb(void *arg) { nni_notify *notify = arg; - nni_sock *sock = notify->n_sock; + nni_sock * sock = notify->n_sock; if (nni_aio_result(¬ify->n_aio) != 0) { return; @@ -164,12 +154,11 @@ nni_sock_cansend_cb(void *arg) notify->n_func(&sock->s_send_ev, notify->n_arg); } - static void nni_sock_canrecv_cb(void *arg) { nni_notify *notify = arg; - nni_sock *sock = notify->n_sock; + nni_sock * sock = notify->n_sock; if (nni_aio_result(¬ify->n_aio) != 0) { return; @@ -178,19 +167,18 @@ nni_sock_canrecv_cb(void *arg) notify->n_func(&sock->s_recv_ev, notify->n_arg); } - nni_notify * nni_sock_notify(nni_sock *sock, int type, nng_notify_func fn, void *arg) { nni_notify *notify; - int rv; + int rv; if ((notify = NNI_ALLOC_STRUCT(notify)) == NULL) { return (NULL); } notify->n_func = fn; - notify->n_arg = arg; + notify->n_arg = arg; notify->n_type = type; notify->n_sock = sock; @@ -223,7 +211,6 @@ fail: return (NULL); } - void nni_sock_unnotify(nni_sock *sock, nni_notify *notify) { @@ -231,14 +218,12 @@ nni_sock_unnotify(nni_sock *sock, nni_notify *notify) NNI_FREE_STRUCT(notify); } - nni_mtx * nni_sock_mtx(nni_sock *sock) { return (&sock->s_mx); } - static nni_msg * nni_sock_nullfilter(void *arg, nni_msg *mp) { @@ -246,7 +231,6 @@ nni_sock_nullfilter(void *arg, nni_msg *mp) return (mp); } - static int nni_sock_nullgetopt(void *arg, int num, void *data, size_t *szp) { @@ -257,7 +241,6 @@ nni_sock_nullgetopt(void *arg, int num, void *data, size_t *szp) return (NNG_ENOTSUP); } - static int nni_sock_nullsetopt(void *arg, int num, const void *data, size_t sz) { @@ -268,14 +251,12 @@ nni_sock_nullsetopt(void *arg, int num, const void *data, size_t sz) return (NNG_ENOTSUP); } - static void nni_sock_nullop(void *arg) { NNI_ARG_UNUSED(arg); } - static int nni_sock_nullstartpipe(void *arg) { @@ -284,25 +265,24 @@ nni_sock_nullstartpipe(void *arg) return (0); } - static void * nni_sock_ctor(uint32_t id) { - int rv; + int rv; nni_sock *sock; if ((sock = NNI_ALLOC_STRUCT(sock)) == NULL) { return (NULL); } // s_protocol, s_peer, and s_flags undefined as yet. - sock->s_linger = 0; - sock->s_sndtimeo = -1; - sock->s_rcvtimeo = -1; - sock->s_closing = 0; - sock->s_reconn = NNI_SECOND; + sock->s_linger = 0; + sock->s_sndtimeo = -1; + sock->s_rcvtimeo = -1; + sock->s_closing = 0; + sock->s_reconn = NNI_SECOND; sock->s_reconnmax = 0; - sock->s_rcvmaxsz = 1024 * 1024; // 1 MB by default - sock->s_id = id; + sock->s_rcvmaxsz = 1024 * 1024; // 1 MB by default + sock->s_id = id; nni_pipe_sock_list_init(&sock->s_pipes); @@ -343,7 +323,6 @@ fail: return (NULL); } - static void nni_sock_dtor(void *ptr) { @@ -351,12 +330,12 @@ nni_sock_dtor(void *ptr) // Close any open notification pipes. if (sock->s_recv_fd.sn_init) { - nni_plat_pipe_close(sock->s_recv_fd.sn_wfd, - sock->s_recv_fd.sn_rfd); + nni_plat_pipe_close( + sock->s_recv_fd.sn_wfd, sock->s_recv_fd.sn_rfd); } if (sock->s_send_fd.sn_init) { - nni_plat_pipe_close(sock->s_send_fd.sn_wfd, - sock->s_send_fd.sn_rfd); + nni_plat_pipe_close( + sock->s_send_fd.sn_wfd, sock->s_send_fd.sn_rfd); } // The protocol needs to clean up its state. @@ -373,7 +352,6 @@ nni_sock_dtor(void *ptr) NNI_FREE_STRUCT(sock); } - int nni_sock_sys_init(void) { @@ -384,7 +362,6 @@ nni_sock_sys_init(void) return (rv); } - void nni_sock_sys_fini(void) { @@ -392,17 +369,16 @@ nni_sock_sys_fini(void) nni_socks = NULL; } - // nn_sock_open creates the underlying socket. int nni_sock_open(nni_sock **sockp, uint16_t pnum) { - nni_sock *sock; - nni_proto *proto; - int rv; + nni_sock * sock; + nni_proto * proto; + int rv; nni_proto_sock_ops *sops; nni_proto_pipe_ops *pops; - uint32_t sockid; + uint32_t sockid; if ((rv = nni_init()) != 0) { return (rv); @@ -418,8 +394,8 @@ nni_sock_open(nni_sock **sockp, uint16_t pnum) // We make a copy of the protocol operations. sock->s_protocol = proto->proto_self; - sock->s_peer = proto->proto_peer; - sock->s_flags = proto->proto_flags; + sock->s_peer = proto->proto_peer; + sock->s_flags = proto->proto_flags; sock->s_sock_ops = *proto->proto_sock_ops; sops = &sock->s_sock_ops; @@ -442,7 +418,7 @@ nni_sock_open(nni_sock **sockp, uint16_t pnum) sops->sock_open = nni_sock_nullop; } sock->s_pipe_ops = *proto->proto_pipe_ops; - pops = &sock->s_pipe_ops; + pops = &sock->s_pipe_ops; if (pops->pipe_start == NULL) { pops->pipe_start = nni_sock_nullstartpipe; } @@ -461,7 +437,6 @@ nni_sock_open(nni_sock **sockp, uint16_t pnum) return (0); } - // nni_sock_shutdown shuts down the socket; after this point no further // access to the socket will function, and any threads blocked in entry // points will be woken (and the functions they are blocked in will return @@ -470,8 +445,8 @@ int nni_sock_shutdown(nni_sock *sock) { nni_pipe *pipe; - nni_ep *ep; - nni_time linger; + nni_ep * ep; + nni_time linger; nni_mtx_lock(&sock->s_mx); if (sock->s_closing) { @@ -566,7 +541,6 @@ nni_sock_shutdown(nni_sock *sock) return (0); } - // nni_sock_ep_add adds a newly created endpoint to the socket. The // caller must hold references on the sock and the ep, and not be holding // the socket lock. The ep acquires a reference against the sock, @@ -586,7 +560,6 @@ nni_sock_ep_add(nni_sock *sock, nni_ep *ep) return (0); } - void nni_sock_ep_remove(nni_sock *sock, nni_ep *ep) { @@ -602,7 +575,6 @@ nni_sock_ep_remove(nni_sock *sock, nni_ep *ep) nni_mtx_unlock(&sock->s_mx); } - // nni_sock_close shuts down the socket, then releases any resources // associated with it. It is a programmer error to reference the socket // after this function is called, as the pointer may reference invalid @@ -637,7 +609,6 @@ nni_sock_close(nni_sock *sock) nni_objhash_unref_wait(nni_socks, sock->s_id); } - int nni_sock_sendmsg(nni_sock *sock, nni_msg *msg, nni_time expire) { @@ -682,11 +653,10 @@ nni_sock_sendmsg(nni_sock *sock, nni_msg *msg, nni_time expire) return (rv); } - int nni_sock_recvmsg(nni_sock *sock, nni_msg **msgp, nni_time expire) { - int rv; + int rv; nni_msg *msg; nni_mtx_lock(&sock->s_mx); @@ -722,7 +692,6 @@ nni_sock_recvmsg(nni_sock *sock, nni_msg **msgp, nni_time expire) return (0); } - // nni_sock_protocol returns the socket's 16-bit protocol number. uint16_t nni_sock_proto(nni_sock *sock) @@ -730,28 +699,24 @@ nni_sock_proto(nni_sock *sock) return (sock->s_protocol); } - uint16_t nni_sock_peer(nni_sock *sock) { return (sock->s_peer); } - nni_duration nni_sock_linger(nni_sock *sock) { return (sock->s_linger); } - size_t nni_sock_rcvmaxsz(nni_sock *sock) { return (sock->s_rcvmaxsz); } - void nni_sock_reconntimes(nni_sock *sock, nni_duration *rcur, nni_duration *rmax) { @@ -762,12 +727,11 @@ nni_sock_reconntimes(nni_sock *sock, nni_duration *rcur, nni_duration *rmax) nni_mtx_unlock(&sock->s_mx); } - int nni_sock_dial(nni_sock *sock, const char *addr, nni_ep **epp, int flags) { nni_ep *ep; - int rv; + int rv; if ((rv = nni_ep_create(&ep, sock, addr, NNI_EP_MODE_DIAL)) != 0) { return (rv); @@ -782,12 +746,11 @@ nni_sock_dial(nni_sock *sock, const char *addr, nni_ep **epp, int flags) return (rv); } - int nni_sock_listen(nni_sock *sock, const char *addr, nni_ep **epp, int flags) { nni_ep *ep; - int rv; + int rv; if ((rv = nni_ep_create(&ep, sock, addr, NNI_EP_MODE_LISTEN)) != 0) { return (rv); @@ -802,21 +765,18 @@ nni_sock_listen(nni_sock *sock, const char *addr, nni_ep **epp, int flags) return (rv); } - void nni_sock_recverr(nni_sock *sock, int err) { sock->s_recverr = err; } - void nni_sock_senderr(nni_sock *sock, int err) { sock->s_senderr = err; } - int nni_sock_setopt(nni_sock *sock, int opt, const void *val, size_t size) { @@ -855,15 +815,14 @@ nni_sock_setopt(nni_sock *sock, int opt, const void *val, size_t size) rv = nni_setopt_buf(sock->s_urq, val, size); break; case NNG_OPT_RCVMAXSZ: - rv = nni_setopt_size(&sock->s_rcvmaxsz, val, size, 0, - NNI_MAXSZ); + rv = nni_setopt_size( + &sock->s_rcvmaxsz, val, size, 0, NNI_MAXSZ); break; } nni_mtx_unlock(&sock->s_mx); return (rv); } - int nni_sock_getopt(nni_sock *sock, int opt, void *val, size_t *sizep) { @@ -905,12 +864,12 @@ nni_sock_getopt(nni_sock *sock, int opt, void *val, size_t *sizep) rv = nni_getopt_size(&sock->s_rcvmaxsz, val, sizep); break; case NNG_OPT_SNDFD: - rv = nni_getopt_fd(sock, &sock->s_send_fd, NNG_EV_CAN_SND, - val, sizep); + rv = nni_getopt_fd( + sock, &sock->s_send_fd, NNG_EV_CAN_SND, val, sizep); break; case NNG_OPT_RCVFD: - rv = nni_getopt_fd(sock, &sock->s_recv_fd, NNG_EV_CAN_RCV, - val, sizep); + rv = nni_getopt_fd( + sock, &sock->s_recv_fd, NNG_EV_CAN_RCV, val, sizep); break; } nni_mtx_unlock(&sock->s_mx); diff --git a/src/core/socket.h b/src/core/socket.h index 928264d9..c4619b42 100644 --- a/src/core/socket.h +++ b/src/core/socket.h @@ -10,82 +10,81 @@ #ifndef CORE_SOCKET_H #define CORE_SOCKET_H - // NB: This structure is supplied here for use by the CORE. Use of this library // OUSIDE of the core is STRICTLY VERBOTEN. NO DIRECT ACCESS BY PROTOCOLS OR // TRANSPORTS. struct nni_socket { - nni_mtx s_mx; - nni_cv s_cv; + nni_mtx s_mx; + nni_cv s_cv; - uint32_t s_id; + uint32_t s_id; - nni_msgq * s_uwq; // Upper write queue - nni_msgq * s_urq; // Upper read queue + nni_msgq *s_uwq; // Upper write queue + nni_msgq *s_urq; // Upper read queue - uint16_t s_protocol; - uint16_t s_peer; - uint32_t s_flags; + uint16_t s_protocol; + uint16_t s_peer; + uint32_t s_flags; - nni_proto_pipe_ops s_pipe_ops; - nni_proto_sock_ops s_sock_ops; + nni_proto_pipe_ops s_pipe_ops; + nni_proto_sock_ops s_sock_ops; - void * s_data; // Protocol private + void *s_data; // Protocol private // XXX: options - nni_duration s_linger; // linger time - nni_duration s_sndtimeo; // send timeout - nni_duration s_rcvtimeo; // receive timeout - nni_duration s_reconn; // reconnect time - nni_duration s_reconnmax; // max reconnect time + nni_duration s_linger; // linger time + nni_duration s_sndtimeo; // send timeout + nni_duration s_rcvtimeo; // receive timeout + nni_duration s_reconn; // reconnect time + nni_duration s_reconnmax; // max reconnect time - nni_list s_eps; // active endpoints - nni_list s_pipes; // active pipes + nni_list s_eps; // active endpoints + nni_list s_pipes; // active pipes - size_t s_rcvmaxsz; // maximum receive size + size_t s_rcvmaxsz; // maximum receive size - int s_ep_pend; // EP dial/listen in progress - int s_closing; // Socket is closing - int s_closed; // Socket closed - int s_besteffort; // Best effort mode delivery - int s_senderr; // Protocol state machine use - int s_recverr; // Protocol state machine use + int s_ep_pend; // EP dial/listen in progress + int s_closing; // Socket is closing + int s_closed; // Socket closed + int s_besteffort; // Best effort mode delivery + int s_senderr; // Protocol state machine use + int s_recverr; // Protocol state machine use - nni_event s_recv_ev; // Event for readability - nni_event s_send_ev; // Event for sendability + nni_event s_recv_ev; // Event for readability + nni_event s_send_ev; // Event for sendability - nni_notifyfd s_send_fd; - nni_notifyfd s_recv_fd; + nni_notifyfd s_send_fd; + nni_notifyfd s_recv_fd; - uint32_t s_nextid; // Next Pipe ID. + uint32_t s_nextid; // Next Pipe ID. }; -extern int nni_sock_sys_init(void); +extern int nni_sock_sys_init(void); extern void nni_sock_sys_fini(void); -extern int nni_sock_find(nni_sock **, uint32_t); -extern void nni_sock_hold(nni_sock *); -extern void nni_sock_rele(nni_sock *); -extern int nni_sock_open(nni_sock **, uint16_t); -extern void nni_sock_close(nni_sock *); -extern int nni_sock_shutdown(nni_sock *); +extern int nni_sock_find(nni_sock **, uint32_t); +extern void nni_sock_hold(nni_sock *); +extern void nni_sock_rele(nni_sock *); +extern int nni_sock_open(nni_sock **, uint16_t); +extern void nni_sock_close(nni_sock *); +extern int nni_sock_shutdown(nni_sock *); extern uint16_t nni_sock_proto(nni_sock *); extern uint16_t nni_sock_peer(nni_sock *); -extern int nni_sock_setopt(nni_sock *, int, const void *, size_t); -extern int nni_sock_getopt(nni_sock *, int, void *, size_t *); -extern int nni_sock_recvmsg(nni_sock *, nni_msg **, nni_time); -extern int nni_sock_sendmsg(nni_sock *, nni_msg *, nni_time); -extern int nni_sock_dial(nni_sock *, const char *, nni_ep **, int); -extern int nni_sock_listen(nni_sock *, const char *, nni_ep **, int); +extern int nni_sock_setopt(nni_sock *, int, const void *, size_t); +extern int nni_sock_getopt(nni_sock *, int, void *, size_t *); +extern int nni_sock_recvmsg(nni_sock *, nni_msg **, nni_time); +extern int nni_sock_sendmsg(nni_sock *, nni_msg *, nni_time); +extern int nni_sock_dial(nni_sock *, const char *, nni_ep **, int); +extern int nni_sock_listen(nni_sock *, const char *, nni_ep **, int); extern uint32_t nni_sock_id(nni_sock *); extern void nni_sock_lock(nni_sock *); extern void nni_sock_unlock(nni_sock *); extern nni_notify *nni_sock_notify(nni_sock *, int, nng_notify_func, void *); -extern void nni_sock_unnotify(nni_sock *, nni_notify *); +extern void nni_sock_unnotify(nni_sock *, nni_notify *); -extern int nni_sock_ep_add(nni_sock *, nni_ep *); +extern int nni_sock_ep_add(nni_sock *, nni_ep *); extern void nni_sock_ep_remove(nni_sock *, nni_ep *); extern void nni_sock_pipe_stop(nni_sock *, nni_pipe *); @@ -123,7 +122,7 @@ extern nni_msgq *nni_sock_recvq(nni_sock *); extern nni_mtx *nni_sock_mtx(nni_sock *); extern nni_duration nni_sock_linger(nni_sock *); -extern size_t nni_sock_rcvmaxsz(nni_sock *); +extern size_t nni_sock_rcvmaxsz(nni_sock *); extern void nni_sock_reconntimes(nni_sock *, nni_duration *, nni_duration *); -#endif // CORE_SOCKET_H +#endif // CORE_SOCKET_H diff --git a/src/core/taskq.c b/src/core/taskq.c index f4a0beee..e45819b5 100644 --- a/src/core/taskq.c +++ b/src/core/taskq.c @@ -9,20 +9,20 @@ #include "core/nng_impl.h" -typedef struct nni_taskq_thr nni_taskq_thr; +typedef struct nni_taskq_thr nni_taskq_thr; struct nni_taskq_thr { - nni_taskq * tqt_tq; - nni_thr tqt_thread; - nni_taskq_ent * tqt_running; - int tqt_wait; + nni_taskq * tqt_tq; + nni_thr tqt_thread; + nni_taskq_ent *tqt_running; + int tqt_wait; }; struct nni_taskq { - nni_list tq_ents; - nni_mtx tq_mtx; - nni_cv tq_cv; - nni_taskq_thr * tq_threads; - int tq_nthreads; - int tq_close; + nni_list tq_ents; + nni_mtx tq_mtx; + nni_cv tq_cv; + nni_taskq_thr *tq_threads; + int tq_nthreads; + int tq_close; }; static nni_taskq *nni_taskq_systq = NULL; @@ -31,14 +31,14 @@ static void nni_taskq_thread(void *self) { nni_taskq_thr *thr = self; - nni_taskq *tq = thr->tqt_tq; + nni_taskq * tq = thr->tqt_tq; nni_taskq_ent *ent; nni_mtx_lock(&tq->tq_mtx); for (;;) { if ((ent = nni_list_first(&tq->tq_ents)) != NULL) { nni_list_remove(&tq->tq_ents, ent); - ent->tqe_tq = NULL; + ent->tqe_tq = NULL; thr->tqt_running = ent; nni_mtx_unlock(&tq->tq_mtx); ent->tqe_cb(ent->tqe_arg); @@ -60,13 +60,12 @@ nni_taskq_thread(void *self) nni_mtx_unlock(&tq->tq_mtx); } - int nni_taskq_init(nni_taskq **tqp, int nthr) { - int rv; + int rv; nni_taskq *tq; - int i; + int i; if ((tq = NNI_ALLOC_STRUCT(tq)) == NULL) { return (NNG_ENOMEM); @@ -83,7 +82,7 @@ nni_taskq_init(nni_taskq **tqp, int nthr) tq->tq_close = 0; NNI_LIST_INIT(&tq->tq_ents, nni_taskq_ent, tqe_node); - tq->tq_threads = nni_alloc(sizeof (nni_taskq_thr) * nthr); + tq->tq_threads = nni_alloc(sizeof(nni_taskq_thr) * nthr); if (tq->tq_threads == NULL) { nni_cv_fini(&tq->tq_cv); nni_mtx_fini(&tq->tq_mtx); @@ -92,10 +91,10 @@ nni_taskq_init(nni_taskq **tqp, int nthr) } tq->tq_nthreads = nthr; for (i = 0; i < nthr; i++) { - tq->tq_threads[i].tqt_tq = tq; + tq->tq_threads[i].tqt_tq = tq; tq->tq_threads[i].tqt_running = NULL; rv = nni_thr_init(&tq->tq_threads[i].tqt_thread, - nni_taskq_thread, &tq->tq_threads[i]); + nni_taskq_thread, &tq->tq_threads[i]); if (rv != 0) { goto fail; } @@ -113,7 +112,6 @@ fail: return (rv); } - void nni_taskq_fini(nni_taskq *tq) { @@ -126,13 +124,12 @@ nni_taskq_fini(nni_taskq *tq) for (i = 0; i < tq->tq_nthreads; i++) { nni_thr_fini(&tq->tq_threads[i].tqt_thread); } - nni_free(tq->tq_threads, tq->tq_nthreads * sizeof (nni_taskq_thr)); + nni_free(tq->tq_threads, tq->tq_nthreads * sizeof(nni_taskq_thr)); nni_cv_fini(&tq->tq_cv); nni_mtx_fini(&tq->tq_mtx); NNI_FREE_STRUCT(tq); } - int nni_taskq_dispatch(nni_taskq *tq, nni_taskq_ent *ent) { @@ -155,7 +152,6 @@ nni_taskq_dispatch(nni_taskq *tq, nni_taskq_ent *ent) return (0); } - int nni_taskq_cancel(nni_taskq *tq, nni_taskq_ent *ent) { @@ -191,17 +187,15 @@ nni_taskq_cancel(nni_taskq *tq, nni_taskq_ent *ent) return (0); } - void nni_taskq_ent_init(nni_taskq_ent *ent, nni_cb cb, void *arg) { NNI_LIST_NODE_INIT(&ent->tqe_node); - ent->tqe_cb = cb; + ent->tqe_cb = cb; ent->tqe_arg = arg; - ent->tqe_tq = NULL; + ent->tqe_tq = NULL; } - int nni_taskq_sys_init(void) { @@ -212,7 +206,6 @@ nni_taskq_sys_init(void) return (rv); } - void nni_taskq_sys_fini(void) { diff --git a/src/core/taskq.h b/src/core/taskq.h index 7f717c3c..f1e29a34 100644 --- a/src/core/taskq.h +++ b/src/core/taskq.h @@ -13,24 +13,24 @@ #include "core/defs.h" #include "core/list.h" -typedef struct nni_taskq nni_taskq; -typedef struct nni_taskq_ent nni_taskq_ent; +typedef struct nni_taskq nni_taskq; +typedef struct nni_taskq_ent nni_taskq_ent; struct nni_taskq_ent { - nni_list_node tqe_node; - void * tqe_arg; - nni_cb tqe_cb; - nni_taskq * tqe_tq; + nni_list_node tqe_node; + void * tqe_arg; + nni_cb tqe_cb; + nni_taskq * tqe_tq; }; -extern int nni_taskq_init(nni_taskq **, int); +extern int nni_taskq_init(nni_taskq **, int); extern void nni_taskq_fini(nni_taskq *); -extern int nni_taskq_dispatch(nni_taskq *, nni_taskq_ent *); -extern int nni_taskq_cancel(nni_taskq *, nni_taskq_ent *); +extern int nni_taskq_dispatch(nni_taskq *, nni_taskq_ent *); +extern int nni_taskq_cancel(nni_taskq *, nni_taskq_ent *); extern void nni_taskq_ent_init(nni_taskq_ent *, nni_cb, void *); -extern int nni_taskq_sys_init(void); +extern int nni_taskq_sys_init(void); extern void nni_taskq_sys_fini(void); #endif // CORE_TASKQ_H diff --git a/src/core/thread.c b/src/core/thread.c index bccb4ff3..e603e32b 100644 --- a/src/core/thread.c +++ b/src/core/thread.c @@ -15,49 +15,42 @@ nni_mtx_init(nni_mtx *mtx) return (nni_plat_mtx_init(&mtx->mtx)); } - void nni_mtx_fini(nni_mtx *mtx) { nni_plat_mtx_fini(&mtx->mtx); } - void nni_mtx_lock(nni_mtx *mtx) { nni_plat_mtx_lock(&mtx->mtx); } - void nni_mtx_unlock(nni_mtx *mtx) { nni_plat_mtx_unlock(&mtx->mtx); } - int nni_cv_init(nni_cv *cv, nni_mtx *mtx) { return (nni_plat_cv_init(&cv->cv, &mtx->mtx)); } - void nni_cv_fini(nni_cv *cv) { nni_plat_cv_fini(&cv->cv); } - void nni_cv_wait(nni_cv *cv) { nni_plat_cv_wait(&cv->cv); } - int nni_cv_until(nni_cv *cv, nni_time until) { @@ -74,19 +67,17 @@ nni_cv_until(nni_cv *cv, nni_time until) return (nni_plat_cv_until(&cv->cv, until)); } - void nni_cv_wake(nni_cv *cv) { nni_plat_cv_wake(&cv->cv); } - static void nni_thr_wrap(void *arg) { nni_thr *thr = arg; - int start; + int start; nni_plat_mtx_lock(&thr->mtx); while (((start = thr->start) == 0) && (thr->stop == 0)) { @@ -102,17 +93,16 @@ nni_thr_wrap(void *arg) nni_plat_mtx_unlock(&thr->mtx); } - int nni_thr_init(nni_thr *thr, nni_thr_func fn, void *arg) { int rv; - thr->done = 0; + thr->done = 0; thr->start = 0; - thr->stop = 0; - thr->fn = fn; - thr->arg = arg; + thr->stop = 0; + thr->fn = fn; + thr->arg = arg; if ((rv = nni_plat_mtx_init(&thr->mtx)) != 0) { thr->done = 1; @@ -138,7 +128,6 @@ nni_thr_init(nni_thr *thr, nni_thr_func fn, void *arg) return (0); } - void nni_thr_run(nni_thr *thr) { @@ -148,7 +137,6 @@ nni_thr_run(nni_thr *thr) nni_plat_mtx_unlock(&thr->mtx); } - void nni_thr_wait(nni_thr *thr) { @@ -164,7 +152,6 @@ nni_thr_wait(nni_thr *thr) nni_plat_mtx_unlock(&thr->mtx); } - void nni_thr_fini(nni_thr *thr) { diff --git a/src/core/thread.h b/src/core/thread.h index b99c8ffb..ebd8db83 100644 --- a/src/core/thread.h +++ b/src/core/thread.h @@ -22,15 +22,15 @@ struct nni_cv { }; struct nni_thr { - nni_plat_thr thr; - nni_plat_mtx mtx; - nni_plat_cv cv; - nni_thr_func fn; - void * arg; - int start; - int stop; - int done; - int init; + nni_plat_thr thr; + nni_plat_mtx mtx; + nni_plat_cv cv; + nni_thr_func fn; + void * arg; + int start; + int stop; + int done; + int init; }; // nni_mtx_init initializes the mutex. (Win32 programmers take note; diff --git a/src/core/timer.c b/src/core/timer.c index b6be0b52..6058b067 100644 --- a/src/core/timer.c +++ b/src/core/timer.c @@ -16,27 +16,26 @@ static void nni_timer_loop(void *); // XXX: replace this timer list with a minHeap based priority queue. struct nni_timer { - nni_mtx t_mx; - nni_cv t_cv; - nni_list t_entries; - nni_thr t_thr; - int t_close; - int t_waiting; - nni_timer_node *t_active; // Must never ever be dereferenced! + nni_mtx t_mx; + nni_cv t_cv; + nni_list t_entries; + nni_thr t_thr; + int t_close; + int t_waiting; + nni_timer_node *t_active; // Must never ever be dereferenced! }; -typedef struct nni_timer nni_timer; +typedef struct nni_timer nni_timer; static nni_timer nni_global_timer; - int nni_timer_sys_init(void) { - int rv; + int rv; nni_timer *timer = &nni_global_timer; - memset(timer, 0, sizeof (*timer)); + memset(timer, 0, sizeof(*timer)); NNI_LIST_INIT(&timer->t_entries, nni_timer_node, t_node); timer->t_close = 0; @@ -50,7 +49,6 @@ nni_timer_sys_init(void) return (0); } - void nni_timer_sys_fini(void) { @@ -66,22 +64,19 @@ nni_timer_sys_fini(void) nni_mtx_fini(&timer->t_mx); } - void nni_timer_init(nni_timer_node *node, nni_cb cb, void *arg) { - node->t_cb = cb; + node->t_cb = cb; node->t_arg = arg; } - void nni_timer_fini(nni_timer_node *node) { NNI_ARG_UNUSED(node); } - void nni_timer_cancel(nni_timer_node *node) { @@ -98,13 +93,12 @@ nni_timer_cancel(nni_timer_node *node) nni_mtx_unlock(&timer->t_mx); } - void nni_timer_schedule(nni_timer_node *node, nni_time when) { - nni_timer *timer = &nni_global_timer; + nni_timer * timer = &nni_global_timer; nni_timer_node *srch; - int wake = 1; + int wake = 1; nni_mtx_lock(&timer->t_mx); node->t_expire = when; @@ -129,12 +123,11 @@ nni_timer_schedule(nni_timer_node *node, nni_time when) nni_mtx_unlock(&timer->t_mx); } - static void nni_timer_loop(void *arg) { - nni_timer *timer = arg; - nni_time now; + nni_timer * timer = arg; + nni_time now; nni_timer_node *node; for (;;) { diff --git a/src/core/timer.h b/src/core/timer.h index 09883e0b..a8108d5f 100644 --- a/src/core/timer.h +++ b/src/core/timer.h @@ -16,19 +16,19 @@ // For the sake of simplicity, we just maintain a single global timer thread. struct nni_timer_node { - nni_time t_expire; - nni_cb t_cb; - void * t_arg; - nni_list_node t_node; + nni_time t_expire; + nni_cb t_cb; + void * t_arg; + nni_list_node t_node; }; -typedef struct nni_timer_node nni_timer_node; +typedef struct nni_timer_node nni_timer_node; extern void nni_timer_init(nni_timer_node *, nni_cb, void *); extern void nni_timer_fini(nni_timer_node *); extern void nni_timer_schedule(nni_timer_node *, nni_time); extern void nni_timer_cancel(nni_timer_node *); -extern int nni_timer_sys_init(void); +extern int nni_timer_sys_init(void); extern void nni_timer_sys_fini(void); #endif // CORE_TIMER_H diff --git a/src/core/transport.c b/src/core/transport.c index ce61f722..61b1a0c3 100644 --- a/src/core/transport.c +++ b/src/core/transport.c @@ -18,21 +18,22 @@ extern nni_tran nni_tcp_tran; extern nni_tran nni_ipc_tran; static nni_tran *transports[] = { + // clang-format off &nni_inproc_tran, &nni_tcp_tran, &nni_ipc_tran, NULL + // clang-format on }; - nni_tran * nni_tran_find(const char *addr) { // address is of the form "<scheme>://blah..." const char *end; - int len; - int i; - nni_tran *tran; + int len; + int i; + nni_tran * tran; if ((end = strstr(addr, "://")) == NULL) { return (NULL); @@ -47,13 +48,12 @@ nni_tran_find(const char *addr) return (NULL); } - // nni_tran_sys_init initializes the entire transport subsystem, including // each individual transport. void nni_tran_sys_init(void) { - int i; + int i; nni_tran *tran; for (i = 0; (tran = transports[i]) != NULL; i++) { @@ -61,13 +61,12 @@ nni_tran_sys_init(void) } } - // nni_tran_sys_fini finalizes the entire transport system, including all // transports. void nni_tran_sys_fini(void) { - int i; + int i; nni_tran *tran; for (i = 0; (tran = transports[i]) != NULL; i++) { diff --git a/src/core/transport.h b/src/core/transport.h index 50da473d..72f4cc61 100644 --- a/src/core/transport.h +++ b/src/core/transport.h @@ -15,24 +15,23 @@ struct nni_tran { // tran_scheme is the transport scheme, such as "tcp" or "inproc". - const char * tran_scheme; + const char *tran_scheme; // tran_ep links our endpoint-specific operations. - const nni_tran_ep * tran_ep; + const nni_tran_ep *tran_ep; // tran_pipe links our pipe-specific operations. - const nni_tran_pipe * tran_pipe; + const nni_tran_pipe *tran_pipe; // tran_init, if not NULL, is called once during library // initialization. - int (*tran_init)(void); + int (*tran_init)(void); // tran_fini, if not NULL, is called during library deinitialization. // It should release any global resources, close any open files, etc. - void (*tran_fini)(void); + void (*tran_fini)(void); }; - // Endpoint operations are called by the socket in a protocol-independent // fashion. The socket makes individual calls, which are expected to block // if appropriate (except for destroy). Endpoints are unable to call back @@ -40,36 +39,36 @@ struct nni_tran { struct nni_tran_ep { // ep_init creates a vanilla endpoint. The value created is // used for the first argument for all other endpoint functions. - int (*ep_init)(void **, const char *, nni_sock *, int); + int (*ep_init)(void **, const char *, nni_sock *, int); // ep_fini frees the resources associated with the endpoint. // The endpoint will already have been closed. - void (*ep_fini)(void *); + void (*ep_fini)(void *); // ep_connect establishes a connection. It can return errors // NNG_EACCESS, NNG_ECONNREFUSED, NNG_EBADADDR, NNG_ECONNFAILED, // NNG_ETIMEDOUT, and NNG_EPROTO. - void (*ep_connect)(void *, nni_aio *); + void (*ep_connect)(void *, nni_aio *); // ep_bind just does the bind() and listen() work, // reserving the address but not creating any connections. // It should return NNG_EADDRINUSE if the address is already // taken. It can also return NNG_EBADADDR for an unsuitable // address, or NNG_EACCESS for permission problems. - int (*ep_bind)(void *); + int (*ep_bind)(void *); // ep_accept accepts an inbound connection. - void (*ep_accept)(void *, nni_aio *); + void (*ep_accept)(void *, nni_aio *); // ep_close stops the endpoint from operating altogether. It does // not affect pipes that have already been created. - void (*ep_close)(void *); + void (*ep_close)(void *); // ep_setopt sets an endpoint (transport-specific) option. - int (*ep_setopt)(void *, int, const void *, size_t); + int (*ep_setopt)(void *, int, const void *, size_t); // ep_getopt gets an endpoint (transport-specific) option. - int (*ep_getopt)(void *, int, void *, size_t *); + int (*ep_getopt)(void *, int, void *, size_t *); }; // Pipe operations are entry points called by the socket. These may be called @@ -81,14 +80,14 @@ struct nni_tran_pipe { // resources, including closing files and freeing memory, used by // the pipe. After this call returns, the system will not make // further calls on the same pipe. - void (*p_fini)(void *); + void (*p_fini)(void *); // p_start starts the pipe running. This gives the transport a // chance to hook into any transport specific negotiation phase. // The pipe will not have its p_send or p_recv calls started, and // will not be access by the "socket" until the pipe has indicated // its readiness by finishing the aio. - void (*p_start)(void *, nni_aio *); + void (*p_start)(void *, nni_aio *); // p_aio_send queues the message for transmit. If this fails, then // the caller may try again with the same message (or free it). If @@ -96,30 +95,30 @@ struct nni_tran_pipe { // message, and the caller may not use it again. The transport will // have the responsibility to free the message (nng_msg_free()) when // it is finished with it. - void (*p_send)(void *, nni_aio *); + void (*p_send)(void *, nni_aio *); // p_recv schedules a message receive. This will be performed even for // cases where no data is expected, to allow detection of a remote // disconnect. - void (*p_recv)(void *, nni_aio *); + void (*p_recv)(void *, nni_aio *); // p_close closes the pipe. Further recv or send operations should // return back NNG_ECLOSED. - void (*p_close)(void *); + void (*p_close)(void *); // p_peer returns the peer protocol. This may arrive in whatever // transport specific manner is appropriate. - uint16_t (*p_peer)(void *); + uint16_t (*p_peer)(void *); // p_getopt gets an pipe (transport-specific) property. These values // may not be changed once the pipe is created. - int (*p_getopt)(void *, int, void *, size_t *); + int (*p_getopt)(void *, int, void *, size_t *); }; // These APIs are used by the framework internally, and not for use by // transport implementations. extern nni_tran *nni_tran_find(const char *); -extern void nni_tran_sys_init(void); -extern void nni_tran_sys_fini(void); +extern void nni_tran_sys_init(void); +extern void nni_tran_sys_fini(void); #endif // CORE_TRANSPORT_H |
