netsniff-ng: Move variable definition
[netsniff-ng.git] / locking.h
blob51034b3681b9b61efaa9cb9addce4596dbdcb719
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 static inline int spinlock_init(struct spinlock *l)
20 return -pthread_spin_init(&l->lock, 0);
23 static inline void spinlock_destroy(struct spinlock *l)
25 pthread_spin_destroy(&l->lock);
28 static inline void spinlock_lock(struct spinlock *l)
30 pthread_spin_lock(&l->lock);
33 static inline void spinlock_unlock(struct spinlock *l)
35 pthread_spin_unlock(&l->lock);
38 static inline int mutexlock_init(struct mutexlock *l)
40 return -pthread_mutex_init(&l->lock, 0);
43 static inline void mutexlock_destroy(struct mutexlock *l)
45 pthread_mutex_destroy(&l->lock);
48 static inline void mutexlock_lock(struct mutexlock *l)
50 pthread_mutex_lock(&l->lock);
53 static inline void mutexlock_unlock(struct mutexlock *l)
55 pthread_mutex_unlock(&l->lock);
58 static inline int rwlock_init(struct rwlock *l)
60 return -pthread_rwlock_init(&l->lock, 0);
63 static inline int rwlock_init2(struct rwlock *l,
64 pthread_rwlockattr_t *attr)
66 return -pthread_rwlock_init(&l->lock, attr);
69 static inline void rwlock_destroy(struct rwlock *l)
71 pthread_rwlock_destroy(&l->lock);
74 static inline void rwlock_rd_lock(struct rwlock *l)
76 pthread_rwlock_rdlock(&l->lock);
79 static inline void rwlock_wr_lock(struct rwlock *l)
81 pthread_rwlock_wrlock(&l->lock);
84 static inline void rwlock_unlock(struct rwlock *l)
86 pthread_rwlock_unlock(&l->lock);
89 #endif /* LOCKING_H */