]>
Commit | Line | Data |
---|---|---|
f1a1da6c A |
1 | /* |
2 | * Copyright (c) 2007, 2012 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 | ||
24 | #ifndef __PTHREAD_WORKQUEUE_H__ | |
25 | #define __PTHREAD_WORKQUEUE_H__ | |
26 | ||
a0619f9c | 27 | #include <stdbool.h> |
f1a1da6c | 28 | #include <sys/cdefs.h> |
964d3577 | 29 | #include <sys/event.h> |
f1a1da6c A |
30 | #include <Availability.h> |
31 | #include <pthread/pthread.h> | |
32 | #include <pthread/qos.h> | |
33 | #ifndef _PTHREAD_BUILDING_PTHREAD_ | |
34 | #include <pthread/qos_private.h> | |
35 | #endif | |
36 | ||
a0619f9c | 37 | #define PTHREAD_WORKQUEUE_SPI_VERSION 20170201 |
f1a1da6c A |
38 | |
39 | /* Feature checking flags, returned by _pthread_workqueue_supported() | |
40 | * | |
41 | * Note: These bits should match the definition of PTHREAD_FEATURE_* | |
42 | * bits defined in libpthread/kern/kern_internal.h */ | |
43 | ||
44 | #define WORKQ_FEATURE_DISPATCHFUNC 0x01 // pthread_workqueue_setdispatch_np is supported (or not) | |
45 | #define WORKQ_FEATURE_FINEPRIO 0x02 // fine grained pthread workq priorities | |
46 | #define WORKQ_FEATURE_MAINTENANCE 0x10 // QOS class maintenance | |
2546420a | 47 | #define WORKQ_FEATURE_KEVENT 0x40 // Support for direct kevent delivery |
a0619f9c | 48 | #define WORKQ_FEATURE_WORKLOOP 0x80 // Support for direct workloop requests |
f1a1da6c A |
49 | |
50 | /* Legacy dispatch priority bands */ | |
51 | ||
52 | #define WORKQ_NUM_PRIOQUEUE 4 | |
53 | ||
54 | #define WORKQ_HIGH_PRIOQUEUE 0 // high priority queue | |
55 | #define WORKQ_DEFAULT_PRIOQUEUE 1 // default priority queue | |
56 | #define WORKQ_LOW_PRIOQUEUE 2 // low priority queue | |
57 | #define WORKQ_BG_PRIOQUEUE 3 // background priority queue | |
58 | #define WORKQ_NON_INTERACTIVE_PRIOQUEUE 128 // libdispatch SPI level | |
59 | ||
60 | /* Legacy dispatch workqueue function flags */ | |
61 | #define WORKQ_ADDTHREADS_OPTION_OVERCOMMIT 0x00000001 | |
62 | ||
63 | __BEGIN_DECLS | |
64 | ||
65 | // Legacy callback prototype, used with pthread_workqueue_setdispatch_np | |
66 | typedef void (*pthread_workqueue_function_t)(int queue_priority, int options, void *ctxt); | |
67 | // New callback prototype, used with pthread_workqueue_init | |
68 | typedef void (*pthread_workqueue_function2_t)(pthread_priority_t priority); | |
2546420a | 69 | |
964d3577 A |
70 | // Newer callback prototype, used in conjection with function2 when there are kevents to deliver |
71 | // both parameters are in/out parameters | |
2546420a | 72 | #define WORKQ_KEVENT_EVENT_BUFFER_LEN 16 |
964d3577 | 73 | typedef void (*pthread_workqueue_function_kevent_t)(void **events, int *nevents); |
f1a1da6c | 74 | |
a0619f9c A |
75 | typedef void (*pthread_workqueue_function_workloop_t)(uint64_t *workloop_id, void **events, int *nevents); |
76 | ||
c6e5f90c A |
77 | #define PTHREAD_WORKQUEUE_CONFIG_VERSION 2 |
78 | #define PTHREAD_WORKQUEUE_CONFIG_MIN_SUPPORTED_VERSION 1 | |
79 | #define PTHREAD_WORKQUEUE_CONFIG_SUPPORTED_FLAGS 0 | |
80 | struct pthread_workqueue_config { | |
81 | uint32_t flags; | |
82 | uint32_t version; | |
83 | pthread_workqueue_function_kevent_t kevent_cb; | |
84 | pthread_workqueue_function_workloop_t workloop_cb; | |
85 | pthread_workqueue_function2_t workq_cb; | |
86 | uint64_t queue_serialno_offs; | |
87 | uint64_t queue_label_offs; | |
88 | }; | |
89 | ||
90 | __API_AVAILABLE(macos(10.15), ios(13.0)) | |
91 | int | |
92 | pthread_workqueue_setup(struct pthread_workqueue_config *cfg, size_t cfg_size); | |
93 | ||
f1a1da6c A |
94 | // Initialises the pthread workqueue subsystem, passing the new-style callback prototype, |
95 | // the dispatchoffset and an unused flags field. | |
a0619f9c | 96 | __API_AVAILABLE(macos(10.10), ios(8.0)) |
f1a1da6c A |
97 | int |
98 | _pthread_workqueue_init(pthread_workqueue_function2_t func, int offset, int flags); | |
99 | ||
a0619f9c | 100 | __API_AVAILABLE(macos(10.11), ios(9.0)) |
964d3577 A |
101 | int |
102 | _pthread_workqueue_init_with_kevent(pthread_workqueue_function2_t queue_func, pthread_workqueue_function_kevent_t kevent_func, int offset, int flags); | |
103 | ||
a0619f9c A |
104 | __API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0)) |
105 | int | |
106 | _pthread_workqueue_init_with_workloop(pthread_workqueue_function2_t queue_func, pthread_workqueue_function_kevent_t kevent_func, pthread_workqueue_function_workloop_t workloop_func, int offset, int flags); | |
107 | ||
f1a1da6c | 108 | // Non-zero enables kill on current thread, zero disables it. |
a0619f9c | 109 | __API_AVAILABLE(macos(10.6), ios(3.2)) |
f1a1da6c A |
110 | int |
111 | __pthread_workqueue_setkill(int); | |
112 | ||
113 | // Dispatch function to be called when new worker threads are created. | |
a0619f9c | 114 | __API_AVAILABLE(macos(10.8), ios(6.0)) |
f1a1da6c A |
115 | int |
116 | pthread_workqueue_setdispatch_np(pthread_workqueue_function_t worker_func); | |
117 | ||
118 | // Dispatch offset to be set in the kernel. | |
a0619f9c | 119 | __API_AVAILABLE(macos(10.9), ios(7.0)) |
f1a1da6c A |
120 | void |
121 | pthread_workqueue_setdispatchoffset_np(int offset); | |
122 | ||
123 | // Request additional worker threads. | |
a0619f9c | 124 | __API_AVAILABLE(macos(10.8), ios(6.0)) |
f1a1da6c A |
125 | int |
126 | pthread_workqueue_addthreads_np(int queue_priority, int options, int numthreads); | |
127 | ||
128 | // Retrieve the supported pthread feature set | |
a0619f9c | 129 | __API_AVAILABLE(macos(10.10), ios(8.0)) |
f1a1da6c A |
130 | int |
131 | _pthread_workqueue_supported(void); | |
132 | ||
133 | // Request worker threads (fine grained priority) | |
a0619f9c | 134 | __API_AVAILABLE(macos(10.10), ios(8.0)) |
f1a1da6c A |
135 | int |
136 | _pthread_workqueue_addthreads(int numthreads, pthread_priority_t priority); | |
137 | ||
a0619f9c A |
138 | // Should this thread return to the kernel? |
139 | __API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0)) | |
140 | bool | |
141 | _pthread_workqueue_should_narrow(pthread_priority_t priority); | |
142 | ||
143 | __API_AVAILABLE(macos(10.11), ios(9.0)) | |
964d3577 A |
144 | int |
145 | _pthread_workqueue_set_event_manager_priority(pthread_priority_t priority); | |
146 | ||
f1a1da6c | 147 | // Apply a QoS override without allocating userspace memory |
a0619f9c | 148 | __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0)) |
2546420a A |
149 | int |
150 | _pthread_qos_override_start_direct(mach_port_t thread, pthread_priority_t priority, void *resource); | |
151 | ||
152 | // Drop a corresponding QoS override made above, if the resource matches | |
a0619f9c | 153 | __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0)) |
2546420a A |
154 | int |
155 | _pthread_qos_override_end_direct(mach_port_t thread, void *resource); | |
156 | ||
157 | // Apply a QoS override without allocating userspace memory | |
a0619f9c A |
158 | __API_DEPRECATED_WITH_REPLACEMENT("_pthread_qos_override_start_direct", |
159 | macos(10.10, 10.12), ios(8.0, 10.0), tvos(8.0, 10.0), watchos(1.0, 3.0)) | |
f1a1da6c A |
160 | int |
161 | _pthread_override_qos_class_start_direct(mach_port_t thread, pthread_priority_t priority); | |
162 | ||
163 | // Drop a corresponding QoS override made above. | |
a0619f9c A |
164 | __API_DEPRECATED_WITH_REPLACEMENT("_pthread_qos_override_end_direct", |
165 | macos(10.10, 10.12), ios(8.0, 10.0), tvos(8.0, 10.0), watchos(1.0, 3.0)) | |
f1a1da6c A |
166 | int |
167 | _pthread_override_qos_class_end_direct(mach_port_t thread); | |
168 | ||
169 | // Apply a QoS override on a given workqueue thread. | |
a0619f9c | 170 | __API_AVAILABLE(macos(10.10), ios(8.0)) |
f1a1da6c A |
171 | int |
172 | _pthread_workqueue_override_start_direct(mach_port_t thread, pthread_priority_t priority); | |
173 | ||
2546420a | 174 | // Apply a QoS override on a given workqueue thread. |
a0619f9c | 175 | __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0)) |
2546420a A |
176 | int |
177 | _pthread_workqueue_override_start_direct_check_owner(mach_port_t thread, pthread_priority_t priority, mach_port_t *ulock_addr); | |
178 | ||
f1a1da6c | 179 | // Drop all QoS overrides on the current workqueue thread. |
a0619f9c | 180 | __API_AVAILABLE(macos(10.10), ios(8.0)) |
f1a1da6c A |
181 | int |
182 | _pthread_workqueue_override_reset(void); | |
183 | ||
215aeb03 | 184 | // Apply a QoS override on a given thread (can be non-workqueue as well) with a resource/queue token |
a0619f9c | 185 | __API_AVAILABLE(macos(10.10.2)) |
215aeb03 A |
186 | int |
187 | _pthread_workqueue_asynchronous_override_add(mach_port_t thread, pthread_priority_t priority, void *resource); | |
188 | ||
189 | // Reset overrides for the given resource for the current thread | |
a0619f9c | 190 | __API_AVAILABLE(macos(10.10.2)) |
215aeb03 A |
191 | int |
192 | _pthread_workqueue_asynchronous_override_reset_self(void *resource); | |
193 | ||
194 | // Reset overrides for all resources for the current thread | |
a0619f9c | 195 | __API_AVAILABLE(macos(10.10.2)) |
215aeb03 A |
196 | int |
197 | _pthread_workqueue_asynchronous_override_reset_all_self(void); | |
198 | ||
214d78a2 A |
199 | __API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0)) |
200 | int | |
201 | _pthread_workloop_create(uint64_t workloop_id, uint64_t options, pthread_attr_t *attr); | |
202 | ||
203 | __API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0)) | |
204 | int | |
205 | _pthread_workloop_destroy(uint64_t workloop_id); | |
206 | ||
f1a1da6c A |
207 | __END_DECLS |
208 | ||
209 | #endif // __PTHREAD_WORKQUEUE_H__ |