glusterfs  8.2
About: GlusterFS is a network/cluster filesystem. The storage server (or each in a cluster) runs glusterfsd and the clients use mount command or glusterfs client to mount the exported filesystem. Release series 8.x (latest version).
  Fossies Dox: glusterfs-8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

locking.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
3  This file is part of GlusterFS.
4 
5  This file is licensed to you under your choice of the GNU Lesser
6  General Public License, version 3 or any later version (LGPLv3 or
7  later), or the GNU General Public License, version 2 (GPLv2), in all
8  cases as published by the Free Software Foundation.
9 */
10 
11 #ifndef _LOCKING_H
12 #define _LOCKING_H
13 
14 #include <pthread.h>
15 
16 #if defined(GF_DARWIN_HOST_OS)
17 #include <libkern/OSAtomic.h>
18 #define pthread_spinlock_t OSSpinLock
19 #define pthread_spin_lock(l) OSSpinLockLock(l)
20 #define pthread_spin_unlock(l) OSSpinLockUnlock(l)
21 #define pthread_spin_destroy(l) 0
22 #define pthread_spin_init(l, v) (*l = v)
23 #endif
24 
25 #if defined(HAVE_SPINLOCK)
26 
27 typedef union {
28  pthread_spinlock_t spinlock;
29  pthread_mutex_t mutex;
30 } gf_lock_t;
31 
32 #if !defined(LOCKING_IMPL)
33 extern int use_spinlocks;
34 
35 /*
36  * Using a dispatch table would be unpleasant because we're dealing with two
37  * different types. If the dispatch contains direct pointers to pthread_xx
38  * or mutex_xxx then we have to hope that every possible union alternative
39  * starts at the same address as the union itself. I'm old enough to remember
40  * compilers where this was not the case (for alignment reasons) so I'm a bit
41  * paranoid about that. Also, I don't like casting arguments through "void *"
42  * which we'd also have to do to avoid type errors. The other alternative would
43  * be to define actual functions which pick out the right union member, and put
44  * those in the dispatch tables. Now we have a pointer dereference through the
45  * dispatch table plus a function call, which is likely to be worse than the
46  * branching here from the ?: construct. If it were a clear win it might be
47  * worth the extra complexity, but for now this way seems preferable.
48  */
49 
50 #define LOCK_INIT(x) \
51  (use_spinlocks ? pthread_spin_init(&((x)->spinlock), 0) \
52  : pthread_mutex_init(&((x)->mutex), 0))
53 
54 #define LOCK(x) \
55  (use_spinlocks ? pthread_spin_lock(&((x)->spinlock)) \
56  : pthread_mutex_lock(&((x)->mutex)))
57 
58 #define TRY_LOCK(x) \
59  (use_spinlocks ? pthread_spin_trylock(&((x)->spinlock)) \
60  : pthread_mutex_trylock(&((x)->mutex)))
61 
62 #define UNLOCK(x) \
63  (use_spinlocks ? pthread_spin_unlock(&((x)->spinlock)) \
64  : pthread_mutex_unlock(&((x)->mutex)))
65 
66 #define LOCK_DESTROY(x) \
67  (use_spinlocks ? pthread_spin_destroy(&((x)->spinlock)) \
68  : pthread_mutex_destroy(&((x)->mutex)))
69 
70 #endif
71 
72 #else
73 
74 typedef pthread_mutex_t gf_lock_t;
75 
76 #define LOCK_INIT(x) pthread_mutex_init(x, 0)
77 #define LOCK(x) pthread_mutex_lock(x)
78 #define TRY_LOCK(x) pthread_mutex_trylock(x)
79 #define UNLOCK(x) pthread_mutex_unlock(x)
80 #define LOCK_DESTROY(x) pthread_mutex_destroy(x)
81 
82 #endif /* HAVE_SPINLOCK */
83 
84 #endif /* _LOCKING_H */
gf_lock_t
pthread_mutex_t gf_lock_t
Definition: locking.h:74