forked from nanomsg/nng
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes nanomsg#1409 reader/writer lock desired
This provides the initial implementation, and converts the transport lookup routines to use it. This is probably of limited performance benefit, but rwlock's may be useful in further future work.
- Loading branch information
Showing
6 changed files
with
145 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Copyright 2020 Staysail Systems, Inc. <[email protected]> | ||
// Copyright 2021 Staysail Systems, Inc. <[email protected]> | ||
// Copyright 2018 Capitar IT Group BV <[email protected]> | ||
// Copyright 2018 Devolutions <[email protected]> | ||
// | ||
|
@@ -88,9 +88,11 @@ 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_cv nni_plat_cv; | ||
typedef struct nni_plat_thr nni_plat_thr; | ||
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; | ||
|
||
// | ||
// Threading & Synchronization Support | ||
|
@@ -112,6 +114,15 @@ 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 | ||
|
@@ -161,6 +172,7 @@ extern bool nni_plat_thr_is_self(nni_plat_thr *); | |
// should be a short ASCII string. It may or may not be supported -- | ||
// this is intended to facilitate debugging. | ||
extern void nni_plat_thr_set_name(nni_plat_thr *, const char *); | ||
|
||
// | ||
// Atomics support. This will evolve over time. | ||
// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Copyright 2020 Staysail Systems, Inc. <[email protected]> | ||
// Copyright 2021 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 | ||
|
@@ -58,6 +58,10 @@ struct nni_plat_mtx { | |
pthread_mutex_t mtx; | ||
}; | ||
|
||
struct nni_rwlock { | ||
pthread_rwlock_t rwl; | ||
}; | ||
|
||
struct nni_plat_cv { | ||
pthread_cond_t cv; | ||
nni_plat_mtx * mtx; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Copyright 2020 Staysail Systems, Inc. <[email protected]> | ||
// Copyright 2021 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 | ||
|
@@ -81,7 +81,6 @@ static void | |
nni_pthread_mutex_lock(pthread_mutex_t *m) | ||
{ | ||
int rv; | ||
|
||
if ((rv = pthread_mutex_lock(m)) != 0) { | ||
nni_panic("pthread_mutex_lock: %s", strerror(rv)); | ||
} | ||
|
@@ -91,7 +90,6 @@ static void | |
nni_pthread_mutex_unlock(pthread_mutex_t *m) | ||
{ | ||
int rv; | ||
|
||
if ((rv = pthread_mutex_unlock(m)) != 0) { | ||
nni_panic("pthread_mutex_unlock: %s", strerror(rv)); | ||
} | ||
|
@@ -101,7 +99,6 @@ static void | |
nni_pthread_cond_broadcast(pthread_cond_t *c) | ||
{ | ||
int rv; | ||
|
||
if ((rv = pthread_cond_broadcast(c)) != 0) { | ||
nni_panic("pthread_cond_broadcast: %s", strerror(rv)); | ||
} | ||
|
@@ -155,6 +152,53 @@ 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) | ||
{ | ||
|
@@ -264,7 +308,7 @@ nni_plat_thr_set_name(nni_plat_thr *thr, const char *name) | |
#if defined(__APPLE__) | ||
// Darwin is weird, it can only set the name of pthread_self. | ||
if ((thr == NULL) || (pthread_self() == thr->tid)) { | ||
pthread_setname_np(name); | ||
pthread_setname_np(name); | ||
} | ||
#elif defined(__NetBSD__) | ||
if (thr == NULL) { | ||
|
@@ -283,7 +327,7 @@ nni_plat_thr_set_name(nni_plat_thr *thr, const char *name) | |
if (thr == NULL) { | ||
pthread_set_name_np(pthread_self(), name); | ||
} else { | ||
pthread_set_name_np(thr->tid, name); | ||
pthread_set_name_np(thr->tid, name); | ||
} | ||
#endif | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters