1*9034ec65Schristos /* $NetBSD: timer_api.c,v 1.6 2020/05/25 20:47:20 christos Exp $ */
22b3787f6Schristos
32b3787f6Schristos /*
42b3787f6Schristos * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
52b3787f6Schristos *
62b3787f6Schristos * Permission to use, copy, modify, and/or distribute this software for any
72b3787f6Schristos * purpose with or without fee is hereby granted, provided that the above
82b3787f6Schristos * copyright notice and this permission notice appear in all copies.
92b3787f6Schristos *
102b3787f6Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
112b3787f6Schristos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
122b3787f6Schristos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
132b3787f6Schristos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
142b3787f6Schristos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
152b3787f6Schristos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
162b3787f6Schristos * PERFORMANCE OF THIS SOFTWARE.
172b3787f6Schristos */
182b3787f6Schristos
19*9034ec65Schristos /* Id: timer_api.c,v 1.4 2009/09/02 23:48:02 tbox Exp */
202b3787f6Schristos
212b3787f6Schristos #include <config.h>
222b3787f6Schristos
232b3787f6Schristos #include <unistd.h>
242b3787f6Schristos
252b3787f6Schristos #include <isc/app.h>
262b3787f6Schristos #include <isc/magic.h>
272b3787f6Schristos #include <isc/mutex.h>
282b3787f6Schristos #include <isc/once.h>
292b3787f6Schristos #include <isc/timer.h>
302b3787f6Schristos #include <isc/util.h>
312b3787f6Schristos
322b3787f6Schristos static isc_mutex_t createlock;
332b3787f6Schristos static isc_once_t once = ISC_ONCE_INIT;
342b3787f6Schristos static isc_timermgrcreatefunc_t timermgr_createfunc = NULL;
352b3787f6Schristos
362b3787f6Schristos static void
initialize(void)372b3787f6Schristos initialize(void) {
382b3787f6Schristos RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
392b3787f6Schristos }
402b3787f6Schristos
412b3787f6Schristos isc_result_t
isc_timer_register(isc_timermgrcreatefunc_t createfunc)422b3787f6Schristos isc_timer_register(isc_timermgrcreatefunc_t createfunc) {
432b3787f6Schristos isc_result_t result = ISC_R_SUCCESS;
442b3787f6Schristos
452b3787f6Schristos RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
462b3787f6Schristos
472b3787f6Schristos LOCK(&createlock);
482b3787f6Schristos if (timermgr_createfunc == NULL)
492b3787f6Schristos timermgr_createfunc = createfunc;
502b3787f6Schristos else
512b3787f6Schristos result = ISC_R_EXISTS;
522b3787f6Schristos UNLOCK(&createlock);
532b3787f6Schristos
542b3787f6Schristos return (result);
552b3787f6Schristos }
562b3787f6Schristos
572b3787f6Schristos isc_result_t
isc_timermgr_createinctx(isc_mem_t * mctx,isc_appctx_t * actx,isc_timermgr_t ** managerp)582b3787f6Schristos isc_timermgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx,
592b3787f6Schristos isc_timermgr_t **managerp)
602b3787f6Schristos {
612b3787f6Schristos isc_result_t result;
622b3787f6Schristos
632b3787f6Schristos LOCK(&createlock);
642b3787f6Schristos
652b3787f6Schristos REQUIRE(timermgr_createfunc != NULL);
662b3787f6Schristos result = (*timermgr_createfunc)(mctx, managerp);
672b3787f6Schristos
682b3787f6Schristos UNLOCK(&createlock);
692b3787f6Schristos
702b3787f6Schristos if (result == ISC_R_SUCCESS)
712b3787f6Schristos isc_appctx_settimermgr(actx, *managerp);
722b3787f6Schristos
732b3787f6Schristos return (result);
742b3787f6Schristos }
752b3787f6Schristos
762b3787f6Schristos isc_result_t
isc_timermgr_create(isc_mem_t * mctx,isc_timermgr_t ** managerp)772b3787f6Schristos isc_timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp) {
782b3787f6Schristos isc_result_t result;
792b3787f6Schristos
802b3787f6Schristos LOCK(&createlock);
812b3787f6Schristos
822b3787f6Schristos REQUIRE(timermgr_createfunc != NULL);
832b3787f6Schristos result = (*timermgr_createfunc)(mctx, managerp);
842b3787f6Schristos
852b3787f6Schristos UNLOCK(&createlock);
862b3787f6Schristos
872b3787f6Schristos return (result);
882b3787f6Schristos }
892b3787f6Schristos
902b3787f6Schristos void
isc_timermgr_destroy(isc_timermgr_t ** managerp)912b3787f6Schristos isc_timermgr_destroy(isc_timermgr_t **managerp) {
922b3787f6Schristos REQUIRE(*managerp != NULL && ISCAPI_TIMERMGR_VALID(*managerp));
932b3787f6Schristos
942b3787f6Schristos (*managerp)->methods->destroy(managerp);
952b3787f6Schristos
962b3787f6Schristos ENSURE(*managerp == NULL);
972b3787f6Schristos }
982b3787f6Schristos
992b3787f6Schristos isc_result_t
isc_timer_create(isc_timermgr_t * manager,isc_timertype_t type,isc_time_t * expires,isc_interval_t * interval,isc_task_t * task,isc_taskaction_t action,const void * arg,isc_timer_t ** timerp)1002b3787f6Schristos isc_timer_create(isc_timermgr_t *manager, isc_timertype_t type,
1012b3787f6Schristos isc_time_t *expires, isc_interval_t *interval,
1022b3787f6Schristos isc_task_t *task, isc_taskaction_t action, const void *arg,
1032b3787f6Schristos isc_timer_t **timerp)
1042b3787f6Schristos {
1052b3787f6Schristos REQUIRE(ISCAPI_TIMERMGR_VALID(manager));
1062b3787f6Schristos
1072b3787f6Schristos return (manager->methods->timercreate(manager, type, expires,
1082b3787f6Schristos interval, task, action, arg,
1092b3787f6Schristos timerp));
1102b3787f6Schristos }
1112b3787f6Schristos
1122b3787f6Schristos void
isc_timer_attach(isc_timer_t * timer,isc_timer_t ** timerp)1132b3787f6Schristos isc_timer_attach(isc_timer_t *timer, isc_timer_t **timerp) {
1142b3787f6Schristos REQUIRE(ISCAPI_TIMER_VALID(timer));
1152b3787f6Schristos REQUIRE(timerp != NULL && *timerp == NULL);
1162b3787f6Schristos
1172b3787f6Schristos timer->methods->attach(timer, timerp);
1182b3787f6Schristos
1192b3787f6Schristos ENSURE(*timerp == timer);
1202b3787f6Schristos }
1212b3787f6Schristos
1222b3787f6Schristos void
isc_timer_detach(isc_timer_t ** timerp)1232b3787f6Schristos isc_timer_detach(isc_timer_t **timerp) {
1242b3787f6Schristos REQUIRE(timerp != NULL && ISCAPI_TIMER_VALID(*timerp));
1252b3787f6Schristos
1262b3787f6Schristos (*timerp)->methods->detach(timerp);
1272b3787f6Schristos
1282b3787f6Schristos ENSURE(*timerp == NULL);
1292b3787f6Schristos }
1302b3787f6Schristos
1312b3787f6Schristos isc_result_t
isc_timer_reset(isc_timer_t * timer,isc_timertype_t type,isc_time_t * expires,isc_interval_t * interval,isc_boolean_t purge)1322b3787f6Schristos isc_timer_reset(isc_timer_t *timer, isc_timertype_t type,
1332b3787f6Schristos isc_time_t *expires, isc_interval_t *interval,
1342b3787f6Schristos isc_boolean_t purge)
1352b3787f6Schristos {
1362b3787f6Schristos REQUIRE(ISCAPI_TIMER_VALID(timer));
1372b3787f6Schristos
1382b3787f6Schristos return (timer->methods->reset(timer, type, expires, interval, purge));
1392b3787f6Schristos }
1402b3787f6Schristos
1412b3787f6Schristos isc_result_t
isc_timer_touch(isc_timer_t * timer)1422b3787f6Schristos isc_timer_touch(isc_timer_t *timer) {
1432b3787f6Schristos REQUIRE(ISCAPI_TIMER_VALID(timer));
1442b3787f6Schristos
1452b3787f6Schristos return (timer->methods->touch(timer));
1462b3787f6Schristos }
147