@@ -13,7 +13,122 @@ module core.sync.event;
1313
1414import core.time ;
1515
16- import core.sync.event2 ;
16+ struct EventAwaiter
17+ {
18+ nothrow @nogc :
19+ /**
20+ * Creates an event object.
21+ *
22+ * Params:
23+ * manualReset = the state of the event is not reset automatically after resuming waiting clients
24+ * initialState = initial state of the signal
25+ */
26+ this (bool manualReset, bool initialState)
27+ {
28+ osEvent = OsEvent(manualReset, initialState);
29+ }
30+
31+ // copying not allowed, can produce resource leaks
32+ @disable this (this );
33+
34+ ref EventAwaiter opAssign (return scope EventAwaiter s) @live
35+ {
36+ this .osEvent = s.osEvent;
37+
38+ return this ;
39+ }
40+
41+ // / Set the event to "signaled", so that waiting clients are resumed
42+ void set ()
43+ {
44+ osEvent.set();
45+ }
46+
47+ // / Reset the event manually
48+ void reset ()
49+ {
50+ osEvent.reset();
51+ }
52+
53+ /**
54+ * Wait for the event to be signaled without timeout.
55+ *
56+ * Returns:
57+ * `true` if the event is in signaled state, `false` if the event is uninitialized or another error occured
58+ */
59+ bool wait ()
60+ {
61+ return osEvent.wait();
62+ }
63+
64+ /**
65+ * Wait for the event to be signaled with timeout.
66+ *
67+ * Params:
68+ * tmout = the maximum time to wait
69+ * Returns:
70+ * `true` if the event is in signaled state, `false` if the event was nonsignaled for the given time or
71+ * the event is uninitialized or another error occured
72+ */
73+ bool wait (Duration tmout)
74+ {
75+ return osEvent.wait(tmout);
76+ }
77+
78+ private :
79+ import rt.sys.config;
80+
81+ mixin (" import " ~ osEventImport ~ " ;" );
82+ OsEvent osEvent;
83+ }
84+
85+ // Test single-thread (non-shared) use.
86+ @nogc nothrow unittest
87+ {
88+ // auto-reset, initial state false
89+ EventAwaiter ev1 = EventAwaiter(false , false );
90+ assert (! ev1.wait(1. dur! " msecs" ));
91+ ev1.set();
92+ assert (ev1.wait());
93+ assert (! ev1.wait(1. dur! " msecs" ));
94+
95+ // manual-reset, initial state true
96+ EventAwaiter ev2 = EventAwaiter(true , true );
97+ assert (ev2.wait());
98+ assert (ev2.wait());
99+ ev2.reset();
100+ assert (! ev2.wait(1. dur! " msecs" ));
101+ }
102+
103+ unittest
104+ {
105+ import core.thread , core.atomic ;
106+
107+ scope event = new EventAwaiter(true , false );
108+ int numThreads = 10 ;
109+ shared int numRunning = 0 ;
110+
111+ void testFn ()
112+ {
113+ event.wait(8. dur! " seconds" ); // timeout below limit for druntime test_runner
114+ numRunning.atomicOp! " +=" (1 );
115+ }
116+
117+ auto group = new ThreadGroup ;
118+
119+ for (int i = 0 ; i < numThreads; ++ i)
120+ group.create(&testFn);
121+
122+ auto start = MonoTime.currTime;
123+ assert (numRunning == 0 );
124+
125+ event.set();
126+ group.joinAll();
127+
128+ assert (numRunning == numThreads);
129+
130+ assert (MonoTime.currTime - start < 5. dur! " seconds" );
131+ }
17132
18133/**
19134 * represents an event. Clients of an event are suspended while waiting
@@ -51,7 +166,7 @@ struct ProcessFile
51166}
52167---
53168 */
54- deprecated (" Please use core.sync.event2.Event2 instead" )
169+ deprecated (" Please use core.sync.event.EventAwaiter instead" )
55170struct Event
56171{
57172nothrow @nogc :
@@ -76,7 +191,7 @@ nothrow @nogc:
76191 */
77192 void initialize (bool manualReset, bool initialState) @live
78193 {
79- osEvent = Event2 (manualReset, initialState);
194+ osEvent = EventAwaiter (manualReset, initialState);
80195 m_initalized = true ;
81196 }
82197
@@ -150,7 +265,7 @@ nothrow @nogc:
150265 }
151266
152267private :
153- Event2 osEvent;
268+ EventAwaiter osEvent;
154269 bool m_initalized;
155270}
156271
0 commit comments