annotate druntime/src/compiler/dmd/monitor.c @ 759:d3eb054172f9

Added copy of druntime from DMD 2.020 modified for LDC.
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 11 Nov 2008 01:52:37 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
759
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
1 // D programming language runtime library
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
2 // Public Domain
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
3 // written by Walter Bright, Digital Mars
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
4 // www.digitalmars.com
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
5
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
6 // This is written in C because nobody has written a pthreads interface
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
7 // to D yet.
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
8
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
9
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
10 #include <stdio.h>
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
11 #include <stdlib.h>
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
12 #include <assert.h>
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
13
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
14 #if _WIN32
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
15 #elif linux
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
16 #define USE_PTHREADS 1
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
17 #else
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
18 #endif
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
19
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
20 #if _WIN32
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
21 #include <windows.h>
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
22 #endif
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
23
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
24 #if USE_PTHREADS
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
25 #include <pthread.h>
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
26 #endif
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
27
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
28 #include "mars.h"
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
29
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
30 // This is what the monitor reference in Object points to
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
31 typedef struct Monitor
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
32 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
33 void* impl; // for user-level monitors
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
34 Array devt; // for internal monitors
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
35
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
36 #if _WIN32
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
37 CRITICAL_SECTION mon;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
38 #endif
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
39
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
40 #if USE_PTHREADS
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
41 pthread_mutex_t mon;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
42 #endif
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
43 } Monitor;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
44
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
45 #define MONPTR(h) (&((Monitor *)(h)->monitor)->mon)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
46
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
47 static volatile int inited;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
48
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
49 /* =============================== Win32 ============================ */
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
50
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
51 #if _WIN32
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
52
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
53 static CRITICAL_SECTION _monitor_critsec;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
54
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
55 void _STI_monitor_staticctor()
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
56 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
57 if (!inited)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
58 { InitializeCriticalSection(&_monitor_critsec);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
59 inited = 1;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
60 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
61 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
62
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
63 void _STD_monitor_staticdtor()
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
64 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
65 if (inited)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
66 { inited = 0;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
67 DeleteCriticalSection(&_monitor_critsec);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
68 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
69 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
70
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
71 void _d_monitor_create(Object *h)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
72 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
73 /*
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
74 * NOTE: Assume this is only called when h->monitor is null prior to the
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
75 * call. However, please note that another thread may call this function
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
76 * at the same time, so we can not assert this here. Instead, try and
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
77 * create a lock, and if one already exists then forget about it.
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
78 */
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
79
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
80 //printf("+_d_monitor_create(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
81 assert(h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
82 Monitor *cs = NULL;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
83 EnterCriticalSection(&_monitor_critsec);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
84 if (!h->monitor)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
85 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
86 cs = (Monitor *)calloc(sizeof(Monitor), 1);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
87 assert(cs);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
88 InitializeCriticalSection(&cs->mon);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
89 h->monitor = (void *)cs;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
90 cs = NULL;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
91 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
92 LeaveCriticalSection(&_monitor_critsec);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
93 if (cs)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
94 free(cs);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
95 //printf("-_d_monitor_create(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
96 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
97
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
98 void _d_monitor_destroy(Object *h)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
99 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
100 //printf("+_d_monitor_destroy(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
101 assert(h && h->monitor && !(((Monitor*)h->monitor)->impl));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
102 DeleteCriticalSection(MONPTR(h));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
103 free((void *)h->monitor);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
104 h->monitor = NULL;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
105 //printf("-_d_monitor_destroy(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
106 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
107
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
108 int _d_monitor_lock(Object *h)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
109 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
110 //printf("+_d_monitor_acquire(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
111 assert(h && h->monitor && !(((Monitor*)h->monitor)->impl));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
112 EnterCriticalSection(MONPTR(h));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
113 //printf("-_d_monitor_acquire(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
114 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
115
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
116 void _d_monitor_unlock(Object *h)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
117 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
118 //printf("+_d_monitor_release(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
119 assert(h && h->monitor && !(((Monitor*)h->monitor)->impl));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
120 LeaveCriticalSection(MONPTR(h));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
121 //printf("-_d_monitor_release(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
122 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
123
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
124 #endif
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
125
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
126 /* =============================== linux ============================ */
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
127
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
128 #if USE_PTHREADS
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
129
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
130 // Includes attribute fixes from David Friedman's GDC port
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
131
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
132 static pthread_mutex_t _monitor_critsec;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
133 static pthread_mutexattr_t _monitors_attr;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
134
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
135 void _STI_monitor_staticctor()
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
136 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
137 if (!inited)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
138 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
139 pthread_mutexattr_init(&_monitors_attr);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
140 pthread_mutexattr_settype(&_monitors_attr, PTHREAD_MUTEX_RECURSIVE_NP);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
141 pthread_mutex_init(&_monitor_critsec, 0);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
142 inited = 1;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
143 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
144 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
145
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
146 void _STD_monitor_staticdtor()
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
147 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
148 if (inited)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
149 { inited = 0;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
150 pthread_mutex_destroy(&_monitor_critsec);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
151 pthread_mutexattr_destroy(&_monitors_attr);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
152 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
153 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
154
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
155 void _d_monitor_create(Object *h)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
156 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
157 /*
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
158 * NOTE: Assume this is only called when h->monitor is null prior to the
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
159 * call. However, please note that another thread may call this function
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
160 * at the same time, so we can not assert this here. Instead, try and
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
161 * create a lock, and if one already exists then forget about it.
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
162 */
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
163
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
164 //printf("+_d_monitor_create(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
165 assert(h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
166 Monitor *cs = NULL;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
167 pthread_mutex_lock(&_monitor_critsec);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
168 if (!h->monitor)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
169 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
170 cs = (Monitor *)calloc(sizeof(Monitor), 1);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
171 assert(cs);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
172 pthread_mutex_init(&cs->mon, & _monitors_attr);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
173 h->monitor = (void *)cs;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
174 cs = NULL;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
175 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
176 pthread_mutex_unlock(&_monitor_critsec);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
177 if (cs)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
178 free(cs);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
179 //printf("-_d_monitor_create(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
180 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
181
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
182 void _d_monitor_destroy(Object *h)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
183 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
184 //printf("+_d_monitor_destroy(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
185 assert(h && h->monitor && !(((Monitor*)h->monitor)->impl));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
186 pthread_mutex_destroy(MONPTR(h));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
187 free((void *)h->monitor);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
188 h->monitor = NULL;
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
189 //printf("-_d_monitor_destroy(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
190 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
191
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
192 int _d_monitor_lock(Object *h)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
193 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
194 //printf("+_d_monitor_acquire(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
195 assert(h && h->monitor && !(((Monitor*)h->monitor)->impl));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
196 pthread_mutex_lock(MONPTR(h));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
197 //printf("-_d_monitor_acquire(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
198 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
199
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
200 void _d_monitor_unlock(Object *h)
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
201 {
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
202 //printf("+_d_monitor_release(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
203 assert(h && h->monitor && !(((Monitor*)h->monitor)->impl));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
204 pthread_mutex_unlock(MONPTR(h));
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
205 //printf("-_d_monitor_release(%p)\n", h);
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
206 }
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
207
d3eb054172f9 Added copy of druntime from DMD 2.020 modified for LDC.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
diff changeset
208 #endif