-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR address issue #89002. #### Changes in this PR * Added a simple implementation of `cpp::lock_guard` (an equivalent of `std::lock_guard`) in libc/src/__support/CPP inspired by the libstdc++ implementation * Added tests for `cpp::lock_guard` in /libc/test/src/__support/CPP/mutex_test.cpp * Replaced all references to `MutexLock` with `cpp::lock_guard` --------- Co-authored-by: Guillaume Chatelet <gchatelet@google.com>
- Loading branch information
Showing
13 changed files
with
165 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//===--- A self contained equivalent of std::mutex --------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_MUTEX_H | ||
#define LLVM_LIBC_SRC___SUPPORT_CPP_MUTEX_H | ||
|
||
namespace LIBC_NAMESPACE { | ||
namespace cpp { | ||
|
||
// Assume the calling thread has already obtained mutex ownership. | ||
struct adopt_lock_t { | ||
explicit adopt_lock_t() = default; | ||
}; | ||
|
||
// Tag used to make a scoped lock take ownership of a locked mutex. | ||
constexpr adopt_lock_t adopt_lock{}; | ||
|
||
// An RAII class for easy locking and unlocking of mutexes. | ||
template <typename MutexType> class lock_guard { | ||
MutexType &mutex; | ||
|
||
public: | ||
// Calls `m.lock()` upon resource acquisition. | ||
explicit lock_guard(MutexType &m) : mutex(m) { mutex.lock(); } | ||
|
||
// Acquires ownership of the mutex object `m` without attempting to lock | ||
// it. The behavior is undefined if the current thread does not hold the | ||
// lock on `m`. Does not call `m.lock()` upon resource acquisition. | ||
lock_guard(MutexType &m, adopt_lock_t t) : mutex(m) {} | ||
|
||
~lock_guard() { mutex.unlock(); } | ||
|
||
// non-copyable | ||
lock_guard &operator=(const lock_guard &) = delete; | ||
lock_guard(const lock_guard &) = delete; | ||
}; | ||
|
||
} // namespace cpp | ||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC___SUPPORT_CPP_MUTEX_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//===-- Unittests for mutex -----------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/__support/CPP/mutex.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
using LIBC_NAMESPACE::cpp::adopt_lock; | ||
using LIBC_NAMESPACE::cpp::lock_guard; | ||
|
||
// Simple struct for testing cpp::lock_guard. It defines methods 'lock' and | ||
// 'unlock' which are required for the cpp::lock_guard class template. | ||
struct Mutex { | ||
// Flag to show whether this mutex is locked. | ||
bool locked = false; | ||
|
||
// Flag to show if this mutex has been double locked. | ||
bool double_locked = false; | ||
|
||
// Flag to show if this mutex has been double unlocked. | ||
bool double_unlocked = false; | ||
|
||
Mutex() {} | ||
|
||
void lock() { | ||
if (locked) | ||
double_locked = true; | ||
|
||
locked = true; | ||
} | ||
|
||
void unlock() { | ||
if (!locked) | ||
double_unlocked = true; | ||
|
||
locked = false; | ||
} | ||
}; | ||
|
||
TEST(LlvmLibcMutexTest, Basic) { | ||
Mutex m; | ||
ASSERT_FALSE(m.locked); | ||
ASSERT_FALSE(m.double_locked); | ||
ASSERT_FALSE(m.double_unlocked); | ||
|
||
{ | ||
lock_guard lg(m); | ||
ASSERT_TRUE(m.locked); | ||
ASSERT_FALSE(m.double_locked); | ||
} | ||
|
||
ASSERT_FALSE(m.locked); | ||
ASSERT_FALSE(m.double_unlocked); | ||
} | ||
|
||
TEST(LlvmLibcMutexTest, AcquireLocked) { | ||
Mutex m; | ||
ASSERT_FALSE(m.locked); | ||
ASSERT_FALSE(m.double_locked); | ||
ASSERT_FALSE(m.double_unlocked); | ||
|
||
// Lock the mutex before placing a lock guard on it. | ||
m.lock(); | ||
ASSERT_TRUE(m.locked); | ||
ASSERT_FALSE(m.double_locked); | ||
|
||
{ | ||
lock_guard lg(m, adopt_lock); | ||
ASSERT_TRUE(m.locked); | ||
ASSERT_FALSE(m.double_locked); | ||
} | ||
|
||
ASSERT_FALSE(m.locked); | ||
ASSERT_FALSE(m.double_unlocked); | ||
} |