"Fossies" - the Fresh Open Source Software Archive

Member "glusterfs-8.2/libglusterfs/src/glusterfs/locking.h" (16 Sep 2020, 3246 Bytes) of package /linux/misc/glusterfs-8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "locking.h" see the Fossies "Dox" file reference documentation.

    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 */