semaphore.d 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /**
  2. * The semaphore module provides a general use semaphore for synchronization.
  3. *
  4. * Copyright: Copyright Sean Kelly 2005 - 2009.
  5. * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
  6. * Authors: Sean Kelly
  7. * Source: $(DRUNTIMESRC core/sync/_semaphore.d)
  8. */
  9. /* Copyright Sean Kelly 2005 - 2009.
  10. * Distributed under the Boost Software License, Version 1.0.
  11. * (See accompanying file LICENSE or copy at
  12. * http://www.boost.org/LICENSE_1_0.txt)
  13. */
  14. module core.sync.semaphore;
  15. public import core.sync.exception;
  16. public import core.time;
  17. version (OSX)
  18. version = Darwin;
  19. else version (iOS)
  20. version = Darwin;
  21. else version (TVOS)
  22. version = Darwin;
  23. else version (WatchOS)
  24. version = Darwin;
  25. version (Windows)
  26. {
  27. import core.sys.windows.basetsd /+: HANDLE+/;
  28. import core.sys.windows.winbase /+: CloseHandle, CreateSemaphoreA, INFINITE,
  29. ReleaseSemaphore, WAIT_OBJECT_0, WaitForSingleObject+/;
  30. import core.sys.windows.windef /+: BOOL, DWORD+/;
  31. import core.sys.windows.winerror /+: WAIT_TIMEOUT+/;
  32. }
  33. else version (Darwin)
  34. {
  35. import core.sync.config;
  36. import core.stdc.errno;
  37. import core.sys.posix.time;
  38. import core.sys.darwin.mach.semaphore;
  39. }
  40. else version (Posix)
  41. {
  42. import core.sync.config;
  43. import core.stdc.errno;
  44. import core.sys.posix.pthread;
  45. import core.sys.posix.semaphore;
  46. }
  47. else
  48. {
  49. static assert(false, "Platform not supported");
  50. }
  51. ////////////////////////////////////////////////////////////////////////////////
  52. // Semaphore
  53. //
  54. // void wait();
  55. // void notify();
  56. // bool tryWait();
  57. ////////////////////////////////////////////////////////////////////////////////
  58. /**
  59. * This class represents a general counting semaphore as concieved by Edsger
  60. * Dijkstra. As per Mesa type monitors however, "signal" has been replaced
  61. * with "notify" to indicate that control is not transferred to the waiter when
  62. * a notification is sent.
  63. */
  64. class Semaphore
  65. {
  66. ////////////////////////////////////////////////////////////////////////////
  67. // Initialization
  68. ////////////////////////////////////////////////////////////////////////////
  69. /**
  70. * Initializes a semaphore object with the specified initial count.
  71. *
  72. * Params:
  73. * count = The initial count for the semaphore.
  74. *
  75. * Throws:
  76. * SyncError on error.
  77. */
  78. this( uint count = 0 )
  79. {
  80. version (Windows)
  81. {
  82. m_hndl = CreateSemaphoreA( null, count, int.max, null );
  83. if ( m_hndl == m_hndl.init )
  84. throw new SyncError( "Unable to create semaphore" );
  85. }
  86. else version (Darwin)
  87. {
  88. auto rc = semaphore_create( mach_task_self(), &m_hndl, SYNC_POLICY_FIFO, count );
  89. if ( rc )
  90. throw new SyncError( "Unable to create semaphore" );
  91. }
  92. else version (Posix)
  93. {
  94. int rc = sem_init( &m_hndl, 0, count );
  95. if ( rc )
  96. throw new SyncError( "Unable to create semaphore" );
  97. }
  98. }
  99. ~this()
  100. {
  101. version (Windows)
  102. {
  103. BOOL rc = CloseHandle( m_hndl );
  104. assert( rc, "Unable to destroy semaphore" );
  105. }
  106. else version (Darwin)
  107. {
  108. auto rc = semaphore_destroy( mach_task_self(), m_hndl );
  109. assert( !rc, "Unable to destroy semaphore" );
  110. }
  111. else version (Posix)
  112. {
  113. int rc = sem_destroy( &m_hndl );
  114. assert( !rc, "Unable to destroy semaphore" );
  115. }
  116. }
  117. ////////////////////////////////////////////////////////////////////////////
  118. // General Actions
  119. ////////////////////////////////////////////////////////////////////////////
  120. /**
  121. * Wait until the current count is above zero, then atomically decrement
  122. * the count by one and return.
  123. *
  124. * Throws:
  125. * SyncError on error.
  126. */
  127. void wait()
  128. {
  129. version (Windows)
  130. {
  131. DWORD rc = WaitForSingleObject( m_hndl, INFINITE );
  132. if ( rc != WAIT_OBJECT_0 )
  133. throw new SyncError( "Unable to wait for semaphore" );
  134. }
  135. else version (Darwin)
  136. {
  137. while ( true )
  138. {
  139. auto rc = semaphore_wait( m_hndl );
  140. if ( !rc )
  141. return;
  142. if ( rc == KERN_ABORTED && errno == EINTR )
  143. continue;
  144. throw new SyncError( "Unable to wait for semaphore" );
  145. }
  146. }
  147. else version (Posix)
  148. {
  149. while ( true )
  150. {
  151. if ( !sem_wait( &m_hndl ) )
  152. return;
  153. if ( errno != EINTR )
  154. throw new SyncError( "Unable to wait for semaphore" );
  155. }
  156. }
  157. }
  158. /**
  159. * Suspends the calling thread until the current count moves above zero or
  160. * until the supplied time period has elapsed. If the count moves above
  161. * zero in this interval, then atomically decrement the count by one and
  162. * return true. Otherwise, return false.
  163. *
  164. * Params:
  165. * period = The time to wait.
  166. *
  167. * In:
  168. * period must be non-negative.
  169. *
  170. * Throws:
  171. * SyncError on error.
  172. *
  173. * Returns:
  174. * true if notified before the timeout and false if not.
  175. */
  176. bool wait( Duration period )
  177. in
  178. {
  179. assert( !period.isNegative );
  180. }
  181. do
  182. {
  183. version (Windows)
  184. {
  185. auto maxWaitMillis = dur!("msecs")( uint.max - 1 );
  186. while ( period > maxWaitMillis )
  187. {
  188. auto rc = WaitForSingleObject( m_hndl, cast(uint)
  189. maxWaitMillis.total!"msecs" );
  190. switch ( rc )
  191. {
  192. case WAIT_OBJECT_0:
  193. return true;
  194. case WAIT_TIMEOUT:
  195. period -= maxWaitMillis;
  196. continue;
  197. default:
  198. throw new SyncError( "Unable to wait for semaphore" );
  199. }
  200. }
  201. switch ( WaitForSingleObject( m_hndl, cast(uint) period.total!"msecs" ) )
  202. {
  203. case WAIT_OBJECT_0:
  204. return true;
  205. case WAIT_TIMEOUT:
  206. return false;
  207. default:
  208. throw new SyncError( "Unable to wait for semaphore" );
  209. }
  210. }
  211. else version (Darwin)
  212. {
  213. mach_timespec_t t = void;
  214. (cast(byte*) &t)[0 .. t.sizeof] = 0;
  215. if ( period.total!"seconds" > t.tv_sec.max )
  216. {
  217. t.tv_sec = t.tv_sec.max;
  218. t.tv_nsec = cast(typeof(t.tv_nsec)) period.split!("seconds", "nsecs")().nsecs;
  219. }
  220. else
  221. period.split!("seconds", "nsecs")(t.tv_sec, t.tv_nsec);
  222. while ( true )
  223. {
  224. auto rc = semaphore_timedwait( m_hndl, t );
  225. if ( !rc )
  226. return true;
  227. if ( rc == KERN_OPERATION_TIMED_OUT )
  228. return false;
  229. if ( rc != KERN_ABORTED || errno != EINTR )
  230. throw new SyncError( "Unable to wait for semaphore" );
  231. }
  232. }
  233. else version (Posix)
  234. {
  235. import core.sys.posix.time : clock_gettime, CLOCK_REALTIME;
  236. timespec t = void;
  237. clock_gettime( CLOCK_REALTIME, &t );
  238. mvtspec( t, period );
  239. while ( true )
  240. {
  241. if ( !sem_timedwait( &m_hndl, &t ) )
  242. return true;
  243. if ( errno == ETIMEDOUT )
  244. return false;
  245. if ( errno != EINTR )
  246. throw new SyncError( "Unable to wait for semaphore" );
  247. }
  248. }
  249. }
  250. /**
  251. * Atomically increment the current count by one. This will notify one
  252. * waiter, if there are any in the queue.
  253. *
  254. * Throws:
  255. * SyncError on error.
  256. */
  257. void notify()
  258. {
  259. version (Windows)
  260. {
  261. if ( !ReleaseSemaphore( m_hndl, 1, null ) )
  262. throw new SyncError( "Unable to notify semaphore" );
  263. }
  264. else version (Darwin)
  265. {
  266. auto rc = semaphore_signal( m_hndl );
  267. if ( rc )
  268. throw new SyncError( "Unable to notify semaphore" );
  269. }
  270. else version (Posix)
  271. {
  272. int rc = sem_post( &m_hndl );
  273. if ( rc )
  274. throw new SyncError( "Unable to notify semaphore" );
  275. }
  276. }
  277. /**
  278. * If the current count is equal to zero, return. Otherwise, atomically
  279. * decrement the count by one and return true.
  280. *
  281. * Throws:
  282. * SyncError on error.
  283. *
  284. * Returns:
  285. * true if the count was above zero and false if not.
  286. */
  287. bool tryWait()
  288. {
  289. version (Windows)
  290. {
  291. switch ( WaitForSingleObject( m_hndl, 0 ) )
  292. {
  293. case WAIT_OBJECT_0:
  294. return true;
  295. case WAIT_TIMEOUT:
  296. return false;
  297. default:
  298. throw new SyncError( "Unable to wait for semaphore" );
  299. }
  300. }
  301. else version (Darwin)
  302. {
  303. return wait( dur!"hnsecs"(0) );
  304. }
  305. else version (Posix)
  306. {
  307. while ( true )
  308. {
  309. if ( !sem_trywait( &m_hndl ) )
  310. return true;
  311. if ( errno == EAGAIN )
  312. return false;
  313. if ( errno != EINTR )
  314. throw new SyncError( "Unable to wait for semaphore" );
  315. }
  316. }
  317. }
  318. protected:
  319. /// Aliases the operating-system-specific semaphore type.
  320. version (Windows) alias Handle = HANDLE;
  321. /// ditto
  322. else version (Darwin) alias Handle = semaphore_t;
  323. /// ditto
  324. else version (Posix) alias Handle = sem_t;
  325. /// Handle to the system-specific semaphore.
  326. Handle m_hndl;
  327. }
  328. ////////////////////////////////////////////////////////////////////////////////
  329. // Unit Tests
  330. ////////////////////////////////////////////////////////////////////////////////
  331. unittest
  332. {
  333. import core.thread, core.atomic;
  334. void testWait()
  335. {
  336. auto semaphore = new Semaphore;
  337. shared bool stopConsumption = false;
  338. immutable numToProduce = 20;
  339. immutable numConsumers = 10;
  340. shared size_t numConsumed;
  341. shared size_t numComplete;
  342. void consumer()
  343. {
  344. while (true)
  345. {
  346. semaphore.wait();
  347. if (atomicLoad(stopConsumption))
  348. break;
  349. atomicOp!"+="(numConsumed, 1);
  350. }
  351. atomicOp!"+="(numComplete, 1);
  352. }
  353. void producer()
  354. {
  355. assert(!semaphore.tryWait());
  356. foreach (_; 0 .. numToProduce)
  357. semaphore.notify();
  358. // wait until all items are consumed
  359. while (atomicLoad(numConsumed) != numToProduce)
  360. Thread.yield();
  361. // mark consumption as finished
  362. atomicStore(stopConsumption, true);
  363. // wake all consumers
  364. foreach (_; 0 .. numConsumers)
  365. semaphore.notify();
  366. // wait until all consumers completed
  367. while (atomicLoad(numComplete) != numConsumers)
  368. Thread.yield();
  369. assert(!semaphore.tryWait());
  370. semaphore.notify();
  371. assert(semaphore.tryWait());
  372. assert(!semaphore.tryWait());
  373. }
  374. auto group = new ThreadGroup;
  375. for ( int i = 0; i < numConsumers; ++i )
  376. group.create(&consumer);
  377. group.create(&producer);
  378. group.joinAll();
  379. }
  380. void testWaitTimeout()
  381. {
  382. auto sem = new Semaphore;
  383. shared bool semReady;
  384. bool alertedOne, alertedTwo;
  385. void waiter()
  386. {
  387. while (!atomicLoad(semReady))
  388. Thread.yield();
  389. alertedOne = sem.wait(dur!"msecs"(1));
  390. alertedTwo = sem.wait(dur!"msecs"(1));
  391. assert(alertedOne && !alertedTwo);
  392. }
  393. auto thread = new Thread(&waiter);
  394. thread.start();
  395. sem.notify();
  396. atomicStore(semReady, true);
  397. thread.join();
  398. assert(alertedOne && !alertedTwo);
  399. }
  400. testWait();
  401. testWaitTimeout();
  402. }