|
| 1 | +//===--- MutexPThread.cpp - Supports Mutex.h using PThreads ---------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 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 | +// Mutex, ConditionVariable, Read/Write lock, and Scoped lock implementations |
| 14 | +// using PThreads. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#if __has_include(<unistd.h>) |
| 19 | +#include <unistd.h> |
| 20 | +#endif |
| 21 | + |
| 22 | +#if defined(_POSIX_THREADS) && !defined(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME) |
| 23 | + |
| 24 | +// Notes: swift::fatalError is not shared between libswiftCore and libswift_Concurrency |
| 25 | +// and libswift_Concurrency uses swift_Concurrency_fatalError instead. |
| 26 | +/* #ifndef SWIFT_FATAL_ERROR */ |
| 27 | +/* #define SWIFT_FATAL_ERROR swift::fatalError */ |
| 28 | +/* #endif */ |
| 29 | + |
| 30 | +// This is the concurrency Mutex implementation. We're forcing the concurrency |
| 31 | +// fatalError here. |
| 32 | +#include "Concurrency/Error.h" |
| 33 | +#define SWIFT_FATAL_ERROR swift_Concurrency_fatalError |
| 34 | + |
| 35 | +#include "Concurrency/Threading/Mutex.h" |
| 36 | + |
| 37 | +#include "swift/Runtime/Debug.h" |
| 38 | +#include <errno.h> |
| 39 | +#include <stdlib.h> |
| 40 | + |
| 41 | +using namespace swift; |
| 42 | + |
| 43 | +#define reportError(PThreadFunction) \ |
| 44 | + do { \ |
| 45 | + int errorcode = PThreadFunction; \ |
| 46 | + if (errorcode != 0) { \ |
| 47 | + SWIFT_FATAL_ERROR(/* flags = */ 0, "'%s' failed with error '%s'(%d)\n", \ |
| 48 | + #PThreadFunction, errorName(errorcode), errorcode); \ |
| 49 | + } \ |
| 50 | + } while (false) |
| 51 | + |
| 52 | +#define returnTrueOrReportError(PThreadFunction, returnFalseOnEBUSY) \ |
| 53 | + do { \ |
| 54 | + int errorcode = PThreadFunction; \ |
| 55 | + if (errorcode == 0) \ |
| 56 | + return true; \ |
| 57 | + if (returnFalseOnEBUSY && errorcode == EBUSY) \ |
| 58 | + return false; \ |
| 59 | + SWIFT_FATAL_ERROR(/* flags = */ 0, "'%s' failed with error '%s'(%d)\n", \ |
| 60 | + #PThreadFunction, errorName(errorcode), errorcode); \ |
| 61 | + } while (false) |
| 62 | + |
| 63 | +static const char *errorName(int errorcode) { |
| 64 | + switch (errorcode) { |
| 65 | + case EINVAL: |
| 66 | + return "EINVAL"; |
| 67 | + case EPERM: |
| 68 | + return "EPERM"; |
| 69 | + case EDEADLK: |
| 70 | + return "EDEADLK"; |
| 71 | + case ENOMEM: |
| 72 | + return "ENOMEM"; |
| 73 | + case EAGAIN: |
| 74 | + return "EAGAIN"; |
| 75 | + case EBUSY: |
| 76 | + return "EBUSY"; |
| 77 | + default: |
| 78 | + return "<unknown>"; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void ConditionPlatformHelper::init(pthread_cond_t &condition) { |
| 83 | + reportError(pthread_cond_init(&condition, nullptr)); |
| 84 | +} |
| 85 | + |
| 86 | +void ConditionPlatformHelper::destroy(pthread_cond_t &condition) { |
| 87 | + reportError(pthread_cond_destroy(&condition)); |
| 88 | +} |
| 89 | + |
| 90 | +void ConditionPlatformHelper::notifyOne(pthread_cond_t &condition) { |
| 91 | + reportError(pthread_cond_signal(&condition)); |
| 92 | +} |
| 93 | + |
| 94 | +void ConditionPlatformHelper::notifyAll(pthread_cond_t &condition) { |
| 95 | + reportError(pthread_cond_broadcast(&condition)); |
| 96 | +} |
| 97 | + |
| 98 | +void ConditionPlatformHelper::wait(pthread_cond_t &condition, |
| 99 | + pthread_mutex_t &mutex) { |
| 100 | + reportError(pthread_cond_wait(&condition, &mutex)); |
| 101 | +} |
| 102 | + |
| 103 | +void MutexPlatformHelper::init(pthread_mutex_t &mutex, bool checked) { |
| 104 | + pthread_mutexattr_t attr; |
| 105 | + int kind = (checked ? PTHREAD_MUTEX_ERRORCHECK : PTHREAD_MUTEX_NORMAL); |
| 106 | + reportError(pthread_mutexattr_init(&attr)); |
| 107 | + reportError(pthread_mutexattr_settype(&attr, kind)); |
| 108 | + reportError(pthread_mutex_init(&mutex, &attr)); |
| 109 | + reportError(pthread_mutexattr_destroy(&attr)); |
| 110 | +} |
| 111 | + |
| 112 | +void MutexPlatformHelper::destroy(pthread_mutex_t &mutex) { |
| 113 | + reportError(pthread_mutex_destroy(&mutex)); |
| 114 | +} |
| 115 | + |
| 116 | +void MutexPlatformHelper::lock(pthread_mutex_t &mutex) { |
| 117 | + reportError(pthread_mutex_lock(&mutex)); |
| 118 | +} |
| 119 | + |
| 120 | +void MutexPlatformHelper::unlock(pthread_mutex_t &mutex) { |
| 121 | + reportError(pthread_mutex_unlock(&mutex)); |
| 122 | +} |
| 123 | + |
| 124 | +bool MutexPlatformHelper::try_lock(pthread_mutex_t &mutex) { |
| 125 | + returnTrueOrReportError(pthread_mutex_trylock(&mutex), |
| 126 | + /* returnFalseOnEBUSY = */ true); |
| 127 | +} |
| 128 | + |
| 129 | +#if HAS_OS_UNFAIR_LOCK |
| 130 | + |
| 131 | +void MutexPlatformHelper::init(os_unfair_lock &lock, bool checked) { |
| 132 | + (void)checked; // Unfair locks are always checked. |
| 133 | + lock = OS_UNFAIR_LOCK_INIT; |
| 134 | +} |
| 135 | + |
| 136 | +void MutexPlatformHelper::destroy(os_unfair_lock &lock) {} |
| 137 | + |
| 138 | +void MutexPlatformHelper::lock(os_unfair_lock &lock) { |
| 139 | + os_unfair_lock_lock(&lock); |
| 140 | +} |
| 141 | + |
| 142 | +void MutexPlatformHelper::unlock(os_unfair_lock &lock) { |
| 143 | + os_unfair_lock_unlock(&lock); |
| 144 | +} |
| 145 | + |
| 146 | +bool MutexPlatformHelper::try_lock(os_unfair_lock &lock) { |
| 147 | + return os_unfair_lock_trylock(&lock); |
| 148 | +} |
| 149 | + |
| 150 | +#endif |
| 151 | + |
| 152 | +void ReadWriteLockPlatformHelper::init(pthread_rwlock_t &rwlock) { |
| 153 | + reportError(pthread_rwlock_init(&rwlock, nullptr)); |
| 154 | +} |
| 155 | + |
| 156 | +void ReadWriteLockPlatformHelper::destroy(pthread_rwlock_t &rwlock) { |
| 157 | + reportError(pthread_rwlock_destroy(&rwlock)); |
| 158 | +} |
| 159 | + |
| 160 | +void ReadWriteLockPlatformHelper::readLock(pthread_rwlock_t &rwlock) { |
| 161 | + reportError(pthread_rwlock_rdlock(&rwlock)); |
| 162 | +} |
| 163 | + |
| 164 | +bool ReadWriteLockPlatformHelper::try_readLock(pthread_rwlock_t &rwlock) { |
| 165 | + returnTrueOrReportError(pthread_rwlock_tryrdlock(&rwlock), |
| 166 | + /* returnFalseOnEBUSY = */ true); |
| 167 | +} |
| 168 | + |
| 169 | +void ReadWriteLockPlatformHelper::writeLock(pthread_rwlock_t &rwlock) { |
| 170 | + reportError(pthread_rwlock_wrlock(&rwlock)); |
| 171 | +} |
| 172 | + |
| 173 | +bool ReadWriteLockPlatformHelper::try_writeLock(pthread_rwlock_t &rwlock) { |
| 174 | + returnTrueOrReportError(pthread_rwlock_trywrlock(&rwlock), |
| 175 | + /* returnFalseOnEBUSY = */ true); |
| 176 | +} |
| 177 | + |
| 178 | +void ReadWriteLockPlatformHelper::readUnlock(pthread_rwlock_t &rwlock) { |
| 179 | + reportError(pthread_rwlock_unlock(&rwlock)); |
| 180 | +} |
| 181 | + |
| 182 | +void ReadWriteLockPlatformHelper::writeUnlock(pthread_rwlock_t &rwlock) { |
| 183 | + reportError(pthread_rwlock_unlock(&rwlock)); |
| 184 | +} |
| 185 | +#endif |
0 commit comments