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>
26 #include <mach/mach_time.h>
27 #include <sys/errno.h>
30 struct work_interval
{
32 uint64_t work_interval_id
;
35 extern uint64_t __thread_selfid(void);
37 /* Create a new work interval handle (currently for the current thread only). Flags is unused */
39 work_interval_create(work_interval_t
*interval_handle
, uint32_t flags __unused
)
42 uint64_t work_interval_id
;
43 work_interval_t handle
;
45 ret
= __work_interval_ctl(WORK_INTERVAL_OPERATION_CREATE
, 0, &work_interval_id
, sizeof(work_interval_id
));
50 handle
= malloc(sizeof(*handle
));
56 handle
->thread_id
= __thread_selfid();
57 handle
->work_interval_id
= work_interval_id
;
59 *interval_handle
= handle
;
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
)
67 uint64_t work_interval_id
;
68 struct work_interval_notification notification
= {
72 .next_start
= next_start
,
77 if (interval_handle
== NULL
) {
82 work_interval_id
= interval_handle
->work_interval_id
;
84 ret
= __work_interval_ctl(WORK_INTERVAL_OPERATION_NOTIFY
, work_interval_id
, ¬ification
, sizeof(notification
));
89 work_interval_notify_simple(work_interval_t interval_handle
, uint64_t start
, uint64_t deadline
, uint64_t next_start
)
91 return work_interval_notify(interval_handle
, start
, mach_absolute_time(), deadline
, next_start
, 0);
95 work_interval_destroy(work_interval_t interval_handle
)
98 uint64_t work_interval_id
;
100 if (interval_handle
== NULL
) {
105 work_interval_id
= interval_handle
->work_interval_id
;
107 ret
= __work_interval_ctl(WORK_INTERVAL_OPERATION_DESTROY
, work_interval_id
, NULL
, 0);
109 free(interval_handle
);