forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSwiftPrivateThreadExtras.swift
148 lines (131 loc) · 4.6 KB
/
SwiftPrivateThreadExtras.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//===--- SwiftPrivateThreadExtras.swift ----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file contains wrappers for pthread APIs that are less painful to use
// than the C APIs.
//
//===----------------------------------------------------------------------===//
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
import Glibc
#endif
/// An abstract base class to encapsulate the context necessary to invoke
/// a block from pthread_create.
internal class ThreadBlockContext {
/// Execute the block, and return an `UnsafeMutablePointer` to memory
/// allocated with `UnsafeMutablePointer.alloc` containing the result of the
/// block.
func run() -> UnsafeMutableRawPointer { fatalError("abstract") }
}
internal class ThreadBlockContextImpl<Argument, Result>: ThreadBlockContext {
let block: (Argument) -> Result
let arg: Argument
init(block: @escaping (Argument) -> Result, arg: Argument) {
self.block = block
self.arg = arg
super.init()
}
override func run() -> UnsafeMutableRawPointer {
let result = UnsafeMutablePointer<Result>.allocate(capacity: 1)
result.initialize(to: block(arg))
return UnsafeMutableRawPointer(result)
}
}
/// Entry point for `pthread_create` that invokes a block context.
internal func invokeBlockContext(
_ contextAsVoidPointer: UnsafeMutableRawPointer?
) -> UnsafeMutableRawPointer! {
// The context is passed in +1; we're responsible for releasing it.
let context = Unmanaged<ThreadBlockContext>
.fromOpaque(contextAsVoidPointer!)
.takeRetainedValue()
return context.run()
}
#if os(Cygwin) || os(FreeBSD) || os(Haiku)
public typealias _stdlib_pthread_attr_t = UnsafePointer<pthread_attr_t?>
#else
public typealias _stdlib_pthread_attr_t = UnsafePointer<pthread_attr_t>
#endif
/// Block-based wrapper for `pthread_create`.
public func _stdlib_pthread_create_block<Argument, Result>(
_ attr: _stdlib_pthread_attr_t?,
_ start_routine: @escaping (Argument) -> Result,
_ arg: Argument
) -> (CInt, pthread_t?) {
let context = ThreadBlockContextImpl(block: start_routine, arg: arg)
// We hand ownership off to `invokeBlockContext` through its void context
// argument.
let contextAsVoidPointer = Unmanaged.passRetained(context).toOpaque()
var threadID = _make_pthread_t()
let result = pthread_create(&threadID, attr,
{ invokeBlockContext($0) }, contextAsVoidPointer)
if result == 0 {
return (result, threadID)
} else {
return (result, nil)
}
}
#if os(Linux) || os(Android)
internal func _make_pthread_t() -> pthread_t {
return pthread_t()
}
#else
internal func _make_pthread_t() -> pthread_t? {
return nil
}
#endif
/// Block-based wrapper for `pthread_join`.
public func _stdlib_pthread_join<Result>(
_ thread: pthread_t,
_ resultType: Result.Type
) -> (CInt, Result?) {
var threadResultRawPtr: UnsafeMutableRawPointer?
let result = pthread_join(thread, &threadResultRawPtr)
if result == 0 {
let threadResultPtr = threadResultRawPtr!.assumingMemoryBound(
to: Result.self)
let threadResult = threadResultPtr.pointee
threadResultPtr.deinitialize(count: 1)
threadResultPtr.deallocate()
return (result, threadResult)
} else {
return (result, nil)
}
}
public class _stdlib_Barrier {
var _pthreadBarrier: _stdlib_pthread_barrier_t
var _pthreadBarrierPtr: UnsafeMutablePointer<_stdlib_pthread_barrier_t> {
return _getUnsafePointerToStoredProperties(self)
.assumingMemoryBound(to: _stdlib_pthread_barrier_t.self)
}
public init(threadCount: Int) {
self._pthreadBarrier = _stdlib_pthread_barrier_t()
let ret = _stdlib_pthread_barrier_init(
_pthreadBarrierPtr, CUnsignedInt(threadCount))
if ret != 0 {
fatalError("_stdlib_pthread_barrier_init() failed")
}
}
deinit {
let ret = _stdlib_pthread_barrier_destroy(_pthreadBarrierPtr)
if ret != 0 {
fatalError("_stdlib_pthread_barrier_destroy() failed")
}
}
public func wait() {
let ret = _stdlib_pthread_barrier_wait(_pthreadBarrierPtr)
if !(ret == 0 || ret == _stdlib_PTHREAD_BARRIER_SERIAL_THREAD) {
fatalError("_stdlib_pthread_barrier_wait() failed")
}
}
}