1275970Scy/*
2275970Scy * Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
3275970Scy *
4275970Scy * Permission to use, copy, modify, and/or distribute this software for any
5275970Scy * purpose with or without fee is hereby granted, provided that the above
6275970Scy * copyright notice and this permission notice appear in all copies.
7275970Scy *
8275970Scy * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9275970Scy * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10275970Scy * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11275970Scy * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12275970Scy * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13275970Scy * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14275970Scy * PERFORMANCE OF THIS SOFTWARE.
15275970Scy */
16275970Scy
17275970Scy/* $Id: app_api.c,v 1.5 2009/09/02 23:48:02 tbox Exp $ */
18275970Scy
19275970Scy#include <config.h>
20275970Scy
21275970Scy#include <unistd.h>
22275970Scy
23275970Scy#include <isc/app.h>
24275970Scy#include <isc/magic.h>
25275970Scy#include <isc/mutex.h>
26275970Scy#include <isc/once.h>
27275970Scy#include <isc/util.h>
28275970Scy
29275970Scystatic isc_mutex_t createlock;
30275970Scystatic isc_once_t once = ISC_ONCE_INIT;
31275970Scystatic isc_appctxcreatefunc_t appctx_createfunc = NULL;
32275970Scy
33275970Scy#define ISCAPI_APPMETHODS_VALID(m) ISC_MAGIC_VALID(m, ISCAPI_APPMETHODS_MAGIC)
34275970Scy
35275970Scystatic void
36275970Scyinitialize(void) {
37275970Scy	RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
38275970Scy}
39275970Scy
40275970Scyisc_result_t
41275970Scyisc_app_register(isc_appctxcreatefunc_t createfunc) {
42275970Scy	isc_result_t result = ISC_R_SUCCESS;
43275970Scy
44275970Scy	RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
45275970Scy
46275970Scy	LOCK(&createlock);
47275970Scy	if (appctx_createfunc == NULL)
48275970Scy		appctx_createfunc = createfunc;
49275970Scy	else
50275970Scy		result = ISC_R_EXISTS;
51275970Scy	UNLOCK(&createlock);
52275970Scy
53275970Scy	return (result);
54275970Scy}
55275970Scy
56275970Scyisc_result_t
57275970Scyisc_appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
58275970Scy	isc_result_t result;
59275970Scy
60275970Scy	LOCK(&createlock);
61275970Scy
62275970Scy	REQUIRE(appctx_createfunc != NULL);
63275970Scy	result = (*appctx_createfunc)(mctx, ctxp);
64275970Scy
65275970Scy	UNLOCK(&createlock);
66275970Scy
67275970Scy	return (result);
68275970Scy}
69275970Scy
70275970Scyvoid
71275970Scyisc_appctx_destroy(isc_appctx_t **ctxp) {
72275970Scy	REQUIRE(ctxp != NULL && ISCAPI_APPCTX_VALID(*ctxp));
73275970Scy
74275970Scy	(*ctxp)->methods->ctxdestroy(ctxp);
75275970Scy
76275970Scy	ENSURE(*ctxp == NULL);
77275970Scy}
78275970Scy
79275970Scyisc_result_t
80275970Scyisc_app_ctxstart(isc_appctx_t *ctx) {
81275970Scy	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
82275970Scy
83275970Scy	return (ctx->methods->ctxstart(ctx));
84275970Scy}
85275970Scy
86275970Scyisc_result_t
87275970Scyisc_app_ctxrun(isc_appctx_t *ctx) {
88275970Scy	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
89275970Scy
90275970Scy	return (ctx->methods->ctxrun(ctx));
91275970Scy}
92275970Scy
93275970Scyisc_result_t
94275970Scyisc_app_ctxsuspend(isc_appctx_t *ctx) {
95275970Scy	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
96275970Scy
97275970Scy	return (ctx->methods->ctxsuspend(ctx));
98275970Scy}
99275970Scy
100275970Scyisc_result_t
101275970Scyisc_app_ctxshutdown(isc_appctx_t *ctx) {
102275970Scy	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
103275970Scy
104275970Scy	return (ctx->methods->ctxshutdown(ctx));
105275970Scy}
106275970Scy
107275970Scyvoid
108275970Scyisc_app_ctxfinish(isc_appctx_t *ctx) {
109275970Scy	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
110275970Scy
111275970Scy	ctx->methods->ctxfinish(ctx);
112275970Scy}
113275970Scy
114275970Scyvoid
115275970Scyisc_appctx_settaskmgr(isc_appctx_t *ctx, isc_taskmgr_t *taskmgr) {
116275970Scy	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
117275970Scy	REQUIRE(taskmgr != NULL);
118275970Scy
119275970Scy	ctx->methods->settaskmgr(ctx, taskmgr);
120275970Scy}
121275970Scy
122275970Scyvoid
123275970Scyisc_appctx_setsocketmgr(isc_appctx_t *ctx, isc_socketmgr_t *socketmgr) {
124275970Scy	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
125275970Scy	REQUIRE(socketmgr != NULL);
126275970Scy
127275970Scy	ctx->methods->setsocketmgr(ctx, socketmgr);
128275970Scy}
129275970Scy
130275970Scyvoid
131275970Scyisc_appctx_settimermgr(isc_appctx_t *ctx, isc_timermgr_t *timermgr) {
132275970Scy	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
133275970Scy	REQUIRE(timermgr != NULL);
134275970Scy
135275970Scy	ctx->methods->settimermgr(ctx, timermgr);
136275970Scy}
137