2 * Copyright (c) 2007, 2012 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@
24 #ifndef __PTHREAD_WORKQUEUE_H__
25 #define __PTHREAD_WORKQUEUE_H__
28 #include <sys/cdefs.h>
29 #include <sys/event.h>
30 #include <Availability.h>
31 #include <pthread/pthread.h>
32 #include <pthread/qos_private.h>
34 #define PTHREAD_WORKQUEUE_SPI_VERSION 20170201
36 /* Feature checking flags, returned by _pthread_workqueue_supported()
38 * Note: These bits should match the definition of PTHREAD_FEATURE_*
39 * bits defined in libpthread/kern/kern_internal.h */
41 #define WORKQ_FEATURE_DISPATCHFUNC 0x01 // pthread_workqueue_setdispatch_np is supported (or not)
42 #define WORKQ_FEATURE_FINEPRIO 0x02 // fine grained pthread workq priorities
43 #define WORKQ_FEATURE_MAINTENANCE 0x10 // QOS class maintenance
44 #define WORKQ_FEATURE_KEVENT 0x40 // Support for direct kevent delivery
45 #define WORKQ_FEATURE_WORKLOOP 0x80 // Support for direct workloop requests
47 /* Legacy dispatch priority bands */
49 #define WORKQ_NUM_PRIOQUEUE 4
51 #define WORKQ_HIGH_PRIOQUEUE 0 // high priority queue
52 #define WORKQ_DEFAULT_PRIOQUEUE 1 // default priority queue
53 #define WORKQ_LOW_PRIOQUEUE 2 // low priority queue
54 #define WORKQ_BG_PRIOQUEUE 3 // background priority queue
55 #define WORKQ_NON_INTERACTIVE_PRIOQUEUE 128 // libdispatch SPI level
57 /* Legacy dispatch workqueue function flags */
58 #define WORKQ_ADDTHREADS_OPTION_OVERCOMMIT 0x00000001
62 // Legacy callback prototype, used with pthread_workqueue_setdispatch_np
63 typedef void (*pthread_workqueue_function_t
)(int queue_priority
, int options
, void *ctxt
);
64 // New callback prototype, used with pthread_workqueue_init
65 typedef void (*pthread_workqueue_function2_t
)(pthread_priority_t priority
);
67 // Newer callback prototype, used in conjection with function2 when there are kevents to deliver
68 // both parameters are in/out parameters
69 #define WORKQ_KEVENT_EVENT_BUFFER_LEN 16
70 typedef void (*pthread_workqueue_function_kevent_t
)(void **events
, int *nevents
);
72 typedef void (*pthread_workqueue_function_workloop_t
)(uint64_t *workloop_id
, void **events
, int *nevents
);
74 #define PTHREAD_WORKQUEUE_CONFIG_VERSION 2
75 #define PTHREAD_WORKQUEUE_CONFIG_MIN_SUPPORTED_VERSION 1
76 #define PTHREAD_WORKQUEUE_CONFIG_SUPPORTED_FLAGS 0
77 struct pthread_workqueue_config
{
80 pthread_workqueue_function_kevent_t kevent_cb
;
81 pthread_workqueue_function_workloop_t workloop_cb
;
82 pthread_workqueue_function2_t workq_cb
;
83 uint64_t queue_serialno_offs
;
84 uint64_t queue_label_offs
;
87 __API_AVAILABLE(macos(10.15), ios(13.0))
89 pthread_workqueue_setup(struct pthread_workqueue_config
*cfg
, size_t cfg_size
);
91 // Initialises the pthread workqueue subsystem, passing the new-style callback prototype,
92 // the dispatchoffset and an unused flags field.
93 __API_AVAILABLE(macos(10.10), ios(8.0))
95 _pthread_workqueue_init(pthread_workqueue_function2_t func
, int offset
, int flags
);
97 __API_AVAILABLE(macos(10.11), ios(9.0))
99 _pthread_workqueue_init_with_kevent(pthread_workqueue_function2_t queue_func
, pthread_workqueue_function_kevent_t kevent_func
, int offset
, int flags
);
101 __API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0))
103 _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
);
105 // Non-zero enables kill on current thread, zero disables it.
106 __API_AVAILABLE(macos(10.6), ios(3.2))
108 __pthread_workqueue_setkill(int);
110 // Dispatch function to be called when new worker threads are created.
111 __API_AVAILABLE(macos(10.8), ios(6.0))
113 pthread_workqueue_setdispatch_np(pthread_workqueue_function_t worker_func
);
115 // Dispatch offset to be set in the kernel.
116 __API_AVAILABLE(macos(10.9), ios(7.0))
118 pthread_workqueue_setdispatchoffset_np(int offset
);
120 // Request additional worker threads.
121 __API_AVAILABLE(macos(10.8), ios(6.0))
123 pthread_workqueue_addthreads_np(int queue_priority
, int options
, int numthreads
);
125 // Retrieve the supported pthread feature set
126 __API_AVAILABLE(macos(10.10), ios(8.0))
128 _pthread_workqueue_supported(void);
130 // Request worker threads (fine grained priority)
131 __API_AVAILABLE(macos(10.10), ios(8.0))
133 _pthread_workqueue_addthreads(int numthreads
, pthread_priority_t priority
);
135 // Should this thread return to the kernel?
136 __API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0))
138 _pthread_workqueue_should_narrow(pthread_priority_t priority
);
140 __API_AVAILABLE(macos(10.11), ios(9.0))
142 _pthread_workqueue_set_event_manager_priority(pthread_priority_t priority
);
144 // Apply a QoS override without allocating userspace memory
145 __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
147 _pthread_qos_override_start_direct(mach_port_t thread
, pthread_priority_t priority
, void *resource
);
149 // Drop a corresponding QoS override made above, if the resource matches
150 __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
152 _pthread_qos_override_end_direct(mach_port_t thread
, void *resource
);
154 // Apply a QoS override without allocating userspace memory
155 __API_DEPRECATED_WITH_REPLACEMENT("_pthread_qos_override_start_direct",
156 macos(10.10, 10.12), ios(8.0, 10.0), tvos(8.0, 10.0), watchos(1.0, 3.0))
158 _pthread_override_qos_class_start_direct(mach_port_t thread
, pthread_priority_t priority
);
160 // Drop a corresponding QoS override made above.
161 __API_DEPRECATED_WITH_REPLACEMENT("_pthread_qos_override_end_direct",
162 macos(10.10, 10.12), ios(8.0, 10.0), tvos(8.0, 10.0), watchos(1.0, 3.0))
164 _pthread_override_qos_class_end_direct(mach_port_t thread
);
166 // Apply a QoS override on a given workqueue thread.
167 __API_AVAILABLE(macos(10.10), ios(8.0))
169 _pthread_workqueue_override_start_direct(mach_port_t thread
, pthread_priority_t priority
);
171 // Apply a QoS override on a given workqueue thread.
172 __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
174 _pthread_workqueue_override_start_direct_check_owner(mach_port_t thread
, pthread_priority_t priority
, mach_port_t
*ulock_addr
);
176 // Drop all QoS overrides on the current workqueue thread.
177 __API_AVAILABLE(macos(10.10), ios(8.0))
179 _pthread_workqueue_override_reset(void);
181 // Apply a QoS override on a given thread (can be non-workqueue as well) with a resource/queue token
182 __API_AVAILABLE(macos(10.10.2))
184 _pthread_workqueue_asynchronous_override_add(mach_port_t thread
, pthread_priority_t priority
, void *resource
);
186 // Reset overrides for the given resource for the current thread
187 __API_AVAILABLE(macos(10.10.2))
189 _pthread_workqueue_asynchronous_override_reset_self(void *resource
);
191 // Reset overrides for all resources for the current thread
192 __API_AVAILABLE(macos(10.10.2))
194 _pthread_workqueue_asynchronous_override_reset_all_self(void);
196 __API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
198 _pthread_workloop_create(uint64_t workloop_id
, uint64_t options
, pthread_attr_t
*attr
);
200 __API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
202 _pthread_workloop_destroy(uint64_t workloop_id
);
206 #endif // __PTHREAD_WORKQUEUE_H__