flowtop: Don't init screen until collector is ready
[netsniff-ng.git] / locking.h
blobcb57a9df79de8396540cd915a2e91c482142a7d3
1 #ifndef LOCKING_H
2 #define LOCKING_H
4 #include <pthread.h>
6 struct spinlock {
7 pthread_spinlock_t lock;
8 };
10 struct mutexlock {
11 pthread_mutex_t lock;
14 struct rwlock {
15 pthread_rwlock_t lock;
18 struct condlock {
19 pthread_mutex_t lock;
20 pthread_cond_t cond;
23 static inline int spinlock_init(struct spinlock *l)
25 return -pthread_spin_init(&l->lock, 0);
28 static inline void spinlock_destroy(struct spinlock *l)
30 pthread_spin_destroy(&l->lock);
33 static inline void spinlock_lock(struct spinlock *l)
35 pthread_spin_lock(&l->lock);
38 static inline void spinlock_unlock(struct spinlock *l)
40 pthread_spin_unlock(&l->lock);
43 static inline int mutexlock_init(struct mutexlock *l)
45 return -pthread_mutex_init(&l->lock, 0);
48 static inline void mutexlock_destroy(struct mutexlock *l)
50 pthread_mutex_destroy(&l->lock);
53 static inline void mutexlock_lock(struct mutexlock *l)
55 pthread_mutex_lock(&l->lock);
58 static inline void mutexlock_unlock(struct mutexlock *l)
60 pthread_mutex_unlock(&l->lock);
63 static inline int rwlock_init(struct rwlock *l)
65 return -pthread_rwlock_init(&l->lock, 0);
68 static inline int rwlock_init2(struct rwlock *l,
69 pthread_rwlockattr_t *attr)
71 return -pthread_rwlock_init(&l->lock, attr);
74 static inline void rwlock_destroy(struct rwlock *l)
76 pthread_rwlock_destroy(&l->lock);
79 static inline void rwlock_rd_lock(struct rwlock *l)
81 pthread_rwlock_rdlock(&l->lock);
84 static inline void rwlock_wr_lock(struct rwlock *l)
86 pthread_rwlock_wrlock(&l->lock);
89 static inline void rwlock_unlock(struct rwlock *l)
91 pthread_rwlock_unlock(&l->lock);
94 static inline void condlock_init(struct condlock *c)
96 pthread_mutex_init(&c->lock, NULL);
97 pthread_cond_init(&c->cond, NULL);
100 static inline void condlock_signal(struct condlock *c)
102 pthread_mutex_lock(&c->lock);
103 pthread_cond_signal(&c->cond);
104 pthread_mutex_unlock(&c->lock);
107 static inline void condlock_wait(struct condlock *c)
109 pthread_mutex_lock(&c->lock);
110 pthread_cond_wait(&c->cond, &c->lock);
111 pthread_mutex_unlock(&c->lock);
114 static inline void condlock_destroy(struct condlock *c)
116 pthread_mutex_destroy(&c->lock);
117 pthread_cond_destroy(&c->cond);
120 #endif /* LOCKING_H */