dispatch_semaphore_create.3 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. .\" Copyright (c) 2008-2012 Apple Inc. All rights reserved.
  2. .Dd May 1, 2009
  3. .Dt dispatch_semaphore_create 3
  4. .Os Darwin
  5. .Sh NAME
  6. .Nm dispatch_semaphore_create ,
  7. .Nm dispatch_semaphore_signal ,
  8. .Nm dispatch_semaphore_wait
  9. .Nd synchronized counting semaphore
  10. .Sh SYNOPSIS
  11. .Fd #include <dispatch/dispatch.h>
  12. .Ft dispatch_semaphore_t
  13. .Fo dispatch_semaphore_create
  14. .Fa "long count"
  15. .Fc
  16. .Ft long
  17. .Fo dispatch_semaphore_signal
  18. .Fa "dispatch_semaphore_t semaphore"
  19. .Fc
  20. .Ft long
  21. .Fo dispatch_semaphore_wait
  22. .Fa "dispatch_semaphore_t semaphore" "dispatch_time_t timeout"
  23. .Fc
  24. .Sh DESCRIPTION
  25. Dispatch semaphores are used to synchronize threads.
  26. .Pp
  27. The
  28. .Fn dispatch_semaphore_wait
  29. function decrements the semaphore. If the resulting value is less than zero,
  30. it waits for a signal from a thread that increments the semaphore by calling
  31. .Fn dispatch_semaphore_signal
  32. before returning.
  33. The
  34. .Fa timeout
  35. parameter is creatable with the
  36. .Xr dispatch_time 3
  37. or
  38. .Xr dispatch_walltime 3
  39. functions. If the timeout is reached without a signal being received, the semaphore
  40. is re-incremented before the function returns.
  41. .Pp
  42. The
  43. .Fn dispatch_semaphore_signal
  44. function increments the counting semaphore. If the previous value was less than zero,
  45. it wakes one of the threads that are waiting in
  46. .Fn dispatch_semaphore_wait
  47. before returning.
  48. .Sh COMPLETION SYNCHRONIZATION
  49. If the
  50. .Fa count
  51. parameter is equal to zero, then the semaphore is useful for synchronizing
  52. completion of work.
  53. For example:
  54. .Bd -literal -offset indent
  55. sema = dispatch_semaphore_create(0);
  56. dispatch_async(queue, ^{
  57. foo();
  58. dispatch_semaphore_signal(sema);
  59. });
  60. bar();
  61. dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
  62. .Ed
  63. .Sh FINITE RESOURCE POOL
  64. If the
  65. .Fa count
  66. parameter is greater than zero, then the semaphore is useful for managing a
  67. finite pool of resources.
  68. For example, a library that wants to limit Unix descriptor usage:
  69. .Bd -literal -offset indent
  70. sema = dispatch_semaphore_create(getdtablesize() / 4);
  71. .Ed
  72. .Pp
  73. At each Unix FD allocation:
  74. .Bd -literal -offset indent
  75. dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
  76. fd = open("/etc/services", O_RDONLY);
  77. .Ed
  78. .Pp
  79. When each FD is closed:
  80. .Bd -literal -offset indent
  81. close(fd);
  82. dispatch_semaphore_signal(sema);
  83. .Ed
  84. .Sh RETURN VALUES
  85. The
  86. .Fn dispatch_semaphore_create
  87. function returns NULL if no memory is available or if the
  88. .Fa count
  89. parameter is less than zero.
  90. .Pp
  91. The
  92. .Fn dispatch_semaphore_signal
  93. function returns non-zero when a thread is woken.
  94. Otherwise, zero is returned.
  95. .Pp
  96. The
  97. .Fn dispatch_semaphore_wait
  98. function returns zero upon success and non-zero after the timeout expires. If
  99. the timeout is DISPATCH_TIME_FOREVER, then
  100. .Fn dispatch_semaphore_wait
  101. waits forever and always returns zero.
  102. .Sh MEMORY MODEL
  103. Dispatch semaphores are retained and released via calls to
  104. .Fn dispatch_retain
  105. and
  106. .Fn dispatch_release .
  107. .Sh CAVEATS
  108. Unbalanced dispatch semaphores cannot be released.
  109. For a given semaphore, calls to
  110. .Fn dispatch_semaphore_signal
  111. and
  112. .Fn dispatch_semaphore_wait
  113. must be balanced before
  114. .Fn dispatch_release
  115. is called on it.
  116. .Sh SEE ALSO
  117. .Xr dispatch 3 ,
  118. .Xr dispatch_object 3