aboutsummaryrefslogtreecommitdiff
path: root/src/nng.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2024-11-09 23:45:21 -0800
committerGarrett D'Amore <garrett@damore.org>2024-11-11 11:03:12 -0800
commit713b80f440cb414cd0b856bde0ea1b31f939777f (patch)
tree1186c42418559c85719023bde3e919aa2df7fcef /src/nng.c
parentcbe9a27ef7485977fbc7c713376b096b6723da3d (diff)
downloadnng-713b80f440cb414cd0b856bde0ea1b31f939777f.tar.gz
nng-713b80f440cb414cd0b856bde0ea1b31f939777f.tar.bz2
nng-713b80f440cb414cd0b856bde0ea1b31f939777f.zip
refactor initialization/finalization
Applications must now call nng_init(), but they can supply a set of parameters optionally. The code is now safe for multiple libraries to do this concurrently, meaning nng_fini no longer can race against another instance starting up. The nni_init checks on all public APIs are removed now.
Diffstat (limited to 'src/nng.c')
-rw-r--r--src/nng.c96
1 files changed, 16 insertions, 80 deletions
diff --git a/src/nng.c b/src/nng.c
index dc8be731..f8a4d13f 100644
--- a/src/nng.c
+++ b/src/nng.c
@@ -25,13 +25,6 @@
#include <stdio.h>
#include <string.h>
-void
-nng_fini(void)
-{
- nni_sock_closeall();
- nni_fini();
-}
-
int
nng_close(nng_socket s)
{
@@ -410,8 +403,7 @@ ctx_get(nng_ctx id, const char *n, void *v, size_t *szp, nni_type t)
nni_ctx *ctx;
int rv;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_ctx_find(&ctx, id.id, false)) != 0)) {
+ if ((rv = nni_ctx_find(&ctx, id.id, false)) != 0) {
return (rv);
}
rv = nni_ctx_getopt(ctx, n, v, szp, t);
@@ -467,8 +459,7 @@ ctx_set(nng_ctx id, const char *n, const void *v, size_t sz, nni_type t)
nni_ctx *ctx;
int rv;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_ctx_find(&ctx, id.id, false)) != 0)) {
+ if ((rv = nni_ctx_find(&ctx, id.id, false)) != 0) {
return (rv);
}
rv = nni_ctx_setopt(ctx, n, v, sz, t);
@@ -662,9 +653,6 @@ dialer_set(nng_dialer id, const char *n, const void *v, size_t sz, nni_type t)
nni_dialer *d;
int rv;
- if ((rv = nni_init()) != 0) {
- return (rv);
- }
if ((rv = nni_dialer_find(&d, id.id)) != 0) {
return (rv);
}
@@ -728,9 +716,6 @@ dialer_get(nng_dialer id, const char *n, void *v, size_t *szp, nni_type t)
nni_dialer *d;
int rv;
- if ((rv = nni_init()) != 0) {
- return (rv);
- }
if ((rv = nni_dialer_find(&d, id.id)) != 0) {
return (rv);
}
@@ -792,8 +777,7 @@ nng_dialer_get_tls(nng_dialer id, nng_tls_config **cfgp)
{
int rv;
nni_dialer *d;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_dialer_find(&d, id.id)) != 0)) {
+ if ((rv = nni_dialer_find(&d, id.id)) != 0) {
return (rv);
}
rv = nni_dialer_get_tls(d, cfgp);
@@ -806,8 +790,7 @@ nng_dialer_set_tls(nng_dialer id, nng_tls_config *cfg)
{
int rv;
nni_dialer *d;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_dialer_find(&d, id.id)) != 0)) {
+ if ((rv = nni_dialer_find(&d, id.id)) != 0) {
return (rv);
}
rv = nni_dialer_set_tls(d, cfg);
@@ -822,9 +805,6 @@ listener_set(
nni_listener *l;
int rv;
- if ((rv = nni_init()) != 0) {
- return (rv);
- }
if ((rv = nni_listener_find(&l, lid.id)) != 0) {
return (rv);
}
@@ -889,9 +869,6 @@ listener_get(
nni_listener *l;
int rv;
- if ((rv = nni_init()) != 0) {
- return (rv);
- }
if ((rv = nni_listener_find(&l, lid.id)) != 0) {
return (rv);
}
@@ -953,8 +930,7 @@ nng_listener_get_tls(nng_listener id, nng_tls_config **cfgp)
{
int rv;
nni_listener *l;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_listener_find(&l, id.id)) != 0)) {
+ if ((rv = nni_listener_find(&l, id.id)) != 0) {
return (rv);
}
rv = nni_listener_get_tls(l, cfgp);
@@ -967,8 +943,7 @@ nng_listener_set_tls(nng_listener id, nng_tls_config *cfg)
{
int rv;
nni_listener *l;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_listener_find(&l, id.id)) != 0)) {
+ if ((rv = nni_listener_find(&l, id.id)) != 0) {
return (rv);
}
rv = nni_listener_set_tls(l, cfg);
@@ -1008,9 +983,6 @@ socket_set(
nni_sock *sock;
int rv;
- if ((rv = nni_init()) != 0) {
- return (rv);
- }
if ((rv = nni_sock_find(&sock, s.id)) != 0) {
return (rv);
}
@@ -1068,9 +1040,6 @@ socket_get(nng_socket s, const char *name, void *val, size_t *szp, nni_type t)
nni_sock *sock;
int rv;
- if ((rv = nni_init()) != 0) {
- return (rv);
- }
if ((rv = nni_sock_find(&sock, s.id)) != 0) {
return (rv);
}
@@ -1127,8 +1096,7 @@ nng_socket_get_recv_poll_fd(nng_socket id, int *fdp)
int rv;
nni_sock *sock;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_sock_find(&sock, id.id)) != 0)) {
+ if ((rv = nni_sock_find(&sock, id.id)) != 0) {
return (rv);
}
@@ -1143,8 +1111,7 @@ nng_socket_get_send_poll_fd(nng_socket id, int *fdp)
int rv;
nni_sock *sock;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_sock_find(&sock, id.id)) != 0)) {
+ if ((rv = nni_sock_find(&sock, id.id)) != 0) {
return (rv);
}
@@ -1159,8 +1126,7 @@ nng_socket_proto_id(nng_socket id, uint16_t *idp)
int rv;
nni_sock *sock;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_sock_find(&sock, id.id)) != 0)) {
+ if ((rv = nni_sock_find(&sock, id.id)) != 0) {
return (rv);
}
@@ -1175,8 +1141,7 @@ nng_socket_peer_id(nng_socket id, uint16_t *idp)
int rv;
nni_sock *sock;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_sock_find(&sock, id.id)) != 0)) {
+ if ((rv = nni_sock_find(&sock, id.id)) != 0) {
return (rv);
}
@@ -1191,8 +1156,7 @@ nng_socket_proto_name(nng_socket id, const char **name)
int rv;
nni_sock *sock;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_sock_find(&sock, id.id)) != 0)) {
+ if ((rv = nni_sock_find(&sock, id.id)) != 0) {
return (rv);
}
@@ -1207,8 +1171,7 @@ nng_socket_peer_name(nng_socket id, const char **name)
int rv;
nni_sock *sock;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_sock_find(&sock, id.id)) != 0)) {
+ if ((rv = nni_sock_find(&sock, id.id)) != 0) {
return (rv);
}
@@ -1223,8 +1186,7 @@ nng_socket_raw(nng_socket id, bool *rawp)
int rv;
nni_sock *sock;
- if (((rv = nni_init()) != 0) ||
- ((rv = nni_sock_find(&sock, id.id)) != 0)) {
+ if ((rv = nni_sock_find(&sock, id.id)) != 0) {
return (rv);
}
*rawp = nni_sock_raw(sock);
@@ -1238,9 +1200,6 @@ nng_pipe_notify(nng_socket s, nng_pipe_ev ev, nng_pipe_cb cb, void *arg)
int rv;
nni_sock *sock;
- if ((rv = nni_init()) != 0) {
- return (rv);
- }
if ((rv = nni_sock_find(&sock, s.id)) != 0) {
return (rv);
}
@@ -1289,9 +1248,6 @@ nng_device(nng_socket s1, nng_socket s2)
{
nni_aio aio;
int rv;
- if ((rv = nni_init()) != 0) {
- return (rv);
- }
nni_aio_init(&aio, NULL, NULL);
nng_device_aio(&aio, s1, s2);
nni_aio_wait(&aio);
@@ -1375,9 +1331,6 @@ pipe_get(nng_pipe p, const char *name, void *val, size_t *szp, nni_type t)
int rv;
nni_pipe *pipe;
- if ((rv = nni_init()) < 0) {
- return (rv);
- }
if ((rv = nni_pipe_find(&pipe, p.id)) != 0) {
return (rv);
}
@@ -1440,7 +1393,7 @@ nng_pipe_socket(nng_pipe p)
nng_socket s = NNG_SOCKET_INITIALIZER;
nni_pipe *pipe;
- if ((nni_init() == 0) && (nni_pipe_find(&pipe, p.id) == 0)) {
+ if (nni_pipe_find(&pipe, p.id) == 0) {
s.id = nni_pipe_sock_id(pipe);
nni_pipe_rele(pipe);
}
@@ -1452,7 +1405,7 @@ nng_pipe_dialer(nng_pipe p)
{
nng_dialer d = NNG_DIALER_INITIALIZER;
nni_pipe *pipe;
- if ((nni_init() == 0) && (nni_pipe_find(&pipe, p.id) == 0)) {
+ if (nni_pipe_find(&pipe, p.id) == 0) {
d.id = nni_pipe_dialer_id(pipe);
nni_pipe_rele(pipe);
}
@@ -1464,7 +1417,7 @@ nng_pipe_listener(nng_pipe p)
{
nng_listener l = NNG_LISTENER_INITIALIZER;
nni_pipe *pipe;
- if ((nni_init() == 0) && (nni_pipe_find(&pipe, p.id) == 0)) {
+ if (nni_pipe_find(&pipe, p.id) == 0) {
l.id = nni_pipe_listener_id(pipe);
nni_pipe_rele(pipe);
}
@@ -1917,9 +1870,6 @@ nng_aio_alloc(nng_aio **app, void (*cb)(void *), void *arg)
nng_aio *aio;
int rv;
- if ((rv = nni_init()) != 0) {
- return (rv);
- }
if ((rv = nni_aio_alloc(&aio, (nni_cb) cb, arg)) == 0) {
nng_aio_set_timeout(aio, NNG_DURATION_DEFAULT);
*app = aio;
@@ -2098,16 +2048,9 @@ nng_version(void)
NNG_PATCH_VERSION) NNG_RELEASE_SUFFIX);
}
-void
-nng_init_set_parameter(nng_init_parameter p, uint64_t value)
-{
- nni_init_set_param(p, value);
-}
-
nng_time
nng_clock(void)
{
- (void) nni_init();
return (nni_clock());
}
@@ -2115,7 +2058,6 @@ nng_clock(void)
void
nng_msleep(nng_duration dur)
{
- (void) nni_init();
nni_msleep(dur);
}
@@ -2129,8 +2071,6 @@ nng_thread_create(nng_thread **thrp, void (*func)(void *), void *arg)
nni_thr *thr;
int rv;
- (void) nni_init();
-
if ((thr = NNI_ALLOC_STRUCT(thr)) == NULL) {
return (NNG_ENOMEM);
}
@@ -2167,8 +2107,6 @@ nng_mtx_alloc(nng_mtx **mpp)
{
nng_mtx *mp;
- (void) nni_init();
-
if ((mp = NNI_ALLOC_STRUCT(mp)) == NULL) {
return (NNG_ENOMEM);
}
@@ -2251,7 +2189,6 @@ nng_cv_wake1(nng_cv *cv)
uint32_t
nng_random(void)
{
- (void) nni_init();
return (nni_random());
}
@@ -2264,7 +2201,6 @@ nng_socket_pair(int fds[2])
int
nng_udp_open(nng_udp **udp, nng_sockaddr *sa)
{
- (void) nni_init();
return (nni_plat_udp_open((nni_plat_udp **) udp, sa));
}