2 * Copyright (C) 2004, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: app.c,v 1.9 2009/09/02 23:48:03 tbox Exp $ */
22 #include <sys/types.h>
31 #include <isc/boolean.h>
32 #include <isc/condition.h>
34 #include <isc/mutex.h>
35 #include <isc/event.h>
36 #include <isc/platform.h>
37 #include <isc/string.h>
41 #include <isc/thread.h>
43 static isc_eventlist_t on_run;
44 static isc_mutex_t lock;
45 static isc_boolean_t shutdown_requested = ISC_FALSE;
46 static isc_boolean_t running = ISC_FALSE;
48 * We assume that 'want_shutdown' can be read and written atomically.
50 static isc_boolean_t want_shutdown = ISC_FALSE;
52 * We assume that 'want_reload' can be read and written atomically.
54 static isc_boolean_t want_reload = ISC_FALSE;
56 static isc_boolean_t blocked = ISC_FALSE;
58 static isc_thread_t blockedthread;
60 /* Events to wait for */
69 static HANDLE hEvents[NUM_EVENTS];
73 * We need to remember which thread is the main thread...
75 static isc_thread_t main_thread;
78 isc__app_start(void) {
82 * Start an ISC library application.
85 main_thread = GetCurrentThread();
87 result = isc_mutex_init(&lock);
88 if (result != ISC_R_SUCCESS)
91 /* Create the reload event in a non-signaled state */
92 hEvents[RELOAD_EVENT] = CreateEvent(NULL, FALSE, FALSE, NULL);
94 /* Create the shutdown event in a non-signaled state */
95 hEvents[SHUTDOWN_EVENT] = CreateEvent(NULL, FALSE, FALSE, NULL);
97 ISC_LIST_INIT(on_run);
98 return (ISC_R_SUCCESS);
102 isc__app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
105 isc_task_t *cloned_task = NULL;
111 result = ISC_R_ALREADYRUNNING;
116 * Note that we store the task to which we're going to send the event
117 * in the event's "sender" field.
119 isc_task_attach(task, &cloned_task);
120 event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
121 action, arg, sizeof(*event));
123 result = ISC_R_NOMEMORY;
127 ISC_LIST_APPEND(on_run, event, ev_link);
128 result = ISC_R_SUCCESS;
137 isc_event_t *event, *next_event;
139 HANDLE *pHandles = NULL;
141 REQUIRE(main_thread == GetCurrentThread());
147 * Post any on-run events (in FIFO order).
149 for (event = ISC_LIST_HEAD(on_run);
151 event = next_event) {
152 next_event = ISC_LIST_NEXT(event, ev_link);
153 ISC_LIST_UNLINK(on_run, event, ev_link);
154 task = event->ev_sender;
155 event->ev_sender = NULL;
156 isc_task_sendanddetach(&task, &event);
164 * There is no danger if isc_app_shutdown() is called before we wait
168 while (!want_shutdown) {
169 dwWaitResult = WaitForMultipleObjects(NUM_EVENTS, hEvents,
172 /* See why we returned */
174 if (WaitSucceeded(dwWaitResult, NUM_EVENTS)) {
176 * The return was due to one of the events
179 switch (WaitSucceededIndex(dwWaitResult)) {
181 want_reload = ISC_TRUE;
185 want_shutdown = ISC_TRUE;
190 want_reload = ISC_FALSE;
191 return (ISC_R_RELOAD);
194 if (want_shutdown && blocked)
198 return (ISC_R_SUCCESS);
202 isc__app_shutdown(void) {
203 isc_boolean_t want_kill = ISC_TRUE;
208 if (shutdown_requested)
209 want_kill = ISC_FALSE; /* We're only signaling once */
211 shutdown_requested = ISC_TRUE;
215 SetEvent(hEvents[SHUTDOWN_EVENT]);
217 return (ISC_R_SUCCESS);
221 isc__app_reload(void) {
222 isc_boolean_t want_reload = ISC_TRUE;
228 * Don't send the reload signal if we're shutting down.
230 if (shutdown_requested)
231 want_reload = ISC_FALSE;
235 SetEvent(hEvents[RELOAD_EVENT]);
237 return (ISC_R_SUCCESS);
241 isc__app_finish(void) {
246 isc__app_block(void) {
251 blockedthread = GetCurrentThread();
255 isc__app_unblock(void) {
259 REQUIRE(blockedthread == GetCurrentThread());