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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
//===--- ThreadBarriers.swift ---------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 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
//
//===----------------------------------------------------------------------===//
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Android)
import Android
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
import WinSDK
#endif
//
// Implement pthread barriers.
//
// (OS X does not implement them.)
//
public var _stdlib_THREAD_BARRIER_SERIAL_THREAD: CInt {
return 1
}
public struct _stdlib_thread_barrier_t {
#if os(Windows)
var mutex: UnsafeMutablePointer<SRWLOCK>?
var cond: UnsafeMutablePointer<CONDITION_VARIABLE>?
#elseif os(Cygwin) || os(FreeBSD) || os(OpenBSD)
var mutex: UnsafeMutablePointer<pthread_mutex_t?>?
var cond: UnsafeMutablePointer<pthread_cond_t?>?
#elseif os(WASI)
// pthread is currently not available on WASI
#else
var mutex: UnsafeMutablePointer<pthread_mutex_t>?
var cond: UnsafeMutablePointer<pthread_cond_t>?
#endif
/// The number of threads to synchronize.
var count: CUnsignedInt = 0
/// The number of threads already waiting on the barrier.
///
/// This shared variable is protected by `mutex`.
var numThreadsWaiting: CUnsignedInt = 0
public init() {}
}
public func _stdlib_thread_barrier_init(
_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>,
_ count: CUnsignedInt
) -> CInt {
barrier.pointee = _stdlib_thread_barrier_t()
if count == 0 {
errno = EINVAL
return -1
}
#if os(Windows)
barrier.pointee.mutex = UnsafeMutablePointer.allocate(capacity: 1)
InitializeSRWLock(barrier.pointee.mutex!)
barrier.pointee.cond = UnsafeMutablePointer.allocate(capacity: 1)
InitializeConditionVariable(barrier.pointee.cond!)
#elseif os(WASI)
// WASI environment has only a single thread
#else
barrier.pointee.mutex = UnsafeMutablePointer.allocate(capacity: 1)
barrier.pointee.cond = UnsafeMutablePointer.allocate(capacity: 1)
guard _stdlib_thread_barrier_mutex_and_cond_init(barrier) == 0 else {
barrier.pointee.mutex!.deinitialize(count: 1)
barrier.pointee.mutex!.deallocate()
barrier.pointee.cond!.deinitialize(count: 1)
barrier.pointee.cond!.deallocate()
return -1
}
#endif
barrier.pointee.count = count
return 0
}
#if !os(Windows) && !os(WASI)
private func _stdlib_thread_barrier_mutex_and_cond_init(_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>) -> CInt {
guard pthread_mutex_init(barrier.pointee.mutex!, nil) == 0 else {
return -1
}
guard pthread_cond_init(barrier.pointee.cond!, nil) == 0 else {
pthread_mutex_destroy(barrier.pointee.mutex!)
return -1
}
return 0
}
#endif
public func _stdlib_thread_barrier_destroy(
_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>
) {
#if os(Windows)
// Condition Variables do not need to be explicitly destroyed
// Mutexes do not need to be explicitly destroyed
#elseif os(WASI)
// WASI environment has only a single thread
#else
guard pthread_cond_destroy(barrier.pointee.cond!) == 0 &&
pthread_mutex_destroy(barrier.pointee.mutex!) == 0 else {
fatalError("_stdlib_thread_barrier_destroy() failed")
}
#endif
#if !os(WASI)
barrier.pointee.cond!.deinitialize(count: 1)
barrier.pointee.cond!.deallocate()
barrier.pointee.mutex!.deinitialize(count: 1)
barrier.pointee.mutex!.deallocate()
#endif
return
}
public func _stdlib_thread_barrier_wait(
_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>
) -> CInt {
#if os(Windows)
AcquireSRWLockExclusive(barrier.pointee.mutex!)
#elseif os(WASI)
// WASI environment has only a single thread
#else
if pthread_mutex_lock(barrier.pointee.mutex!) != 0 {
return -1
}
#endif
barrier.pointee.numThreadsWaiting += 1
if barrier.pointee.numThreadsWaiting < barrier.pointee.count {
// Put the thread to sleep.
#if os(Windows)
if !SleepConditionVariableSRW(barrier.pointee.cond!, barrier.pointee.mutex!,
INFINITE, 0) {
return -1
}
ReleaseSRWLockExclusive(barrier.pointee.mutex!)
#elseif os(WASI)
// WASI environment has a only single thread
#else
if pthread_cond_wait(barrier.pointee.cond!, barrier.pointee.mutex!) != 0 {
return -1
}
if pthread_mutex_unlock(barrier.pointee.mutex!) != 0 {
return -1
}
#endif
return 0
} else {
// Reset thread count.
barrier.pointee.numThreadsWaiting = 0
// Wake up all threads.
#if os(Windows)
WakeAllConditionVariable(barrier.pointee.cond!)
ReleaseSRWLockExclusive(barrier.pointee.mutex!)
#elseif os(WASI)
// WASI environment has a only single thread
#else
if pthread_cond_broadcast(barrier.pointee.cond!) != 0 {
return -1
}
if pthread_mutex_unlock(barrier.pointee.mutex!) != 0 {
return -1
}
#endif
return _stdlib_THREAD_BARRIER_SERIAL_THREAD
}
}
|