2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
32 #include <mach/mach_types.h>
33 #include <mach/mach_traps.h>
34 #include <mach/mach_port_server.h>
36 #include <mach/mk_timer.h>
38 #include <ipc/ipc_space.h>
40 #include <kern/mk_timer.h>
41 #include <kern/thread_call.h>
43 static zone_t mk_timer_zone
;
45 static mach_port_qos_t mk_timer_qos
= {
46 FALSE
, TRUE
, 0, sizeof (mk_timer_expire_msg_t
)
49 static void mk_timer_expire(
55 __unused
struct mk_timer_create_trap_args
*args
)
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
, timer
);
75 return (MACH_PORT_NULL
);
78 simple_lock_init(&timer
->lock
, 0);
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
, timer
);
119 ipc_port_release_send(port
);
123 simple_unlock(&timer
->lock
);
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 mk_timer_t timer
= p0
;
145 simple_lock(&timer
->lock
);
147 if (timer
->active
> 1) {
149 simple_unlock(&timer
->lock
);
154 assert(port
!= IP_NULL
);
155 assert(timer
->active
== 1);
157 while (timer
->is_armed
&& timer
->active
== 1) {
158 mk_timer_expire_msg_t msg
;
160 timer
->is_armed
= FALSE
;
161 simple_unlock(&timer
->lock
);
163 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
164 msg
.header
.msgh_remote_port
= port
;
165 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
166 msg
.header
.msgh_reserved
= msg
.header
.msgh_id
= 0;
168 msg
.unused
[0] = msg
.unused
[1] = msg
.unused
[2] = 0;
170 (void) mach_msg_send_from_kernel(&msg
.header
, sizeof (msg
));
172 simple_lock(&timer
->lock
);
175 if (--timer
->active
== 0 && timer
->is_dead
) {
176 simple_unlock(&timer
->lock
);
177 zfree(mk_timer_zone
, timer
);
179 ipc_port_release_send(port
);
183 simple_unlock(&timer
->lock
);
187 mk_timer_destroy_trap(
188 struct mk_timer_destroy_trap_args
*args
)
190 mach_port_name_t name
= args
->name
;
191 ipc_space_t myspace
= current_space();
193 kern_return_t result
;
195 result
= ipc_port_translate_receive(myspace
, name
, &port
);
196 if (result
!= KERN_SUCCESS
)
199 if (ip_kotype(port
) == IKOT_TIMER
) {
201 result
= mach_port_destroy(myspace
, name
);
205 result
= KERN_INVALID_ARGUMENT
;
213 struct mk_timer_arm_trap_args
*args
)
215 mach_port_name_t name
= args
->name
;
216 uint64_t expire_time
= args
->expire_time
;
218 ipc_space_t myspace
= current_space();
220 kern_return_t result
;
222 result
= ipc_port_translate_receive(myspace
, name
, &port
);
223 if (result
!= KERN_SUCCESS
)
226 if (ip_kotype(port
) == IKOT_TIMER
) {
227 timer
= (mk_timer_t
)port
->ip_kobject
;
228 assert(timer
!= NULL
);
229 simple_lock(&timer
->lock
);
230 assert(timer
->port
== port
);
233 if (!timer
->is_dead
) {
234 timer
->is_armed
= TRUE
;
236 if (!thread_call_enter_delayed(&timer
->call_entry
, expire_time
))
240 simple_unlock(&timer
->lock
);
244 result
= KERN_INVALID_ARGUMENT
;
251 mk_timer_cancel_trap(
252 struct mk_timer_cancel_trap_args
*args
)
254 mach_port_name_t name
= args
->name
;
255 mach_vm_address_t result_time_addr
= args
->result_time
;
256 uint64_t armed_time
= 0;
258 ipc_space_t myspace
= current_space();
260 kern_return_t result
;
262 result
= ipc_port_translate_receive(myspace
, name
, &port
);
263 if (result
!= KERN_SUCCESS
)
266 if (ip_kotype(port
) == IKOT_TIMER
) {
267 timer
= (mk_timer_t
)port
->ip_kobject
;
268 assert(timer
!= NULL
);
269 simple_lock(&timer
->lock
);
270 assert(timer
->port
== port
);
273 if (timer
->is_armed
) {
274 armed_time
= timer
->call_entry
.deadline
;
275 if (thread_call_cancel(&timer
->call_entry
))
277 timer
->is_armed
= FALSE
;
280 simple_unlock(&timer
->lock
);
284 result
= KERN_INVALID_ARGUMENT
;
287 if (result
== KERN_SUCCESS
)
288 if ( result_time_addr
!= 0 &&
289 copyout((void *)&armed_time
, result_time_addr
,
290 sizeof (armed_time
)) != 0 )
291 result
= KERN_FAILURE
;