1 /*-------------------------------------------------------------------------
4 * Platform-independent API for semaphores.
6 * PostgreSQL requires counting semaphores (the kind that keep track of
7 * multiple unlock operations, and will allow an equal number of subsequent
8 * lock operations before blocking). The underlying implementation is
9 * not the same on every platform. This file defines the API that must
10 * be provided by each port.
13 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
14 * Portions Copyright (c) 1994, Regents of the University of California
18 *-------------------------------------------------------------------------
24 * PGSemaphoreData and pointer type PGSemaphore are the data structure
25 * representing an individual semaphore. The contents of PGSemaphoreData
26 * vary across implementations and must never be touched by platform-
27 * independent code. PGSemaphoreData structures are always allocated
28 * in shared memory (to support implementations where the data changes during
31 * pg_config.h must define exactly one of the USE_xxx_SEMAPHORES symbols.
34 #ifdef USE_NAMED_POSIX_SEMAPHORES
36 #include <semaphore.h>
38 typedef sem_t
*PGSemaphoreData
;
41 #ifdef USE_UNNAMED_POSIX_SEMAPHORES
43 #include <semaphore.h>
45 typedef sem_t PGSemaphoreData
;
48 #ifdef USE_SYSV_SEMAPHORES
50 typedef struct PGSemaphoreData
52 int semId
; /* semaphore set identifier */
53 int semNum
; /* semaphore number within set */
57 #ifdef USE_WIN32_SEMAPHORES
59 typedef HANDLE PGSemaphoreData
;
62 typedef PGSemaphoreData
*PGSemaphore
;
65 /* Module initialization (called during postmaster start or shmem reinit) */
66 extern void PGReserveSemaphores(int maxSemas
, int port
);
68 /* Initialize a PGSemaphore structure to represent a sema with count 1 */
69 extern void PGSemaphoreCreate(PGSemaphore sema
);
71 /* Reset a previously-initialized PGSemaphore to have count 0 */
72 extern void PGSemaphoreReset(PGSemaphore sema
);
74 /* Lock a semaphore (decrement count), blocking if count would be < 0 */
75 extern void PGSemaphoreLock(PGSemaphore sema
, bool interruptOK
);
77 /* Unlock a semaphore (increment count) */
78 extern void PGSemaphoreUnlock(PGSemaphore sema
);
80 /* Lock a semaphore only if able to do so without blocking */
81 extern bool PGSemaphoreTryLock(PGSemaphore sema
);
83 #endif /* PG_SEMA_H */