2 * Copyright (c) 2000-2004 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_traps.h>
33 #include <mach/mach_port_server.h>
35 #include <mach/mk_timer.h>
37 #include <ipc/ipc_space.h>
39 #include <kern/mk_timer.h>
40 #include <kern/thread_call.h>
42 static zone_t mk_timer_zone
;
44 static mach_port_qos_t mk_timer_qos
= {
45 FALSE
, TRUE
, 0, sizeof (mk_timer_expire_msg_t
)
48 static void mk_timer_expire(
54 __unused
struct mk_timer_create_trap_args
*args
)
57 ipc_space_t myspace
= current_space();
58 mach_port_name_t name
= MACH_PORT_NULL
;
62 timer
= (mk_timer_t
)zalloc(mk_timer_zone
);
64 return (MACH_PORT_NULL
);
66 result
= mach_port_allocate_qos(myspace
, MACH_PORT_RIGHT_RECEIVE
,
67 &mk_timer_qos
, &name
);
68 if (result
== KERN_SUCCESS
)
69 result
= ipc_port_translate_receive(myspace
, name
, &port
);
71 if (result
!= KERN_SUCCESS
) {
72 zfree(mk_timer_zone
, timer
);
74 return (MACH_PORT_NULL
);
77 simple_lock_init(&timer
->lock
, 0);
78 call_entry_setup(&timer
->call_entry
, mk_timer_expire
, timer
);
79 timer
->is_armed
= timer
->is_dead
= FALSE
;
83 ipc_kobject_set_atomically(port
, (ipc_kobject_t
)timer
, IKOT_TIMER
);
93 mk_timer_port_destroy(
96 mk_timer_t timer
= NULL
;
99 if (ip_kotype(port
) == IKOT_TIMER
) {
100 timer
= (mk_timer_t
)port
->ip_kobject
;
101 assert(timer
!= NULL
);
102 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
103 simple_lock(&timer
->lock
);
104 assert(timer
->port
== port
);
109 if (thread_call_cancel(&timer
->call_entry
))
111 timer
->is_armed
= FALSE
;
113 timer
->is_dead
= TRUE
;
114 if (timer
->active
== 0) {
115 simple_unlock(&timer
->lock
);
116 zfree(mk_timer_zone
, timer
);
118 ipc_port_release_send(port
);
122 simple_unlock(&timer
->lock
);
129 int s
= sizeof (mk_timer_data_t
);
131 assert(!(mk_timer_zone
!= NULL
));
133 mk_timer_zone
= zinit(s
, (4096 * s
), (16 * s
), "mk_timer");
141 mk_timer_t timer
= p0
;
144 simple_lock(&timer
->lock
);
146 if (timer
->active
> 1) {
148 simple_unlock(&timer
->lock
);
153 assert(port
!= IP_NULL
);
154 assert(timer
->active
== 1);
156 while (timer
->is_armed
&& timer
->active
== 1) {
157 mk_timer_expire_msg_t msg
;
159 timer
->is_armed
= FALSE
;
160 simple_unlock(&timer
->lock
);
162 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
163 msg
.header
.msgh_remote_port
= port
;
164 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
165 msg
.header
.msgh_reserved
= msg
.header
.msgh_id
= 0;
167 msg
.unused
[0] = msg
.unused
[1] = msg
.unused
[2] = 0;
169 (void) mach_msg_send_from_kernel(&msg
.header
, sizeof (msg
));
171 simple_lock(&timer
->lock
);
174 if (--timer
->active
== 0 && timer
->is_dead
) {
175 simple_unlock(&timer
->lock
);
176 zfree(mk_timer_zone
, timer
);
178 ipc_port_release_send(port
);
182 simple_unlock(&timer
->lock
);
186 mk_timer_destroy_trap(
187 struct mk_timer_destroy_trap_args
*args
)
189 mach_port_name_t name
= args
->name
;
190 ipc_space_t myspace
= current_space();
192 kern_return_t result
;
194 result
= ipc_port_translate_receive(myspace
, name
, &port
);
195 if (result
!= KERN_SUCCESS
)
198 if (ip_kotype(port
) == IKOT_TIMER
) {
200 result
= mach_port_destroy(myspace
, name
);
204 result
= KERN_INVALID_ARGUMENT
;
212 struct mk_timer_arm_trap_args
*args
)
214 mach_port_name_t name
= args
->name
;
215 uint64_t expire_time
= args
->expire_time
;
217 ipc_space_t myspace
= current_space();
219 kern_return_t result
;
221 result
= ipc_port_translate_receive(myspace
, name
, &port
);
222 if (result
!= KERN_SUCCESS
)
225 if (ip_kotype(port
) == IKOT_TIMER
) {
226 timer
= (mk_timer_t
)port
->ip_kobject
;
227 assert(timer
!= NULL
);
228 simple_lock(&timer
->lock
);
229 assert(timer
->port
== port
);
232 if (!timer
->is_dead
) {
233 timer
->is_armed
= TRUE
;
235 if (!thread_call_enter_delayed(&timer
->call_entry
, expire_time
))
239 simple_unlock(&timer
->lock
);
243 result
= KERN_INVALID_ARGUMENT
;
250 mk_timer_cancel_trap(
251 struct mk_timer_cancel_trap_args
*args
)
253 mach_port_name_t name
= args
->name
;
254 mach_vm_address_t result_time_addr
= args
->result_time
;
255 uint64_t armed_time
= 0;
257 ipc_space_t myspace
= current_space();
259 kern_return_t result
;
261 result
= ipc_port_translate_receive(myspace
, name
, &port
);
262 if (result
!= KERN_SUCCESS
)
265 if (ip_kotype(port
) == IKOT_TIMER
) {
266 timer
= (mk_timer_t
)port
->ip_kobject
;
267 assert(timer
!= NULL
);
268 simple_lock(&timer
->lock
);
269 assert(timer
->port
== port
);
272 if (timer
->is_armed
) {
273 armed_time
= timer
->call_entry
.deadline
;
274 if (thread_call_cancel(&timer
->call_entry
))
276 timer
->is_armed
= FALSE
;
279 simple_unlock(&timer
->lock
);
283 result
= KERN_INVALID_ARGUMENT
;
286 if (result
== KERN_SUCCESS
)
287 if ( result_time_addr
!= 0 &&
288 copyout((void *)&armed_time
, result_time_addr
,
289 sizeof (armed_time
)) != 0 )
290 result
= KERN_FAILURE
;