1/*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * From: @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 37 */ 38 39#include <sys/cdefs.h> 40 41#include <sys/callout.h> 42#include <sys/mutex.h> 43#include "los_swtmr.h" 44 45/* 46 * Note: Convert freebsd callout to Huawei LiteOS timeout, follow events would happen 47 * 1. function handle must forece convert 48 * 2. Huawei LiteOS timer expires scale is millisecond, so check input ticks is millisecond scale 49 */ 50 51void 52callout_init_mtx(struct callout *c, struct pthread_mutex *mtx, int flag) 53{ 54 (void)flag; 55 if (c != NULL) { 56 c->tm_mtx = mtx; 57 mtx_init(&c->callout_mtx,0,0,0); 58 LOS_SpinInit(&c->lock); 59 c->timer_id = OS_SWTMR_MAX_TIMERID; 60 } 61} 62 63void 64callout_function(void *arg) 65{ 66 struct callout* callout = arg; 67 68 (callout->callout_data.func)((uintptr_t)(callout->callout_data.arg)); 69} 70 71void 72callout_reset(struct callout *c, int to_ticks, void (*func)(void *), void *arg) 73{ 74 uint32_t int_save; 75 uint32_t ret; 76 77 if (c != NULL) { 78 callout_stop(c); 79 LOS_SpinLockSave(&c->lock, (unsigned int *)&int_save); 80 81 c->callout_data.func = (timer_func)func; 82 c->callout_data.arg = arg; 83 ret = LOS_SwtmrCreate((to_ticks==0 ? 1 : to_ticks), 84 LOS_SWTMR_MODE_NO_SELFDELETE, 85 (SWTMR_PROC_FUNC)callout_function, 86 (unsigned short *)&c->timer_id, 87 (uintptr_t)c); 88 if (ret != LOS_OK) { 89 PRINTK("add_timer<error> timer create failed: %u \n", ret); 90 } else { 91 ret = LOS_SwtmrStart(c->timer_id); 92 if (ret != LOS_OK) { 93 PRINTK("add_timer<error> timer start failed: %u \n", ret); 94 } 95 } 96 LOS_SpinUnlockRestore(&c->lock, int_save); 97 } 98} 99 100void 101callout_stop(struct callout *c) 102{ 103 uint32_t int_save; 104 if (c != NULL) { 105 LOS_SpinLockSave(&c->lock, (unsigned int *)&int_save); 106 (void)LOS_SwtmrDelete(c->timer_id); 107 LOS_SpinUnlockRestore(&c->lock, int_save); 108 } 109} 110 111void 112callout_drain(struct callout *c) 113{ 114 if(!c || !c->tm_mtx){ 115 return ; 116 } 117 mtx_lock(c->tm_mtx); 118 callout_stop(c); 119 mtx_unlock(c->tm_mtx); 120} 121