Initializes a semaphore object with the specified initial count.
A destructor is present on this object, but not explicitly documented in the source.
Atomically increment the current count by one. This will notify one waiter, if there are any in the queue.
If the current count is equal to zero, return. Otherwise, atomically decrement the count by one and return true.
Wait until the current count is above zero, then atomically decrement the count by one and return.
Suspends the calling thread until the current count moves above zero or until the supplied time period has elapsed. If the count moves above zero in this interval, then atomically decrement the count by one and return true. Otherwise, return false.
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
1 import core.thread, core.atomic; 2 3 void testWait() 4 { 5 auto semaphore = new Semaphore; 6 shared bool stopConsumption = false; 7 immutable numToProduce = 20; 8 immutable numConsumers = 10; 9 shared size_t numConsumed; 10 shared size_t numComplete; 11 12 void consumer() 13 { 14 while (true) 15 { 16 semaphore.wait(); 17 18 if (atomicLoad(stopConsumption)) 19 break; 20 atomicOp!"+="(numConsumed, 1); 21 } 22 atomicOp!"+="(numComplete, 1); 23 } 24 25 void producer() 26 { 27 assert(!semaphore.tryWait()); 28 29 foreach (_; 0 .. numToProduce) 30 semaphore.notify(); 31 32 // wait until all items are consumed 33 while (atomicLoad(numConsumed) != numToProduce) 34 Thread.yield(); 35 36 // mark consumption as finished 37 atomicStore(stopConsumption, true); 38 39 // wake all consumers 40 foreach (_; 0 .. numConsumers) 41 semaphore.notify(); 42 43 // wait until all consumers completed 44 while (atomicLoad(numComplete) != numConsumers) 45 Thread.yield(); 46 47 assert(!semaphore.tryWait()); 48 semaphore.notify(); 49 assert(semaphore.tryWait()); 50 assert(!semaphore.tryWait()); 51 } 52 53 auto group = new ThreadGroup; 54 55 for ( int i = 0; i < numConsumers; ++i ) 56 group.create(&consumer); 57 group.create(&producer); 58 group.joinAll(); 59 } 60 61 62 void testWaitTimeout() 63 { 64 auto sem = new Semaphore; 65 shared bool semReady; 66 bool alertedOne, alertedTwo; 67 68 void waiter() 69 { 70 while (!atomicLoad(semReady)) 71 Thread.yield(); 72 alertedOne = sem.wait(dur!"msecs"(1)); 73 alertedTwo = sem.wait(dur!"msecs"(1)); 74 assert(alertedOne && !alertedTwo); 75 } 76 77 auto thread = new Thread(&waiter); 78 thread.start(); 79 80 sem.notify(); 81 atomicStore(semReady, true); 82 thread.join(); 83 assert(alertedOne && !alertedTwo); 84 } 85 86 testWait(); 87 testWaitTimeout();
This class represents a general counting semaphore as concieved by Edsger Dijkstra. As per Mesa type monitors however, "signal" has been replaced with "notify" to indicate that control is not transferred to the waiter when a notification is sent.