mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Swift Concurrency would like to be able to use condition variables. Add support to the threading packages. rdar://100236038
59 lines
1.6 KiB
C++
59 lines
1.6 KiB
C++
//===--- 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
|