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-2019, 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 * struct PGSemaphoreData and pointer type PGSemaphore are the data structure
25 * representing an individual semaphore. The contents of PGSemaphoreData vary
26 * across implementations and must never be touched by platform-independent
27 * code; hence, PGSemaphoreData is declared as an opaque struct here.
28 *
29 * However, Windows is sufficiently unlike our other ports that it doesn't
30 * seem worth insisting on ABI compatibility for Windows too. Hence, on
31 * that platform just define PGSemaphore as HANDLE.
32 */
33#ifndef USE_WIN32_SEMAPHORES
34typedef struct PGSemaphoreData *PGSemaphore;
35#else
36typedef HANDLE PGSemaphore;
37#endif
38
39
40/* Report amount of shared memory needed */
41extern Size PGSemaphoreShmemSize(int maxSemas);
42
43/* Module initialization (called during postmaster start or shmem reinit) */
44extern void PGReserveSemaphores(int maxSemas, int port);
45
46/* Allocate a PGSemaphore structure with initial count 1 */
47extern PGSemaphore PGSemaphoreCreate(void);
48
49/* Reset a previously-initialized PGSemaphore to have count 0 */
50extern void PGSemaphoreReset(PGSemaphore sema);
51
52/* Lock a semaphore (decrement count), blocking if count would be < 0 */
53extern void PGSemaphoreLock(PGSemaphore sema);
54
55/* Unlock a semaphore (increment count) */
56extern void PGSemaphoreUnlock(PGSemaphore sema);
57
58/* Lock a semaphore only if able to do so without blocking */
59extern bool PGSemaphoreTryLock(PGSemaphore sema);
60
61#endif /* PG_SEMA_H */
62