Rabbit Tree
Radix bit tries for implementing associative arrays and sets in C.
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)
28typedef
29struct 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, ...) \
114pthread_mutex_lock(&(root.mutex)); \
115while (root.writers) \
116{ \
117 pthread_cond_wait(&(root.cond), &(root.mutex)); \
118} \
119root.readers ++; \
120pthread_mutex_unlock(&(root.mutex)); \
121func(root.node, ##__VA_ARGS__); \
122pthread_mutex_lock(&(root.mutex)); \
123root.readers --; \
124if (! root.readers) \
125{ \
126 pthread_cond_signal(&(root.cond)); \
127} \
128pthread_mutex_unlock(&(root.mutex))
129
130
156#undef RBT_NODE_ROOT_WRITE
157#define RBT_NODE_ROOT_WRITE(root, func, ...) \
158pthread_mutex_lock(&(root.mutex)); \
159root.writers ++; \
160while (root.readers) \
161{ \
162 pthread_cond_wait(&(root.cond), &(root.mutex)); \
163} \
164func(root.node, ##__VA_ARGS__); \
165root.writers --; \
166pthread_cond_signal(&(root.cond)); \
167pthread_mutex_unlock(&(root.mutex))
168
169
170
171#ifdef RBT_NODE_CACHE_SIZE
175#undef RBT_NODE_CACHE_MUTEX
176#define RBT_NODE_CACHE_MUTEX RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_cache_mutex)
184pthread_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_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
RBT_NODE_ROOT_T RBT_NODE_NEW()
Create a new root node.
Definition: node_pthread.h:76
struct RBT_NODE_ROOT_T RBT_NODE_ROOT_T
Definition: node_pthread.h:30
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 * node
The root node.
Definition: node_pthread.h:35
pthread_cond_t cond
The condition variable used to signal waiting threads.
Definition: node_pthread.h:59
unsigned int readers
The number of concurrent read operations.
Definition: node_pthread.h:41
Rabbit Tree node type.
Definition: node.h:420
Contact
echo xyne.archlinux.org | sed 's/\./@/'