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
);
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 mk_timer_t timer
= p0
;
142 simple_lock(&timer
->lock
);
144 if (timer
->active
> 1) {
146 simple_unlock(&timer
->lock
);
151 assert(port
!= IP_NULL
);
152 assert(timer
->active
== 1);
154 while (timer
->is_armed
&& timer
->active
== 1) {
155 mk_timer_expire_msg_t msg
;
157 timer
->is_armed
= FALSE
;
158 simple_unlock(&timer
->lock
);
160 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
161 msg
.header
.msgh_remote_port
= port
;
162 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
163 msg
.header
.msgh_reserved
= msg
.header
.msgh_id
= 0;
165 msg
.unused
[0] = msg
.unused
[1] = msg
.unused
[2] = 0;
167 (void) mach_msg_send_from_kernel(&msg
.header
, sizeof (msg
));
169 simple_lock(&timer
->lock
);
172 if (--timer
->active
== 0 && timer
->is_dead
) {
173 simple_unlock(&timer
->lock
);
174 zfree(mk_timer_zone
, (vm_offset_t
)timer
);
176 ipc_port_release_send(port
);
180 simple_unlock(&timer
->lock
);
185 mach_port_name_t name
)
187 ipc_space_t myspace
= current_space();
189 kern_return_t result
;
191 result
= ipc_port_translate_receive(myspace
, name
, &port
);
192 if (result
!= KERN_SUCCESS
)
195 if (ip_kotype(port
) == IKOT_TIMER
) {
197 result
= mach_port_destroy(myspace
, name
);
201 result
= KERN_INVALID_ARGUMENT
;
209 mach_port_name_t name
,
210 uint64_t expire_time
)
212 uint64_t time_of_arming
;
214 ipc_space_t myspace
= current_space();
216 kern_return_t result
;
218 clock_get_uptime(&time_of_arming
);
220 result
= ipc_port_translate_receive(myspace
, name
, &port
);
221 if (result
!= KERN_SUCCESS
)
224 if (ip_kotype(port
) == IKOT_TIMER
) {
225 timer
= (mk_timer_t
)port
->ip_kobject
;
226 assert(timer
!= NULL
);
227 simple_lock(&timer
->lock
);
228 assert(timer
->port
== port
);
231 if (!timer
->is_dead
) {
232 timer
->time_of_arming
= time_of_arming
;
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
;
251 mach_port_name_t name
,
252 uint64_t *result_time
)
254 uint64_t armed_time
= 0;
256 ipc_space_t myspace
= current_space();
258 kern_return_t result
;
260 result
= ipc_port_translate_receive(myspace
, name
, &port
);
261 if (result
!= KERN_SUCCESS
)
264 if (ip_kotype(port
) == IKOT_TIMER
) {
265 timer
= (mk_timer_t
)port
->ip_kobject
;
266 assert(timer
!= NULL
);
267 simple_lock(&timer
->lock
);
268 assert(timer
->port
== port
);
271 if (timer
->is_armed
) {
272 armed_time
= timer
->call_entry
.deadline
;
273 if (thread_call_cancel(&timer
->call_entry
))
275 timer
->is_armed
= FALSE
;
278 simple_unlock(&timer
->lock
);
282 result
= KERN_INVALID_ARGUMENT
;
285 if (result
== KERN_SUCCESS
)
286 if ( result_time
!= NULL
&&
287 copyout((void *)&armed_time
, (void *)result_time
,
288 sizeof (armed_time
)) != 0 )
289 result
= KERN_FAILURE
;