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-2004, PostgreSQL Global Development Group
14 * Portions Copyright (c) 1994, Regents of the University of California
16 * $PostgreSQL: pgsql/src/include/storage/pg_sema.h,v 1.6 2004/08/29 04:13:10 momjian Exp $
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 typedef PGSemaphoreData *PGSemaphore;
60 /* Module initialization (called during postmaster start or shmem reinit) */
61 extern void PGReserveSemaphores(int maxSemas, int port);
63 /* Initialize a PGSemaphore structure to represent a sema with count 1 */
64 extern void PGSemaphoreCreate(PGSemaphore sema);
66 /* Reset a previously-initialized PGSemaphore to have count 0 */
67 extern void PGSemaphoreReset(PGSemaphore sema);
69 /* Lock a semaphore (decrement count), blocking if count would be < 0 */
70 extern void PGSemaphoreLock(PGSemaphore sema, bool interruptOK);
72 /* Unlock a semaphore (increment count) */
73 extern void PGSemaphoreUnlock(PGSemaphore sema);
75 /* Lock a semaphore only if able to do so without blocking */
76 extern bool PGSemaphoreTryLock(PGSemaphore sema);
78 #endif /* PG_SEMA_H */