pg_sema.h 2.1 KB

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