-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpsee_monitors.c
246 lines (213 loc) · 7.67 KB
/
gpsee_monitors.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is PageMail, Inc.
*
* Portions created by the Initial Developer are
* Copyright (c) 2010, PageMail, Inc. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK *****
*
* @file gpsee_monitors.c Monitor-pattern concurrency for GPSEE.
*
* Auto monitors are monitors which are
* created on demand (by noticing that
* their containing pointer is NULL) and
* cleaned up automatically when the
* monitor system is shut down.
* @date May 2010
* @author Wes Garland
* @version $Id: gpsee_monitors.c,v 1.2 2011/12/05 19:13:36 wes Exp $
*
*/
#include "gpsee.h"
#include <prmon.h>
gpsee_monitor_t nilMonitor = (gpsee_monitor_t)"NIL Monitor - using this monitor is like running unlocked";
/** Initialize the monitor subsystem associated with the passed grt.
* This routine runs unlocked: it is the job of the caller to
* insure that no other threads are running.
*
* @see gpsee_createRuntime()
*
* @param cx The current JS context (used for throwing exceptions and gpsee_ds_create())
* @param grt The runtime whose monitor subsystem is to be initialized
* @returns JS_TRUE on success, JS_FALSE if we threw
*/
JSBool gpsee_initializeMonitorSystem(JSContext *cx, gpsee_runtime_t *grt)
{
#ifdef JS_THREADSAFE
grt->monitors.monitor = PR_NewMonitor();
if (!grt->monitors.monitor)
return gpsee_throw(cx, GPSEE_GLOBAL_NAMESPACE_NAME ".monitors.initialize: Could not initialize monitor subsystem");
/* Access to monitorList_unlocked is guarded by grt->monitors.monitor henceforth */
grt->monitorList_unlocked = gpsee_ds_create(NULL, GPSEE_DS_UNLOCKED, 5);
#else
grt->monitors.monitor = gpsee_getNilMonitor();
#endif
return JS_TRUE;
}
/**
* Create a monitor. Infallible.
*
* @param grt The GPSEE runtime that owns the monitor
* @param monitor_p A pointer to the monitor. This address must stay valid for the lifetime of the runtime.
*
* @returns The new monitor, or NULL if this was not a JS_THREADSAFE build.
*/
gpsee_monitor_t gpsee_createMonitor(gpsee_runtime_t *grt)
{
#ifdef JS_THREADSAFE
gpsee_monitor_t monitor;
GPSEE_ASSERT(grt->monitors.monitor);
PR_EnterMonitor(grt->monitors.monitor);
monitor = PR_NewMonitor();
gpsee_ds_put(grt->monitorList_unlocked, monitor, NULL);
PR_ExitMonitor(grt->monitors.monitor);
return monitor;
#else
return gpsee_getNilMonitor();
#endif
}
/**
* Get the nil monitor. The nil monitor is a special monitor which can be
* passed to gpsee_enterMonitor() and gpsee_leaveMonitor(), allowing them
* to run unlocked but without reporting an error:
* NULL: Uninitialized monitor
* nil: Complete absence of a monitor
*/
gpsee_monitor_t gpsee_getNilMonitor()
{
return nilMonitor;
}
/**
* Enter an auto-monitor (RAII). Infallible. Creates the monitor as-needed.
*
* @param cx A context belonging to the current runtime. Only used if we need to create
* the monitor, to find the GPSEE runtime pointer.
* @param monitor_p A pointer to the monitor. This address must stay valid for the lifetime of the runtime.
*
* @see gpsee_createMonitor(), gpsee_enterAutoMonitorRT();
*/
void gpsee_enterAutoMonitor(JSContext *cx, gpsee_autoMonitor_t *monitor_p)
{
gpsee_runtime_t *grt = JS_GetRuntimePrivate(JS_GetRuntime(cx));
gpsee_enterAutoMonitorRT(grt, monitor_p);
}
/**
* Enter an auto-monitor (RAII). Infallible. Creates the monitor as-needed.
*
* @param grt A pointer to the current GPSEE runtime.
* @param monitor_p A pointer to the monitor. This address must stay valid for the lifetime of the runtime.
*
* @see gpsee_createMonitor(), gpsee_enterAutoMonitor();
*/
void gpsee_enterAutoMonitorRT(gpsee_runtime_t *grt, gpsee_autoMonitor_t *monitor_p)
{
GPSEE_ASSERT(grt->monitors.monitor);
if (!*monitor_p)
*monitor_p = gpsee_createMonitor(grt);
gpsee_enterMonitor(*monitor_p);
}
/**
* Enter a monitor. Infallible
*
* @param monitor The monitor to enter.
*/
void gpsee_enterMonitor(gpsee_monitor_t monitor)
{
#ifdef JS_THREADSAFE
if (monitor == nilMonitor)
return;
PR_EnterMonitor(monitor);
#endif
}
/**
* Leave a monitor. Infallible.
*
* @param monitor The monitor to leave
*/
void gpsee_leaveMonitor(gpsee_monitor_t monitor)
{
GPSEE_ASSERT(monitor);
#ifdef JS_THREADSAFE
PRStatus status;
if (monitor == nilMonitor)
return;
status = PR_ExitMonitor(monitor);
GPSEE_ASSERT(status == PR_SUCCESS); /* An assertion failure here means we were not in the monitor */
#endif
}
/**
* Leave a monitor. Infallible.
*
* @param monitor The monitor to leave
*/
void gpsee_leaveAutoMonitor(gpsee_autoMonitor_t monitor)
{
gpsee_leaveMonitor(monitor);
}
/**
* Destroy a monitor that was created by gpsee_createMonitor(). It is the caller's
* responsibility to insure that no other threads may want to use the monitor while
* this routine is running.
*
* @param grt The GPSEE runtime which owns the monitor
* @param monitor The monitor to destroy
*/
void gpsee_destroyMonitor(gpsee_runtime_t *grt, gpsee_monitor_t monitor)
{
#ifdef JS_THREADSAFE
if (monitor == nilMonitor)
return;
PR_EnterMonitor(grt->monitors.monitor);
gpsee_ds_remove(grt->monitorList_unlocked, monitor);
PR_ExitMonitor(grt->monitors.monitor);
PR_DestroyMonitor(monitor);
#endif
}
#ifdef JS_THREADSAFE
static JSBool destroyMonitor_cb(JSContext *nullcx, const void *key, void *value, void *private)
{
gpsee_monitor_t monitor = (void *)key;
GPSEE_ASSERT(monitor && monitor != nilMonitor);
PR_DestroyMonitor(monitor);
return JS_TRUE;
}
#endif
/**
* Shut down the monitor system. It is the caller's responsibility to insure
* that no monitors are held when this routine is called.
*/
void gpsee_shutdownMonitorSystem(gpsee_runtime_t *grt)
{
#ifdef JS_THREADSAFE
gpsee_ds_forEach(NULL, grt->monitorList_unlocked, destroyMonitor_cb, NULL);
gpsee_ds_destroy(grt->monitorList_unlocked);
PR_DestroyMonitor(grt->monitors.monitor);
#endif
return;
}