Debian Codegraph @

libphobos/libdruntime/rt/monitor_.d

raw
1 /**
2 * Contains the implementation for object monitors.
3 *
4 * Copyright: Copyright Digital Mars 2000 - 2015.
5 * License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 * Authors: Walter Bright, Sean Kelly, Martin Nowak
7 * Source: $(DRUNTIMESRC rt/_monitor_.d)
8 */
9 module rt.monitor_;
10
11 import core.atomic;
12 import core.stdc.stdlib : calloc, free, realloc;
13 import core.stdc.string : memmove;
14
15 version (Windows)
16 {
17 import core.sys.windows.winbase /+: CRITICAL_SECTION, DeleteCriticalSection,
18 EnterCriticalSection, InitializeCriticalSection, LeaveCriticalSection+/;
19 }
20 else version (Posix)
21 {
22 import core.sys.posix.pthread : pthread_mutex_destroy, pthread_mutex_init, pthread_mutex_lock,
23 PTHREAD_MUTEX_RECURSIVE, pthread_mutex_unlock, pthread_mutexattr_destroy, pthread_mutexattr_init,
24 pthread_mutexattr_settype;
25 import core.sys.posix.sys.types : pthread_mutex_t, pthread_mutexattr_t;
26 }
27 else
28 {
29 static assert(0, "Unsupported platform");
30 }
31
32 // NOTE: The dtor callback feature is only supported for monitors that are not
33 // supplied by the user. The assumption is that any object with a user-
34 // supplied monitor may have special storage or lifetime requirements and
35 // that as a result, storing references to local objects within Monitor
36 // may not be safe or desirable. Thus, devt is only valid if impl is
37 // null.
38
39 extern (C) void _d_setSameMutex(shared Object ownee, shared Object owner) nothrow
40 in
41 {
42 assert(ownee.__monitor is null);
43 }
44 do
45 {
46 auto m = ensureMonitor(cast(Object) owner);
47 if (m.impl is null)
48 {
49 atomicOp!"+="(m.refs, size_t(1));
50 }
51 // Assume the monitor is garbage collected and simply copy the reference.
52 ownee.__monitor = owner.__monitor;
53 }
54
55 extern (C) void _d_monitordelete(Object h, bool det)
56 {
57 auto m = getMonitor(h);
58 if (m is null)
59 return;
60
61 if (m.impl)
62 {
63 // let the GC collect the monitor
64 setMonitor(h, null);
65 }
66 else if (!atomicOp!"-="(m.refs, size_t(1)))
67 {
68 // refcount == 0 means unshared => no synchronization required
69 disposeEvent(cast(Monitor*) m, h);
70 deleteMonitor(cast(Monitor*) m);
71 setMonitor(h, null);
72 }
73 }
74
75 // does not call dispose events, for internal use only
76 extern (C) void _d_monitordelete_nogc(Object h) @nogc nothrow
77 {
78 auto m = getMonitor(h);
79 if (m is null)
80 return;
81
82 if (m.impl)
83 {
84 // let the GC collect the monitor
85 setMonitor(h, null);
86 }
87 else if (!atomicOp!"-="(m.refs, size_t(1)))
88 {
89 // refcount == 0 means unshared => no synchronization required
90 deleteMonitor(cast(Monitor*) m);
91 setMonitor(h, null);
92 }
93 }
94
95 extern (C) void _d_monitorenter(Object h)
96 in
97 {
98 assert(h !is null, "Synchronized object must not be null.");
99 }
100 do
101 {
102 auto m = cast(Monitor*) ensureMonitor(h);
103 auto i = m.impl;
104 if (i is null)
105 lockMutex(&m.mtx);
106 else
107 i.lock();
108 }
109
110 extern (C) void _d_monitorexit(Object h)
111 {
112 auto m = cast(Monitor*) getMonitor(h);
113 auto i = m.impl;
114 if (i is null)
115 unlockMutex(&m.mtx);
116 else
117 i.unlock();
118 }
119
120 extern (C) void rt_attachDisposeEvent(Object h, DEvent e)
121 {
122 synchronized (h)
123 {
124 auto m = cast(Monitor*) getMonitor(h);
125 assert(m.impl is null);
126
127 foreach (ref v; m.devt)
128 {
129 if (v is null || v == e)
130 {
131 v = e;
132 return;
133 }
134 }
135
136 auto len = m.devt.length + 4; // grow by 4 elements
137 auto pos = m.devt.length; // insert position
138 auto p = realloc(m.devt.ptr, DEvent.sizeof * len);
139 import core.exception : onOutOfMemoryError;
140
141 if (!p)
142 onOutOfMemoryError();
143 m.devt = (cast(DEvent*) p)[0 .. len];
144 m.devt[pos + 1 .. len] = null;
145 m.devt[pos] = e;
146 }
147 }
148
149 extern (C) void rt_detachDisposeEvent(Object h, DEvent e)
150 {
151 synchronized (h)
152 {
153 auto m = cast(Monitor*) getMonitor(h);
154 assert(m.impl is null);
155
156 foreach (p, v; m.devt)
157 {
158 if (v == e)
159 {
160 memmove(&m.devt[p], &m.devt[p + 1], (m.devt.length - p - 1) * DEvent.sizeof);
161 m.devt[$ - 1] = null;
162 return;
163 }
164 }
165 }
166 }
167
168 nothrow:
169
170 extern (C) void _d_monitor_staticctor() @nogc nothrow
171 {
172 version (Posix)
173 {
174 pthread_mutexattr_init(&gattr);
175 pthread_mutexattr_settype(&gattr, PTHREAD_MUTEX_RECURSIVE);
176 }
177 initMutex(&gmtx);
178 }
179
180 extern (C) void _d_monitor_staticdtor() @nogc nothrow
181 {
182 destroyMutex(&gmtx);
183 version (Posix)
184 pthread_mutexattr_destroy(&gattr);
185 }
186
187 package:
188
189 // This is what the monitor reference in Object points to
190 alias IMonitor = Object.Monitor;
191 alias DEvent = void delegate(Object);
192
193 version (Windows)
194 {
195 alias Mutex = CRITICAL_SECTION;
196
197 alias initMutex = InitializeCriticalSection;
198 alias destroyMutex = DeleteCriticalSection;
199 alias lockMutex = EnterCriticalSection;
200 alias unlockMutex = LeaveCriticalSection;
201 }
202 else version (Posix)
203 {
204 @nogc:
205 alias Mutex = pthread_mutex_t;
206 __gshared pthread_mutexattr_t gattr;
207
208 void initMutex(pthread_mutex_t* mtx)
209 {
210 pthread_mutex_init(mtx, &gattr) && assert(0);
211 }
212
213 void destroyMutex(pthread_mutex_t* mtx)
214 {
215 pthread_mutex_destroy(mtx) && assert(0);
216 }
217
218 void lockMutex(pthread_mutex_t* mtx)
219 {
220 pthread_mutex_lock(mtx) && assert(0);
221 }
222
223 void unlockMutex(pthread_mutex_t* mtx)
224 {
225 pthread_mutex_unlock(mtx) && assert(0);
226 }
227 }
228
229 struct Monitor
230 {
231 IMonitor impl; // for user-level monitors
232 DEvent[] devt; // for internal monitors
233 size_t refs; // reference count
234 Mutex mtx;
235 }
236
237 private:
238
239 __gshared Mutex gmtx;
240
241 @property ref shared(Monitor*) monitor(return scope Object h) pure nothrow @nogc
242 {
243 return *cast(shared Monitor**)&h.__monitor;
244 }
245
246 shared(Monitor)* getMonitor(Object h) pure @nogc
247 {
248 return atomicLoad!(MemoryOrder.acq)(h.monitor);
249 }
250
251 void setMonitor(Object h, shared(Monitor)* m) pure @nogc
252 {
253 atomicStore!(MemoryOrder.rel)(h.monitor, m);
254 }
255
256 shared(Monitor)* ensureMonitor(Object h)
257 {
258 if (auto m = getMonitor(h))
259 return m;
260
261 auto m = cast(Monitor*) calloc(Monitor.sizeof, 1);
262 assert(m);
263 initMutex(&m.mtx);
264
265 bool success;
266 lockMutex(&gmtx);
267 if (getMonitor(h) is null)
268 {
269 m.refs = 1;
270 setMonitor(h, cast(shared) m);
271 success = true;
272 }
273 unlockMutex(&gmtx);
274
275 if (success)
276 {
277 // Set the finalize bit so that the monitor gets collected (Bugzilla 14573)
278 import core.memory : GC;
279
280 if (!(typeid(h).m_flags & TypeInfo_Class.ClassFlags.hasDtor))
281 GC.setAttr(cast(void*) h, GC.BlkAttr.FINALIZE);
282 return cast(shared(Monitor)*) m;
283 }
284 else // another thread succeeded instead
285 {
286 deleteMonitor(m);
287 return getMonitor(h);
288 }
289 }
290
291 void deleteMonitor(Monitor* m) @nogc
292 {
293 destroyMutex(&m.mtx);
294 free(m);
295 }
296
297 void disposeEvent(Monitor* m, Object h)
298 {
299 foreach (v; m.devt)
300 {
301 if (v)
302 v(h);
303 }
304 if (m.devt.ptr)
305 free(m.devt.ptr);
306 }
307
308 // Bugzilla 14573
309 unittest
310 {
311 import core.memory : GC;
312
313 auto obj = new Object;
314 assert(!(GC.getAttr(cast(void*) obj) & GC.BlkAttr.FINALIZE));
315 ensureMonitor(obj);
316 assert(getMonitor(obj) !is null);
317 assert(GC.getAttr(cast(void*) obj) & GC.BlkAttr.FINALIZE);
318 }