Skip to content

Commit 4c28c12

Browse files
committed
Rollup commit.
Should be split up into logical components.
1 parent aacc68d commit 4c28c12

File tree

12 files changed

+103
-30
lines changed

12 files changed

+103
-30
lines changed

Diff for: cmake/modules/SwiftSupport.cmake

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ function(get_swift_host_arch result_var_name)
2323
set("${result_var_name}" "armv7" PARENT_SCOPE)
2424
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
2525
set("${result_var_name}" "armv7" PARENT_SCOPE)
26+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "amd64")
27+
set("${result_var_name}" "amd64" PARENT_SCOPE)
2628
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
2729
set("${result_var_name}" "x86_64" PARENT_SCOPE)
2830
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")

Diff for: private/private.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ void _dispatch_prohibit_transition_to_multithreaded(bool prohibit);
177177

178178
#if TARGET_OS_MAC
179179
#define DISPATCH_COCOA_COMPAT 1
180-
#elif defined(__linux__) || defined(__FreeBSD__) || defined(_WIN32)
180+
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(_WIN32)
181181
#define DISPATCH_COCOA_COMPAT 1
182182
#else
183183
#define DISPATCH_COCOA_COMPAT 0

Diff for: src/event/event_internal.h

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ _dispatch_timer_flags_from_clock(dispatch_clock_t clock)
125125

126126
#if defined(_WIN32)
127127
typedef uintptr_t dispatch_unote_ident_t;
128+
#elif defined(__OpenBSD__)
129+
typedef uint32_t dispatch_unote_ident_t;
128130
#else
129131
typedef uint32_t dispatch_unote_ident_t;
130132
#endif

Diff for: src/event/event_kevent.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ _dispatch_kq_create(intptr_t *fd_ptr)
599599
#else
600600
.ident = DISPATCH_KEVENT_ERSATZ_EVFILT_USER_MASK,
601601
.filter = EVFILT_TIMER,
602-
.flags = EV_ADD|EV_ONESHOT,
602+
.flags = EV_ADD|EV_DISABLE,
603603
.data = 1,
604604
#endif
605605
};
@@ -745,6 +745,10 @@ _dispatch_kq_poll(dispatch_wlh_t wlh, dispatch_kevent_t ke, int n,
745745
(void)avail;
746746
const struct timespec timeout_immediately = {}, *timeout = NULL;
747747
if (flags & KEVENT_FLAG_IMMEDIATE) timeout = &timeout_immediately;
748+
#if EVFILT_USER
749+
const struct timespec timeout_1ms = {.tv_sec = 0, .tv_nsec = 1000000}
750+
if (ke.ident == DISPATCH_KEVENT_ERSATZ_EVFILT_USER_MASK) timeout = &timeout_1ms;
751+
#endif
748752
r = kevent(kqfd, ke, n, ke_out, n_out, timeout);
749753
#endif
750754
#if DISPATCH_USE_KEVENT_WORKLOOP
@@ -1920,7 +1924,7 @@ _dispatch_event_loop_poke(dispatch_wlh_t wlh, uint64_t dq_state, uint32_t flags)
19201924
#else
19211925
.ident = DISPATCH_KEVENT_ERSATZ_EVFILT_USER_MASK,
19221926
.filter = EVFILT_TIMER,
1923-
.flags = EV_ADD|EV_ONESHOT,
1927+
.flags = EV_ADD|EV_ENABLE,
19241928
.data = 1
19251929
#endif
19261930
};

Diff for: src/init.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ _dispatch_bug_kevent_vanished(dispatch_unote_t du)
10461046
"{ %p[%s], ident: %" PRIdPTR " / 0x%" PRIxPTR ", handler: %p }",
10471047
dux_type(du._du)->dst_kind, dou._dq,
10481048
dou._dq->dq_label ? dou._dq->dq_label : "<unknown>",
1049-
du._du->du_ident, du._du->du_ident, func);
1049+
(intptr_t)du._du->du_ident, (uintptr_t)du._du->du_ident, func);
10501050
}
10511051

10521052
#endif // RDAR_49023449
@@ -1151,8 +1151,8 @@ _dispatch_logv_init(void *context DISPATCH_UNUSED)
11511151
}
11521152
#else
11531153
dprintf(dispatch_logfile, "=== log file opened for %s[%u] at "
1154-
"%ld.%06u ===\n", getprogname() ?: "", getpid(),
1155-
tv.tv_sec, (int)tv.tv_usec);
1154+
"%lld.%06u ===\n", getprogname() ?: "", getpid(),
1155+
(time_t)tv.tv_sec, (int)tv.tv_usec);
11561156
#endif
11571157
}
11581158
}

Diff for: src/queue.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -6941,7 +6941,7 @@ _dispatch_runloop_root_queue_wakeup_4CF(dispatch_queue_t dq)
69416941
_dispatch_runloop_queue_wakeup(upcast(dq)._dl, 0, false);
69426942
}
69436943

6944-
#if TARGET_OS_MAC || defined(_WIN32)
6944+
#if TARGET_OS_MAC || defined(_WIN32) || defined(__OpenBSD__)
69456945
dispatch_runloop_handle_t
69466946
_dispatch_runloop_root_queue_get_port_4CF(dispatch_queue_t dq)
69476947
{
@@ -6976,6 +6976,7 @@ void
69766976
_dispatch_main_queue_callback_4CF(
69776977
void *ignored DISPATCH_UNUSED)
69786978
{
6979+
struct dispatch_tsd *tsd = _dispatch_get_tsd_base();
69796980
// the main queue cannot be suspended and no-one looks at this bit
69806981
// so abuse it to avoid dirtying more memory
69816982

@@ -6985,6 +6986,7 @@ _dispatch_main_queue_callback_4CF(
69856986
_dispatch_main_q.dq_side_suspend_cnt = true;
69866987
_dispatch_main_queue_drain(&_dispatch_main_q);
69876988
_dispatch_main_q.dq_side_suspend_cnt = false;
6989+
(void)tsd;
69886990
}
69896991

69906992
#endif // DISPATCH_COCOA_COMPAT
@@ -7331,6 +7333,13 @@ _gettid(void)
73317333
{
73327334
return (pid_t)pthread_getthreadid_np();
73337335
}
7336+
#elif defined(__OpenBSD__)
7337+
DISPATCH_ALWAYS_INLINE
7338+
static inline pid_t
7339+
_gettid(void)
7340+
{
7341+
return getthrid();
7342+
}
73347343
#elif defined(_WIN32)
73357344
DISPATCH_ALWAYS_INLINE
73367345
static inline DWORD

Diff for: src/shims/lock.c

+31-6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ _dispatch_thread_switch(dispatch_lock value, dispatch_lock_options_t flags,
5656
#endif
5757
#endif
5858

59+
#if defined(__unix__)
60+
DISPATCH_ALWAYS_INLINE
61+
static inline void
62+
_dispatch_thread_switch(dispatch_lock value, dispatch_lock_options_t flags,
63+
uint32_t timeout)
64+
{
65+
(void)value;
66+
(void)flags;
67+
(void)timeout;
68+
}
69+
#endif
70+
5971
#pragma mark - semaphores
6072

6173
#if USE_MACH_SEM
@@ -394,8 +406,10 @@ _dispatch_unfair_lock_wake(uint32_t *uaddr, uint32_t flags)
394406
#include <sys/time.h>
395407
#ifdef __ANDROID__
396408
#include <sys/syscall.h>
397-
#else
409+
#elif __linux__
398410
#include <syscall.h>
411+
#else
412+
#include <sys/futex.h>
399413
#endif /* __ANDROID__ */
400414

401415
DISPATCH_ALWAYS_INLINE
@@ -404,7 +418,12 @@ _dispatch_futex(uint32_t *uaddr, int op, uint32_t val,
404418
const struct timespec *timeout, uint32_t *uaddr2, uint32_t val3,
405419
int opflags)
406420
{
421+
#if __linux__
407422
return (int)syscall(SYS_futex, uaddr, op | opflags, val, timeout, uaddr2, val3);
423+
#else
424+
(void)val3;
425+
return futex(uaddr, op | opflags, (int)val, timeout, uaddr2);
426+
#endif
408427
}
409428

410429
// returns 0, ETIMEDOUT, EFAULT, EINTR, EWOULDBLOCK
@@ -414,11 +433,15 @@ _futex_blocking_op(uint32_t *uaddr, int futex_op, uint32_t val,
414433
const struct timespec *timeout, int flags)
415434
{
416435
for (;;) {
417-
int rc = _dispatch_futex(uaddr, futex_op, val, timeout, NULL, 0, flags);
418-
if (!rc) {
436+
int err = _dispatch_futex(uaddr, futex_op, val, timeout, NULL, 0, flags);
437+
if (!err) {
419438
return 0;
420439
}
421-
switch (errno) {
440+
#if __linux__
441+
// syscall sets errno to communicate error code.
442+
err = errno
443+
#endif
444+
switch (err) {
422445
case EINTR:
423446
/*
424447
* if we have a timeout, we need to return for the caller to
@@ -454,6 +477,7 @@ _dispatch_futex_wake(uint32_t *uaddr, int wake, int opflags)
454477
DISPATCH_INTERNAL_CRASH(errno, "_dlock_wake() failed");
455478
}
456479

480+
#if HAVE_FUTEX_PI
457481
static void
458482
_dispatch_futex_lock_pi(uint32_t *uaddr, struct timespec *timeout, int detect,
459483
int opflags)
@@ -471,6 +495,7 @@ _dispatch_futex_unlock_pi(uint32_t *uaddr, int opflags)
471495
if (rc == 0) return;
472496
DISPATCH_CLIENT_CRASH(errno, "futex_unlock_pi() failed");
473497
}
498+
#endif
474499

475500
#endif
476501
#pragma mark - wait for address
@@ -599,7 +624,7 @@ _dispatch_unfair_lock_lock_slow(dispatch_unfair_lock_t dul,
599624
}
600625
}
601626
}
602-
#elif HAVE_FUTEX
627+
#elif HAVE_FUTEX_PI
603628
void
604629
_dispatch_unfair_lock_lock_slow(dispatch_unfair_lock_t dul,
605630
dispatch_lock_options_t flags)
@@ -636,7 +661,7 @@ _dispatch_unfair_lock_unlock_slow(dispatch_unfair_lock_t dul, dispatch_lock cur)
636661
if (_dispatch_lock_has_waiters(cur)) {
637662
_dispatch_unfair_lock_wake(&dul->dul_lock, 0);
638663
}
639-
#elif HAVE_FUTEX
664+
#elif HAVE_FUTEX_PI
640665
// futex_unlock_pi() handles both OWNER_DIED which we abuse & WAITERS
641666
_dispatch_futex_unlock_pi(&dul->dul_lock, FUTEX_PRIVATE_FLAG);
642667
#else

Diff for: src/shims/lock.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ _dispatch_lock_owner(dispatch_lock lock_value)
100100
return lock_value & DLOCK_OWNER_MASK;
101101
}
102102

103+
#elif defined(__OpenBSD__)
104+
105+
typedef uint32_t dispatch_tid;
106+
typedef uint32_t dispatch_lock;
107+
108+
#define DLOCK_OWNER_NULL ((dispatch_tid)0)
109+
#define DLOCK_OWNER_MASK ((dispatch_lock)0xfffffffc)
110+
#define DLOCK_WAITERS_BIT ((dispatch_lock)0x00000001)
111+
#define DLOCK_FAILED_TRYLOCK_BIT ((dispatch_lock)0x00000002)
112+
113+
#define _dispatch_tid_self() ((dispatch_tid)(_dispatch_get_tsd_base()->tid))
114+
115+
DISPATCH_ALWAYS_INLINE
116+
static inline dispatch_tid
117+
_dispatch_lock_owner(dispatch_lock lock_value)
118+
{
119+
return lock_value & DLOCK_OWNER_MASK;
120+
}
121+
103122
#else
104123
# error define _dispatch_lock encoding scheme for your platform here
105124
#endif
@@ -167,10 +186,15 @@ _dispatch_lock_has_failed_trylock(dispatch_lock lock_value)
167186
#endif
168187

169188
#ifndef HAVE_FUTEX
170-
#ifdef __linux__
189+
#if defined(__linux__)
190+
#define HAVE_FUTEX 1
191+
#define HAVE_FUTEX_PI 1
192+
#elif defined(__OpenBSD__)
171193
#define HAVE_FUTEX 1
194+
#define HAVE_FUTEX_PI 0
172195
#else
173196
#define HAVE_FUTEX 0
197+
#define HAVE_FUTEX_PI 0
174198
#endif
175199
#endif // HAVE_FUTEX
176200

Diff for: src/swift/Source.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import CDispatch
1414
import _SwiftDispatchOverlayShims
15-
#if os(Windows)
15+
#if os(Windows) && !os(OpenBSD)
1616
import WinSDK
1717
#endif
1818

@@ -116,7 +116,7 @@ extension DispatchSource {
116116
}
117117
#endif
118118

119-
#if !os(Linux) && !os(Android) && !os(Windows)
119+
#if !os(Linux) && !os(Android) && !os(Windows) && !os(OpenBSD)
120120
public struct ProcessEvent : OptionSet, RawRepresentable {
121121
public let rawValue: UInt
122122
public init(rawValue: UInt) { self.rawValue = rawValue }
@@ -174,22 +174,22 @@ extension DispatchSource {
174174
}
175175
#endif
176176

177-
#if !os(Linux) && !os(Android) && !os(Windows)
177+
#if !os(Linux) && !os(Android) && !os(Windows) && !os(OpenBSD)
178178
public class func makeProcessSource(identifier: pid_t, eventMask: ProcessEvent, queue: DispatchQueue? = nil) -> DispatchSourceProcess {
179179
let source = dispatch_source_create(_swift_dispatch_source_type_PROC(), UInt(identifier), eventMask.rawValue, queue?.__wrapped)
180180
return DispatchSource(source: source) as DispatchSourceProcess
181181
}
182182
#endif
183183

184-
#if os(Windows)
184+
#if os(Windows) && !os(OpenBSD)
185185
public class func makeReadSource(handle: HANDLE, queue: DispatchQueue? = nil) -> DispatchSourceRead {
186186
let source = dispatch_source_create(_swift_dispatch_source_type_READ(), UInt(bitPattern: handle), 0, queue?.__wrapped)
187187
return DispatchSource(source: source) as DispatchSourceRead
188188
}
189189
#endif
190190

191191
public class func makeReadSource(fileDescriptor: Int32, queue: DispatchQueue? = nil) -> DispatchSourceRead {
192-
#if os(Windows)
192+
#if os(Windows) && !os(OpenBSD)
193193
let handle: UInt = UInt(_get_osfhandle(fileDescriptor))
194194
if handle == UInt(bitPattern: INVALID_HANDLE_VALUE) { fatalError("unable to get underlying handle from file descriptor") }
195195
#else
@@ -224,22 +224,22 @@ extension DispatchSource {
224224
return DispatchSource(source: source) as DispatchSourceUserDataReplace
225225
}
226226

227-
#if !os(Linux) && !os(Android) && !os(Windows)
227+
#if !os(Linux) && !os(Android) && !os(Windows) && !os(OpenBSD)
228228
public class func makeFileSystemObjectSource(fileDescriptor: Int32, eventMask: FileSystemEvent, queue: DispatchQueue? = nil) -> DispatchSourceFileSystemObject {
229229
let source = dispatch_source_create(_swift_dispatch_source_type_VNODE(), UInt(fileDescriptor), eventMask.rawValue, queue?.__wrapped)
230230
return DispatchSource(source: source) as DispatchSourceFileSystemObject
231231
}
232232
#endif
233233

234-
#if os(Windows)
234+
#if os(Windows) && !os(OpenBSD)
235235
public class func makeWriteSource(handle: HANDLE, queue: DispatchQueue? = nil) -> DispatchSourceWrite {
236236
let source = dispatch_source_create(_swift_dispatch_source_type_WRITE(), UInt(bitPattern: handle), 0, queue?.__wrapped)
237237
return DispatchSource(source: source) as DispatchSourceWrite
238238
}
239239
#endif
240240

241241
public class func makeWriteSource(fileDescriptor: Int32, queue: DispatchQueue? = nil) -> DispatchSourceWrite {
242-
#if os(Windows)
242+
#if os(Windows) && !os(OpenBSD)
243243
let handle: UInt = UInt(_get_osfhandle(fileDescriptor))
244244
if handle == UInt(bitPattern: INVALID_HANDLE_VALUE) { fatalError("unable to get underlying handle from file descriptor") }
245245
#else
@@ -290,7 +290,7 @@ extension DispatchSourceMemoryPressure {
290290
}
291291
#endif
292292

293-
#if !os(Linux) && !os(Android) && !os(Windows)
293+
#if !os(Linux) && !os(Android) && !os(Windows) && !os(OpenBSD)
294294
extension DispatchSourceProcess {
295295
public var handle: pid_t {
296296
return pid_t(dispatch_source_get_handle(self as! DispatchSource))
@@ -646,7 +646,7 @@ extension DispatchSourceTimer {
646646
}
647647
}
648648

649-
#if !os(Linux) && !os(Android) && !os(Windows)
649+
#if !os(Linux) && !os(Android) && !os(Windows) && !os(OpenBSD)
650650
extension DispatchSourceFileSystemObject {
651651
public var handle: Int32 {
652652
return Int32(dispatch_source_get_handle((self as! DispatchSource).__wrapped))

Diff for: src/swift/Wrapper.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ extension DispatchSource : DispatchSourceMachSend,
181181
}
182182
#endif
183183

184-
#if !os(Linux) && !os(Android) && !os(Windows)
184+
#if !os(Linux) && !os(Android) && !os(Windows) && !os(OpenBSD)
185185
extension DispatchSource : DispatchSourceProcess,
186186
DispatchSourceFileSystemObject {
187187
}
@@ -272,7 +272,7 @@ public protocol DispatchSourceMemoryPressure : DispatchSourceProtocol {
272272
}
273273
#endif
274274

275-
#if !os(Linux) && !os(Android) && !os(Windows)
275+
#if !os(Linux) && !os(Android) && !os(Windows) && !os(OpenBSD)
276276
public protocol DispatchSourceProcess : DispatchSourceProtocol {
277277
var handle: pid_t { get }
278278

@@ -302,7 +302,7 @@ public protocol DispatchSourceTimer : DispatchSourceProtocol {
302302
func scheduleRepeating(wallDeadline: DispatchWallTime, interval: Double, leeway: DispatchTimeInterval)
303303
}
304304

305-
#if !os(Linux) && !os(Android) && !os(Windows)
305+
#if !os(Linux) && !os(Android) && !os(Windows) && !os(OpenBSD)
306306
public protocol DispatchSourceFileSystemObject : DispatchSourceProtocol {
307307
var handle: Int32 { get }
308308

Diff for: tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ target_link_libraries(bsdtestharness
5656
PRIVATE
5757
bsdtests
5858
dispatch)
59+
target_compile_options(bsdtestharness PRIVATE -fblocks)
5960

6061
function(add_unit_test name)
6162
set(options DISABLED_TEST)

0 commit comments

Comments
 (0)