Skip to content

Commit 46c0f4a

Browse files
committed
Add a platform executor module for OpenBSD.
This is basically the same as the one for Linux, but it would be somewhat awkward to add the platform conditional on a file named for Linux when OpenBSD is not Linux. Important note: if Dispatch is disabled, then this will cause a compilation error (probably not just for OpenBSD either), because PlatformExecutorFactory is both defined in PlatformExecutorNone.swift and PlatformExecutor<...>.swift in this case. Because this only bites OpenBSD bootstrap builds, and since OpenBSD support has been upstreamed to Dispatch, default to the Dispatch implementation for now to get this in, and we'll refactor in a different pr.
1 parent 6f696a4 commit 46c0f4a

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

Runtimes/Core/Concurrency/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ add_library(swift_Concurrency
8282
PlatformExecutorDarwin.swift
8383
PlatformExecutorLinux.swift
8484
PlatformExecutorFreeBSD.swift
85+
PlatformExecutorOpenBSD.swift
8586
PlatformExecutorWindows.swift
8687
PriorityQueue.swift
8788
SourceCompatibilityShims.swift

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ set(SWIFT_RUNTIME_CONCURRENCY_SWIFT_SOURCES
164164
PlatformExecutorDarwin.swift
165165
PlatformExecutorLinux.swift
166166
PlatformExecutorWindows.swift
167+
PlatformExecutorOpenBSD.swift
167168
PlatformExecutorFreeBSD.swift
168169
)
169170

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 - 2025 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+
13+
#if !$Embedded && os(OpenBSD)
14+
15+
import Swift
16+
17+
// The default executors for now are Dispatch-based
18+
@available(SwiftStdlib 6.2, *)
19+
public struct PlatformExecutorFactory: ExecutorFactory {
20+
public static let mainExecutor: any MainExecutor = DispatchMainExecutor()
21+
public static let defaultExecutor: any TaskExecutor
22+
= DispatchGlobalTaskExecutor()
23+
}
24+
25+
#endif
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Atomics open source project
4+
//
5+
// Copyright (c) 2024 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+
13+
import Glibc
14+
15+
@available(SwiftStdlib 6.0, *)
16+
@frozen
17+
@_staticExclusiveOnly
18+
public struct _MutexHandle: ~Copyable {
19+
@usableFromInline
20+
let value: _Cell<pthread_mutex_t?>
21+
22+
@available(SwiftStdlib 6.0, *)
23+
@_alwaysEmitIntoClient
24+
@_transparent
25+
public init() {
26+
var mx = pthread_mutex_t(bitPattern: 0)
27+
pthread_mutex_init(&mx, nil)
28+
value = _Cell(mx)
29+
}
30+
31+
@available(SwiftStdlib 6.0, *)
32+
@_alwaysEmitIntoClient
33+
@_transparent
34+
internal borrowing func _lock() {
35+
pthread_mutex_lock(value._address)
36+
}
37+
38+
@available(SwiftStdlib 6.0, *)
39+
@_alwaysEmitIntoClient
40+
@_transparent
41+
internal borrowing func _tryLock() -> Bool {
42+
pthread_mutex_trylock(value._address) != 0
43+
}
44+
45+
@available(SwiftStdlib 6.0, *)
46+
@_alwaysEmitIntoClient
47+
@_transparent
48+
internal borrowing func _unlock() {
49+
pthread_mutex_unlock(value._address)
50+
}
51+
52+
@available(SwiftStdlib 6.0, *)
53+
@_alwaysEmitIntoClient
54+
@_transparent
55+
deinit {
56+
pthread_mutex_destroy(value._address)
57+
}
58+
}

0 commit comments

Comments
 (0)