1/*
2 * Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* $Id: app_api.c,v 1.5 2009/09/02 23:48:02 tbox Exp $ */
18
19#include <config.h>
20
21#include <unistd.h>
22
23#include <isc/app.h>
24#include <isc/magic.h>
25#include <isc/mutex.h>
26#include <isc/once.h>
27#include <isc/util.h>
28
29static isc_mutex_t createlock;
30static isc_once_t once = ISC_ONCE_INIT;
31static isc_appctxcreatefunc_t appctx_createfunc = NULL;
32
33#define ISCAPI_APPMETHODS_VALID(m) ISC_MAGIC_VALID(m, ISCAPI_APPMETHODS_MAGIC)
34
35static void
36initialize(void) {
37	RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
38}
39
40isc_result_t
41isc_app_register(isc_appctxcreatefunc_t createfunc) {
42	isc_result_t result = ISC_R_SUCCESS;
43
44	RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
45
46	LOCK(&createlock);
47	if (appctx_createfunc == NULL)
48		appctx_createfunc = createfunc;
49	else
50		result = ISC_R_EXISTS;
51	UNLOCK(&createlock);
52
53	return (result);
54}
55
56isc_result_t
57isc_appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
58	isc_result_t result;
59
60	LOCK(&createlock);
61
62	REQUIRE(appctx_createfunc != NULL);
63	result = (*appctx_createfunc)(mctx, ctxp);
64
65	UNLOCK(&createlock);
66
67	return (result);
68}
69
70void
71isc_appctx_destroy(isc_appctx_t **ctxp) {
72	REQUIRE(ctxp != NULL && ISCAPI_APPCTX_VALID(*ctxp));
73
74	(*ctxp)->methods->ctxdestroy(ctxp);
75
76	ENSURE(*ctxp == NULL);
77}
78
79isc_result_t
80isc_app_ctxstart(isc_appctx_t *ctx) {
81	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
82
83	return (ctx->methods->ctxstart(ctx));
84}
85
86isc_result_t
87isc_app_ctxrun(isc_appctx_t *ctx) {
88	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
89
90	return (ctx->methods->ctxrun(ctx));
91}
92
93isc_result_t
94isc_app_ctxsuspend(isc_appctx_t *ctx) {
95	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
96
97	return (ctx->methods->ctxsuspend(ctx));
98}
99
100isc_result_t
101isc_app_ctxshutdown(isc_appctx_t *ctx) {
102	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
103
104	return (ctx->methods->ctxshutdown(ctx));
105}
106
107void
108isc_app_ctxfinish(isc_appctx_t *ctx) {
109	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
110
111	ctx->methods->ctxfinish(ctx);
112}
113
114void
115isc_appctx_settaskmgr(isc_appctx_t *ctx, isc_taskmgr_t *taskmgr) {
116	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
117	REQUIRE(taskmgr != NULL);
118
119	ctx->methods->settaskmgr(ctx, taskmgr);
120}
121
122void
123isc_appctx_setsocketmgr(isc_appctx_t *ctx, isc_socketmgr_t *socketmgr) {
124	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
125	REQUIRE(socketmgr != NULL);
126
127	ctx->methods->setsocketmgr(ctx, socketmgr);
128}
129
130void
131isc_appctx_settimermgr(isc_appctx_t *ctx, isc_timermgr_t *timermgr) {
132	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
133	REQUIRE(timermgr != NULL);
134
135	ctx->methods->settimermgr(ctx, timermgr);
136}
137