timer_api.c revision 290001
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: timer_api.c,v 1.4 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/timer.h>
28#include <isc/util.h>
29
30static isc_mutex_t createlock;
31static isc_once_t once = ISC_ONCE_INIT;
32static isc_timermgrcreatefunc_t timermgr_createfunc = NULL;
33
34static void
35initialize(void) {
36	RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
37}
38
39isc_result_t
40isc_timer_register(isc_timermgrcreatefunc_t createfunc) {
41	isc_result_t result = ISC_R_SUCCESS;
42
43	RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
44
45	LOCK(&createlock);
46	if (timermgr_createfunc == NULL)
47		timermgr_createfunc = createfunc;
48	else
49		result = ISC_R_EXISTS;
50	UNLOCK(&createlock);
51
52	return (result);
53}
54
55isc_result_t
56isc_timermgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx,
57			 isc_timermgr_t **managerp)
58{
59	isc_result_t result;
60
61	LOCK(&createlock);
62
63	REQUIRE(timermgr_createfunc != NULL);
64	result = (*timermgr_createfunc)(mctx, managerp);
65
66	UNLOCK(&createlock);
67
68	if (result == ISC_R_SUCCESS)
69		isc_appctx_settimermgr(actx, *managerp);
70
71	return (result);
72}
73
74isc_result_t
75isc_timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp) {
76	isc_result_t result;
77
78	LOCK(&createlock);
79
80	REQUIRE(timermgr_createfunc != NULL);
81	result = (*timermgr_createfunc)(mctx, managerp);
82
83	UNLOCK(&createlock);
84
85	return (result);
86}
87
88void
89isc_timermgr_destroy(isc_timermgr_t **managerp) {
90	REQUIRE(*managerp != NULL && ISCAPI_TIMERMGR_VALID(*managerp));
91
92	(*managerp)->methods->destroy(managerp);
93
94	ENSURE(*managerp == NULL);
95}
96
97isc_result_t
98isc_timer_create(isc_timermgr_t *manager, isc_timertype_t type,
99		 isc_time_t *expires, isc_interval_t *interval,
100		 isc_task_t *task, isc_taskaction_t action, const void *arg,
101		 isc_timer_t **timerp)
102{
103	REQUIRE(ISCAPI_TIMERMGR_VALID(manager));
104
105	return (manager->methods->timercreate(manager, type, expires,
106					      interval, task, action, arg,
107					      timerp));
108}
109
110void
111isc_timer_attach(isc_timer_t *timer, isc_timer_t **timerp) {
112	REQUIRE(ISCAPI_TIMER_VALID(timer));
113	REQUIRE(timerp != NULL && *timerp == NULL);
114
115	timer->methods->attach(timer, timerp);
116
117	ENSURE(*timerp == timer);
118}
119
120void
121isc_timer_detach(isc_timer_t **timerp) {
122	REQUIRE(timerp != NULL && ISCAPI_TIMER_VALID(*timerp));
123
124	(*timerp)->methods->detach(timerp);
125
126	ENSURE(*timerp == NULL);
127}
128
129isc_result_t
130isc_timer_reset(isc_timer_t *timer, isc_timertype_t type,
131		isc_time_t *expires, isc_interval_t *interval,
132		isc_boolean_t purge)
133{
134	REQUIRE(ISCAPI_TIMER_VALID(timer));
135
136	return (timer->methods->reset(timer, type, expires, interval, purge));
137}
138
139isc_result_t
140isc_timer_touch(isc_timer_t *timer) {
141	REQUIRE(ISCAPI_TIMER_VALID(timer));
142
143	return (timer->methods->touch(timer));
144}
145