Minor cleanups
[elliptics.git] / library / lock.h
blobfd6c68154385a7ef0ec514618e9956122d31e5b3
1 /*
2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3 * All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #ifndef __DNET_LOCK_H
17 #define __DNET_LOCK_H
19 #include <pthread.h>
21 #ifdef HAVE_PTHREAD_SPINLOCK
22 struct dnet_lock {
23 pthread_spinlock_t lock;
26 static inline int dnet_lock_init(struct dnet_lock *l)
28 return -pthread_spin_init(&l->lock, 0);
31 static inline void dnet_lock_destroy(struct dnet_lock *l)
33 pthread_spin_destroy(&l->lock);
36 static inline void dnet_lock_lock(struct dnet_lock *l)
38 pthread_spin_lock(&l->lock);
41 static inline void dnet_lock_unlock(struct dnet_lock *l)
43 pthread_spin_unlock(&l->lock);
45 #else
46 struct dnet_lock {
47 pthread_mutex_t lock;
50 static inline int dnet_lock_init(struct dnet_lock *l)
52 return -pthread_mutex_init(&l->lock, NULL);
55 static inline void dnet_lock_destroy(struct dnet_lock *l)
57 pthread_mutex_destroy(&l->lock);
60 static inline void dnet_lock_lock(struct dnet_lock *l)
62 pthread_mutex_lock(&l->lock);
65 static inline void dnet_lock_unlock(struct dnet_lock *l)
67 pthread_mutex_unlock(&l->lock);
69 #endif
71 #endif /* __DNET_LOCK_H */