PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
spin.c File Reference
#include "postgres.h"
#include "storage/pg_sema.h"
#include "storage/shmem.h"
#include "storage/spin.h"
Include dependency graph for spin.c:

Go to the source code of this file.

Functions

Size SpinlockSemaSize (void)
 
int SpinlockSemas (void)
 
void SpinlockSemaInit (void)
 
void s_init_lock_sema (volatile slock_t *lock, bool nested)
 
void s_unlock_sema (volatile slock_t *lock)
 
bool s_lock_free_sema (volatile slock_t *lock)
 
int tas_sema (volatile slock_t *lock)
 

Variables

PGSemaphoreSpinlockSemaArray
 

Function Documentation

void s_init_lock_sema ( volatile slock_t lock,
bool  nested 
)

Definition at line 107 of file spin.c.

References NUM_SPINLOCK_SEMAPHORES.

Referenced by pg_atomic_init_flag_impl(), pg_atomic_init_u32_impl(), and pg_atomic_init_u64_impl().

108 {
109  static int counter = 0;
110 
111  *lock = ((++counter) % NUM_SPINLOCK_SEMAPHORES) + 1;
112 }
#define NUM_SPINLOCK_SEMAPHORES
bool s_lock_free_sema ( volatile slock_t lock)

Definition at line 125 of file spin.c.

References elog, and ERROR.

126 {
127  /* We don't currently use S_LOCK_FREE anyway */
128  elog(ERROR, "spin.c does not support S_LOCK_FREE()");
129  return false;
130 }
#define ERROR
Definition: elog.h:43
#define elog
Definition: elog.h:219
void s_unlock_sema ( volatile slock_t lock)

Definition at line 115 of file spin.c.

References elog, ERROR, NUM_SPINLOCK_SEMAPHORES, and PGSemaphoreUnlock().

116 {
117  int lockndx = *lock;
118 
119  if (lockndx <= 0 || lockndx > NUM_SPINLOCK_SEMAPHORES)
120  elog(ERROR, "invalid spinlock number: %d", lockndx);
121  PGSemaphoreUnlock(SpinlockSemaArray[lockndx - 1]);
122 }
void PGSemaphoreUnlock(PGSemaphore sema)
Definition: posix_sema.c:323
#define ERROR
Definition: elog.h:43
PGSemaphore * SpinlockSemaArray
Definition: spin.c:31
#define elog
Definition: elog.h:219
#define NUM_SPINLOCK_SEMAPHORES
void SpinlockSemaInit ( void  )

Definition at line 76 of file spin.c.

References i, PGSemaphoreCreate(), ShmemAllocUnlocked(), SpinlockSemas(), and SpinlockSemaSize().

Referenced by CreateSharedMemoryAndSemaphores().

77 {
78  PGSemaphore *spinsemas;
79  int nsemas = SpinlockSemas();
80  int i;
81 
82  /*
83  * We must use ShmemAllocUnlocked(), since the spinlock protecting
84  * ShmemAlloc() obviously can't be ready yet.
85  */
87  for (i = 0; i < nsemas; ++i)
88  spinsemas[i] = PGSemaphoreCreate();
89  SpinlockSemaArray = spinsemas;
90 }
Size SpinlockSemaSize(void)
Definition: spin.c:39
PGSemaphore PGSemaphoreCreate(void)
Definition: posix_sema.c:245
int SpinlockSemas(void)
Definition: spin.c:65
void * ShmemAllocUnlocked(Size size)
Definition: shmem.c:227
int i
PGSemaphore * SpinlockSemaArray
Definition: spin.c:31
int SpinlockSemas ( void  )

Definition at line 65 of file spin.c.

References NUM_ATOMICS_SEMAPHORES, and NUM_SPINLOCK_SEMAPHORES.

Referenced by CreateSharedMemoryAndSemaphores(), SpinlockSemaInit(), and SpinlockSemaSize().

66 {
68 }
#define NUM_ATOMICS_SEMAPHORES
#define NUM_SPINLOCK_SEMAPHORES
Size SpinlockSemaSize ( void  )

Definition at line 39 of file spin.c.

References SpinlockSemas().

Referenced by CreateSharedMemoryAndSemaphores(), and SpinlockSemaInit().

40 {
41  return SpinlockSemas() * sizeof(PGSemaphore);
42 }
int SpinlockSemas(void)
Definition: spin.c:65
struct PGSemaphoreData * PGSemaphore
Definition: pg_sema.h:34
int tas_sema ( volatile slock_t lock)

Definition at line 133 of file spin.c.

References elog, ERROR, NUM_SPINLOCK_SEMAPHORES, and PGSemaphoreTryLock().

134 {
135  int lockndx = *lock;
136 
137  if (lockndx <= 0 || lockndx > NUM_SPINLOCK_SEMAPHORES)
138  elog(ERROR, "invalid spinlock number: %d", lockndx);
139  /* Note that TAS macros return 0 if *success* */
140  return !PGSemaphoreTryLock(SpinlockSemaArray[lockndx - 1]);
141 }
#define ERROR
Definition: elog.h:43
PGSemaphore * SpinlockSemaArray
Definition: spin.c:31
bool PGSemaphoreTryLock(PGSemaphore sema)
Definition: posix_sema.c:348
#define elog
Definition: elog.h:219
#define NUM_SPINLOCK_SEMAPHORES

Variable Documentation

PGSemaphore* SpinlockSemaArray

Definition at line 31 of file spin.c.