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 <[email protected]>
---
 sheep/sheep_priv.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index 6f19fc8..0f66613 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -219,6 +219,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

-- 
sheepdog mailing list
[email protected]
http://lists.wpkg.org/mailman/listinfo/sheepdog

Reply via email to