aboutsummaryrefslogtreecommitdiff
path: root/src/platform/posix/posix_atomic.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-07-02 22:36:08 -0700
committerGarrett D'Amore <garrett@damore.org>2018-07-03 19:00:19 -0700
commitd1a9c84a6b375cb25a8b7475957130e364b41753 (patch)
tree5444721d96a84d92e3ed258b4d51f80adf6b200c /src/platform/posix/posix_atomic.c
parenta772bcc6ebe198f939889abbda18eded2a326941 (diff)
downloadnng-d1a9c84a6b375cb25a8b7475957130e364b41753.tar.gz
nng-d1a9c84a6b375cb25a8b7475957130e364b41753.tar.bz2
nng-d1a9c84a6b375cb25a8b7475957130e364b41753.zip
fixes #572 Several locking errors found
fixes #573 atomic flags could help This introduces a new atomic flag, and reduces some of the global locking. The lock refactoring work is not yet complete, but this is a positive step forward, and should help with certain things. While here we also fixed a compile warning due to incorrect types.
Diffstat (limited to 'src/platform/posix/posix_atomic.c')
-rw-r--r--src/platform/posix/posix_atomic.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/platform/posix/posix_atomic.c b/src/platform/posix/posix_atomic.c
new file mode 100644
index 00000000..a8d2579d
--- /dev/null
+++ b/src/platform/posix/posix_atomic.c
@@ -0,0 +1,57 @@
+//
+// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2018 Capitar IT Group BV <info@capitar.com>
+//
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
+
+// POSIX atomics.
+
+#include "core/nng_impl.h"
+
+#ifdef NNG_PLATFORM_POSIX
+
+#ifdef NNG_HAVE_STDATOMIC
+
+#include <stdatomic.h>
+bool
+nni_atomic_flag_test_and_set(nni_atomic_flag *f)
+{
+ return (atomic_flag_test_and_set(&f->f));
+}
+
+void
+nni_atomic_flag_reset(nni_atomic_flag *f)
+{
+ atomic_flag_clear(&f->f);
+}
+#else
+
+#include <pthread.h>
+
+static pthread_mutex_t plat_atomic_lock = PTHREAD_MUTEX_INITIALIZER;
+
+bool
+nni_atomic_flag_test_and_set(nni_atomic_flag *f)
+{
+ bool v;
+ pthread_mutex_lock(&plat_atomic_lock);
+ v = f->f;
+ f->f = true;
+ pthread_mutex_unlock(&plat_atomic_lock);
+ return (v);
+}
+
+void
+nni_atomic_flag_reset(nni_atomic_flag *f)
+{
+ pthread_mutex_lock(&plat_atomic_lock);
+ f->f = false;
+ pthread_mutex_unlock(&plat_atomic_lock);
+}
+#endif
+
+#endif // NNG_PLATFORM_POSIX