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: timer_api.c,v 1.4 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/timer.h>
28275970Scy#include <isc/util.h>
29275970Scy
30275970Scystatic isc_mutex_t createlock;
31275970Scystatic isc_once_t once = ISC_ONCE_INIT;
32275970Scystatic isc_timermgrcreatefunc_t timermgr_createfunc = NULL;
33275970Scy
34275970Scystatic void
35275970Scyinitialize(void) {
36275970Scy	RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
37275970Scy}
38275970Scy
39275970Scyisc_result_t
40275970Scyisc_timer_register(isc_timermgrcreatefunc_t createfunc) {
41275970Scy	isc_result_t result = ISC_R_SUCCESS;
42275970Scy
43275970Scy	RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
44275970Scy
45275970Scy	LOCK(&createlock);
46275970Scy	if (timermgr_createfunc == NULL)
47275970Scy		timermgr_createfunc = createfunc;
48275970Scy	else
49275970Scy		result = ISC_R_EXISTS;
50275970Scy	UNLOCK(&createlock);
51275970Scy
52275970Scy	return (result);
53275970Scy}
54275970Scy
55275970Scyisc_result_t
56275970Scyisc_timermgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx,
57275970Scy			 isc_timermgr_t **managerp)
58275970Scy{
59275970Scy	isc_result_t result;
60275970Scy
61275970Scy	LOCK(&createlock);
62275970Scy
63275970Scy	REQUIRE(timermgr_createfunc != NULL);
64275970Scy	result = (*timermgr_createfunc)(mctx, managerp);
65275970Scy
66275970Scy	UNLOCK(&createlock);
67275970Scy
68275970Scy	if (result == ISC_R_SUCCESS)
69275970Scy		isc_appctx_settimermgr(actx, *managerp);
70275970Scy
71275970Scy	return (result);
72275970Scy}
73275970Scy
74275970Scyisc_result_t
75275970Scyisc_timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp) {
76275970Scy	isc_result_t result;
77275970Scy
78275970Scy	LOCK(&createlock);
79275970Scy
80275970Scy	REQUIRE(timermgr_createfunc != NULL);
81275970Scy	result = (*timermgr_createfunc)(mctx, managerp);
82275970Scy
83275970Scy	UNLOCK(&createlock);
84275970Scy
85275970Scy	return (result);
86275970Scy}
87275970Scy
88275970Scyvoid
89275970Scyisc_timermgr_destroy(isc_timermgr_t **managerp) {
90275970Scy	REQUIRE(*managerp != NULL && ISCAPI_TIMERMGR_VALID(*managerp));
91275970Scy
92275970Scy	(*managerp)->methods->destroy(managerp);
93275970Scy
94275970Scy	ENSURE(*managerp == NULL);
95275970Scy}
96275970Scy
97275970Scyisc_result_t
98275970Scyisc_timer_create(isc_timermgr_t *manager, isc_timertype_t type,
99275970Scy		 isc_time_t *expires, isc_interval_t *interval,
100275970Scy		 isc_task_t *task, isc_taskaction_t action, const void *arg,
101275970Scy		 isc_timer_t **timerp)
102275970Scy{
103275970Scy	REQUIRE(ISCAPI_TIMERMGR_VALID(manager));
104275970Scy
105275970Scy	return (manager->methods->timercreate(manager, type, expires,
106275970Scy					      interval, task, action, arg,
107275970Scy					      timerp));
108275970Scy}
109275970Scy
110275970Scyvoid
111275970Scyisc_timer_attach(isc_timer_t *timer, isc_timer_t **timerp) {
112275970Scy	REQUIRE(ISCAPI_TIMER_VALID(timer));
113275970Scy	REQUIRE(timerp != NULL && *timerp == NULL);
114275970Scy
115275970Scy	timer->methods->attach(timer, timerp);
116275970Scy
117275970Scy	ENSURE(*timerp == timer);
118275970Scy}
119275970Scy
120275970Scyvoid
121275970Scyisc_timer_detach(isc_timer_t **timerp) {
122275970Scy	REQUIRE(timerp != NULL && ISCAPI_TIMER_VALID(*timerp));
123275970Scy
124275970Scy	(*timerp)->methods->detach(timerp);
125275970Scy
126275970Scy	ENSURE(*timerp == NULL);
127275970Scy}
128275970Scy
129275970Scyisc_result_t
130275970Scyisc_timer_reset(isc_timer_t *timer, isc_timertype_t type,
131275970Scy		isc_time_t *expires, isc_interval_t *interval,
132275970Scy		isc_boolean_t purge)
133275970Scy{
134275970Scy	REQUIRE(ISCAPI_TIMER_VALID(timer));
135275970Scy
136275970Scy	return (timer->methods->reset(timer, type, expires, interval, purge));
137275970Scy}
138275970Scy
139275970Scyisc_result_t
140275970Scyisc_timer_touch(isc_timer_t *timer) {
141275970Scy	REQUIRE(ISCAPI_TIMER_VALID(timer));
142275970Scy
143275970Scy	return (timer->methods->touch(timer));
144275970Scy}
145