Skip to content

Commit 07384ca

Browse files
committed
revise based on review by das.
1 parent 2e73a01 commit 07384ca

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

private/private.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void _dispatch_prohibit_transition_to_multithreaded(bool prohibit);
166166
* SPI for CoreFoundation/Foundation ONLY
167167
*/
168168

169-
#if (TARGET_OS_MAC || TARGET_OS_WIN32)
169+
#if TARGET_OS_MAC
170170
#define DISPATCH_COCOA_COMPAT 1
171171
#elif defined(__linux__)
172172
#define DISPATCH_COCOA_COMPAT 1
@@ -189,22 +189,17 @@ __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
189189
DISPATCH_EXPORT DISPATCH_CONST DISPATCH_WARN_RESULT DISPATCH_NOTHROW
190190
dispatch_runloop_handle_t
191191
_dispatch_get_main_queue_port_4CF(void);
192+
#endif
192193

193-
__OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
194-
DISPATCH_EXPORT DISPATCH_NOTHROW
195-
void
196-
_dispatch_main_queue_callback_4CF(mach_msg_header_t *_Null_unspecified msg);
197-
#else
198-
__OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
194+
__OSX_AVAILABLE_STARTING(__MAC_10_12,__IPHONE_10_0)
199195
DISPATCH_EXPORT DISPATCH_NOTHROW
200196
dispatch_runloop_handle_t
201197
_dispatch_get_main_queue_handle_4CF(void);
202198

203199
__OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
204200
DISPATCH_EXPORT DISPATCH_NOTHROW
205201
void
206-
_dispatch_main_queue_callback_4CF(void);
207-
#endif
202+
_dispatch_main_queue_callback_4CF(void *_Null_unspecified msg);
208203

209204
__OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_7_0)
210205
DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT

src/queue.c

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4084,6 +4084,7 @@ _dispatch_runloop_queue_get_handle(dispatch_queue_t dq)
40844084
#if TARGET_OS_MAC
40854085
return ((dispatch_runloop_handle_t)(uintptr_t)dq->do_ctxt);
40864086
#elif defined(__linux__)
4087+
// decode: 0 is a valid fd, so offset by 1 to distinguish from NULL
40874088
return ((dispatch_runloop_handle_t)(uintptr_t)dq->do_ctxt) - 1;
40884089
#else
40894090
#error "runloop support not implemented on this platform"
@@ -4097,6 +4098,7 @@ _dispatch_runloop_queue_set_handle(dispatch_queue_t dq, dispatch_runloop_handle_
40974098
#if TARGET_OS_MAC
40984099
dq->do_ctxt = (void *)(uintptr_t)handle;
40994100
#elif defined(__linux__)
4101+
// encode: 0 is a valid fd, so offset by 1 to distinguish from NULL
41004102
dq->do_ctxt = (void *)(uintptr_t)(handle + 1);
41014103
#else
41024104
#error "runloop support not implemented on this platform"
@@ -4169,7 +4171,8 @@ _dispatch_runloop_queue_class_poke(dispatch_queue_t dq)
41694171
}
41704172

41714173
#if TARGET_OS_MAC
4172-
kern_return_t kr = _dispatch_send_wakeup_runloop_thread(handle, 0);
4174+
mach_port_t mp = handle;
4175+
kern_return_t kr = _dispatch_send_wakeup_runloop_thread(mp, 0);
41734176
switch (kr) {
41744177
case MACH_SEND_TIMEOUT:
41754178
case MACH_SEND_TIMED_OUT:
@@ -5656,30 +5659,52 @@ _dispatch_runloop_queue_handle_init(void *ctxt)
56565659
_dispatch_fork_becomes_unsafe();
56575660

56585661
#if TARGET_OS_MAC
5662+
mach_port_t mp;
56595663
kern_return_t kr;
5660-
kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &handle);
5664+
kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
56615665
DISPATCH_VERIFY_MIG(kr);
56625666
(void)dispatch_assume_zero(kr);
5663-
kr = mach_port_insert_right(mach_task_self(), handle, handle,
5667+
kr = mach_port_insert_right(mach_task_self(), mp, mp,
56645668
MACH_MSG_TYPE_MAKE_SEND);
56655669
DISPATCH_VERIFY_MIG(kr);
56665670
(void)dispatch_assume_zero(kr);
56675671
if (dq != &_dispatch_main_q) {
56685672
struct mach_port_limits limits = {
56695673
.mpl_qlimit = 1,
56705674
};
5671-
kr = mach_port_set_attributes(mach_task_self(), handle,
5675+
kr = mach_port_set_attributes(mach_task_self(), mp,
56725676
MACH_PORT_LIMITS_INFO, (mach_port_info_t)&limits,
56735677
sizeof(limits));
56745678
DISPATCH_VERIFY_MIG(kr);
56755679
(void)dispatch_assume_zero(kr);
56765680
}
5681+
handle = mp;
56775682
#elif defined(__linux__)
5678-
handle = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
5683+
int fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
5684+
if (fd == -1) {
5685+
int err = errno;
5686+
switch (err) {
5687+
case EMFILE:
5688+
DISPATCH_CLIENT_CRASH(err, "eventfd() failure: "
5689+
"process is out of file descriptors");
5690+
break;
5691+
case ENFILE:
5692+
DISPATCH_CLIENT_CRASH(err, "eventfd() failure: "
5693+
"system is out of file descriptors");
5694+
break;
5695+
case ENOMEM:
5696+
DISPATCH_CLIENT_CRASH(err, "eventfd() failure: "
5697+
"kernel is out of memory");
5698+
break;
5699+
default:
5700+
DISPATCH_INTERNAL_CRASH(err, "eventfd() failure");
5701+
break;
5702+
}
5703+
}
5704+
handle = fd;
56795705
#else
56805706
#error "runloop support not implemented on this platform"
56815707
#endif
5682-
(void)dispatch_assume(_dispatch_runloop_handle_is_valid(handle));
56835708
_dispatch_runloop_queue_set_handle(dq, handle);
56845709

56855710
_dispatch_program_is_probably_callback_driven = true;
@@ -5694,7 +5719,8 @@ _dispatch_runloop_queue_handle_dispose(dispatch_queue_t dq)
56945719
}
56955720
dq->do_ctxt = NULL;
56965721
#if TARGET_OS_MAC
5697-
kern_return_t kr = mach_port_deallocate(mach_task_self(), hand);
5722+
mach_port_t mp = handle;
5723+
kern_return_t kr = mach_port_deallocate(mach_task_self(), mp);
56985724
DISPATCH_VERIFY_MIG(kr);
56995725
(void)dispatch_assume_zero(kr);
57005726
kr = mach_port_mod_refs(mach_task_self(), mp, MACH_PORT_RIGHT_RECEIVE, -1);
@@ -5712,18 +5738,22 @@ _dispatch_runloop_queue_handle_dispose(dispatch_queue_t dq)
57125738
#pragma mark dispatch_main_queue
57135739

57145740
dispatch_runloop_handle_t
5715-
#if TARGET_OS_MAC
5716-
_dispatch_get_main_queue_port_4CF(void)
5717-
#else
57185741
_dispatch_get_main_queue_handle_4CF(void)
5719-
#endif
57205742
{
57215743
dispatch_queue_t dq = &_dispatch_main_q;
57225744
dispatch_once_f(&_dispatch_main_q_handle_pred, dq,
57235745
_dispatch_runloop_queue_handle_init);
57245746
return _dispatch_runloop_queue_get_handle(dq);
57255747
}
57265748

5749+
#if TARGET_OS_MAC
5750+
dispatch_runloop_handle_t
5751+
_dispatch_get_main_queue_port_4CF(void)
5752+
{
5753+
return _dispatch_get_main_queue_handle_4CF();
5754+
}
5755+
#endif
5756+
57275757
static bool main_q_is_draining;
57285758

57295759
// 6618342 Contact the team that owns the Instrument DTrace probe before
@@ -5736,11 +5766,7 @@ _dispatch_queue_set_mainq_drain_state(bool arg)
57365766
}
57375767

57385768
void
5739-
#if TARGET_OS_MAC
5740-
_dispatch_main_queue_callback_4CF(mach_msg_header_t *msg DISPATCH_UNUSED)
5741-
#else
5742-
_dispatch_main_queue_callback_4CF(void)
5743-
#endif
5769+
_dispatch_main_queue_callback_4CF(void *ignored DISPATCH_UNUSED)
57445770
{
57455771
if (main_q_is_draining) {
57465772
return;

0 commit comments

Comments
 (0)