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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/*
* Copyright (C) 1999-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $ISC: app.c,v 1.3 2001/07/09 21:06:03 gson Exp $ */
#include <config.h>
#include <sys/types.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <process.h>
#include <isc/app.h>
#include <isc/boolean.h>
#include <isc/condition.h>
#include <isc/msgs.h>
#include <isc/mutex.h>
#include <isc/event.h>
#include <isc/platform.h>
#include <isc/string.h>
#include <isc/task.h>
#include <isc/time.h>
#include <isc/util.h>
#include <isc/thread.h>
static isc_eventlist_t on_run;
static isc_mutex_t lock;
static isc_boolean_t shutdown_requested = ISC_FALSE;
static isc_boolean_t running = ISC_FALSE;
/*
* We assume that 'want_shutdown' can be read and written atomically.
*/
static isc_boolean_t want_shutdown = ISC_FALSE;
/*
* We assume that 'want_reload' can be read and written atomically.
*/
static isc_boolean_t want_reload = ISC_FALSE;
static isc_boolean_t blocked = ISC_FALSE;
static isc_thread_t blockedthread;
/* Events to wait for */
#define NUM_EVENTS 2
enum {
RELOAD_EVENT,
SHUTDOWN_EVENT
};
static HANDLE hEvents[NUM_EVENTS];
DWORD dwWaitResult;
/*
* We need to remember which thread is the main thread...
*/
static isc_thread_t main_thread;
isc_result_t
isc_app_start(void) {
isc_result_t result;
/*
* Start an ISC library application.
*/
main_thread = GetCurrentThread();
result = isc_mutex_init(&lock);
if (result != ISC_R_SUCCESS)
return (result);
/* Create the reload event in a non-signaled state */
hEvents[RELOAD_EVENT] = CreateEvent(NULL, FALSE, FALSE, NULL);
/* Create the shutdown event in a non-signaled state */
hEvents[SHUTDOWN_EVENT] = CreateEvent(NULL, FALSE, FALSE, NULL);
ISC_LIST_INIT(on_run);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
void *arg) {
isc_event_t *event;
isc_task_t *cloned_task = NULL;
isc_result_t result;
LOCK(&lock);
if (running) {
result = ISC_R_ALREADYRUNNING;
goto unlock;
}
/*
* Note that we store the task to which we're going to send the event
* in the event's "sender" field.
*/
isc_task_attach(task, &cloned_task);
event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
action, arg, sizeof *event);
if (event == NULL) {
result = ISC_R_NOMEMORY;
goto unlock;
}
ISC_LIST_APPEND(on_run, event, ev_link);
result = ISC_R_SUCCESS;
unlock:
UNLOCK(&lock);
return (result);
}
isc_result_t
isc_app_run(void) {
isc_event_t *event, *next_event;
isc_task_t *task;
HANDLE *pHandles = NULL;
REQUIRE(main_thread == GetCurrentThread());
LOCK(&lock);
if (!running) {
running = ISC_TRUE;
/*
* Post any on-run events (in FIFO order).
*/
for (event = ISC_LIST_HEAD(on_run);
event != NULL;
event = next_event) {
next_event = ISC_LIST_NEXT(event, ev_link);
ISC_LIST_UNLINK(on_run, event, ev_link);
task = event->ev_sender;
event->ev_sender = NULL;
isc_task_sendanddetach(&task, &event);
}
}
UNLOCK(&lock);
/*
* There is no danger if isc_app_shutdown() is called before we wait
* for events.
*/
while (!want_shutdown) {
dwWaitResult = WaitForMultipleObjects(NUM_EVENTS, hEvents,
FALSE, INFINITE);
/* See why we returned */
if (WaitSucceeded(dwWaitResult, NUM_EVENTS)) {
/*
* The return was due to one of the events
* being signaled
*/
switch (WaitSucceededIndex(dwWaitResult)) {
case RELOAD_EVENT:
want_reload = ISC_TRUE;
break;
case SHUTDOWN_EVENT:
want_shutdown = ISC_TRUE;
break;
}
}
if (want_reload) {
want_reload = ISC_FALSE;
return (ISC_R_RELOAD);
}
if (want_shutdown && blocked)
exit(-1);
}
return (ISC_R_SUCCESS);
}
isc_result_t
isc_app_shutdown(void) {
isc_boolean_t want_kill = ISC_TRUE;
LOCK(&lock);
REQUIRE(running);
if (shutdown_requested)
want_kill = ISC_FALSE; /* We're only signaling once */
else
shutdown_requested = ISC_TRUE;
UNLOCK(&lock);
if (want_kill)
SetEvent(hEvents[SHUTDOWN_EVENT]);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_app_reload(void) {
isc_boolean_t want_reload = ISC_TRUE;
LOCK(&lock);
REQUIRE(running);
/*
* Don't send the reload signal if we're shutting down.
*/
if (shutdown_requested)
want_reload = ISC_FALSE;
UNLOCK(&lock);
if (want_reload)
SetEvent(hEvents[RELOAD_EVENT]);
return (ISC_R_SUCCESS);
}
void
isc_app_finish(void) {
DESTROYLOCK(&lock);
}
void
isc_app_block(void) {
REQUIRE(running);
REQUIRE(!blocked);
blocked = ISC_TRUE;
blockedthread = GetCurrentThread();
}
void
isc_app_unblock(void) {
REQUIRE(running);
REQUIRE(blocked);
blocked = ISC_FALSE;
REQUIRE(blockedthread == GetCurrentThread());
}
|