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
);
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
, (vm_offset_t
)timer
);
179 ipc_port_release_send(port
);
183 simple_unlock(&timer
->lock
);
188 mach_port_name_t 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 mach_port_name_t name
,
213 uint64_t expire_time
)
215 uint64_t time_of_arming
;
217 ipc_space_t myspace
= current_space();
219 kern_return_t result
;
221 clock_get_uptime(&time_of_arming
);
223 result
= ipc_port_translate_receive(myspace
, name
, &port
);
224 if (result
!= KERN_SUCCESS
)
227 if (ip_kotype(port
) == IKOT_TIMER
) {
228 timer
= (mk_timer_t
)port
->ip_kobject
;
229 assert(timer
!= NULL
);
230 simple_lock(&timer
->lock
);
231 assert(timer
->port
== port
);
234 if (!timer
->is_dead
) {
235 timer
->time_of_arming
= time_of_arming
;
236 timer
->is_armed
= TRUE
;
238 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
;