|
| 1 | +//===--- DispatchOverlayShims.h - Compatibility decls -----------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | + |
| 12 | +#ifndef SWIFT_SHIMS_DISPATCH_OVERLAY_SHIMS_H |
| 13 | +#define SWIFT_SHIMS_DISPATCH_OVERLAY_SHIMS_H |
| 14 | + |
| 15 | +#include <dispatch/dispatch.h> |
| 16 | + |
| 17 | +#ifdef __OBJC__ |
| 18 | +#define SWIFT_DISPATCH_RETURNS_RETAINED __attribute__((__ns_returns_retained__)) |
| 19 | +#else |
| 20 | +#define SWIFT_DISPATCH_RETURNS_RETAINED |
| 21 | +#endif |
| 22 | + |
| 23 | +#define SWIFT_DISPATCH_NOESCAPE __attribute__((__noescape__)) |
| 24 | + |
| 25 | +#pragma clang assume_nonnull begin |
| 26 | + |
| 27 | +#ifdef __cplusplus |
| 28 | +extern "C" { |
| 29 | +#endif |
| 30 | + |
| 31 | +typedef void (^__swift_shims_dispatch_block_t)(void); |
| 32 | + |
| 33 | +#ifdef __OBJC__ |
| 34 | +typedef id __swift_shims_dispatch_data_t; |
| 35 | +#else |
| 36 | +typedef void *__swift_shims_dispatch_data_t; |
| 37 | +#endif |
| 38 | + |
| 39 | + |
| 40 | +static inline dispatch_queue_attr_t |
| 41 | +_swift_dispatch_queue_concurrent(void) { |
| 42 | + return DISPATCH_QUEUE_CONCURRENT; |
| 43 | +} |
| 44 | + |
| 45 | +static inline dispatch_queue_t |
| 46 | +_swift_dispatch_get_main_queue(void) { |
| 47 | + return dispatch_get_main_queue(); |
| 48 | +} |
| 49 | + |
| 50 | +static inline dispatch_data_t |
| 51 | +_swift_dispatch_data_empty(void) { |
| 52 | + return dispatch_data_empty; |
| 53 | +} |
| 54 | + |
| 55 | +static inline __swift_shims_dispatch_block_t _Nullable |
| 56 | +_swift_dispatch_data_destructor_default(void) { |
| 57 | + return DISPATCH_DATA_DESTRUCTOR_DEFAULT; |
| 58 | +} |
| 59 | + |
| 60 | +static inline __swift_shims_dispatch_block_t |
| 61 | +_swift_dispatch_data_destructor_free(void) { |
| 62 | + return _dispatch_data_destructor_free; |
| 63 | +} |
| 64 | + |
| 65 | +static inline __swift_shims_dispatch_block_t |
| 66 | +_swift_dispatch_data_destructor_munmap(void) { |
| 67 | + return _dispatch_data_destructor_munmap; |
| 68 | +} |
| 69 | + |
| 70 | +#define SWIFT_DISPATCH_SOURCE_TYPE(t) \ |
| 71 | + static inline dispatch_source_type_t _swift_dispatch_source_type_##t(void) { \ |
| 72 | + return DISPATCH_SOURCE_TYPE_##t; \ |
| 73 | + } |
| 74 | + |
| 75 | +SWIFT_DISPATCH_SOURCE_TYPE(DATA_ADD) |
| 76 | +SWIFT_DISPATCH_SOURCE_TYPE(DATA_OR) |
| 77 | +SWIFT_DISPATCH_SOURCE_TYPE(DATA_REPLACE) |
| 78 | +SWIFT_DISPATCH_SOURCE_TYPE(READ) |
| 79 | +SWIFT_DISPATCH_SOURCE_TYPE(SIGNAL) |
| 80 | +SWIFT_DISPATCH_SOURCE_TYPE(TIMER) |
| 81 | +SWIFT_DISPATCH_SOURCE_TYPE(WRITE) |
| 82 | + |
| 83 | +#if __APPLE__ |
| 84 | +SWIFT_DISPATCH_SOURCE_TYPE(MACH_SEND) |
| 85 | +SWIFT_DISPATCH_SOURCE_TYPE(MACH_RECV) |
| 86 | +SWIFT_DISPATCH_SOURCE_TYPE(MEMORYPRESSURE) |
| 87 | +SWIFT_DISPATCH_SOURCE_TYPE(PROC) |
| 88 | +SWIFT_DISPATCH_SOURCE_TYPE(VNODE) |
| 89 | +#endif |
| 90 | + |
| 91 | +extern void |
| 92 | +_swift_dispatch_source_create_abort(void); |
| 93 | + |
| 94 | +SWIFT_DISPATCH_RETURNS_RETAINED |
| 95 | +static inline dispatch_source_t |
| 96 | +_swift_dispatch_source_create( |
| 97 | + dispatch_source_type_t type, |
| 98 | + uintptr_t handle, |
| 99 | + unsigned long mask, |
| 100 | + dispatch_queue_t _Nullable queue) |
| 101 | +{ |
| 102 | + dispatch_source_t source = dispatch_source_create(type, handle, mask, queue); |
| 103 | + if (!source) { |
| 104 | + _swift_dispatch_source_create_abort(); |
| 105 | + } |
| 106 | + return source; |
| 107 | +} |
| 108 | + |
| 109 | +static inline SWIFT_DISPATCH_RETURNS_RETAINED __swift_shims_dispatch_block_t |
| 110 | +_swift_dispatch_block_create_with_qos_class( |
| 111 | + dispatch_block_flags_t flags, dispatch_qos_class_t qos, |
| 112 | + int relative_priority, __swift_shims_dispatch_block_t _Nonnull block) { |
| 113 | + return dispatch_block_create_with_qos_class( |
| 114 | + flags, qos, relative_priority, block); |
| 115 | +} |
| 116 | + |
| 117 | +static inline __swift_shims_dispatch_block_t |
| 118 | +_swift_dispatch_block_create_noescape( |
| 119 | + dispatch_block_flags_t flags, |
| 120 | + __swift_shims_dispatch_block_t SWIFT_DISPATCH_NOESCAPE block) { |
| 121 | + return dispatch_block_create(flags, block); |
| 122 | +} |
| 123 | + |
| 124 | +static inline int _swift_dispatch_block_wait( |
| 125 | + __swift_shims_dispatch_block_t block, |
| 126 | + unsigned long long timeout) { |
| 127 | + return dispatch_block_wait(block, timeout); |
| 128 | +} |
| 129 | + |
| 130 | +static inline void _swift_dispatch_block_notify( |
| 131 | + __swift_shims_dispatch_block_t block, |
| 132 | + dispatch_queue_t queue, |
| 133 | + __swift_shims_dispatch_block_t notifier) { |
| 134 | + dispatch_block_notify(block, queue, notifier); |
| 135 | +} |
| 136 | + |
| 137 | +static inline void _swift_dispatch_block_cancel( |
| 138 | + __swift_shims_dispatch_block_t block) { |
| 139 | + dispatch_block_cancel(block); |
| 140 | +} |
| 141 | + |
| 142 | +static inline int _swift_dispatch_block_testcancel( |
| 143 | + __swift_shims_dispatch_block_t block) { |
| 144 | + return dispatch_block_testcancel(block); |
| 145 | +} |
| 146 | + |
| 147 | +static inline void _swift_dispatch_async( |
| 148 | + dispatch_queue_t queue, |
| 149 | + __swift_shims_dispatch_block_t block) { |
| 150 | + dispatch_async(queue, block); |
| 151 | +} |
| 152 | + |
| 153 | +static inline void _swift_dispatch_sync( |
| 154 | + dispatch_queue_t queue, |
| 155 | + __swift_shims_dispatch_block_t block) { |
| 156 | + dispatch_sync(queue, block); |
| 157 | +} |
| 158 | + |
| 159 | +static inline void _swift_dispatch_barrier_async( |
| 160 | + dispatch_queue_t queue, |
| 161 | + __swift_shims_dispatch_block_t block) { |
| 162 | + dispatch_barrier_async(queue, block); |
| 163 | +} |
| 164 | + |
| 165 | +static inline void _swift_dispatch_group_async( |
| 166 | + dispatch_group_t group, |
| 167 | + dispatch_queue_t queue, |
| 168 | + __swift_shims_dispatch_block_t block) { |
| 169 | + dispatch_group_async((dispatch_group_t)group, queue, block); |
| 170 | +} |
| 171 | + |
| 172 | +static inline void _swift_dispatch_group_notify( |
| 173 | + dispatch_group_t group, |
| 174 | + dispatch_queue_t queue, |
| 175 | + __swift_shims_dispatch_block_t block) { |
| 176 | + dispatch_group_notify(group, queue, block); |
| 177 | +} |
| 178 | + |
| 179 | +static inline void _swift_dispatch_after( |
| 180 | + dispatch_time_t when, |
| 181 | + dispatch_queue_t queue, |
| 182 | + __swift_shims_dispatch_block_t block) { |
| 183 | + dispatch_after(when, queue, block); |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +static inline void _swift_dispatch_apply_current( |
| 188 | + size_t iterations, |
| 189 | + void SWIFT_DISPATCH_NOESCAPE (^block)(intptr_t)) { |
| 190 | + dispatch_apply(iterations, (dispatch_queue_t _Nonnull)0, ^(size_t i){ |
| 191 | + block((intptr_t)i); |
| 192 | + }); |
| 193 | +} |
| 194 | + |
| 195 | +SWIFT_DISPATCH_RETURNS_RETAINED |
| 196 | +static inline __swift_shims_dispatch_data_t |
| 197 | +_swift_dispatch_data_create( |
| 198 | + const void *buffer, |
| 199 | + size_t size, |
| 200 | + dispatch_queue_t _Nullable queue, |
| 201 | + __swift_shims_dispatch_block_t _Nullable destructor) { |
| 202 | + return dispatch_data_create(buffer, size, queue, destructor); |
| 203 | +} |
| 204 | + |
| 205 | +typedef unsigned int (^__swift_shims_dispatch_data_applier)(__swift_shims_dispatch_data_t, size_t, const void *, size_t); |
| 206 | + |
| 207 | +static inline unsigned int |
| 208 | +_swift_dispatch_data_apply( |
| 209 | + __swift_shims_dispatch_data_t data, |
| 210 | + __swift_shims_dispatch_data_applier SWIFT_DISPATCH_NOESCAPE applier) { |
| 211 | + return dispatch_data_apply((dispatch_data_t)data, ^bool(dispatch_data_t data, size_t off, const void *loc, size_t size){ |
| 212 | + return applier((__swift_shims_dispatch_data_t)data, off, loc, size); |
| 213 | + }); |
| 214 | +} |
| 215 | + |
| 216 | +static inline void _swift_dispatch_source_set_event_handler( |
| 217 | + dispatch_source_t source, |
| 218 | + __swift_shims_dispatch_block_t _Nullable block) { |
| 219 | + dispatch_source_set_event_handler(source, block); |
| 220 | +} |
| 221 | + |
| 222 | +static inline void _swift_dispatch_source_set_cancel_handler( |
| 223 | + dispatch_source_t source, |
| 224 | + __swift_shims_dispatch_block_t _Nullable block) { |
| 225 | + dispatch_source_set_cancel_handler(source, block); |
| 226 | +} |
| 227 | + |
| 228 | +static inline void _swift_dispatch_source_set_registration_handler( |
| 229 | + dispatch_source_t source, |
| 230 | + __swift_shims_dispatch_block_t _Nullable block) { |
| 231 | + dispatch_source_set_registration_handler(source, block); |
| 232 | +} |
| 233 | + |
| 234 | +#if defined(__ANDROID__) |
| 235 | +extern void _dispatch_install_thread_detach_callback(void (*cb)(void)); |
| 236 | +#endif |
| 237 | + |
| 238 | +static inline void _swift_dispatch_retain(dispatch_object_t object) { |
| 239 | + dispatch_retain(object); |
| 240 | +} |
| 241 | + |
| 242 | +static inline void _swift_dispatch_release(dispatch_object_t object) { |
| 243 | + dispatch_release(object); |
| 244 | +} |
| 245 | + |
| 246 | +#ifdef __cplusplus |
| 247 | +} // extern "C" |
| 248 | +#endif |
| 249 | + |
| 250 | +#pragma clang assume_nonnull end |
| 251 | + |
| 252 | +#endif // SWIFT_SHIMS_DISPATCH_OVERLAY_SHIMS_H |
| 253 | + |
0 commit comments