#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;
static isc_boolean_t want_shutdown = ISC_FALSE;
static isc_boolean_t want_reload = ISC_FALSE;
static isc_boolean_t blocked = ISC_FALSE;
static isc_thread_t blockedthread;
#define NUM_EVENTS 2
enum {
RELOAD_EVENT,
SHUTDOWN_EVENT
};
static HANDLE hEvents[NUM_EVENTS];
DWORD dwWaitResult;
static isc_thread_t main_thread;
isc_result_t
isc_app_start(void) {
isc_result_t result;
main_thread = GetCurrentThread();
result = isc_mutex_init(&lock);
if (result != ISC_R_SUCCESS)
return (result);
hEvents[RELOAD_EVENT] = CreateEvent(NULL, FALSE, FALSE, NULL);
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;
}
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;
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);
while (!want_shutdown) {
dwWaitResult = WaitForMultipleObjects(NUM_EVENTS, hEvents,
FALSE, INFINITE);
if (WaitSucceeded(dwWaitResult, NUM_EVENTS)) {
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;
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);
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());
}