-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][__support] move CndVar to __support #89329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,52 @@ | ||
//===-- A platform independent abstraction layer for cond vars --*- 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___SUPPORT_SRC_THREADS_LINUX_CNDVAR_H | ||
#define LLVM_LIBC___SUPPORT_SRC_THREADS_LINUX_CNDVAR_H | ||
|
||
#include "src/__support/threads/linux/futex_utils.h" // Futex | ||
#include "src/__support/threads/mutex.h" // Mutex | ||
|
||
#include <stdint.h> // uint32_t | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
struct CndVar { | ||
enum CndWaiterStatus : uint32_t { | ||
WS_Waiting = 0xE, | ||
WS_Signalled = 0x5, | ||
}; | ||
|
||
struct CndWaiter { | ||
Futex futex_word = WS_Waiting; | ||
CndWaiter *next = nullptr; | ||
}; | ||
|
||
CndWaiter *waitq_front; | ||
CndWaiter *waitq_back; | ||
Mutex qmtx; | ||
|
||
static int init(CndVar *cv) { | ||
cv->waitq_front = cv->waitq_back = nullptr; | ||
auto err = Mutex::init(&cv->qmtx, false, false, false); | ||
return err == MutexError::NONE ? 0 : -1; | ||
} | ||
|
||
static void destroy(CndVar *cv) { | ||
cv->waitq_front = cv->waitq_back = nullptr; | ||
} | ||
|
||
// Returns 0 on success, -1 on error. | ||
int wait(Mutex *m); | ||
void notify_one(); | ||
void broadcast(); | ||
}; | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_LINUX_CNDVAR_H |
This file contains hidden or 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 hidden or 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,103 @@ | ||
//===-- Utility condition variable class ------------------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/__support/threads/CndVar.h" | ||
#include "src/__support/OSUtil/syscall.h" // syscall_impl | ||
#include "src/__support/threads/linux/futex_word.h" // FutexWordType | ||
#include "src/__support/threads/mutex.h" // Mutex, MutexLock | ||
|
||
#include <sys/syscall.h> // For syscall numbers. | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
int CndVar::wait(Mutex *m) { | ||
// The goal is to perform "unlock |m| and wait" in an | ||
// atomic operation. However, it is not possible to do it | ||
// in the true sense so we do it in spirit. Before unlocking | ||
// |m|, a new waiter object is added to the waiter queue with | ||
// the waiter queue locked. Iff a signalling thread signals | ||
// the waiter before the waiter actually starts waiting, the | ||
// wait operation will not begin at all and the waiter immediately | ||
// returns. | ||
|
||
CndWaiter waiter; | ||
{ | ||
MutexLock ml(&qmtx); | ||
CndWaiter *old_back = nullptr; | ||
if (waitq_front == nullptr) { | ||
waitq_front = waitq_back = &waiter; | ||
} else { | ||
old_back = waitq_back; | ||
waitq_back->next = &waiter; | ||
waitq_back = &waiter; | ||
} | ||
|
||
if (m->unlock() != MutexError::NONE) { | ||
// If we do not remove the queued up waiter before returning, | ||
// then another thread can potentially signal a non-existing | ||
// waiter. Note also that we do this with |qmtx| locked. This | ||
// ensures that another thread will not signal the withdrawing | ||
// waiter. | ||
waitq_back = old_back; | ||
if (waitq_back == nullptr) | ||
waitq_front = nullptr; | ||
else | ||
waitq_back->next = nullptr; | ||
|
||
return -1; | ||
} | ||
} | ||
|
||
waiter.futex_word.wait(WS_Waiting, cpp::nullopt, true); | ||
|
||
// At this point, if locking |m| fails, we can simply return as the | ||
// queued up waiter would have been removed from the queue. | ||
auto err = m->lock(); | ||
return err == MutexError::NONE ? 0 : -1; | ||
} | ||
|
||
void CndVar::notify_one() { | ||
// We don't use an RAII locker in this method as we want to unlock | ||
// |qmtx| and signal the waiter using a single FUTEX_WAKE_OP signal. | ||
qmtx.lock(); | ||
if (waitq_front == nullptr) | ||
qmtx.unlock(); | ||
|
||
CndWaiter *first = waitq_front; | ||
waitq_front = waitq_front->next; | ||
if (waitq_front == nullptr) | ||
waitq_back = nullptr; | ||
|
||
qmtx.futex_word = FutexWordType(Mutex::LockState::Free); | ||
|
||
// this is a special WAKE_OP, so we use syscall directly | ||
LIBC_NAMESPACE::syscall_impl<long>( | ||
FUTEX_SYSCALL_ID, &qmtx.futex_word.val, FUTEX_WAKE_OP, 1, 1, | ||
&first->futex_word.val, | ||
FUTEX_OP(FUTEX_OP_SET, WS_Signalled, FUTEX_OP_CMP_EQ, WS_Waiting)); | ||
} | ||
|
||
void CndVar::broadcast() { | ||
MutexLock ml(&qmtx); | ||
uint32_t dummy_futex_word; | ||
CndWaiter *waiter = waitq_front; | ||
waitq_front = waitq_back = nullptr; | ||
while (waiter != nullptr) { | ||
// FUTEX_WAKE_OP is used instead of just FUTEX_WAKE as it allows us to | ||
// atomically update the waiter status to WS_Signalled before waking | ||
// up the waiter. A dummy location is used for the other futex of | ||
// FUTEX_WAKE_OP. | ||
LIBC_NAMESPACE::syscall_impl<long>( | ||
FUTEX_SYSCALL_ID, &dummy_futex_word, FUTEX_WAKE_OP, 1, 1, | ||
&waiter->futex_word.val, | ||
FUTEX_OP(FUTEX_OP_SET, WS_Signalled, FUTEX_OP_CMP_EQ, WS_Waiting)); | ||
waiter = waiter->next; | ||
} | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
This file contains hidden or 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.