1/* $NetBSD: timer_api.c,v 1.6 2020/05/25 20:47:20 christos Exp $ */ 2 3/* 4 * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC") 5 * 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. 9 * 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. 17 */ 18 19/* Id: timer_api.c,v 1.4 2009/09/02 23:48:02 tbox Exp */ 20 21#include <config.h> 22 23#include <unistd.h> 24 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> 31 32static isc_mutex_t createlock; 33static isc_once_t once = ISC_ONCE_INIT; 34static isc_timermgrcreatefunc_t timermgr_createfunc = NULL; 35 36static void 37initialize(void) { 38 RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS); 39} 40 41isc_result_t 42isc_timer_register(isc_timermgrcreatefunc_t createfunc) { 43 isc_result_t result = ISC_R_SUCCESS; 44 45 RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS); 46 47 LOCK(&createlock); 48 if (timermgr_createfunc == NULL) 49 timermgr_createfunc = createfunc; 50 else 51 result = ISC_R_EXISTS; 52 UNLOCK(&createlock); 53 54 return (result); 55} 56 57isc_result_t 58isc_timermgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx, 59 isc_timermgr_t **managerp) 60{ 61 isc_result_t result; 62 63 LOCK(&createlock); 64 65 REQUIRE(timermgr_createfunc != NULL); 66 result = (*timermgr_createfunc)(mctx, managerp); 67 68 UNLOCK(&createlock); 69 70 if (result == ISC_R_SUCCESS) 71 isc_appctx_settimermgr(actx, *managerp); 72 73 return (result); 74} 75 76isc_result_t 77isc_timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp) { 78 isc_result_t result; 79 80 LOCK(&createlock); 81 82 REQUIRE(timermgr_createfunc != NULL); 83 result = (*timermgr_createfunc)(mctx, managerp); 84 85 UNLOCK(&createlock); 86 87 return (result); 88} 89 90void 91isc_timermgr_destroy(isc_timermgr_t **managerp) { 92 REQUIRE(*managerp != NULL && ISCAPI_TIMERMGR_VALID(*managerp)); 93 94 (*managerp)->methods->destroy(managerp); 95 96 ENSURE(*managerp == NULL); 97} 98 99isc_result_t 100isc_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) 104{ 105 REQUIRE(ISCAPI_TIMERMGR_VALID(manager)); 106 107 return (manager->methods->timercreate(manager, type, expires, 108 interval, task, action, arg, 109 timerp)); 110} 111 112void 113isc_timer_attach(isc_timer_t *timer, isc_timer_t **timerp) { 114 REQUIRE(ISCAPI_TIMER_VALID(timer)); 115 REQUIRE(timerp != NULL && *timerp == NULL); 116 117 timer->methods->attach(timer, timerp); 118 119 ENSURE(*timerp == timer); 120} 121 122void 123isc_timer_detach(isc_timer_t **timerp) { 124 REQUIRE(timerp != NULL && ISCAPI_TIMER_VALID(*timerp)); 125 126 (*timerp)->methods->detach(timerp); 127 128 ENSURE(*timerp == NULL); 129} 130 131isc_result_t 132isc_timer_reset(isc_timer_t *timer, isc_timertype_t type, 133 isc_time_t *expires, isc_interval_t *interval, 134 isc_boolean_t purge) 135{ 136 REQUIRE(ISCAPI_TIMER_VALID(timer)); 137 138 return (timer->methods->reset(timer, type, expires, interval, purge)); 139} 140 141isc_result_t 142isc_timer_touch(isc_timer_t *timer) { 143 REQUIRE(ISCAPI_TIMER_VALID(timer)); 144 145 return (timer->methods->touch(timer)); 146} 147