Skip to content

Commit

Permalink
platform: remove reader/writer locks
Browse files Browse the repository at this point in the history
The only thing using this was the transport lookups, but as
those transports are now fully initialized in nng_init, we
no longer need to lock that at all.
  • Loading branch information
gdamore committed Jan 5, 2025
1 parent cf7dda7 commit e5d5b62
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 124 deletions.
17 changes: 3 additions & 14 deletions src/core/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ extern void *nni_zalloc(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_rwlock nni_plat_rwlock;
typedef struct nni_plat_cv nni_plat_cv;
typedef struct nni_plat_thr nni_plat_thr;
typedef struct nni_rwlock nni_rwlock;
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
Expand All @@ -111,15 +109,6 @@ extern void nni_plat_mtx_lock(nni_plat_mtx *);
// thread that owned the mutex.
extern void nni_plat_mtx_unlock(nni_plat_mtx *);

// read/write locks - these work like mutexes except that multiple readers
// can acquire the lock. These are not safe for recursive use, and it is
// unspecified whether any measures are provided to prevent starvation.
extern void nni_rwlock_init(nni_rwlock *);
extern void nni_rwlock_fini(nni_rwlock *);
extern void nni_rwlock_rdlock(nni_rwlock *);
extern void nni_rwlock_wrlock(nni_rwlock *);
extern void nni_rwlock_unlock(nni_rwlock *);

// nni_plat_cv_init initializes a condition variable. We require a mutex be
// supplied with it, and that mutex must always be held when performing any
// operations on the condition variable (other than fini.) As with mutexes, an
Expand Down
9 changes: 0 additions & 9 deletions src/platform/posix/posix_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,6 @@ struct nni_plat_mtx {
PTHREAD_MUTEX_INITIALIZER \
}

struct nni_rwlock {
pthread_rwlock_t rwl;
};

#define NNI_RWLOCK_INITIALIZER \
{ \
PTHREAD_RWLOCK_INITIALIZER \
}

// No static form of CV initialization because of the need to use
// attributes to set the clock type.
struct nni_plat_cv {
Expand Down
47 changes: 0 additions & 47 deletions src/platform/posix/posix_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,53 +155,6 @@ nni_plat_mtx_unlock(nni_plat_mtx *mtx)
nni_pthread_mutex_unlock(&mtx->mtx);
}

void
nni_rwlock_init(nni_rwlock *rwl)
{
while (pthread_rwlock_init(&rwl->rwl, NULL) != 0) {
// We must have memory exhaustion -- ENOMEM, or
// in some cases EAGAIN. Wait a bit before we try to
// give things a chance to settle down.
nni_msleep(10);
}
}

void
nni_rwlock_fini(nni_rwlock *rwl)
{
int rv;
if ((rv = pthread_rwlock_destroy(&rwl->rwl)) != 0) {
nni_panic("pthread_rwlock_destroy: %s", strerror(rv));
}
}

void
nni_rwlock_rdlock(nni_rwlock *rwl)
{
int rv;
if ((rv = pthread_rwlock_rdlock(&rwl->rwl)) != 0) {
nni_panic("pthread_rwlock_rdlock: %s", strerror(rv));
}
}

void
nni_rwlock_wrlock(nni_rwlock *rwl)
{
int rv;
if ((rv = pthread_rwlock_wrlock(&rwl->rwl)) != 0) {
nni_panic("pthread_rwlock_wrlock: %s", strerror(rv));
}
}

void
nni_rwlock_unlock(nni_rwlock *rwl)
{
int rv;
if ((rv = pthread_rwlock_unlock(&rwl->rwl)) != 0) {
nni_panic("pthread_rwlock_unlock: %s", strerror(rv));
}
}

void
nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx)
{
Expand Down
10 changes: 0 additions & 10 deletions src/platform/windows/win_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ struct nni_plat_mtx {
SRWLOCK_INIT \
}

struct nni_rwlock {
SRWLOCK rwl;
BOOLEAN exclusive;
};

#define NNI_RWLOCK_INITIALIZER \
{ \
SRWLOCK_INIT \
}

struct nni_plat_cv {
CONDITION_VARIABLE cv;
PSRWLOCK srl;
Expand Down
38 changes: 1 addition & 37 deletions src/platform/windows/win_thread.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 Down Expand Up @@ -78,42 +78,6 @@ nni_plat_mtx_unlock(nni_plat_mtx *mtx)
ReleaseSRWLockExclusive(&mtx->srl);
}

void
nni_rwlock_init(nni_rwlock *rwl)
{
InitializeSRWLock(&rwl->rwl);
}

void
nni_rwlock_fini(nni_rwlock *rwl)
{
rwl->exclusive = FALSE;
}

void
nni_rwlock_rdlock(nni_rwlock *rwl)
{
AcquireSRWLockShared(&rwl->rwl);
}

void
nni_rwlock_wrlock(nni_rwlock *rwl)
{
AcquireSRWLockExclusive(&rwl->rwl);
rwl->exclusive = TRUE;
}

void
nni_rwlock_unlock(nni_rwlock *rwl)
{
if (rwl->exclusive) {
rwl->exclusive = FALSE;
ReleaseSRWLockExclusive(&rwl->rwl);
} else {
ReleaseSRWLockShared(&rwl->rwl);
}
}

void
nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx)
{
Expand Down
8 changes: 1 addition & 7 deletions src/sp/transport.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]>
// Copyright 2019 Devolutions <[email protected]>
//
Expand All @@ -16,7 +16,6 @@

static nni_list sp_tran_list =
NNI_LIST_INITIALIZER(sp_tran_list, nni_sp_tran, tran_link);
static nni_rwlock sp_tran_lk = NNI_RWLOCK_INITIALIZER;

void
nni_sp_tran_register(nni_sp_tran *tran)
Expand Down Expand Up @@ -55,14 +54,12 @@ nni_sp_tran_register(nni_sp_tran *tran)
}
#endif

nni_rwlock_wrlock(&sp_tran_lk);
if (!nni_list_node_active(&tran->tran_link)) {
tran->tran_init();
nni_list_append(&sp_tran_list, tran);
nng_log_info(
"NNG-TRAN", "Registered transport: %s", tran->tran_scheme);
}
nni_rwlock_unlock(&sp_tran_lk);
}

nni_sp_tran *
Expand All @@ -71,16 +68,13 @@ nni_sp_tran_find(const char *url)
// address is of the form "<scheme>://blah..."
nni_sp_tran *t;

nni_rwlock_rdlock(&sp_tran_lk);
NNI_LIST_FOREACH (&sp_tran_list, t) {
size_t len = strlen(t->tran_scheme);
if ((strncmp(url, t->tran_scheme, len) == 0) &&
(url[len] == ':' || url[len] == '\0')) {
nni_rwlock_unlock(&sp_tran_lk);
return (t);
}
}
nni_rwlock_unlock(&sp_tran_lk);
return (NULL);
}

Expand Down

0 comments on commit e5d5b62

Please sign in to comment.