Header And Logo

PostgreSQL
| The world's most advanced open source database.

spin.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * spin.h
00004  *     Hardware-independent implementation of spinlocks.
00005  *
00006  *
00007  *  The hardware-independent interface to spinlocks is defined by the
00008  *  typedef "slock_t" and these macros:
00009  *
00010  *  void SpinLockInit(volatile slock_t *lock)
00011  *      Initialize a spinlock (to the unlocked state).
00012  *
00013  *  void SpinLockAcquire(volatile slock_t *lock)
00014  *      Acquire a spinlock, waiting if necessary.
00015  *      Time out and abort() if unable to acquire the lock in a
00016  *      "reasonable" amount of time --- typically ~ 1 minute.
00017  *
00018  *  void SpinLockRelease(volatile slock_t *lock)
00019  *      Unlock a previously acquired lock.
00020  *
00021  *  bool SpinLockFree(slock_t *lock)
00022  *      Tests if the lock is free. Returns TRUE if free, FALSE if locked.
00023  *      This does *not* change the state of the lock.
00024  *
00025  *  Callers must beware that the macro argument may be evaluated multiple
00026  *  times!
00027  *
00028  *  CAUTION: Care must be taken to ensure that loads and stores of
00029  *  shared memory values are not rearranged around spinlock acquire
00030  *  and release. This is done using the "volatile" qualifier: the C
00031  *  standard states that loads and stores of volatile objects cannot
00032  *  be rearranged *with respect to other volatile objects*. The
00033  *  spinlock is always written through a volatile pointer by the
00034  *  spinlock macros, but this is not sufficient by itself: code that
00035  *  protects shared data with a spinlock MUST reference that shared
00036  *  data through a volatile pointer.
00037  *
00038  *  Keep in mind the coding rule that spinlocks must not be held for more
00039  *  than a few instructions.  In particular, we assume it is not possible
00040  *  for a CHECK_FOR_INTERRUPTS() to occur while holding a spinlock, and so
00041  *  it is not necessary to do HOLD/RESUME_INTERRUPTS() in these macros.
00042  *
00043  *  These macros are implemented in terms of hardware-dependent macros
00044  *  supplied by s_lock.h.  There is not currently any extra functionality
00045  *  added by this header, but there has been in the past and may someday
00046  *  be again.
00047  *
00048  *
00049  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00050  * Portions Copyright (c) 1994, Regents of the University of California
00051  *
00052  * src/include/storage/spin.h
00053  *
00054  *-------------------------------------------------------------------------
00055  */
00056 #ifndef SPIN_H
00057 #define SPIN_H
00058 
00059 #include "storage/s_lock.h"
00060 
00061 
00062 #define SpinLockInit(lock)  S_INIT_LOCK(lock)
00063 
00064 #define SpinLockAcquire(lock) S_LOCK(lock)
00065 
00066 #define SpinLockRelease(lock) S_UNLOCK(lock)
00067 
00068 #define SpinLockFree(lock)  S_LOCK_FREE(lock)
00069 
00070 
00071 extern int  SpinlockSemas(void);
00072 
00073 #endif   /* SPIN_H */