2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
34 #include <mach/mach_types.h>
35 #include <mach/mach_port_server.h>
37 #include <mach/mk_timer.h>
39 #include <ipc/ipc_space.h>
41 #include <kern/mk_timer.h>
42 #include <kern/thread_call.h>
44 static zone_t mk_timer_zone
;
46 static mach_port_qos_t mk_timer_qos
= {
47 FALSE
, TRUE
, 0, sizeof (mk_timer_expire_msg_t
)
50 static void mk_timer_expire(
58 ipc_space_t myspace
= current_space();
59 mach_port_name_t name
= MACH_PORT_NULL
;
63 timer
= (mk_timer_t
)zalloc(mk_timer_zone
);
65 return (MACH_PORT_NULL
);
67 result
= mach_port_allocate_qos(myspace
, MACH_PORT_RIGHT_RECEIVE
,
68 &mk_timer_qos
, &name
);
69 if (result
== KERN_SUCCESS
)
70 result
= ipc_port_translate_receive(myspace
, name
, &port
);
72 if (result
!= KERN_SUCCESS
) {
73 zfree(mk_timer_zone
, (vm_offset_t
)timer
);
75 return (MACH_PORT_NULL
);
78 simple_lock_init(&timer
->lock
, ETAP_MISC_TIMER
);
79 call_entry_setup(&timer
->call_entry
, mk_timer_expire
, timer
);
80 timer
->is_armed
= timer
->is_dead
= FALSE
;
84 ipc_kobject_set_atomically(port
, (ipc_kobject_t
)timer
, IKOT_TIMER
);
94 mk_timer_port_destroy(
97 mk_timer_t timer
= NULL
;
100 if (ip_kotype(port
) == IKOT_TIMER
) {
101 timer
= (mk_timer_t
)port
->ip_kobject
;
102 assert(timer
!= NULL
);
103 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
104 simple_lock(&timer
->lock
);
105 assert(timer
->port
== port
);
110 if (thread_call_cancel(&timer
->call_entry
))
112 timer
->is_armed
= FALSE
;
114 timer
->is_dead
= TRUE
;
115 if (timer
->active
== 0) {
116 simple_unlock(&timer
->lock
);
117 zfree(mk_timer_zone
, (vm_offset_t
)timer
);
119 ipc_port_release_send(port
);
123 simple_unlock(&timer
->lock
);
128 mk_timer_initialize(void)
130 int s
= sizeof (mk_timer_data_t
);
132 assert(!(mk_timer_zone
!= NULL
));
134 mk_timer_zone
= zinit(s
, (4096 * s
), (16 * s
), "mk_timer");
142 uint64_t time_of_posting
;
143 mk_timer_t timer
= p0
;
146 clock_get_uptime(&time_of_posting
);
148 simple_lock(&timer
->lock
);
150 if (timer
->active
> 1) {
152 simple_unlock(&timer
->lock
);
157 assert(port
!= IP_NULL
);
159 while ( timer
->is_armed
&&
160 !thread_call_is_delayed(&timer
->call_entry
, NULL
) ) {
161 mk_timer_expire_msg_t msg
;
163 timer
->is_armed
= FALSE
;
165 msg
.time_of_arming
= timer
->time_of_arming
;
166 msg
.armed_time
= timer
->call_entry
.deadline
;
167 msg
.time_of_posting
= time_of_posting
;
169 simple_unlock(&timer
->lock
);
171 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
172 msg
.header
.msgh_remote_port
= port
;
173 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
174 msg
.header
.msgh_reserved
= msg
.header
.msgh_id
= 0;
176 (void) mach_msg_send_from_kernel(&msg
.header
, sizeof (msg
));
178 simple_lock(&timer
->lock
);
181 if (--timer
->active
== 0 && timer
->is_dead
) {
182 simple_unlock(&timer
->lock
);
183 zfree(mk_timer_zone
, (vm_offset_t
)timer
);
185 ipc_port_release_send(port
);
189 simple_unlock(&timer
->lock
);
194 mach_port_name_t name
)
196 ipc_space_t myspace
= current_space();
198 kern_return_t result
;
200 result
= ipc_port_translate_receive(myspace
, name
, &port
);
201 if (result
!= KERN_SUCCESS
)
204 if (ip_kotype(port
) == IKOT_TIMER
) {
206 result
= mach_port_destroy(myspace
, name
);
210 result
= KERN_INVALID_ARGUMENT
;
218 mach_port_name_t name
,
219 uint64_t expire_time
)
221 uint64_t time_of_arming
;
223 ipc_space_t myspace
= current_space();
225 kern_return_t result
;
227 clock_get_uptime(&time_of_arming
);
229 result
= ipc_port_translate_receive(myspace
, name
, &port
);
230 if (result
!= KERN_SUCCESS
)
233 if (ip_kotype(port
) == IKOT_TIMER
) {
234 timer
= (mk_timer_t
)port
->ip_kobject
;
235 assert(timer
!= NULL
);
236 simple_lock(&timer
->lock
);
237 assert(timer
->port
== port
);
240 timer
->time_of_arming
= time_of_arming
;
241 timer
->is_armed
= TRUE
;
243 if (!thread_call_enter_delayed(&timer
->call_entry
, expire_time
))
245 simple_unlock(&timer
->lock
);
249 result
= KERN_INVALID_ARGUMENT
;
257 mach_port_name_t name
,
258 uint64_t *result_time
)
260 uint64_t armed_time
= 0;
262 ipc_space_t myspace
= current_space();
264 kern_return_t result
;
266 result
= ipc_port_translate_receive(myspace
, name
, &port
);
267 if (result
!= KERN_SUCCESS
)
270 if (ip_kotype(port
) == IKOT_TIMER
) {
271 timer
= (mk_timer_t
)port
->ip_kobject
;
272 assert(timer
!= NULL
);
273 simple_lock(&timer
->lock
);
274 assert(timer
->port
== port
);
277 if (timer
->is_armed
) {
278 armed_time
= timer
->call_entry
.deadline
;
279 if (thread_call_cancel(&timer
->call_entry
))
281 timer
->is_armed
= FALSE
;
284 simple_unlock(&timer
->lock
);
288 result
= KERN_INVALID_ARGUMENT
;
291 if (result
== KERN_SUCCESS
)
292 if ( result_time
!= NULL
&&
293 copyout((void *)&armed_time
, (void *)result_time
,
294 sizeof (armed_time
)) != 0 )
295 result
= KERN_FAILURE
;