xref: /xnu-8796.121.2/bsd/net/devtimer.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2004-2021 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions 
29*c54f35caSApple OSS Distributions /*
30*c54f35caSApple OSS Distributions  * devtimer.c
31*c54f35caSApple OSS Distributions  * - timer source based on <kern/thread_call.h>
32*c54f35caSApple OSS Distributions  */
33*c54f35caSApple OSS Distributions 
34*c54f35caSApple OSS Distributions /*
35*c54f35caSApple OSS Distributions  * Modification History:
36*c54f35caSApple OSS Distributions  *
37*c54f35caSApple OSS Distributions  * June 22, 2004	Dieter Siegmund ([email protected])
38*c54f35caSApple OSS Distributions  * - created
39*c54f35caSApple OSS Distributions  */
40*c54f35caSApple OSS Distributions #include <sys/param.h>
41*c54f35caSApple OSS Distributions #include <sys/kernel.h>
42*c54f35caSApple OSS Distributions #include <sys/malloc.h>
43*c54f35caSApple OSS Distributions #include <kern/thread_call.h>
44*c54f35caSApple OSS Distributions #include <net/devtimer.h>
45*c54f35caSApple OSS Distributions #include <os/refcnt.h>
46*c54f35caSApple OSS Distributions 
47*c54f35caSApple OSS Distributions #ifdef DEVTIMER_DEBUG
48*c54f35caSApple OSS Distributions #define _devtimer_printf        printf
49*c54f35caSApple OSS Distributions #else /* !DEVTIMER_DEBUG */
50*c54f35caSApple OSS Distributions static __inline__ void
_devtimer_printf(__unused const char * fmt,...)51*c54f35caSApple OSS Distributions _devtimer_printf(__unused const char * fmt, ...)
52*c54f35caSApple OSS Distributions {
53*c54f35caSApple OSS Distributions }
54*c54f35caSApple OSS Distributions #endif /* !DEVTIMER_DEBUG */
55*c54f35caSApple OSS Distributions 
56*c54f35caSApple OSS Distributions struct devtimer_s {
57*c54f35caSApple OSS Distributions 	void *                      dt_callout;
58*c54f35caSApple OSS Distributions 	devtimer_timeout_func       dt_timeout_func;
59*c54f35caSApple OSS Distributions 	devtimer_process_func       dt_process_func;
60*c54f35caSApple OSS Distributions 	void *                      dt_arg0;
61*c54f35caSApple OSS Distributions 	void *                      dt_arg1;
62*c54f35caSApple OSS Distributions 	void *                      dt_arg2;
63*c54f35caSApple OSS Distributions 	int                         dt_generation;
64*c54f35caSApple OSS Distributions 	struct os_refcnt            dt_retain_count;
65*c54f35caSApple OSS Distributions };
66*c54f35caSApple OSS Distributions 
67*c54f35caSApple OSS Distributions static __inline__ void
timeval_add(struct timeval tv1,struct timeval tv2,struct timeval * result)68*c54f35caSApple OSS Distributions timeval_add(struct timeval tv1, struct timeval tv2,
69*c54f35caSApple OSS Distributions     struct timeval * result)
70*c54f35caSApple OSS Distributions {
71*c54f35caSApple OSS Distributions 	result->tv_sec = tv1.tv_sec + tv2.tv_sec;
72*c54f35caSApple OSS Distributions 	result->tv_usec = tv1.tv_usec + tv2.tv_usec;
73*c54f35caSApple OSS Distributions 	if (result->tv_usec > DEVTIMER_USECS_PER_SEC) {
74*c54f35caSApple OSS Distributions 		result->tv_usec -= DEVTIMER_USECS_PER_SEC;
75*c54f35caSApple OSS Distributions 		result->tv_sec++;
76*c54f35caSApple OSS Distributions 	}
77*c54f35caSApple OSS Distributions 	return;
78*c54f35caSApple OSS Distributions }
79*c54f35caSApple OSS Distributions 
80*c54f35caSApple OSS Distributions static __inline__ uint64_t
timeval_to_absolutetime(struct timeval tv)81*c54f35caSApple OSS Distributions timeval_to_absolutetime(struct timeval tv)
82*c54f35caSApple OSS Distributions {
83*c54f35caSApple OSS Distributions 	uint64_t    secs;
84*c54f35caSApple OSS Distributions 	uint64_t    usecs;
85*c54f35caSApple OSS Distributions 
86*c54f35caSApple OSS Distributions 	clock_interval_to_absolutetime_interval(tv.tv_sec, NSEC_PER_SEC,
87*c54f35caSApple OSS Distributions 	    &secs);
88*c54f35caSApple OSS Distributions 	clock_interval_to_absolutetime_interval(tv.tv_usec, NSEC_PER_USEC,
89*c54f35caSApple OSS Distributions 	    &usecs);
90*c54f35caSApple OSS Distributions 	return secs + usecs;
91*c54f35caSApple OSS Distributions }
92*c54f35caSApple OSS Distributions 
93*c54f35caSApple OSS Distributions 
94*c54f35caSApple OSS Distributions __private_extern__ int
devtimer_valid(devtimer_ref timer)95*c54f35caSApple OSS Distributions devtimer_valid(devtimer_ref timer)
96*c54f35caSApple OSS Distributions {
97*c54f35caSApple OSS Distributions 	return timer->dt_callout != NULL;
98*c54f35caSApple OSS Distributions }
99*c54f35caSApple OSS Distributions 
100*c54f35caSApple OSS Distributions __private_extern__ void
devtimer_retain(devtimer_ref timer)101*c54f35caSApple OSS Distributions devtimer_retain(devtimer_ref timer)
102*c54f35caSApple OSS Distributions {
103*c54f35caSApple OSS Distributions 	os_ref_retain(&timer->dt_retain_count);
104*c54f35caSApple OSS Distributions }
105*c54f35caSApple OSS Distributions 
106*c54f35caSApple OSS Distributions __private_extern__ void
devtimer_invalidate(devtimer_ref timer)107*c54f35caSApple OSS Distributions devtimer_invalidate(devtimer_ref timer)
108*c54f35caSApple OSS Distributions {
109*c54f35caSApple OSS Distributions 	devtimer_cancel(timer);
110*c54f35caSApple OSS Distributions 	timer->dt_arg0 = NULL;
111*c54f35caSApple OSS Distributions 	if (timer->dt_callout != NULL) {
112*c54f35caSApple OSS Distributions 		thread_call_free(timer->dt_callout);
113*c54f35caSApple OSS Distributions 		timer->dt_callout = NULL;
114*c54f35caSApple OSS Distributions 	}
115*c54f35caSApple OSS Distributions 	return;
116*c54f35caSApple OSS Distributions }
117*c54f35caSApple OSS Distributions 
118*c54f35caSApple OSS Distributions __private_extern__ void
devtimer_release(devtimer_ref timer)119*c54f35caSApple OSS Distributions devtimer_release(devtimer_ref timer)
120*c54f35caSApple OSS Distributions {
121*c54f35caSApple OSS Distributions 	if (os_ref_release(&timer->dt_retain_count) == 0) {
122*c54f35caSApple OSS Distributions 		devtimer_invalidate(timer);
123*c54f35caSApple OSS Distributions 		kfree_type(struct devtimer_s, timer);
124*c54f35caSApple OSS Distributions 		_devtimer_printf("devtimer: timer released\n");
125*c54f35caSApple OSS Distributions 	}
126*c54f35caSApple OSS Distributions }
127*c54f35caSApple OSS Distributions 
128*c54f35caSApple OSS Distributions static void
devtimer_process(void * param0,void * param1)129*c54f35caSApple OSS Distributions devtimer_process(void * param0, void * param1)
130*c54f35caSApple OSS Distributions {
131*c54f35caSApple OSS Distributions 	int                         generation = *(int*)param1;
132*c54f35caSApple OSS Distributions 	devtimer_process_func       process_func;
133*c54f35caSApple OSS Distributions 	devtimer_timeout_func       timeout_func;
134*c54f35caSApple OSS Distributions 	devtimer_ref                timer = (devtimer_ref)param0;
135*c54f35caSApple OSS Distributions 
136*c54f35caSApple OSS Distributions 	process_func = timer->dt_process_func;
137*c54f35caSApple OSS Distributions 	if (process_func != NULL) {
138*c54f35caSApple OSS Distributions 		(*process_func)(timer, devtimer_process_func_event_lock);
139*c54f35caSApple OSS Distributions 	}
140*c54f35caSApple OSS Distributions 	timeout_func = timer->dt_timeout_func;
141*c54f35caSApple OSS Distributions 	if (timeout_func != NULL) {
142*c54f35caSApple OSS Distributions 		timer->dt_timeout_func = NULL;
143*c54f35caSApple OSS Distributions 		if (timer->dt_generation == generation) {
144*c54f35caSApple OSS Distributions 			(*timeout_func)(timer->dt_arg0, timer->dt_arg1, timer->dt_arg2);
145*c54f35caSApple OSS Distributions 		}
146*c54f35caSApple OSS Distributions 	}
147*c54f35caSApple OSS Distributions 	devtimer_release(timer);
148*c54f35caSApple OSS Distributions 	if (process_func != NULL) {
149*c54f35caSApple OSS Distributions 		(*process_func)(timer, devtimer_process_func_event_unlock);
150*c54f35caSApple OSS Distributions 	}
151*c54f35caSApple OSS Distributions 	return;
152*c54f35caSApple OSS Distributions }
153*c54f35caSApple OSS Distributions 
154*c54f35caSApple OSS Distributions __private_extern__ void *
devtimer_arg0(devtimer_ref timer)155*c54f35caSApple OSS Distributions devtimer_arg0(devtimer_ref timer)
156*c54f35caSApple OSS Distributions {
157*c54f35caSApple OSS Distributions 	return timer->dt_arg0;
158*c54f35caSApple OSS Distributions }
159*c54f35caSApple OSS Distributions 
160*c54f35caSApple OSS Distributions __private_extern__ devtimer_ref
devtimer_create(devtimer_process_func process_func,void * arg0)161*c54f35caSApple OSS Distributions devtimer_create(devtimer_process_func process_func, void * arg0)
162*c54f35caSApple OSS Distributions {
163*c54f35caSApple OSS Distributions 	devtimer_ref        timer;
164*c54f35caSApple OSS Distributions 
165*c54f35caSApple OSS Distributions 	timer = kalloc_type(struct devtimer_s, Z_WAITOK | Z_ZERO | Z_NOFAIL);
166*c54f35caSApple OSS Distributions 	os_ref_init(&timer->dt_retain_count, NULL);
167*c54f35caSApple OSS Distributions 	timer->dt_callout = thread_call_allocate(devtimer_process, timer);
168*c54f35caSApple OSS Distributions 	if (timer->dt_callout == NULL) {
169*c54f35caSApple OSS Distributions 		_devtimer_printf("devtimer: thread_call_allocate failed\n");
170*c54f35caSApple OSS Distributions 		devtimer_release(timer);
171*c54f35caSApple OSS Distributions 		timer = NULL;
172*c54f35caSApple OSS Distributions 	}
173*c54f35caSApple OSS Distributions 	timer->dt_process_func = process_func;
174*c54f35caSApple OSS Distributions 	timer->dt_arg0 = arg0;
175*c54f35caSApple OSS Distributions 	return timer;
176*c54f35caSApple OSS Distributions }
177*c54f35caSApple OSS Distributions 
178*c54f35caSApple OSS Distributions __private_extern__ void
devtimer_set_absolute(devtimer_ref timer,struct timeval abs_time,devtimer_timeout_func timeout_func,void * arg1,void * arg2)179*c54f35caSApple OSS Distributions devtimer_set_absolute(devtimer_ref timer,
180*c54f35caSApple OSS Distributions     struct timeval abs_time,
181*c54f35caSApple OSS Distributions     devtimer_timeout_func timeout_func,
182*c54f35caSApple OSS Distributions     void * arg1, void * arg2)
183*c54f35caSApple OSS Distributions {
184*c54f35caSApple OSS Distributions 	if (timer->dt_callout == NULL) {
185*c54f35caSApple OSS Distributions 		printf("devtimer_set_absolute: uninitialized/freed timer\n");
186*c54f35caSApple OSS Distributions 		return;
187*c54f35caSApple OSS Distributions 	}
188*c54f35caSApple OSS Distributions 	devtimer_cancel(timer);
189*c54f35caSApple OSS Distributions 	if (timeout_func == NULL) {
190*c54f35caSApple OSS Distributions 		return;
191*c54f35caSApple OSS Distributions 	}
192*c54f35caSApple OSS Distributions 	timer->dt_timeout_func = timeout_func;
193*c54f35caSApple OSS Distributions 	timer->dt_arg1 = arg1;
194*c54f35caSApple OSS Distributions 	timer->dt_arg2 = arg2;
195*c54f35caSApple OSS Distributions 	_devtimer_printf("devtimer: wakeup time is (%d.%d)\n",
196*c54f35caSApple OSS Distributions 	    abs_time.tv_sec, abs_time.tv_usec);
197*c54f35caSApple OSS Distributions 	timer->dt_generation++;
198*c54f35caSApple OSS Distributions 	devtimer_retain(timer);
199*c54f35caSApple OSS Distributions 	thread_call_enter1_delayed(timer->dt_callout,
200*c54f35caSApple OSS Distributions 	    &timer->dt_generation,
201*c54f35caSApple OSS Distributions 	    timeval_to_absolutetime(abs_time));
202*c54f35caSApple OSS Distributions 	return;
203*c54f35caSApple OSS Distributions }
204*c54f35caSApple OSS Distributions 
205*c54f35caSApple OSS Distributions __private_extern__ void
devtimer_set_relative(devtimer_ref timer,struct timeval rel_time,devtimer_timeout_func timeout_func,void * arg1,void * arg2)206*c54f35caSApple OSS Distributions devtimer_set_relative(devtimer_ref timer,
207*c54f35caSApple OSS Distributions     struct timeval rel_time,
208*c54f35caSApple OSS Distributions     devtimer_timeout_func timeout_func,
209*c54f35caSApple OSS Distributions     void * arg1, void * arg2)
210*c54f35caSApple OSS Distributions {
211*c54f35caSApple OSS Distributions 	struct timeval              abs_time;
212*c54f35caSApple OSS Distributions 	struct timeval              current_time;
213*c54f35caSApple OSS Distributions 
214*c54f35caSApple OSS Distributions 	current_time = devtimer_current_time();
215*c54f35caSApple OSS Distributions 	timeval_add(current_time, rel_time, &abs_time);
216*c54f35caSApple OSS Distributions 	devtimer_set_absolute(timer, abs_time, timeout_func, arg1, arg2);
217*c54f35caSApple OSS Distributions 	return;
218*c54f35caSApple OSS Distributions }
219*c54f35caSApple OSS Distributions 
220*c54f35caSApple OSS Distributions __private_extern__ void
devtimer_cancel(devtimer_ref timer)221*c54f35caSApple OSS Distributions devtimer_cancel(devtimer_ref timer)
222*c54f35caSApple OSS Distributions {
223*c54f35caSApple OSS Distributions 	if (timer->dt_timeout_func != NULL) {
224*c54f35caSApple OSS Distributions 		timer->dt_timeout_func = NULL;
225*c54f35caSApple OSS Distributions 		if (timer->dt_callout != NULL) {
226*c54f35caSApple OSS Distributions 			_devtimer_printf("devtimer: cancelling timer source\n");
227*c54f35caSApple OSS Distributions 			if (thread_call_cancel(timer->dt_callout)) {
228*c54f35caSApple OSS Distributions 				devtimer_release(timer);
229*c54f35caSApple OSS Distributions 			} else {
230*c54f35caSApple OSS Distributions 				_devtimer_printf("devtimer: delayed release\n");
231*c54f35caSApple OSS Distributions 			}
232*c54f35caSApple OSS Distributions 		}
233*c54f35caSApple OSS Distributions 	}
234*c54f35caSApple OSS Distributions 	return;
235*c54f35caSApple OSS Distributions }
236*c54f35caSApple OSS Distributions 
237*c54f35caSApple OSS Distributions __private_extern__ int
devtimer_enabled(devtimer_ref timer)238*c54f35caSApple OSS Distributions devtimer_enabled(devtimer_ref timer)
239*c54f35caSApple OSS Distributions {
240*c54f35caSApple OSS Distributions 	return timer->dt_timeout_func != NULL;
241*c54f35caSApple OSS Distributions }
242*c54f35caSApple OSS Distributions 
243*c54f35caSApple OSS Distributions __private_extern__ int32_t
devtimer_current_secs(void)244*c54f35caSApple OSS Distributions devtimer_current_secs(void)
245*c54f35caSApple OSS Distributions {
246*c54f35caSApple OSS Distributions 	struct timeval      tv;
247*c54f35caSApple OSS Distributions 
248*c54f35caSApple OSS Distributions 	tv = devtimer_current_time();
249*c54f35caSApple OSS Distributions 	return tv.tv_sec;
250*c54f35caSApple OSS Distributions }
251*c54f35caSApple OSS Distributions 
252*c54f35caSApple OSS Distributions __private_extern__ struct timeval
devtimer_current_time(void)253*c54f35caSApple OSS Distributions devtimer_current_time(void)
254*c54f35caSApple OSS Distributions {
255*c54f35caSApple OSS Distributions 	struct timeval      tv;
256*c54f35caSApple OSS Distributions 	clock_sec_t sec;
257*c54f35caSApple OSS Distributions 	clock_usec_t usec;
258*c54f35caSApple OSS Distributions 
259*c54f35caSApple OSS Distributions 	clock_get_system_microtime(&sec, &usec);
260*c54f35caSApple OSS Distributions 	tv.tv_sec = sec;
261*c54f35caSApple OSS Distributions 	tv.tv_usec = usec;
262*c54f35caSApple OSS Distributions 	return tv;
263*c54f35caSApple OSS Distributions }
264