Module: xenomai-forge
Branch: next
Commit: 5322161ca0df896657a0d899a0a9926bb94a0c57
URL:    
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=5322161ca0df896657a0d899a0a9926bb94a0c57

Author: Philippe Gerum <r...@xenomai.org>
Date:   Sun May 26 10:39:09 2013 +0200

copperplate/lock: introduce cleanup block handling

This construct combines section unlocking then arbitrary cleanup
handler execution.

---

 include/copperplate/lock.h |   34 +++++++++++++++++++++++++++++++---
 lib/copperplate/internal.c |    6 ++++++
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/include/copperplate/lock.h b/include/copperplate/lock.h
index f5f298f..87e2534 100644
--- a/include/copperplate/lock.h
+++ b/include/copperplate/lock.h
@@ -62,10 +62,28 @@ struct service {
 
 #endif  /* !CONFIG_XENO_ASYNC_CANCEL */
 
-#define push_cleanup_lock(lock)                \
-       pthread_cleanup_push((void (*)(void *))__RT(pthread_mutex_unlock), 
(lock))
+struct cleanup_block {
+       pthread_mutex_t *lock;
+       void (*handler)(void *arg);
+       void *arg;
+};
+
+#define __push_cleanup_args(__cb, __lock, __fn, __arg) \
+       ((__cb)->lock = (__lock)),                      \
+       ((__cb)->handler = (void (*)(void *))(__fn)),   \
+       ((__cb)->arg = (__arg))
+
+#define push_cleanup_handler(__cb, __lock, __fn, __arg)                        
\
+       pthread_cleanup_push((void (*)(void *))__run_cleanup_block,     \
+                            (__push_cleanup_args(__cb, __lock, __fn, __arg), 
(__cb)))
+
+#define pop_cleanup_handler(__cb)      \
+       pthread_cleanup_pop(0)
+
+#define push_cleanup_lock(__lock)      \
+       pthread_cleanup_push((void (*)(void *))__RT(pthread_mutex_unlock), 
(__lock))
 
-#define pop_cleanup_lock(lock)         \
+#define pop_cleanup_lock(__lock)       \
        pthread_cleanup_pop(0)
 
 #ifdef __XENO_DEBUG__
@@ -197,4 +215,14 @@ int __check_cancel_type(const char *locktype);
 #define atomic_add_fetch(v, n) __sync_add_and_fetch(&(v), n)
 #define atomic_cmp_swap(ptr, old, new)  __sync_val_compare_and_swap(ptr, old, 
new)
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void __run_cleanup_block(struct cleanup_block *cb);
+
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* _COPPERPLATE_LOCK_H */
diff --git a/lib/copperplate/internal.c b/lib/copperplate/internal.c
index 2c40da1..1443c4d 100644
--- a/lib/copperplate/internal.c
+++ b/lib/copperplate/internal.c
@@ -274,3 +274,9 @@ const char *symerror(int errnum)
 
        return __esym_map[v];
 }
+
+void __run_cleanup_block(struct cleanup_block *cb)
+{
+       __RT(pthread_mutex_unlock(cb->lock));
+       cb->handler(cb->arg);
+}


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

Reply via email to