[Threading] Add ConditionVariable support.

Swift Concurrency would like to be able to use condition variables.
Add support to the threading packages.

rdar://100236038
This commit is contained in:
Alastair Houghton
2022-09-23 14:45:03 +01:00
parent b1040649f2
commit 4495d63c12
12 changed files with 766 additions and 50 deletions

View File

@@ -0,0 +1,58 @@
//===--- ScopedLock.h - ScopedLock ---------------------------- -*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
//
// Provides a ScopedLockT utility template that is used by Mutex and
// ConditionVariable.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_THREADING_SCOPEDLOCK_H
#define SWIFT_THREADING_SCOPEDLOCK_H
namespace swift {
// -- ScopedLock ---------------------------------------------------------------
/// Compile time adjusted stack based object that locks/unlocks the supplied
/// Mutex type. Use the provided typedefs instead of this directly.
template <typename T, bool Inverted>
class ScopedLockT {
ScopedLockT() = delete;
ScopedLockT(const ScopedLockT &) = delete;
ScopedLockT &operator=(const ScopedLockT &) = delete;
ScopedLockT(ScopedLockT &&) = delete;
ScopedLockT &operator=(ScopedLockT &&) = delete;
public:
explicit ScopedLockT(T &l) : Lock(l) {
if (Inverted) {
Lock.unlock();
} else {
Lock.lock();
}
}
~ScopedLockT() {
if (Inverted) {
Lock.lock();
} else {
Lock.unlock();
}
}
private:
T &Lock;
};
} // namespace swift
#endif // SWIFT_THREADING_SCOPEDLOCK_H