1
/*-------------------------------------------------------------------------
4
* Hardware-independent implementation of spinlocks.
7
* For machines that have test-and-set (TAS) instructions, s_lock.h/.c
8
* define the spinlock implementation. This file contains only a stub
9
* implementation for spinlocks using PGSemaphores. Unless semaphores
10
* are implemented in a way that doesn't involve a kernel call, this
11
* is too slow to be very useful :-(
14
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
15
* Portions Copyright (c) 1994, Regents of the University of California
21
*-------------------------------------------------------------------------
25
#include "miscadmin.h"
26
#include "storage/lwlock.h"
27
#include "storage/spin.h"
33
* Report number of semaphores needed to support spinlocks.
40
#else /* !HAVE_SPINLOCKS */
43
* No TAS, so spinlocks are implemented as PGSemaphores.
48
* Report number of semaphores needed to support spinlocks.
54
* It would be cleaner to distribute this logic into the affected modules,
55
* similar to the way shmem space estimation is handled.
57
* For now, though, we just need a few spinlocks (10 should be plenty)
58
* plus one for each LWLock and one for each buffer header.
60
return NumLWLocks() + NBuffers + 10;
64
* s_lock.h hardware-spinlock emulation
68
s_init_lock_sema(volatile slock_t *lock)
70
PGSemaphoreCreate((PGSemaphore) lock);
74
s_unlock_sema(volatile slock_t *lock)
76
PGSemaphoreUnlock((PGSemaphore) lock);
80
s_lock_free_sema(volatile slock_t *lock)
82
/* We don't currently use S_LOCK_FREE anyway */
83
elog(ERROR, "spin.c does not support S_LOCK_FREE()");
88
tas_sema(volatile slock_t *lock)
90
/* Note that TAS macros return 0 if *success* */
91
return !PGSemaphoreTryLock((PGSemaphore) lock);
94
#endif /* !HAVE_SPINLOCKS */