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