Header And Logo

PostgreSQL
| The world's most advanced open source database.

pg_sema.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * pg_sema.h
00004  *    Platform-independent API for semaphores.
00005  *
00006  * PostgreSQL requires counting semaphores (the kind that keep track of
00007  * multiple unlock operations, and will allow an equal number of subsequent
00008  * lock operations before blocking).  The underlying implementation is
00009  * not the same on every platform.  This file defines the API that must
00010  * be provided by each port.
00011  *
00012  *
00013  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00014  * Portions Copyright (c) 1994, Regents of the University of California
00015  *
00016  * src/include/storage/pg_sema.h
00017  *
00018  *-------------------------------------------------------------------------
00019  */
00020 #ifndef PG_SEMA_H
00021 #define PG_SEMA_H
00022 
00023 /*
00024  * PGSemaphoreData and pointer type PGSemaphore are the data structure
00025  * representing an individual semaphore.  The contents of PGSemaphoreData
00026  * vary across implementations and must never be touched by platform-
00027  * independent code.  PGSemaphoreData structures are always allocated
00028  * in shared memory (to support implementations where the data changes during
00029  * lock/unlock).
00030  *
00031  * pg_config.h must define exactly one of the USE_xxx_SEMAPHORES symbols.
00032  */
00033 
00034 #ifdef USE_NAMED_POSIX_SEMAPHORES
00035 
00036 #include <semaphore.h>
00037 
00038 typedef sem_t *PGSemaphoreData;
00039 #endif
00040 
00041 #ifdef USE_UNNAMED_POSIX_SEMAPHORES
00042 
00043 #include <semaphore.h>
00044 
00045 typedef sem_t PGSemaphoreData;
00046 #endif
00047 
00048 #ifdef USE_SYSV_SEMAPHORES
00049 
00050 typedef struct PGSemaphoreData
00051 {
00052     int         semId;          /* semaphore set identifier */
00053     int         semNum;         /* semaphore number within set */
00054 } PGSemaphoreData;
00055 #endif
00056 
00057 #ifdef USE_WIN32_SEMAPHORES
00058 
00059 typedef HANDLE PGSemaphoreData;
00060 #endif
00061 
00062 typedef PGSemaphoreData *PGSemaphore;
00063 
00064 
00065 /* Module initialization (called during postmaster start or shmem reinit) */
00066 extern void PGReserveSemaphores(int maxSemas, int port);
00067 
00068 /* Initialize a PGSemaphore structure to represent a sema with count 1 */
00069 extern void PGSemaphoreCreate(PGSemaphore sema);
00070 
00071 /* Reset a previously-initialized PGSemaphore to have count 0 */
00072 extern void PGSemaphoreReset(PGSemaphore sema);
00073 
00074 /* Lock a semaphore (decrement count), blocking if count would be < 0 */
00075 extern void PGSemaphoreLock(PGSemaphore sema, bool interruptOK);
00076 
00077 /* Unlock a semaphore (increment count) */
00078 extern void PGSemaphoreUnlock(PGSemaphore sema);
00079 
00080 /* Lock a semaphore only if able to do so without blocking */
00081 extern bool PGSemaphoreTryLock(PGSemaphore sema);
00082 
00083 #endif   /* PG_SEMA_H */