diff options
| author | Garrett D'Amore <garrett@damore.org> | 2017-01-16 16:27:22 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2017-01-16 16:27:22 -0800 |
| commit | ac8415c24ffea645105c3859e814843e81c97f8a (patch) | |
| tree | 7b64b4aab3de6ce5bdd69c3d5b7ead57f4a4b4e7 /src/platform | |
| parent | b8f7236aa2928d70d9bff2e1071654982539eeda (diff) | |
| download | nng-ac8415c24ffea645105c3859e814843e81c97f8a.tar.gz nng-ac8415c24ffea645105c3859e814843e81c97f8a.tar.bz2 nng-ac8415c24ffea645105c3859e814843e81c97f8a.zip | |
Start of event framework.
This compiles correctly, but doesn't actually deliver events yet.
As part of this, I've made most of the initializables in nng
safe to tear-down if uninitialized (or set to zero e.g. via calloc).
This makes it loads easier to write the teardown on error code, since
I can deinit everything, without worrying about which things have been
initialized and which have not.
Diffstat (limited to 'src/platform')
| -rw-r--r-- | src/platform/posix/posix_impl.h | 1 | ||||
| -rw-r--r-- | src/platform/posix/posix_thread.c | 9 | ||||
| -rw-r--r-- | src/platform/windows/win_impl.h | 1 | ||||
| -rw-r--r-- | src/platform/windows/win_thread.c | 6 |
4 files changed, 16 insertions, 1 deletions
diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index 1ab728c5..3faebfd4 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -60,6 +60,7 @@ extern int nni_plat_devnull; // open descriptor on /dev/null // elsewhere. struct nni_plat_mtx { + int init; pthread_mutex_t mtx; }; diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c index 91fbdff7..113dd9ea 100644 --- a/src/platform/posix/posix_thread.c +++ b/src/platform/posix/posix_thread.c @@ -50,6 +50,7 @@ nni_plat_mtx_init(nni_plat_mtx *mtx) nni_panic("pthread_mutex_init: %s", strerror(rv)); } } + mtx->init = 1; return (0); } @@ -59,9 +60,13 @@ nni_plat_mtx_fini(nni_plat_mtx *mtx) { int rv; + if (!mtx->init) { + return; + } if ((rv = pthread_mutex_destroy(&mtx->mtx)) != 0) { nni_panic("pthread_mutex_fini: %s", strerror(rv)); } + mtx->init = 0; } @@ -159,9 +164,13 @@ nni_plat_cv_fini(nni_plat_cv *cv) { int rv; + if (cv->mtx == NULL) { + return; + } if ((rv = pthread_cond_destroy(&cv->cv)) != 0) { nni_panic("pthread_cond_destroy: %s", strerror(rv)); } + cv->mtx = NULL; } diff --git a/src/platform/windows/win_impl.h b/src/platform/windows/win_impl.h index 9de47ce4..c6a68142 100644 --- a/src/platform/windows/win_impl.h +++ b/src/platform/windows/win_impl.h @@ -60,6 +60,7 @@ struct nni_plat_thr { struct nni_plat_mtx { CRITICAL_SECTION cs; DWORD owner; + int init; }; struct nni_plat_cv { diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index e2568626..1b903b26 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -35,6 +35,7 @@ int nni_plat_mtx_init(nni_plat_mtx *mtx) { InitializeCriticalSection(&mtx->cs); + mtx->init = 1; return (0); } @@ -42,7 +43,10 @@ nni_plat_mtx_init(nni_plat_mtx *mtx) void nni_plat_mtx_fini(nni_plat_mtx *mtx) { - DeleteCriticalSection(&mtx->cs); + if (mtx->init) { + DeleteCriticalSection(&mtx->cs); + mtx->init = 0; + } } |
