[sheepdog] [PATCH v2 04/11] sheep: introduce helpers to handle thread-unsafe variables
MORITA Kazutaka
morita.kazutaka at lab.ntt.co.jp
Fri Apr 19 09:14:53 CEST 2013
We have some variables that can be accessed only in the main thread.
This patch introduces the macro 'thrad_unsafe' to guard it with
assert().
Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
sheep/sheep_priv.h | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index e7a453a..2693c0f 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -216,6 +216,22 @@ static inline bool is_worker_thread(void)
return !is_main_thread();
}
+/*
+ * Helper macros to guard variables from being accessed out of the
+ * main thread. Note that we can use these only for pointers.
+ */
+#define thread_unsafe(type) struct { type __val; }
+#define thread_unsafe_get(var) \
+({ \
+ assert(is_main_thread()); \
+ (var).__val; \
+})
+#define thread_unsafe_set(var, val) \
+({ \
+ assert(is_main_thread()); \
+ (var).__val = (val); \
+})
+
/* One should call this function to get sys->epoch outside main thread */
static inline uint32_t sys_epoch(void)
{
--
1.8.1.3.566.gaa39828
More information about the sheepdog
mailing list