Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | /* * Copyright (c) 2015 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <sys/cdefs.h> #include <sys/types.h> #include <sys/work_interval.h> #include <mach/mach.h> #include <mach/mach_time.h> #include <mach/port.h> #include <sys/errno.h> #include <stdlib.h> #include <strings.h> struct work_interval { uint64_t thread_id; uint64_t work_interval_id; uint32_t create_flags; mach_port_t wi_port; }; extern uint64_t __thread_selfid(void); /* Create a new work interval handle (currently for the current thread only). */ int work_interval_create(work_interval_t *interval_handle, uint32_t create_flags) { int ret; work_interval_t handle; if (interval_handle == NULL) { errno = EINVAL; return -1; } struct work_interval_create_params create_params = { .wicp_create_flags = create_flags, }; ret = __work_interval_ctl(WORK_INTERVAL_OPERATION_CREATE2, 0, &create_params, sizeof(create_params)); if (ret == -1) { return ret; } handle = malloc(sizeof(*handle)); if (handle == NULL) { errno = ENOMEM; return -1; } handle->thread_id = __thread_selfid(); handle->work_interval_id = create_params.wicp_id; handle->create_flags = create_params.wicp_create_flags; handle->wi_port = create_params.wicp_port; *interval_handle = handle; return 0; } int work_interval_notify(work_interval_t interval_handle, uint64_t start, uint64_t finish, uint64_t deadline, uint64_t next_start, uint32_t notify_flags) { int ret; uint64_t work_interval_id; struct work_interval_notification notification = { .start = start, .finish = finish, .deadline = deadline, .next_start = next_start, .notify_flags = notify_flags }; if (interval_handle == NULL) { errno = EINVAL; return -1; } notification.create_flags = interval_handle->create_flags; work_interval_id = interval_handle->work_interval_id; ret = __work_interval_ctl(WORK_INTERVAL_OPERATION_NOTIFY, work_interval_id, ¬ification, sizeof(notification)); return ret; } int work_interval_notify_simple(work_interval_t interval_handle, uint64_t start, uint64_t deadline, uint64_t next_start) { return work_interval_notify(interval_handle, start, mach_absolute_time(), deadline, next_start, 0); } int work_interval_destroy(work_interval_t interval_handle) { if (interval_handle == NULL) { errno = EINVAL; return -1; } if (interval_handle->create_flags & WORK_INTERVAL_FLAG_JOINABLE) { mach_port_t wi_port = interval_handle->wi_port; /* * A joinable work interval's lifetime is tied to the port lifetime. * When the last port reference is destroyed, the work interval * is destroyed via no-senders notification. * * Note however that after destroy it can no longer be notified * because the userspace token is gone. * * Additionally, this function does not cause the thread to un-join * the interval. */ kern_return_t kr = mach_port_deallocate(mach_task_self(), wi_port); if (kr != KERN_SUCCESS) { /* * If the deallocate fails, then someone got their port * lifecycle wrong and over-released a port right. * * Return an error so the client can assert on this, * and still find the port name in the interval handle. */ errno = EINVAL; return -1; } interval_handle->wi_port = MACH_PORT_NULL; interval_handle->work_interval_id = 0; free(interval_handle); return 0; } else { uint64_t work_interval_id = interval_handle->work_interval_id; int ret = __work_interval_ctl(WORK_INTERVAL_OPERATION_DESTROY, work_interval_id, NULL, 0); interval_handle->work_interval_id = 0; int saved_errno = errno; free(interval_handle); errno = saved_errno; return ret; } } int work_interval_join(work_interval_t interval_handle) { if (interval_handle == NULL) { errno = EINVAL; return -1; } if ((interval_handle->create_flags & WORK_INTERVAL_FLAG_JOINABLE) == 0) { errno = EINVAL; return -1; } mach_port_t wi_port = interval_handle->wi_port; if (!MACH_PORT_VALID(wi_port)) { errno = EINVAL; return -1; } return work_interval_join_port(wi_port); } int work_interval_join_port(mach_port_t port) { if (port == MACH_PORT_NULL) { errno = EINVAL; return -1; } return __work_interval_ctl(WORK_INTERVAL_OPERATION_JOIN, (uint64_t)port, NULL, 0); } int work_interval_leave(void) { return __work_interval_ctl(WORK_INTERVAL_OPERATION_JOIN, (uint64_t)MACH_PORT_NULL, NULL, 0); } int work_interval_copy_port(work_interval_t interval_handle, mach_port_t *port) { if (port == NULL) { errno = EINVAL; return -1; } if (interval_handle == NULL) { *port = MACH_PORT_NULL; errno = EINVAL; return -1; } if ((interval_handle->create_flags & WORK_INTERVAL_FLAG_JOINABLE) == 0) { *port = MACH_PORT_NULL; errno = EINVAL; return -1; } mach_port_t wi_port = interval_handle->wi_port; kern_return_t kr = mach_port_mod_refs(mach_task_self(), wi_port, MACH_PORT_RIGHT_SEND, 1); if (kr != KERN_SUCCESS) { *port = MACH_PORT_NULL; errno = EINVAL; return -1; } *port = wi_port; return 0; } |