1 /*------------------------------------------------------------------------- 2 * 3 * pg_sema.h 4 * Platform-independent API for semaphores. 5 * 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. 11 * 12 * 13 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group 14 * Portions Copyright (c) 1994, Regents of the University of California 15 * 16 * src/include/storage/pg_sema.h 17 * 18 *------------------------------------------------------------------------- 19 */ 20 #ifndef PG_SEMA_H 21 #define PG_SEMA_H 22 23 /* 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 29 * lock/unlock). 30 * 31 * pg_config.h must define exactly one of the USE_xxx_SEMAPHORES symbols. 32 */ 33 34 #ifdef USE_NAMED_POSIX_SEMAPHORES 35 36 #include <semaphore.h> 37 38 typedef sem_t *PGSemaphoreData; 39 #endif 40 41 #ifdef USE_UNNAMED_POSIX_SEMAPHORES 42 43 #include <semaphore.h> 44 45 typedef sem_t PGSemaphoreData; 46 #endif 47 48 #ifdef USE_SYSV_SEMAPHORES 49 50 typedef struct PGSemaphoreData 51 { 52 int semId; /* semaphore set identifier */ 53 int semNum; /* semaphore number within set */ 54 } PGSemaphoreData; 55 #endif 56 57 #ifdef USE_WIN32_SEMAPHORES 58 59 typedef HANDLE PGSemaphoreData; 60 #endif 61 62 typedef PGSemaphoreData *PGSemaphore; 63 64 65 /* Module initialization (called during postmaster start or shmem reinit) */ 66 extern void PGReserveSemaphores(int maxSemas, int port); 67 68 /* Initialize a PGSemaphore structure to represent a sema with count 1 */ 69 extern void PGSemaphoreCreate(PGSemaphore sema); 70 71 /* Reset a previously-initialized PGSemaphore to have count 0 */ 72 extern void PGSemaphoreReset(PGSemaphore sema); 73 74 /* Lock a semaphore (decrement count), blocking if count would be < 0 */ 75 extern void PGSemaphoreLock(PGSemaphore sema); 76 77 /* Unlock a semaphore (increment count) */ 78 extern void PGSemaphoreUnlock(PGSemaphore sema); 79 80 /* Lock a semaphore only if able to do so without blocking */ 81 extern bool PGSemaphoreTryLock(PGSemaphore sema); 82 83 #endif /* PG_SEMA_H */ 84