pripcsem.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * File: pripcsem.h
  7. *
  8. * Description: named semaphores for interprocess
  9. * synchronization
  10. *
  11. * Unrelated processes obtain access to a shared semaphore
  12. * by specifying its name.
  13. *
  14. * Our goal is to support named semaphores on at least
  15. * Unix and Win32 platforms. The implementation will use
  16. * one of the three native semaphore APIs: POSIX, System V,
  17. * and Win32.
  18. *
  19. * Because POSIX named semaphores have kernel persistence,
  20. * we are forced to have a delete function in this API.
  21. */
  22. #ifndef pripcsem_h___
  23. #define pripcsem_h___
  24. #include "prtypes.h"
  25. #include "prio.h"
  26. PR_BEGIN_EXTERN_C
  27. /*
  28. * PRSem is an opaque structure that represents a named
  29. * semaphore.
  30. */
  31. typedef struct PRSem PRSem;
  32. /*
  33. * PR_OpenSemaphore --
  34. *
  35. * Create or open a named semaphore with the specified name.
  36. * A handle to the semaphore is returned.
  37. *
  38. * If the named semaphore doesn't exist and the PR_SEM_CREATE
  39. * flag is specified, the named semaphore is created. The
  40. * created semaphore needs to be removed from the system with
  41. * a PR_DeleteSemaphore call.
  42. *
  43. * If PR_SEM_CREATE is specified, the third argument is the
  44. * access permission bits of the new semaphore (same
  45. * interpretation as the mode argument to PR_Open) and the
  46. * fourth argument is the initial value of the new semaphore.
  47. * If PR_SEM_CREATE is not specified, the third and fourth
  48. * arguments are ignored.
  49. */
  50. #define PR_SEM_CREATE 0x1 /* create if not exist */
  51. #define PR_SEM_EXCL 0x2 /* fail if already exists */
  52. NSPR_API(PRSem *) PR_OpenSemaphore(
  53. const char *name, PRIntn flags, PRIntn mode, PRUintn value);
  54. /*
  55. * PR_WaitSemaphore --
  56. *
  57. * If the value of the semaphore is > 0, decrement the value and return.
  58. * If the value is 0, sleep until the value becomes > 0, then decrement
  59. * the value and return.
  60. *
  61. * The "test and decrement" operation is performed atomically.
  62. */
  63. NSPR_API(PRStatus) PR_WaitSemaphore(PRSem *sem);
  64. /*
  65. * PR_PostSemaphore --
  66. *
  67. * Increment the value of the named semaphore by 1.
  68. */
  69. NSPR_API(PRStatus) PR_PostSemaphore(PRSem *sem);
  70. /*
  71. * PR_CloseSemaphore --
  72. *
  73. * Close a named semaphore handle.
  74. */
  75. NSPR_API(PRStatus) PR_CloseSemaphore(PRSem *sem);
  76. /*
  77. * PR_DeleteSemaphore --
  78. *
  79. * Remove a named semaphore from the system.
  80. */
  81. NSPR_API(PRStatus) PR_DeleteSemaphore(const char *name);
  82. PR_END_EXTERN_C
  83. #endif /* pripcsem_h___ */