Skip to content

Improve dispatch queue support for Windows #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions private/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,16 @@ void _dispatch_prohibit_transition_to_multithreaded(bool prohibit);
#define DISPATCH_COCOA_COMPAT 0
#endif

#if DISPATCH_COCOA_COMPAT
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)

#define DISPATCH_CF_SPI_VERSION 20160712

#if TARGET_OS_MAC
typedef mach_port_t dispatch_runloop_handle_t;
#elif defined(__linux__) || defined(__FreeBSD__)
typedef int dispatch_runloop_handle_t;
#elif defined(_WIN32)
typedef void *dispatch_runloop_handle_t;
#else
#error "runloop support not implemented on this platform"
#endif
Expand Down Expand Up @@ -218,12 +220,14 @@ dispatch_queue_t
_dispatch_runloop_root_queue_create_4CF(const char *_Nullable label,
unsigned long flags);

#if TARGET_OS_MAC
#if TARGET_OS_MAC || defined(_WIN32)
API_AVAILABLE(macos(10.9), ios(7.0))
DISPATCH_EXPORT DISPATCH_WARN_RESULT DISPATCH_NOTHROW
mach_port_t
dispatch_runloop_handle_t
_dispatch_runloop_root_queue_get_port_4CF(dispatch_queue_t queue);
#endif

#if TARGET_OS_MAC
#ifdef __BLOCKS__
API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
Expand Down Expand Up @@ -259,7 +263,7 @@ API_AVAILABLE(macos(10.6), ios(4.0))
DISPATCH_EXPORT
void (*_Nullable _dispatch_end_NSAutoReleasePool)(void *);

#endif /* DISPATCH_COCOA_COMPAT */
#endif /* DISPATCH_COCOA_COMPAT || defined(_WIN32) */

API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
DISPATCH_EXPORT DISPATCH_NOTHROW
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ if(WIN32)
target_sources(dispatch
PRIVATE
shims/generic_sys_queue.h
shims/generic_win_stubs.c
shims/generic_win_stubs.h
shims/getprogname.c)
endif()
Expand Down
26 changes: 20 additions & 6 deletions src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ _dispatch_worker_thread_thunk(LPVOID lpParameter);
#endif
#endif

#if DISPATCH_COCOA_COMPAT
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)
static dispatch_once_t _dispatch_main_q_handle_pred;
#endif
#if DISPATCH_COCOA_COMPAT
static void _dispatch_runloop_queue_poke(dispatch_queue_t dq,
dispatch_qos_t qos, dispatch_wakeup_flags_t flags);
#endif
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)
static void _dispatch_runloop_queue_handle_init(void *ctxt);
static void _dispatch_runloop_queue_handle_dispose(dispatch_queue_t dq);
#endif
Expand Down Expand Up @@ -4476,7 +4480,7 @@ _dispatch_queue_wakeup(dispatch_queue_t dq, dispatch_qos_t qos,
return _dispatch_queue_class_wakeup(dq, qos, flags, target);
}

#if DISPATCH_COCOA_COMPAT
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)
DISPATCH_ALWAYS_INLINE
static inline bool
_dispatch_runloop_handle_is_valid(dispatch_runloop_handle_t handle)
Expand All @@ -4485,6 +4489,8 @@ _dispatch_runloop_handle_is_valid(dispatch_runloop_handle_t handle)
return MACH_PORT_VALID(handle);
#elif defined(__linux__)
return handle >= 0;
#elif defined(_WIN32)
return handle != INVALID_HANDLE_VALUE;
#else
#error "runloop support not implemented on this platform"
#endif
Expand All @@ -4499,6 +4505,8 @@ _dispatch_runloop_queue_get_handle(dispatch_queue_t dq)
#elif defined(__linux__)
// decode: 0 is a valid fd, so offset by 1 to distinguish from NULL
return ((dispatch_runloop_handle_t)(uintptr_t)dq->do_ctxt) - 1;
#elif defined(_WIN32)
return ((dispatch_runloop_handle_t)(uintptr_t)dq->do_ctxt);
#else
#error "runloop support not implemented on this platform"
#endif
Expand All @@ -4513,6 +4521,8 @@ _dispatch_runloop_queue_set_handle(dispatch_queue_t dq, dispatch_runloop_handle_
#elif defined(__linux__)
// encode: 0 is a valid fd, so offset by 1 to distinguish from NULL
dq->do_ctxt = (void *)(uintptr_t)(handle + 1);
#elif defined(_WIN32)
dq->do_ctxt = (void *)(uintptr_t)handle;
#else
#error "runloop support not implemented on this platform"
#endif
Expand All @@ -4527,7 +4537,7 @@ _dispatch_runloop_queue_reset_max_qos(dispatch_queue_class_t dqu)
old_state = os_atomic_and_orig2o(dqu._dq, dq_state, ~clear_bits, relaxed);
return _dq_state_max_qos(old_state);
}
#endif // DISPATCH_COCOA_COMPAT
#endif

void
_dispatch_runloop_queue_wakeup(dispatch_queue_t dq, dispatch_qos_t qos,
Expand Down Expand Up @@ -5112,7 +5122,7 @@ _dispatch_queue_serial_drain(dispatch_queue_t dq, dispatch_invoke_context_t dic,
return _dispatch_queue_drain(dq, dic, flags, owned, true);
}

#if DISPATCH_COCOA_COMPAT
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)
DISPATCH_NOINLINE
static void
_dispatch_main_queue_update_priority_from_thread(void)
Expand Down Expand Up @@ -6178,7 +6188,7 @@ _dispatch_network_root_queue_create_4NW(const char *label,

static bool _dispatch_program_is_probably_callback_driven;

#if DISPATCH_COCOA_COMPAT
#if DISPATCH_COCOA_COMPAT || defined(_WIN32)

dispatch_queue_t
_dispatch_runloop_root_queue_create_4CF(const char *label, unsigned long flags)
Expand Down Expand Up @@ -6242,7 +6252,7 @@ _dispatch_runloop_root_queue_wakeup_4CF(dispatch_queue_t dq)
_dispatch_runloop_queue_wakeup(dq, 0, false);
}

#if TARGET_OS_MAC
#if TARGET_OS_MAC || defined(_WIN32)
dispatch_runloop_handle_t
_dispatch_runloop_root_queue_get_port_4CF(dispatch_queue_t dq)
{
Expand Down Expand Up @@ -6305,6 +6315,8 @@ _dispatch_runloop_queue_handle_init(void *ctxt)
}
}
handle = fd;
#elif defined(_WIN32)
handle = INVALID_HANDLE_VALUE;
#else
#error "runloop support not implemented on this platform"
#endif
Expand Down Expand Up @@ -6332,6 +6344,8 @@ _dispatch_runloop_queue_handle_dispose(dispatch_queue_t dq)
#elif defined(__linux__)
int rc = close(handle);
(void)dispatch_assume_zero(rc);
#elif defined(_WIN32)
CloseHandle(handle);
#else
#error "runloop support not implemented on this platform"
#endif
Expand Down
24 changes: 0 additions & 24 deletions src/shims/generic_win_stubs.c

This file was deleted.

5 changes: 5 additions & 0 deletions src/swift/DispatchStubs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
#include <dispatch/dispatch.h>
#include <stdio.h>

#if defined(__ELF__) || defined(__MACH__) || defined(__WASM__)
#define DISPATCH_RUNTIME_STDLIB_INTERFACE __attribute__((__visibility__("default")))
#else
#define DISPATCH_RUNTIME_STDLIB_INTERFACE __declspec(dllexport)
#endif

#if USE_OBJC
@protocol OS_dispatch_source;
Expand Down Expand Up @@ -54,6 +58,7 @@ static void _dispatch_overlay_constructor() {
#endif /* USE_OBJC */

#if !USE_OBJC
DISPATCH_RUNTIME_STDLIB_INTERFACE
extern "C" void * objc_retainAutoreleasedReturnValue(void *obj);
#endif

Expand Down