pg_sema.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. * PGSemaphoreData and pointer type PGSemaphore are the data structure
  24. * representing an individual semaphore. The contents of PGSemaphoreData
  25. * vary across implementations and must never be touched by platform-
  26. * independent code. PGSemaphoreData structures are always allocated
  27. * in shared memory (to support implementations where the data changes during
  28. * lock/unlock).
  29. *
  30. * pg_config.h must define exactly one of the USE_xxx_SEMAPHORES symbols.
  31. */
  32. #ifdef USE_NAMED_POSIX_SEMAPHORES
  33. #include <semaphore.h>
  34. typedef sem_t *PGSemaphoreData;
  35. #endif
  36. #ifdef USE_UNNAMED_POSIX_SEMAPHORES
  37. #include <semaphore.h>
  38. typedef sem_t PGSemaphoreData;
  39. #endif
  40. #ifdef USE_SYSV_SEMAPHORES
  41. typedef struct PGSemaphoreData
  42. {
  43. int semId; /* semaphore set identifier */
  44. int semNum; /* semaphore number within set */
  45. } PGSemaphoreData;
  46. #endif
  47. #ifdef USE_WIN32_SEMAPHORES
  48. typedef HANDLE PGSemaphoreData;
  49. #endif
  50. typedef PGSemaphoreData *PGSemaphore;
  51. /* Module initialization (called during postmaster start or shmem reinit) */
  52. extern void PGReserveSemaphores(int maxSemas, int port);
  53. /* Initialize a PGSemaphore structure to represent a sema with count 1 */
  54. extern void PGSemaphoreCreate(PGSemaphore sema);
  55. /* Reset a previously-initialized PGSemaphore to have count 0 */
  56. extern void PGSemaphoreReset(PGSemaphore sema);
  57. /* Lock a semaphore (decrement count), blocking if count would be < 0 */
  58. extern void PGSemaphoreLock(PGSemaphore sema);
  59. /* Unlock a semaphore (increment count) */
  60. extern void PGSemaphoreUnlock(PGSemaphore sema);
  61. /* Lock a semaphore only if able to do so without blocking */
  62. extern bool PGSemaphoreTryLock(PGSemaphore sema);
  63. #endif /* PG_SEMA_H */