Rabbit Tree
Radix bit tries for implementing associative arrays and sets in C.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
node_pthread.h
Go to the documentation of this file.
1 
14 #include <pthread.h>
15 
19 #undef RBT_NODE_ROOT_T
20 #define RBT_NODE_ROOT_T RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_root_t)
21 
28 typedef
29 struct RBT_NODE_ROOT_T
30 {
36 
41  unsigned int readers;
42 
47  unsigned int writers;
48 
53  pthread_mutex_t mutex;
54 
59  pthread_cond_t cond;
60 }
62 
63 
77 {
78  RBT_NODE_ROOT_T root;
79  root.node = RBT_NODE_CREATE(NULL, 0, RBT_VALUE_NULL, NULL, NULL);
80  root.mutex = PTHREAD_MUTEX_INITIALIZER;
81  root.cond = PTHREAD_COND_INITIALIZER;
82  root.readers = 0;
83  root.writers = 0;
84 }
85 
112 #undef RBT_NODE_ROOT_READ
113 #define RBT_NODE_ROOT_READ(root, func, ...) \
114 pthread_mutex_lock(&(root.mutex)); \
115 while (root.writers) \
116 { \
117  pthread_cond_wait(&(root.cond), &(root.mutex)); \
118 } \
119 root.readers ++; \
120 pthread_mutex_unlock(&(root.mutex)); \
121 func(root.node, ##__VA_ARGS__); \
122 pthread_mutex_lock(&(root.mutex)); \
123 root.readers --; \
124 if (! root.readers) \
125 { \
126  pthread_cond_signal(&(root.cond)); \
127 } \
128 pthread_mutex_unlock(&(root.mutex))
129 
130 
156 #undef RBT_NODE_ROOT_WRITE
157 #define RBT_NODE_ROOT_WRITE(root, func, ...) \
158 pthread_mutex_lock(&(root.mutex)); \
159 root.writers ++; \
160 while (root.readers) \
161 { \
162  pthread_cond_wait(&(root.cond), &(root.mutex)); \
163 } \
164 func(root.node, ##__VA_ARGS__); \
165 root.writers --; \
166 pthread_cond_signal(&(root.cond)); \
167 pthread_mutex_unlock(&(root.mutex))
168 
169 
170 
171 #ifdef RBT_NODE_CACHE_SIZE
172 
175 #undef RBT_NODE_CACHE_MUTEX
176 #define RBT_NODE_CACHE_MUTEX RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_cache_mutex)
177 
184 pthread_mutex_t RBT_NODE_CACHE_MUTEX = PTHREAD_MUTEX_INITIALIZER;
185 
189 #undef RBT_NODE_CACHE_LOCK
190 #define RBT_NODE_CACHE_LOCK pthread_mutex_lock(&RBT_NODE_CACHE_MUTEX)
191 
195 #undef RBT_NODE_CACHE_UNLOCK
196 #define RBT_NODE_CACHE_UNLOCK pthread_mutex_unlock(&RBT_NODE_CACHE_MUTEX)
197 
198 #endif //RBT_NODE_CACHE_SIZE
RBT_NODE_ROOT_T RBT_NODE_NEW()
Create a new root node.
Definition: node_pthread.h:76
Definition: node_pthread.h:28
RBT_NODE_T * node
The root node.
Definition: node_pthread.h:35
unsigned int readers
The number of concurrent read operations.
Definition: node_pthread.h:41
unsigned int writers
The number of queued write operations.
Definition: node_pthread.h:47
pthread_mutex_t mutex
The mutex that protects this root node.
Definition: node_pthread.h:53
RBT_NODE_T * RBT_NODE_CREATE(RBT_PIN_T *key, RBT_KEY_SIZE_T bits, RBT_VALUE_T value, RBT_NODE_T *left, RBT_NODE_T *right)
Create a node.
Definition: node.h:1082
Rabbit Tree node type.
Definition: node.h:418
struct RBT_NODE_ROOT_T RBT_NODE_ROOT_T
pthread_cond_t cond
The condition variable used to signal waiting threads.
Definition: node_pthread.h:59