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 __FBSDID("$FreeBSD: releng/12.2/sys/kern/kern_timeout.c 360633 2020-05-04 16:30:36Z jhb $");
41
42 #include <sys/callout.h>
43 #include <sys/mutex.h>
44 #include "los_swtmr.h"
45
46 /*
47 * Note: Convert freebsd callout to Huawei LiteOS timeout, follow events would happen
48 * 1. function handle must forece convert
49 * 2. Huawei LiteOS timer expires scale is millisecond, so check input ticks is millisecond scale
50 */
51
52 void
callout_init_mtx(struct callout * c,struct pthread_mutex * mtx,int flag)53 callout_init_mtx(struct callout *c, struct pthread_mutex *mtx, int flag)
54 {
55 (void)flag;
56 if (c != NULL) {
57 c->tm_mtx = mtx;
58 mtx_init(&c->callout_mtx,0,0,0);
59 LOS_SpinInit(&c->lock);
60 c->timer_id = OS_SWTMR_MAX_TIMERID;
61 }
62 }
63
64 void
callout_function(void * arg)65 callout_function(void *arg)
66 {
67 struct callout* callout = arg;
68
69 (callout->callout_data.func)((uintptr_t)(callout->callout_data.arg));
70 }
71
72 void
callout_reset(struct callout * c,int to_ticks,void (* func)(void *),void * arg)73 callout_reset(struct callout *c, int to_ticks, void (*func)(void *), void *arg)
74 {
75 uint32_t int_save;
76 uint32_t ret;
77
78 if (c != NULL) {
79 callout_stop(c);
80 LOS_SpinLockSave(&c->lock, (unsigned int *)&int_save);
81
82 c->callout_data.func = (timer_func)func;
83 c->callout_data.arg = arg;
84 ret = LOS_SwtmrCreate((to_ticks==0 ? 1 : to_ticks),
85 LOS_SWTMR_MODE_NO_SELFDELETE,
86 (SWTMR_PROC_FUNC)callout_function,
87 (unsigned short *)&c->timer_id,
88 (uintptr_t)c);
89 if (ret != LOS_OK) {
90 PRINTK("add_timer<error> timer create failed: %u \n", ret);
91 } else {
92 ret = LOS_SwtmrStart(c->timer_id);
93 if (ret != LOS_OK) {
94 PRINTK("add_timer<error> timer start failed: %u \n", ret);
95 }
96 }
97 LOS_SpinUnlockRestore(&c->lock, int_save);
98 }
99 }
100
101 void
callout_stop(struct callout * c)102 callout_stop(struct callout *c)
103 {
104 uint32_t int_save;
105 if (c != NULL) {
106 LOS_SpinLockSave(&c->lock, (unsigned int *)&int_save);
107 (void)LOS_SwtmrDelete(c->timer_id);
108 LOS_SpinUnlockRestore(&c->lock, int_save);
109 }
110 }
111
112 void
callout_drain(struct callout * c)113 callout_drain(struct callout *c)
114 {
115 if(!c || !c->tm_mtx){
116 return ;
117 }
118 mtx_lock(c->tm_mtx);
119 callout_stop(c);
120 mtx_unlock(c->tm_mtx);
121 }
122