Skip to content

Commit

Permalink
platform: eliminate NNI_CV_INITIALIZER altogether
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Jan 5, 2025
1 parent a300db4 commit ebd3628
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
8 changes: 0 additions & 8 deletions src/platform/posix/posix_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ struct nni_plat_cv {
pthread_mutex_t *mtx;
};

// NOTE: condition variables initialized with this should *NOT*
// be used with nni_cv_until -- the clock attributes are not passed
// and the wake-up times will not be correct.
#define NNI_CV_INITIALIZER(mxp) \
{ \
.mtx = &((mxp)->mtx), .cv = PTHREAD_COND_INITIALIZER \
}

struct nni_plat_thr {
pthread_t tid;
void (*func)(void *);
Expand Down
16 changes: 10 additions & 6 deletions src/platform/posix/posix_resolv_gai.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2024 Staysail Systems, Inc. <[email protected]>
// Copyright 2025 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This software is supplied under the terms of the MIT License, a
Expand All @@ -8,9 +8,9 @@
// found online at https://opensource.org/licenses/MIT.
//

#include "core/defs.h"
#include "core/init.h"
#include "core/nng_impl.h"
#include "nng/nng.h"

#ifdef NNG_USE_POSIX_RESOLV_GAI

Expand Down Expand Up @@ -45,13 +45,13 @@
#endif
#endif

static nni_mtx resolv_mtx = NNI_MTX_INITIALIZER;
static nni_cv resolv_cv = NNI_CV_INITIALIZER(&resolv_mtx);
static bool resolv_fini = false;
static nni_list resolv_aios;
static nni_thr *resolv_thrs;
static nni_aio **resolv_active;
static int16_t resolv_num_thr;
static nni_mtx resolv_mtx;
static nni_cv resolv_cv;

static void
resolv_cancel(nni_aio *aio, void *arg, int rv)
Expand Down Expand Up @@ -434,13 +434,17 @@ nni_posix_resolv_sysfini(void)
NNI_FREE_STRUCTS(resolv_thrs, resolv_num_thr);
}
if (resolv_active != NULL) {
NNI_FREE_STRUCTS(resolv_active, resolv_num_thr);
nni_free(resolv_active, sizeof(nni_aio *) * resolv_num_thr);
}
nni_cv_fini(&resolv_cv);
nni_mtx_fini(&resolv_mtx);
}

int
nni_posix_resolv_sysinit(nng_init_params *params)
{
nni_mtx_init(&resolv_mtx);
nni_cv_init(&resolv_cv, &resolv_mtx);
resolv_fini = false;
nni_aio_list_init(&resolv_aios);

Expand All @@ -451,7 +455,7 @@ nni_posix_resolv_sysinit(nng_init_params *params)
params->num_resolver_threads = resolv_num_thr;
// no limit on the maximum for now
resolv_thrs = NNI_ALLOC_STRUCTS(resolv_thrs, resolv_num_thr);
resolv_active = NNI_ALLOC_STRUCTS(resolv_active, resolv_num_thr);
resolv_active = nni_zalloc(sizeof(nni_aio *) * resolv_num_thr);
if (resolv_thrs == NULL || resolv_active == NULL) {
nni_posix_resolv_sysfini();
return (NNG_ENOMEM);
Expand Down
5 changes: 0 additions & 5 deletions src/platform/windows/win_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ struct nni_plat_cv {
PSRWLOCK srl;
};

#define NNI_CV_INITIALIZER(mxp) \
{ \
.srl = (void *) mxp, .cv = CONDITION_VARIABLE_INIT \
}

struct nni_atomic_flag {
LONG f;
};
Expand Down
14 changes: 9 additions & 5 deletions src/platform/windows/win_resolv.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2024 Staysail Systems, Inc. <[email protected]>
// Copyright 2025 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This software is supplied under the terms of the MIT License, a
Expand All @@ -22,13 +22,13 @@
// host file, WINS, or other naming services. As a result, we just build
// our own limited asynchronous resolver with threads.

static nni_mtx resolv_mtx = NNI_MTX_INITIALIZER;
static nni_cv resolv_cv = NNI_CV_INITIALIZER(&resolv_mtx);
static bool resolv_fini = false;
static nni_list resolv_aios;
static nni_thr *resolv_thrs;
static nni_aio **resolv_active;
static int16_t resolv_num_thr;
static nni_mtx resolv_mtx;
static nni_cv resolv_cv;

static void
resolv_cancel(nni_aio *aio, void *arg, int rv)
Expand Down Expand Up @@ -383,6 +383,8 @@ nni_get_port_by_name(const char *name, uint32_t *portp)
int
nni_win_resolv_sysinit(nng_init_params *params)
{
nni_mtx_init(&resolv_mtx);
nni_cv_init(&resolv_cv, &resolv_mtx);
nni_aio_list_init(&resolv_aios);
resolv_fini = false;

Expand All @@ -394,7 +396,7 @@ nni_win_resolv_sysinit(nng_init_params *params)

// no limit on the maximum for now
resolv_thrs = NNI_ALLOC_STRUCTS(resolv_thrs, resolv_num_thr);
resolv_active = NNI_ALLOC_STRUCTS(resolv_active, resolv_num_thr);
resolv_active = nni_zalloc(sizeof(nni_aio *) * resolv_num_thr);
if (resolv_thrs == NULL || resolv_active == NULL) {
nni_win_resolv_sysfini();
return (NNG_ENOMEM);
Expand Down Expand Up @@ -428,8 +430,10 @@ nni_win_resolv_sysfini(void)
NNI_FREE_STRUCTS(resolv_thrs, resolv_num_thr);
}
if (resolv_active != NULL) {
NNI_FREE_STRUCTS(resolv_active, resolv_num_thr);
nni_free(resolv_active, sizeof(nni_aio *) * resolv_num_thread);
}
nni_cv_fini(&resolv_cv);
nni_mtx_fini(&resolv_mtx);
}

#endif // NNG_PLATFORM_WINDOWS

0 comments on commit ebd3628

Please sign in to comment.