[sheepdog] [PATCH 4/8] util: introduce a new data type main_refcnt_t for reference counting
Hitoshi Mitake
mitake.hitoshi at lab.ntt.co.jp
Tue Jul 9 04:36:38 CEST 2013
From: Hitoshi Mitake <mitake.hitoshi at gmail.com>
Incrementing or decrementing reference counts of some important data
structures like vnode_info are only allowed for the main thread of
sheep. So we can use raw int for these reference counting because
preemption of the thread doesn't do anything wrong for the counting.
This patch implements main_refcnt_t for the above purpose. The type
detects invalid increment and decrement by worker threads by
is_main_thread() and assert().
Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
include/work.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git a/include/work.h b/include/work.h
index 3349ee2..2385345 100644
--- a/include/work.h
+++ b/include/work.h
@@ -51,6 +51,52 @@ static inline bool is_worker_thread(void)
})
/*
+ * main_refcnt_t: reference counter which can only be accessed by main thread
+ *
+ * This reference counter can only be incremented or decremented by main thread.
+ * Operations by worker threads are treated as bugs.
+ *
+ * If you want to use reference counters which can be accessed by worker threads
+ * too, use refcnt_t (defined in util.h).
+ */
+
+typedef struct {
+ int val;
+} main_refcnt_t;
+
+static inline int main_refcnt_read(main_refcnt_t *rc)
+{
+ assert(is_main_thread());
+ return rc->val;
+}
+
+static inline void main_refcnt_set(main_refcnt_t *rc, int val)
+{
+ assert(is_main_thread());
+ rc->val = val;
+}
+
+static inline void main_refcnt_inc(main_refcnt_t *rc)
+{
+ assert(is_main_thread());
+ rc->val++;
+}
+
+static inline void main_refcnt_dec(main_refcnt_t *rc)
+{
+ assert(is_main_thread());
+ rc->val--;
+ assert(0 <= rc->val);
+}
+
+static inline int main_refcnt_dec_return(main_refcnt_t *rc)
+{
+ assert(is_main_thread());
+ main_refcnt_dec(rc);
+ return main_refcnt_read(rc);
+}
+
+/*
* 'get_nr_nodes' is the function to get the current number of nodes and used
* for dynamic work queues. 'create_cb' will be called when worker threads are
* created and 'destroy_cb' will be called when worker threads are destroyed.
--
1.7.10.4
More information about the sheepdog
mailing list