2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
31 #include <mach/mach_types.h>
32 #include <mach/mach_port_server.h>
34 #include <mach/mk_timer.h>
36 #include <ipc/ipc_space.h>
38 #include <kern/mk_timer.h>
39 #include <kern/thread_call.h>
41 static zone_t mk_timer_zone
;
43 static mach_port_qos_t mk_timer_qos
= {
44 FALSE
, TRUE
, 0, sizeof (mk_timer_expire_msg_t
)
47 static void mk_timer_expire(
55 ipc_space_t myspace
= current_space();
56 mach_port_name_t name
= MACH_PORT_NULL
;
60 timer
= (mk_timer_t
)zalloc(mk_timer_zone
);
62 return (MACH_PORT_NULL
);
64 result
= mach_port_allocate_qos(myspace
, MACH_PORT_RIGHT_RECEIVE
,
65 &mk_timer_qos
, &name
);
66 if (result
== KERN_SUCCESS
)
67 result
= ipc_port_translate_receive(myspace
, name
, &port
);
69 if (result
!= KERN_SUCCESS
) {
70 zfree(mk_timer_zone
, (vm_offset_t
)timer
);
72 return (MACH_PORT_NULL
);
75 simple_lock_init(&timer
->lock
, ETAP_MISC_TIMER
);
76 call_entry_setup(&timer
->call_entry
, mk_timer_expire
, timer
);
77 timer
->is_armed
= timer
->is_dead
= FALSE
;
81 ipc_kobject_set_atomically(port
, (ipc_kobject_t
)timer
, IKOT_TIMER
);
91 mk_timer_port_destroy(
94 mk_timer_t timer
= NULL
;
97 if (ip_kotype(port
) == IKOT_TIMER
) {
98 timer
= (mk_timer_t
)port
->ip_kobject
;
99 assert(timer
!= NULL
);
100 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
101 simple_lock(&timer
->lock
);
102 assert(timer
->port
== port
);
107 if (thread_call_cancel(&timer
->call_entry
))
109 timer
->is_armed
= FALSE
;
111 timer
->is_dead
= TRUE
;
112 if (timer
->active
== 0) {
113 simple_unlock(&timer
->lock
);
114 zfree(mk_timer_zone
, (vm_offset_t
)timer
);
116 ipc_port_release_send(port
);
120 simple_unlock(&timer
->lock
);
125 mk_timer_initialize(void)
127 int s
= sizeof (mk_timer_data_t
);
129 assert(!(mk_timer_zone
!= NULL
));
131 mk_timer_zone
= zinit(s
, (4096 * s
), (16 * s
), "mk_timer");
139 uint64_t time_of_posting
;
140 mk_timer_t timer
= p0
;
143 clock_get_uptime(&time_of_posting
);
145 simple_lock(&timer
->lock
);
147 if (timer
->active
> 1) {
149 simple_unlock(&timer
->lock
);
154 assert(port
!= IP_NULL
);
156 while ( timer
->is_armed
&&
157 !thread_call_is_delayed(&timer
->call_entry
, NULL
) ) {
158 mk_timer_expire_msg_t msg
;
160 timer
->is_armed
= FALSE
;
162 msg
.time_of_arming
= timer
->time_of_arming
;
163 msg
.armed_time
= timer
->call_entry
.deadline
;
164 msg
.time_of_posting
= time_of_posting
;
166 simple_unlock(&timer
->lock
);
168 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
169 msg
.header
.msgh_remote_port
= port
;
170 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
171 msg
.header
.msgh_reserved
= msg
.header
.msgh_id
= 0;
173 (void) mach_msg_send_from_kernel(&msg
.header
, sizeof (msg
));
175 simple_lock(&timer
->lock
);
178 if (--timer
->active
== 0 && timer
->is_dead
) {
179 simple_unlock(&timer
->lock
);
180 zfree(mk_timer_zone
, (vm_offset_t
)timer
);
182 ipc_port_release_send(port
);
186 simple_unlock(&timer
->lock
);
191 mach_port_name_t name
)
193 ipc_space_t myspace
= current_space();
195 kern_return_t result
;
197 result
= ipc_port_translate_receive(myspace
, name
, &port
);
198 if (result
!= KERN_SUCCESS
)
201 if (ip_kotype(port
) == IKOT_TIMER
) {
203 result
= mach_port_destroy(myspace
, name
);
207 result
= KERN_INVALID_ARGUMENT
;
215 mach_port_name_t name
,
216 uint64_t expire_time
)
218 uint64_t time_of_arming
;
220 ipc_space_t myspace
= current_space();
222 kern_return_t result
;
224 clock_get_uptime(&time_of_arming
);
226 result
= ipc_port_translate_receive(myspace
, name
, &port
);
227 if (result
!= KERN_SUCCESS
)
230 if (ip_kotype(port
) == IKOT_TIMER
) {
231 timer
= (mk_timer_t
)port
->ip_kobject
;
232 assert(timer
!= NULL
);
233 simple_lock(&timer
->lock
);
234 assert(timer
->port
== port
);
237 timer
->time_of_arming
= time_of_arming
;
238 timer
->is_armed
= TRUE
;
240 if (!thread_call_enter_delayed(&timer
->call_entry
, expire_time
))
242 simple_unlock(&timer
->lock
);
246 result
= KERN_INVALID_ARGUMENT
;
254 mach_port_name_t name
,
255 uint64_t *result_time
)
257 uint64_t armed_time
= 0;
259 ipc_space_t myspace
= current_space();
261 kern_return_t result
;
263 result
= ipc_port_translate_receive(myspace
, name
, &port
);
264 if (result
!= KERN_SUCCESS
)
267 if (ip_kotype(port
) == IKOT_TIMER
) {
268 timer
= (mk_timer_t
)port
->ip_kobject
;
269 assert(timer
!= NULL
);
270 simple_lock(&timer
->lock
);
271 assert(timer
->port
== port
);
274 if (timer
->is_armed
) {
275 armed_time
= timer
->call_entry
.deadline
;
276 if (thread_call_cancel(&timer
->call_entry
))
278 timer
->is_armed
= FALSE
;
281 simple_unlock(&timer
->lock
);
285 result
= KERN_INVALID_ARGUMENT
;
288 if (result
== KERN_SUCCESS
)
289 if ( result_time
!= NULL
&&
290 copyout((void *)&armed_time
, (void *)result_time
,
291 sizeof (armed_time
)) != 0 )
292 result
= KERN_FAILURE
;