]> git.saurik.com Git - apple/xnu.git/blob - libsyscall/wrappers/work_interval.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / libsyscall / wrappers / work_interval.c
1 /*
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #include <sys/cdefs.h>
24 #include <sys/types.h>
25 #include <sys/work_interval.h>
26
27 #include <mach/mach.h>
28 #include <mach/mach_time.h>
29 #include <mach/port.h>
30
31 #include <sys/errno.h>
32 #include <stdlib.h>
33
34 struct work_interval {
35 uint64_t thread_id;
36 uint64_t work_interval_id;
37 uint32_t create_flags;
38 mach_port_t wi_port;
39 };
40
41 extern uint64_t __thread_selfid(void);
42
43 /* Create a new work interval handle (currently for the current thread only). */
44 int
45 work_interval_create(work_interval_t *interval_handle, uint32_t create_flags)
46 {
47 int ret;
48 work_interval_t handle;
49
50 if (interval_handle == NULL) {
51 errno = EINVAL;
52 return -1;
53 }
54
55 struct work_interval_create_params create_params = {
56 .wicp_create_flags = create_flags,
57 };
58
59 ret = __work_interval_ctl(WORK_INTERVAL_OPERATION_CREATE2, 0,
60 &create_params, sizeof(create_params));
61 if (ret == -1) {
62 return ret;
63 }
64
65 handle = malloc(sizeof(*handle));
66 if (handle == NULL) {
67 errno = ENOMEM;
68 return -1;
69 }
70
71 handle->thread_id = __thread_selfid();
72 handle->work_interval_id = create_params.wicp_id;
73 handle->create_flags = create_params.wicp_create_flags;
74 handle->wi_port = create_params.wicp_port;
75
76 *interval_handle = handle;
77 return 0;
78 }
79
80 int
81 work_interval_notify(work_interval_t interval_handle, uint64_t start,
82 uint64_t finish, uint64_t deadline, uint64_t next_start,
83 uint32_t notify_flags)
84 {
85 int ret;
86 uint64_t work_interval_id;
87 struct work_interval_notification notification = {
88 .start = start,
89 .finish = finish,
90 .deadline = deadline,
91 .next_start = next_start,
92 .notify_flags = notify_flags
93 };
94
95 if (interval_handle == NULL) {
96 errno = EINVAL;
97 return -1;
98 }
99
100 notification.create_flags = interval_handle->create_flags;
101 work_interval_id = interval_handle->work_interval_id;
102
103 ret = __work_interval_ctl(WORK_INTERVAL_OPERATION_NOTIFY, work_interval_id,
104 &notification, sizeof(notification));
105 return ret;
106 }
107
108 int
109 work_interval_notify_simple(work_interval_t interval_handle, uint64_t start,
110 uint64_t deadline, uint64_t next_start)
111 {
112 return work_interval_notify(interval_handle, start, mach_absolute_time(),
113 deadline, next_start, 0);
114 }
115
116 int
117 work_interval_destroy(work_interval_t interval_handle)
118 {
119 if (interval_handle == NULL) {
120 errno = EINVAL;
121 return -1;
122 }
123
124 if (interval_handle->create_flags & WORK_INTERVAL_FLAG_JOINABLE) {
125 mach_port_t wi_port = interval_handle->wi_port;
126
127 /*
128 * A joinable work interval's lifetime is tied to the port lifetime.
129 * When the last port reference is destroyed, the work interval
130 * is destroyed via no-senders notification.
131 *
132 * Note however that after destroy it can no longer be notified
133 * because the userspace token is gone.
134 *
135 * Additionally, this function does not cause the thread to un-join
136 * the interval.
137 */
138 kern_return_t kr = mach_port_deallocate(mach_task_self(), wi_port);
139
140 if (kr != KERN_SUCCESS) {
141 /*
142 * If the deallocate fails, then someone got their port
143 * lifecycle wrong and over-released a port right.
144 *
145 * Return an error so the client can assert on this,
146 * and still find the port name in the interval handle.
147 */
148 errno = EINVAL;
149 return -1;
150 }
151
152 interval_handle->wi_port = MACH_PORT_NULL;
153 interval_handle->work_interval_id = 0;
154
155 free(interval_handle);
156 return 0;
157 } else {
158 uint64_t work_interval_id = interval_handle->work_interval_id;
159
160 int ret = __work_interval_ctl(WORK_INTERVAL_OPERATION_DESTROY,
161 work_interval_id, NULL, 0);
162
163 interval_handle->work_interval_id = 0;
164
165 int saved_errno = errno;
166 free(interval_handle);
167 errno = saved_errno;
168 return ret;
169 }
170 }
171
172 int
173 work_interval_join(work_interval_t interval_handle)
174 {
175 if (interval_handle == NULL) {
176 errno = EINVAL;
177 return -1;
178 }
179
180 if ((interval_handle->create_flags & WORK_INTERVAL_FLAG_JOINABLE) == 0) {
181 errno = EINVAL;
182 return -1;
183 }
184
185 mach_port_t wi_port = interval_handle->wi_port;
186
187 if (!MACH_PORT_VALID(wi_port)) {
188 errno = EINVAL;
189 return -1;
190 }
191
192 return work_interval_join_port(wi_port);
193 }
194
195 int
196 work_interval_join_port(mach_port_t port)
197 {
198 if (port == MACH_PORT_NULL) {
199 errno = EINVAL;
200 return -1;
201 }
202
203 return __work_interval_ctl(WORK_INTERVAL_OPERATION_JOIN,
204 (uint64_t)port, NULL, 0);
205 }
206
207 int
208 work_interval_leave(void)
209 {
210 return __work_interval_ctl(WORK_INTERVAL_OPERATION_JOIN,
211 (uint64_t)MACH_PORT_NULL, NULL, 0);
212 }
213
214 int
215 work_interval_copy_port(work_interval_t interval_handle, mach_port_t *port)
216 {
217 if (port == NULL) {
218 errno = EINVAL;
219 return -1;
220 }
221
222 if (interval_handle == NULL) {
223 *port = MACH_PORT_NULL;
224 errno = EINVAL;
225 return -1;
226 }
227
228 if ((interval_handle->create_flags & WORK_INTERVAL_FLAG_JOINABLE) == 0) {
229 *port = MACH_PORT_NULL;
230 errno = EINVAL;
231 return -1;
232 }
233
234 mach_port_t wi_port = interval_handle->wi_port;
235
236 kern_return_t kr = mach_port_mod_refs(mach_task_self(), wi_port,
237 MACH_PORT_RIGHT_SEND, 1);
238
239 if (kr != KERN_SUCCESS) {
240 *port = MACH_PORT_NULL;
241 errno = EINVAL;
242 return -1;
243 }
244
245 *port = wi_port;
246
247 return 0;
248 }
249
250
251
252