]> git.saurik.com Git - apple/xnu.git/blob - libsyscall/wrappers/work_interval.c
xnu-3789.41.3.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 #include <mach/mach_time.h>
27 #include <sys/errno.h>
28 #include <stdlib.h>
29
30 struct work_interval {
31 uint64_t thread_id;
32 uint64_t work_interval_id;
33 };
34
35 extern uint64_t __thread_selfid(void);
36
37 /* Create a new work interval handle (currently for the current thread only). Flags is unused */
38 int
39 work_interval_create(work_interval_t *interval_handle, uint32_t flags __unused)
40 {
41 int ret;
42 uint64_t work_interval_id;
43 work_interval_t handle;
44
45 ret = __work_interval_ctl(WORK_INTERVAL_OPERATION_CREATE, 0, &work_interval_id, sizeof(work_interval_id));
46 if (ret == -1) {
47 return ret;
48 }
49
50 handle = malloc(sizeof(*handle));
51 if (handle == NULL) {
52 errno = ENOMEM;
53 return -1;
54 }
55
56 handle->thread_id = __thread_selfid();
57 handle->work_interval_id = work_interval_id;
58
59 *interval_handle = handle;
60 return 0;
61 }
62
63 int
64 work_interval_notify(work_interval_t interval_handle, uint64_t start, uint64_t finish, uint64_t deadline, uint64_t next_start, uint32_t flags)
65 {
66 int ret;
67 uint64_t work_interval_id;
68 struct work_interval_notification notification = {
69 .start = start,
70 .finish = finish,
71 .deadline = deadline,
72 .next_start = next_start,
73 .flags = flags,
74 .unused1 = 0
75 };
76
77 if (interval_handle == NULL) {
78 errno = EINVAL;
79 return -1;
80 }
81
82 work_interval_id = interval_handle->work_interval_id;
83
84 ret = __work_interval_ctl(WORK_INTERVAL_OPERATION_NOTIFY, work_interval_id, &notification, sizeof(notification));
85 return ret;
86 }
87
88 int
89 work_interval_notify_simple(work_interval_t interval_handle, uint64_t start, uint64_t deadline, uint64_t next_start)
90 {
91 return work_interval_notify(interval_handle, start, mach_absolute_time(), deadline, next_start, 0);
92 }
93
94 int
95 work_interval_destroy(work_interval_t interval_handle)
96 {
97 int ret, saved_errno;
98 uint64_t work_interval_id;
99
100 if (interval_handle == NULL) {
101 errno = EINVAL;
102 return -1;
103 }
104
105 work_interval_id = interval_handle->work_interval_id;
106
107 ret = __work_interval_ctl(WORK_INTERVAL_OPERATION_DESTROY, work_interval_id, NULL, 0);
108 saved_errno = errno;
109 free(interval_handle);
110 errno = saved_errno;
111
112 return ret;
113 }