File: MutexPThread.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (185 lines) | stat: -rw-r--r-- 6,289 bytes parent folder | download
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
//===--- MutexPThread.cpp - Supports Mutex.h using PThreads ---------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Mutex, ConditionVariable, Read/Write lock, and Scoped lock implementations
// using PThreads.
//
//===----------------------------------------------------------------------===//

#if __has_include(<unistd.h>)
#include <unistd.h>
#endif

#if defined(_POSIX_THREADS) && !defined(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)

// Notes: swift::fatalError is not shared between libswiftCore and libswift_Concurrency
// and libswift_Concurrency uses swift_Concurrency_fatalError instead.
/* #ifndef SWIFT_FATAL_ERROR */
/* #define SWIFT_FATAL_ERROR swift::fatalError */
/* #endif */

// This is the concurrency Mutex implementation. We're forcing the concurrency
// fatalError here.
#include "Concurrency/Error.h"
#define SWIFT_FATAL_ERROR swift_Concurrency_fatalError

#include "Concurrency/Threading/Mutex.h"

#include "swift/Runtime/Debug.h"
#include <errno.h>
#include <stdlib.h>

using namespace swift;

#define reportError(PThreadFunction)                                           \
  do {                                                                         \
    int errorcode = PThreadFunction;                                           \
    if (errorcode != 0) {                                                      \
      SWIFT_FATAL_ERROR(/* flags = */ 0, "'%s' failed with error '%s'(%d)\n",  \
                        #PThreadFunction, errorName(errorcode), errorcode);    \
    }                                                                          \
  } while (false)

#define returnTrueOrReportError(PThreadFunction, returnFalseOnEBUSY)           \
  do {                                                                         \
    int errorcode = PThreadFunction;                                           \
    if (errorcode == 0)                                                        \
      return true;                                                             \
    if (returnFalseOnEBUSY && errorcode == EBUSY)                              \
      return false;                                                            \
    SWIFT_FATAL_ERROR(/* flags = */ 0, "'%s' failed with error '%s'(%d)\n",    \
                      #PThreadFunction, errorName(errorcode), errorcode);      \
  } while (false)

static const char *errorName(int errorcode) {
  switch (errorcode) {
  case EINVAL:
    return "EINVAL";
  case EPERM:
    return "EPERM";
  case EDEADLK:
    return "EDEADLK";
  case ENOMEM:
    return "ENOMEM";
  case EAGAIN:
    return "EAGAIN";
  case EBUSY:
    return "EBUSY";
  default:
    return "<unknown>";
  }
}

void ConditionPlatformHelper::init(pthread_cond_t &condition) {
  reportError(pthread_cond_init(&condition, nullptr));
}

void ConditionPlatformHelper::destroy(pthread_cond_t &condition) {
  reportError(pthread_cond_destroy(&condition));
}

void ConditionPlatformHelper::notifyOne(pthread_cond_t &condition) {
  reportError(pthread_cond_signal(&condition));
}

void ConditionPlatformHelper::notifyAll(pthread_cond_t &condition) {
  reportError(pthread_cond_broadcast(&condition));
}

void ConditionPlatformHelper::wait(pthread_cond_t &condition,
                                   pthread_mutex_t &mutex) {
  reportError(pthread_cond_wait(&condition, &mutex));
}

void MutexPlatformHelper::init(pthread_mutex_t &mutex, bool checked) {
  pthread_mutexattr_t attr;
  int kind = (checked ? PTHREAD_MUTEX_ERRORCHECK : PTHREAD_MUTEX_NORMAL);
  reportError(pthread_mutexattr_init(&attr));
  reportError(pthread_mutexattr_settype(&attr, kind));
  reportError(pthread_mutex_init(&mutex, &attr));
  reportError(pthread_mutexattr_destroy(&attr));
}

void MutexPlatformHelper::destroy(pthread_mutex_t &mutex) {
  reportError(pthread_mutex_destroy(&mutex));
}

void MutexPlatformHelper::lock(pthread_mutex_t &mutex) {
  reportError(pthread_mutex_lock(&mutex));
}

void MutexPlatformHelper::unlock(pthread_mutex_t &mutex) {
  reportError(pthread_mutex_unlock(&mutex));
}

bool MutexPlatformHelper::try_lock(pthread_mutex_t &mutex) {
  returnTrueOrReportError(pthread_mutex_trylock(&mutex),
                          /* returnFalseOnEBUSY = */ true);
}

#if HAS_OS_UNFAIR_LOCK

void MutexPlatformHelper::init(os_unfair_lock &lock, bool checked) {
  (void)checked; // Unfair locks are always checked.
  lock = OS_UNFAIR_LOCK_INIT;
}

void MutexPlatformHelper::destroy(os_unfair_lock &lock) {}

void MutexPlatformHelper::lock(os_unfair_lock &lock) {
  os_unfair_lock_lock(&lock);
}

void MutexPlatformHelper::unlock(os_unfair_lock &lock) {
  os_unfair_lock_unlock(&lock);
}

bool MutexPlatformHelper::try_lock(os_unfair_lock &lock) {
  return os_unfair_lock_trylock(&lock);
}

#endif

void ReadWriteLockPlatformHelper::init(pthread_rwlock_t &rwlock) {
  reportError(pthread_rwlock_init(&rwlock, nullptr));
}

void ReadWriteLockPlatformHelper::destroy(pthread_rwlock_t &rwlock) {
  reportError(pthread_rwlock_destroy(&rwlock));
}

void ReadWriteLockPlatformHelper::readLock(pthread_rwlock_t &rwlock) {
  reportError(pthread_rwlock_rdlock(&rwlock));
}

bool ReadWriteLockPlatformHelper::try_readLock(pthread_rwlock_t &rwlock) {
  returnTrueOrReportError(pthread_rwlock_tryrdlock(&rwlock),
                          /* returnFalseOnEBUSY = */ true);
}

void ReadWriteLockPlatformHelper::writeLock(pthread_rwlock_t &rwlock) {
  reportError(pthread_rwlock_wrlock(&rwlock));
}

bool ReadWriteLockPlatformHelper::try_writeLock(pthread_rwlock_t &rwlock) {
  returnTrueOrReportError(pthread_rwlock_trywrlock(&rwlock),
                          /* returnFalseOnEBUSY = */ true);
}

void ReadWriteLockPlatformHelper::readUnlock(pthread_rwlock_t &rwlock) {
  reportError(pthread_rwlock_unlock(&rwlock));
}

void ReadWriteLockPlatformHelper::writeUnlock(pthread_rwlock_t &rwlock) {
  reportError(pthread_rwlock_unlock(&rwlock));
}
#endif