Skip to content

Commit e6df818

Browse files
Merge pull request #436 from compnerd/dispatch-queues
Improve dispatch queue support for Windows
2 parents afa6cc3 + 85d08fa commit e6df818

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

private/private.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,16 @@ void _dispatch_prohibit_transition_to_multithreaded(bool prohibit);
182182
#define DISPATCH_COCOA_COMPAT 0
183183
#endif
184184

185-
#if DISPATCH_COCOA_COMPAT
185+
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)
186186

187187
#define DISPATCH_CF_SPI_VERSION 20160712
188188

189189
#if TARGET_OS_MAC
190190
typedef mach_port_t dispatch_runloop_handle_t;
191191
#elif defined(__linux__) || defined(__FreeBSD__)
192192
typedef int dispatch_runloop_handle_t;
193+
#elif defined(_WIN32)
194+
typedef void *dispatch_runloop_handle_t;
193195
#else
194196
#error "runloop support not implemented on this platform"
195197
#endif
@@ -218,12 +220,14 @@ dispatch_queue_t
218220
_dispatch_runloop_root_queue_create_4CF(const char *_Nullable label,
219221
unsigned long flags);
220222

221-
#if TARGET_OS_MAC
223+
#if TARGET_OS_MAC || defined(_WIN32)
222224
API_AVAILABLE(macos(10.9), ios(7.0))
223225
DISPATCH_EXPORT DISPATCH_WARN_RESULT DISPATCH_NOTHROW
224-
mach_port_t
226+
dispatch_runloop_handle_t
225227
_dispatch_runloop_root_queue_get_port_4CF(dispatch_queue_t queue);
228+
#endif
226229

230+
#if TARGET_OS_MAC
227231
#ifdef __BLOCKS__
228232
API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
229233
DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
@@ -259,7 +263,7 @@ API_AVAILABLE(macos(10.6), ios(4.0))
259263
DISPATCH_EXPORT
260264
void (*_Nullable _dispatch_end_NSAutoReleasePool)(void *);
261265

262-
#endif /* DISPATCH_COCOA_COMPAT */
266+
#endif /* DISPATCH_COCOA_COMPAT || defined(_WIN32) */
263267

264268
API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
265269
DISPATCH_EXPORT DISPATCH_NOTHROW

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ if(WIN32)
6363
target_sources(dispatch
6464
PRIVATE
6565
shims/generic_sys_queue.h
66-
shims/generic_win_stubs.c
6766
shims/generic_win_stubs.h
6867
shims/getprogname.c)
6968
endif()

src/queue.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ _dispatch_worker_thread_thunk(LPVOID lpParameter);
9393
#endif
9494
#endif
9595

96-
#if DISPATCH_COCOA_COMPAT
96+
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)
9797
static dispatch_once_t _dispatch_main_q_handle_pred;
98+
#endif
99+
#if DISPATCH_COCOA_COMPAT
98100
static void _dispatch_runloop_queue_poke(dispatch_queue_t dq,
99101
dispatch_qos_t qos, dispatch_wakeup_flags_t flags);
102+
#endif
103+
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)
100104
static void _dispatch_runloop_queue_handle_init(void *ctxt);
101105
static void _dispatch_runloop_queue_handle_dispose(dispatch_queue_t dq);
102106
#endif
@@ -4476,7 +4480,7 @@ _dispatch_queue_wakeup(dispatch_queue_t dq, dispatch_qos_t qos,
44764480
return _dispatch_queue_class_wakeup(dq, qos, flags, target);
44774481
}
44784482

4479-
#if DISPATCH_COCOA_COMPAT
4483+
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)
44804484
DISPATCH_ALWAYS_INLINE
44814485
static inline bool
44824486
_dispatch_runloop_handle_is_valid(dispatch_runloop_handle_t handle)
@@ -4485,6 +4489,8 @@ _dispatch_runloop_handle_is_valid(dispatch_runloop_handle_t handle)
44854489
return MACH_PORT_VALID(handle);
44864490
#elif defined(__linux__)
44874491
return handle >= 0;
4492+
#elif defined(_WIN32)
4493+
return handle != INVALID_HANDLE_VALUE;
44884494
#else
44894495
#error "runloop support not implemented on this platform"
44904496
#endif
@@ -4499,6 +4505,8 @@ _dispatch_runloop_queue_get_handle(dispatch_queue_t dq)
44994505
#elif defined(__linux__)
45004506
// decode: 0 is a valid fd, so offset by 1 to distinguish from NULL
45014507
return ((dispatch_runloop_handle_t)(uintptr_t)dq->do_ctxt) - 1;
4508+
#elif defined(_WIN32)
4509+
return ((dispatch_runloop_handle_t)(uintptr_t)dq->do_ctxt);
45024510
#else
45034511
#error "runloop support not implemented on this platform"
45044512
#endif
@@ -4513,6 +4521,8 @@ _dispatch_runloop_queue_set_handle(dispatch_queue_t dq, dispatch_runloop_handle_
45134521
#elif defined(__linux__)
45144522
// encode: 0 is a valid fd, so offset by 1 to distinguish from NULL
45154523
dq->do_ctxt = (void *)(uintptr_t)(handle + 1);
4524+
#elif defined(_WIN32)
4525+
dq->do_ctxt = (void *)(uintptr_t)handle;
45164526
#else
45174527
#error "runloop support not implemented on this platform"
45184528
#endif
@@ -4527,7 +4537,7 @@ _dispatch_runloop_queue_reset_max_qos(dispatch_queue_class_t dqu)
45274537
old_state = os_atomic_and_orig2o(dqu._dq, dq_state, ~clear_bits, relaxed);
45284538
return _dq_state_max_qos(old_state);
45294539
}
4530-
#endif // DISPATCH_COCOA_COMPAT
4540+
#endif
45314541

45324542
void
45334543
_dispatch_runloop_queue_wakeup(dispatch_queue_t dq, dispatch_qos_t qos,
@@ -5112,7 +5122,7 @@ _dispatch_queue_serial_drain(dispatch_queue_t dq, dispatch_invoke_context_t dic,
51125122
return _dispatch_queue_drain(dq, dic, flags, owned, true);
51135123
}
51145124

5115-
#if DISPATCH_COCOA_COMPAT
5125+
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)
51165126
DISPATCH_NOINLINE
51175127
static void
51185128
_dispatch_main_queue_update_priority_from_thread(void)
@@ -6178,7 +6188,7 @@ _dispatch_network_root_queue_create_4NW(const char *label,
61786188

61796189
static bool _dispatch_program_is_probably_callback_driven;
61806190

6181-
#if DISPATCH_COCOA_COMPAT
6191+
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)
61826192

61836193
dispatch_queue_t
61846194
_dispatch_runloop_root_queue_create_4CF(const char *label, unsigned long flags)
@@ -6242,7 +6252,7 @@ _dispatch_runloop_root_queue_wakeup_4CF(dispatch_queue_t dq)
62426252
_dispatch_runloop_queue_wakeup(dq, 0, false);
62436253
}
62446254

6245-
#if TARGET_OS_MAC
6255+
#if TARGET_OS_MAC || defined(_WIN32)
62466256
dispatch_runloop_handle_t
62476257
_dispatch_runloop_root_queue_get_port_4CF(dispatch_queue_t dq)
62486258
{
@@ -6305,6 +6315,8 @@ _dispatch_runloop_queue_handle_init(void *ctxt)
63056315
}
63066316
}
63076317
handle = fd;
6318+
#elif defined(_WIN32)
6319+
handle = INVALID_HANDLE_VALUE;
63086320
#else
63096321
#error "runloop support not implemented on this platform"
63106322
#endif
@@ -6332,6 +6344,8 @@ _dispatch_runloop_queue_handle_dispose(dispatch_queue_t dq)
63326344
#elif defined(__linux__)
63336345
int rc = close(handle);
63346346
(void)dispatch_assume_zero(rc);
6347+
#elif defined(_WIN32)
6348+
CloseHandle(handle);
63356349
#else
63366350
#error "runloop support not implemented on this platform"
63376351
#endif

src/shims/generic_win_stubs.c

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/swift/DispatchStubs.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
#include <dispatch/dispatch.h>
1414
#include <stdio.h>
1515

16+
#if defined(__ELF__) || defined(__MACH__) || defined(__WASM__)
1617
#define DISPATCH_RUNTIME_STDLIB_INTERFACE __attribute__((__visibility__("default")))
18+
#else
19+
#define DISPATCH_RUNTIME_STDLIB_INTERFACE __declspec(dllexport)
20+
#endif
1721

1822
#if USE_OBJC
1923
@protocol OS_dispatch_source;
@@ -54,6 +58,7 @@ static void _dispatch_overlay_constructor() {
5458
#endif /* USE_OBJC */
5559

5660
#if !USE_OBJC
61+
DISPATCH_RUNTIME_STDLIB_INTERFACE
5762
extern "C" void * objc_retainAutoreleasedReturnValue(void *obj);
5863
#endif
5964

0 commit comments

Comments
 (0)