Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / timer_api.c
blob08535857273930dbc2b9edcffc419057e0ec7dc7
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
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.
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.
19 /* Id: timer_api.c,v 1.4 2009/09/02 23:48:02 tbox Exp */
21 #include <config.h>
23 #include <unistd.h>
25 #include <isc/app.h>
26 #include <isc/magic.h>
27 #include <isc/mutex.h>
28 #include <isc/once.h>
29 #include <isc/timer.h>
30 #include <isc/util.h>
32 static isc_mutex_t createlock;
33 static isc_once_t once = ISC_ONCE_INIT;
34 static isc_timermgrcreatefunc_t timermgr_createfunc = NULL;
36 static void
37 initialize(void) {
38 RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
41 isc_result_t
42 isc_timer_register(isc_timermgrcreatefunc_t createfunc) {
43 isc_result_t result = ISC_R_SUCCESS;
45 RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
47 LOCK(&createlock);
48 if (timermgr_createfunc == NULL)
49 timermgr_createfunc = createfunc;
50 else
51 result = ISC_R_EXISTS;
52 UNLOCK(&createlock);
54 return (result);
57 isc_result_t
58 isc_timermgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx,
59 isc_timermgr_t **managerp)
61 isc_result_t result;
63 LOCK(&createlock);
65 REQUIRE(timermgr_createfunc != NULL);
66 result = (*timermgr_createfunc)(mctx, managerp);
68 UNLOCK(&createlock);
70 if (result == ISC_R_SUCCESS)
71 isc_appctx_settimermgr(actx, *managerp);
73 return (result);
76 isc_result_t
77 isc_timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp) {
78 isc_result_t result;
80 LOCK(&createlock);
82 REQUIRE(timermgr_createfunc != NULL);
83 result = (*timermgr_createfunc)(mctx, managerp);
85 UNLOCK(&createlock);
87 return (result);
90 void
91 isc_timermgr_destroy(isc_timermgr_t **managerp) {
92 REQUIRE(*managerp != NULL && ISCAPI_TIMERMGR_VALID(*managerp));
94 (*managerp)->methods->destroy(managerp);
96 ENSURE(*managerp == NULL);
99 isc_result_t
100 isc_timer_create(isc_timermgr_t *manager, isc_timertype_t type,
101 isc_time_t *expires, isc_interval_t *interval,
102 isc_task_t *task, isc_taskaction_t action, const void *arg,
103 isc_timer_t **timerp)
105 REQUIRE(ISCAPI_TIMERMGR_VALID(manager));
107 return (manager->methods->timercreate(manager, type, expires,
108 interval, task, action, arg,
109 timerp));
112 void
113 isc_timer_attach(isc_timer_t *timer, isc_timer_t **timerp) {
114 REQUIRE(ISCAPI_TIMER_VALID(timer));
115 REQUIRE(timerp != NULL && *timerp == NULL);
117 timer->methods->attach(timer, timerp);
119 ENSURE(*timerp == timer);
122 void
123 isc_timer_detach(isc_timer_t **timerp) {
124 REQUIRE(timerp != NULL && ISCAPI_TIMER_VALID(*timerp));
126 (*timerp)->methods->detach(timerp);
128 ENSURE(*timerp == NULL);
131 isc_result_t
132 isc_timer_reset(isc_timer_t *timer, isc_timertype_t type,
133 isc_time_t *expires, isc_interval_t *interval,
134 isc_boolean_t purge)
136 REQUIRE(ISCAPI_TIMER_VALID(timer));
138 return (timer->methods->reset(timer, type, expires, interval, purge));
141 isc_result_t
142 isc_timer_touch(isc_timer_t *timer) {
143 REQUIRE(ISCAPI_TIMER_VALID(timer));
145 return (timer->methods->touch(timer));