]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/bind9/lib/isc/app_api.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / bind9 / lib / isc / app_api.c
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
29 static isc_mutex_t createlock;
30 static isc_once_t once = ISC_ONCE_INIT;
31 static isc_appctxcreatefunc_t appctx_createfunc = NULL;
32
33 #define ISCAPI_APPMETHODS_VALID(m) ISC_MAGIC_VALID(m, ISCAPI_APPMETHODS_MAGIC)
34
35 static void
36 initialize(void) {
37         RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
38 }
39
40 isc_result_t
41 isc_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
56 isc_result_t
57 isc_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
70 void
71 isc_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
79 isc_result_t
80 isc_app_ctxstart(isc_appctx_t *ctx) {
81         REQUIRE(ISCAPI_APPCTX_VALID(ctx));
82
83         return (ctx->methods->ctxstart(ctx));
84 }
85
86 isc_result_t
87 isc_app_ctxrun(isc_appctx_t *ctx) {
88         REQUIRE(ISCAPI_APPCTX_VALID(ctx));
89
90         return (ctx->methods->ctxrun(ctx));
91 }
92
93 isc_result_t
94 isc_app_ctxsuspend(isc_appctx_t *ctx) {
95         REQUIRE(ISCAPI_APPCTX_VALID(ctx));
96
97         return (ctx->methods->ctxsuspend(ctx));
98 }
99
100 isc_result_t
101 isc_app_ctxshutdown(isc_appctx_t *ctx) {
102         REQUIRE(ISCAPI_APPCTX_VALID(ctx));
103
104         return (ctx->methods->ctxshutdown(ctx));
105 }
106
107 void
108 isc_app_ctxfinish(isc_appctx_t *ctx) {
109         REQUIRE(ISCAPI_APPCTX_VALID(ctx));
110
111         ctx->methods->ctxfinish(ctx);
112 }
113
114 void
115 isc_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
122 void
123 isc_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
130 void
131 isc_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 }