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