netsniff-ng: fix snooping on any device when no option is given
[netsniff-ng.git] / locking.h
blob2cb93d1a0c82ffe08ff2c612bb24d9f01fc6c65e
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 #define MUTEXLOCK_INITIALIZER { .lock = PTHREAD_MUTEX_INITIALIZER }
16 struct rwlock {
17 pthread_rwlock_t lock;
20 struct condlock {
21 pthread_mutex_t lock;
22 pthread_cond_t cond;
25 static inline int spinlock_init(struct spinlock *l)
27 return -pthread_spin_init(&l->lock, 0);
30 static inline void spinlock_destroy(struct spinlock *l)
32 pthread_spin_destroy(&l->lock);
35 static inline void spinlock_lock(struct spinlock *l)
37 pthread_spin_lock(&l->lock);
40 static inline void spinlock_unlock(struct spinlock *l)
42 pthread_spin_unlock(&l->lock);
45 static inline int mutexlock_init(struct mutexlock *l)
47 return -pthread_mutex_init(&l->lock, 0);
50 static inline void mutexlock_destroy(struct mutexlock *l)
52 pthread_mutex_destroy(&l->lock);
55 static inline void mutexlock_lock(struct mutexlock *l)
57 pthread_mutex_lock(&l->lock);
60 static inline void mutexlock_unlock(struct mutexlock *l)
62 pthread_mutex_unlock(&l->lock);
65 static inline int rwlock_init(struct rwlock *l)
67 return -pthread_rwlock_init(&l->lock, 0);
70 static inline int rwlock_init2(struct rwlock *l,
71 pthread_rwlockattr_t *attr)
73 return -pthread_rwlock_init(&l->lock, attr);
76 static inline void rwlock_destroy(struct rwlock *l)
78 pthread_rwlock_destroy(&l->lock);
81 static inline void rwlock_rd_lock(struct rwlock *l)
83 pthread_rwlock_rdlock(&l->lock);
86 static inline void rwlock_wr_lock(struct rwlock *l)
88 pthread_rwlock_wrlock(&l->lock);
91 static inline void rwlock_unlock(struct rwlock *l)
93 pthread_rwlock_unlock(&l->lock);
96 static inline void condlock_init(struct condlock *c)
98 pthread_mutex_init(&c->lock, NULL);
99 pthread_cond_init(&c->cond, NULL);
102 static inline void condlock_signal(struct condlock *c)
104 pthread_mutex_lock(&c->lock);
105 pthread_cond_signal(&c->cond);
106 pthread_mutex_unlock(&c->lock);
109 static inline void condlock_wait(struct condlock *c)
111 pthread_mutex_lock(&c->lock);
112 pthread_cond_wait(&c->cond, &c->lock);
113 pthread_mutex_unlock(&c->lock);
116 static inline void condlock_destroy(struct condlock *c)
118 pthread_mutex_destroy(&c->lock);
119 pthread_cond_destroy(&c->cond);
122 #endif /* LOCKING_H */