2 * Copyright (c) 2015 Apple 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@
23 #include <sys/cdefs.h>
24 #include <sys/types.h>
25 #include <sys/work_interval.h>
27 #include <mach/mach.h>
28 #include <mach/mach_time.h>
29 #include <mach/port.h>
31 #include <sys/errno.h>
35 struct work_interval
{
37 uint64_t work_interval_id
;
38 uint32_t create_flags
;
42 extern uint64_t __thread_selfid(void);
44 /* Create a new work interval handle (currently for the current thread only). */
46 work_interval_create(work_interval_t
*interval_handle
, uint32_t create_flags
)
49 work_interval_t handle
;
51 if (interval_handle
== NULL
) {
56 struct work_interval_create_params create_params
= {
57 .wicp_create_flags
= create_flags
,
60 ret
= __work_interval_ctl(WORK_INTERVAL_OPERATION_CREATE2
, 0,
61 &create_params
, sizeof(create_params
));
66 handle
= malloc(sizeof(*handle
));
72 handle
->thread_id
= __thread_selfid();
73 handle
->work_interval_id
= create_params
.wicp_id
;
74 handle
->create_flags
= create_params
.wicp_create_flags
;
75 handle
->wi_port
= create_params
.wicp_port
;
77 *interval_handle
= handle
;
82 work_interval_get_flags_from_port(mach_port_t port
, uint32_t *flags
)
84 if (!MACH_PORT_VALID(port
) || flags
== NULL
) {
89 struct work_interval_create_params create_params
= { 0 };
91 int ret
= __work_interval_ctl(WORK_INTERVAL_OPERATION_GET_FLAGS
, port
,
92 &create_params
, sizeof(create_params
));
97 *flags
= create_params
.wicp_create_flags
;
102 work_interval_notify(work_interval_t interval_handle
, uint64_t start
,
103 uint64_t finish
, uint64_t deadline
, uint64_t next_start
,
104 uint32_t notify_flags
)
107 uint64_t work_interval_id
;
108 struct work_interval_notification notification
= {
111 .deadline
= deadline
,
112 .next_start
= next_start
,
113 .notify_flags
= notify_flags
116 if (interval_handle
== NULL
) {
121 if (interval_handle
->create_flags
& WORK_INTERVAL_FLAG_IGNORED
) {
125 notification
.create_flags
= interval_handle
->create_flags
;
126 work_interval_id
= interval_handle
->work_interval_id
;
128 ret
= __work_interval_ctl(WORK_INTERVAL_OPERATION_NOTIFY
, work_interval_id
,
129 ¬ification
, sizeof(notification
));
134 work_interval_notify_simple(work_interval_t interval_handle
, uint64_t start
,
135 uint64_t deadline
, uint64_t next_start
)
137 return work_interval_notify(interval_handle
, start
, mach_absolute_time(),
138 deadline
, next_start
, 0);
143 work_interval_destroy(work_interval_t interval_handle
)
145 if (interval_handle
== NULL
) {
150 if (interval_handle
->create_flags
& WORK_INTERVAL_FLAG_JOINABLE
) {
151 mach_port_t wi_port
= interval_handle
->wi_port
;
154 * A joinable work interval's lifetime is tied to the port lifetime.
155 * When the last port reference is destroyed, the work interval
156 * is destroyed via no-senders notification.
158 * Note however that after destroy it can no longer be notified
159 * because the userspace token is gone.
161 * Additionally, this function does not cause the thread to un-join
164 kern_return_t kr
= mach_port_deallocate(mach_task_self(), wi_port
);
166 if (kr
!= KERN_SUCCESS
) {
168 * If the deallocate fails, then someone got their port
169 * lifecycle wrong and over-released a port right.
171 * Return an error so the client can assert on this,
172 * and still find the port name in the interval handle.
178 interval_handle
->wi_port
= MACH_PORT_NULL
;
179 interval_handle
->work_interval_id
= 0;
181 free(interval_handle
);
184 uint64_t work_interval_id
= interval_handle
->work_interval_id
;
186 int ret
= __work_interval_ctl(WORK_INTERVAL_OPERATION_DESTROY
,
187 work_interval_id
, NULL
, 0);
189 interval_handle
->work_interval_id
= 0;
191 int saved_errno
= errno
;
192 free(interval_handle
);
199 work_interval_join(work_interval_t interval_handle
)
201 if (interval_handle
== NULL
) {
206 if ((interval_handle
->create_flags
& WORK_INTERVAL_FLAG_JOINABLE
) == 0) {
211 mach_port_t wi_port
= interval_handle
->wi_port
;
213 if (!MACH_PORT_VALID(wi_port
)) {
218 return work_interval_join_port(wi_port
);
222 work_interval_join_port(mach_port_t port
)
224 if (port
== MACH_PORT_NULL
) {
229 return __work_interval_ctl(WORK_INTERVAL_OPERATION_JOIN
,
230 (uint64_t)port
, NULL
, 0);
234 work_interval_leave(void)
236 return __work_interval_ctl(WORK_INTERVAL_OPERATION_JOIN
,
237 (uint64_t)MACH_PORT_NULL
, NULL
, 0);
241 work_interval_copy_port(work_interval_t interval_handle
, mach_port_t
*port
)
248 if (interval_handle
== NULL
) {
249 *port
= MACH_PORT_NULL
;
254 if ((interval_handle
->create_flags
& WORK_INTERVAL_FLAG_JOINABLE
) == 0) {
255 *port
= MACH_PORT_NULL
;
260 mach_port_t wi_port
= interval_handle
->wi_port
;
262 kern_return_t kr
= mach_port_mod_refs(mach_task_self(), wi_port
,
263 MACH_PORT_RIGHT_SEND
, 1);
265 if (kr
!= KERN_SUCCESS
) {
266 *port
= MACH_PORT_NULL
;