Module: xenomai-3
Branch: stable-3.0.x
Commit: f7022c1d0a6ba1ffb15f629bfe049d14be13fea4
URL:    
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=f7022c1d0a6ba1ffb15f629bfe049d14be13fea4

Author: Philippe Gerum <r...@xenomai.org>
Date:   Wed Feb 24 09:06:35 2016 +0100

lib/smokey: add barrier support helpers

---

 include/smokey/smokey.h |   15 +++++++++++++
 lib/smokey/helpers.c    |   57 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)

diff --git a/include/smokey/smokey.h b/include/smokey/smokey.h
index 35b5153..d3c9987 100644
--- a/include/smokey/smokey.h
+++ b/include/smokey/smokey.h
@@ -19,6 +19,7 @@
 #define _XENOMAI_SMOKEY_SMOKEY_H
 
 #include <stdarg.h>
+#include <pthread.h>
 #include <boilerplate/list.h>
 #include <boilerplate/libc.h>
 #include <copperplate/clockobj.h>
@@ -132,6 +133,12 @@ struct smokey_test {
 #define smokey_warning(__fmt, __args...)       \
        __smokey_warning(__FILE__, __LINE__, __fmt, ##__args)
 
+struct smokey_barrier {
+       pthread_mutex_t lock;
+       pthread_cond_t barrier;
+       int signaled;
+};
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -159,6 +166,14 @@ void smokey_note(const char *fmt, ...);
 void __smokey_warning(const char *file, int lineno,
                      const char *fmt, ...);
 
+int smokey_barrier_init(struct smokey_barrier *b);
+
+void smokey_barrier_destroy(struct smokey_barrier *b);
+
+int smokey_barrier_wait(struct smokey_barrier *b);
+
+void smokey_barrier_release(struct smokey_barrier *b);
+       
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/smokey/helpers.c b/lib/smokey/helpers.c
index 279054f..b52afed 100644
--- a/lib/smokey/helpers.c
+++ b/lib/smokey/helpers.c
@@ -164,3 +164,60 @@ void __smokey_warning(const char *file, int lineno,
 
        va_end(ap);
 }
+
+int smokey_barrier_init(struct smokey_barrier *b)
+{
+       pthread_mutexattr_t mattr;
+       pthread_condattr_t cattr;
+       int ret;
+
+       b->signaled = 0;
+       pthread_mutexattr_init(&mattr);
+       pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
+       pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_PRIVATE);
+       pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_NONE);
+       ret = __RT(pthread_mutex_init(&b->lock, &mattr));
+       pthread_mutexattr_destroy(&mattr);
+       if (ret)
+               return ret;
+
+       pthread_condattr_init(&cattr);
+       pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
+       ret = __RT(pthread_cond_init(&b->barrier, &cattr));
+       pthread_condattr_destroy(&cattr);
+       if (ret)
+               __RT(pthread_mutex_destroy(&b->lock));
+
+       return ret;
+}
+
+void smokey_barrier_destroy(struct smokey_barrier *b)
+{
+       __RT(pthread_cond_destroy(&b->barrier));
+       __RT(pthread_mutex_destroy(&b->lock));
+}
+
+int smokey_barrier_wait(struct smokey_barrier *b)
+{
+       int ret;
+       
+       __RT(pthread_mutex_lock(&b->lock));
+
+       while (!b->signaled) {
+               ret = __RT(pthread_cond_wait(&b->barrier, &b->lock));
+               if (ret)
+                       break;
+       }
+
+       __RT(pthread_mutex_unlock(&b->lock));
+
+       return ret;
+}
+
+void smokey_barrier_release(struct smokey_barrier *b)
+{
+       __RT(pthread_mutex_lock(&b->lock));
+       b->signaled = 1;
+       __RT(pthread_cond_broadcast(&b->barrier));
+       __RT(pthread_mutex_unlock(&b->lock));
+}


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://xenomai.org/mailman/listinfo/xenomai-git

Reply via email to