Skip to content

Commit 4f59ece

Browse files
committed
m_initalized flag moved to core.sync.event.Event
This flag now is redundant for Windows platform
1 parent 0655f30 commit 4f59ece

File tree

3 files changed

+35
-50
lines changed

3 files changed

+35
-50
lines changed

druntime/src/core/sync/event.d

+14-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ nothrow @nogc:
7575
*/
7676
void initialize(bool manualReset, bool initialState)
7777
{
78-
osEvent.create(manualReset, initialState);
78+
osEvent = OsEvent(manualReset, initialState);
79+
m_initalized = true;
7980
}
8081

8182
// copying not allowed, can produce resource leaks
@@ -85,6 +86,7 @@ nothrow @nogc:
8586
~this()
8687
{
8788
terminate();
89+
m_initalized = false;
8890
}
8991

9092
/**
@@ -104,13 +106,15 @@ nothrow @nogc:
104106
/// Set the event to "signaled", so that waiting clients are resumed
105107
void setIfInitialized()
106108
{
107-
osEvent.setIfInitialized();
109+
if(m_initalized)
110+
osEvent.set();
108111
}
109112

110113
/// Reset the event manually
111114
void reset()
112115
{
113-
osEvent.reset();
116+
if(m_initalized)
117+
osEvent.reset();
114118
}
115119

116120
/**
@@ -121,6 +125,9 @@ nothrow @nogc:
121125
*/
122126
bool wait()
123127
{
128+
if (!m_initalized)
129+
return false;
130+
124131
return osEvent.wait();
125132
}
126133

@@ -135,12 +142,16 @@ nothrow @nogc:
135142
*/
136143
bool wait(Duration tmout)
137144
{
145+
if (!m_initalized)
146+
return false;
147+
138148
return osEvent.wait(tmout);
139149
}
140150

141151
private:
142152
mixin("import " ~ osEventImport ~ ";");
143153
OsEvent osEvent;
154+
bool m_initalized;
144155
}
145156

146157
// Test single-thread (non-shared) use.

druntime/src/rt/sys/posix/osevent.d

+14-31
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import core.internal.abort : abort;
1111

1212
struct OsEvent
1313
{
14-
void create(bool manualReset, bool initialState) nothrow @trusted @nogc
14+
this(bool manualReset, bool initialState) nothrow @trusted @nogc
1515
{
16-
if (m_initalized)
17-
return;
1816
pthread_mutex_init(cast(pthread_mutex_t*) &m_mutex, null) == 0 ||
1917
abort("Error: pthread_mutex_init failed.");
2018
static if ( is( typeof( pthread_condattr_setclock ) ) )
@@ -37,40 +35,29 @@ struct OsEvent
3735

3836
m_state = initialState;
3937
m_manualReset = manualReset;
40-
m_initalized = true;
4138
}
4239

43-
void destroy() nothrow @trusted @nogc
40+
~this() nothrow @trusted @nogc
4441
{
45-
if (m_initalized)
46-
{
47-
pthread_mutex_destroy(&m_mutex) == 0 ||
48-
abort("Error: pthread_mutex_destroy failed.");
49-
pthread_cond_destroy(&m_cond) == 0 ||
50-
abort("Error: pthread_cond_destroy failed.");
51-
m_initalized = false;
52-
}
42+
pthread_mutex_destroy(&m_mutex) == 0 ||
43+
abort("Error: pthread_mutex_destroy failed.");
44+
pthread_cond_destroy(&m_cond) == 0 ||
45+
abort("Error: pthread_cond_destroy failed.");
5346
}
5447

55-
void setIfInitialized() nothrow @trusted @nogc
48+
void set() nothrow @trusted @nogc
5649
{
57-
if (m_initalized)
58-
{
59-
pthread_mutex_lock(&m_mutex);
60-
m_state = true;
61-
pthread_cond_broadcast(&m_cond);
62-
pthread_mutex_unlock(&m_mutex);
63-
}
50+
pthread_mutex_lock(&m_mutex);
51+
m_state = true;
52+
pthread_cond_broadcast(&m_cond);
53+
pthread_mutex_unlock(&m_mutex);
6454
}
6555

6656
void reset() nothrow @trusted @nogc
6757
{
68-
if (m_initalized)
69-
{
70-
pthread_mutex_lock(&m_mutex);
71-
m_state = false;
72-
pthread_mutex_unlock(&m_mutex);
73-
}
58+
pthread_mutex_lock(&m_mutex);
59+
m_state = false;
60+
pthread_mutex_unlock(&m_mutex);
7461
}
7562

7663
bool wait() nothrow @trusted @nogc
@@ -80,9 +67,6 @@ struct OsEvent
8067

8168
bool wait(Duration tmout) nothrow @trusted @nogc
8269
{
83-
if (!m_initalized)
84-
return false;
85-
8670
pthread_mutex_lock(&m_mutex);
8771

8872
int result = 0;
@@ -114,7 +98,6 @@ private:
11498

11599
pthread_mutex_t m_mutex;
116100
pthread_cond_t m_cond;
117-
bool m_initalized;
118101
bool m_state;
119102
bool m_manualReset;
120103
}

druntime/src/rt/sys/windows/osevent.d

+7-16
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,34 @@ import core.internal.abort : abort;
1212

1313
struct OsEvent
1414
{
15-
void create(bool manualReset, bool initialState) nothrow @trusted @nogc
15+
this(bool manualReset, bool initialState) nothrow @trusted @nogc
1616
{
17-
if (m_event)
18-
return;
1917
m_event = CreateEvent(null, manualReset, initialState, null);
2018
m_event || abort("Error: CreateEvent failed.");
2119
}
2220

23-
void destroy() nothrow @trusted @nogc
21+
~this() nothrow @trusted @nogc
2422
{
25-
if (m_event)
26-
CloseHandle(m_event);
27-
m_event = null;
23+
CloseHandle(m_event);
2824
}
2925

30-
void setIfInitialized() nothrow @trusted @nogc
26+
void set() nothrow @trusted @nogc
3127
{
32-
if (m_event)
33-
SetEvent(m_event);
28+
SetEvent(m_event);
3429
}
3530

3631
void reset() nothrow @trusted @nogc
3732
{
38-
if (m_event)
39-
ResetEvent(m_event);
33+
ResetEvent(m_event);
4034
}
4135

4236
bool wait() nothrow @trusted @nogc
4337
{
44-
return m_event && WaitForSingleObject(m_event, INFINITE) == WAIT_OBJECT_0;
38+
return WaitForSingleObject(m_event, INFINITE) == WAIT_OBJECT_0;
4539
}
4640

4741
bool wait(Duration tmout) nothrow @trusted @nogc
4842
{
49-
if (!m_event)
50-
return false;
51-
5243
auto maxWaitMillis = dur!("msecs")(uint.max - 1);
5344

5445
while (tmout > maxWaitMillis)

0 commit comments

Comments
 (0)