Skip to content
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

Fibtex improvements #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions include/ftl/atomic_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,34 @@ class AtomicCounter {
return success;
}

/**
* Use the AtomicCounter in binary mode - sets the Counter to 1 and does not signal tasks
* @param memoryOrder The memory order to use for the Set
* @return If the Set succeded
*/
bool Set(std::memory_order const memoryOrder = std::memory_order_seq_cst) {
return m_value.exchange(1, memoryOrder) == 0;
}

/**
* Use the AtomicCounter in binary mode - resets the Counter to 0 and signals tasks waiting for 0
* @param memoryOrder The memory order to use for the Reset
* @return If the Reset succeded
*/
bool Reset(std::memory_order const memoryOrder = std::memory_order_seq_cst) {
m_lock.fetch_add(1U, std::memory_order_seq_cst);

bool const success = m_value.exchange(0, memoryOrder) == 1;
if (!success) {
m_lock.fetch_sub(1U, std::memory_order_seq_cst);
return false;
}
CheckWaitingFibers(0);

m_lock.fetch_sub(1U, std::memory_order_seq_cst);
return true;
}

private:
/**
* Add a fiber to the list of waiting fibers
Expand Down
14 changes: 8 additions & 6 deletions include/ftl/fibtex.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Fibtex {
// ReSharper disable once CppInconsistentNaming
void lock(bool const pinToThread = false) {
while (true) {
if (m_atomicCounter.CompareExchange(0, 1, std::memory_order_acq_rel)) {
if (m_atomicCounter.Set(std::memory_order_acq_rel)) {
return;
}

Expand All @@ -91,10 +91,12 @@ class Fibtex {
// Spin for a bit
for (unsigned i = 0; i < iterations; ++i) {
// Spin
if (m_atomicCounter.CompareExchange(0, 1, std::memory_order_acq_rel)) {
if (m_atomicCounter.Set(std::memory_order_acq_rel)) {
return;
}
FTL_PAUSE();
while(m_atomicCounter.Load(std::memory_order_relaxed)){
FTL_PAUSE();
}
}

// Spinning didn't grab the lock, we're in for the long haul. Yield.
Expand All @@ -114,7 +116,7 @@ class Fibtex {

while (true) {
// Spin
if (m_atomicCounter.CompareExchange(0, 1, std::memory_order_acq_rel)) {
if (m_atomicCounter.Set(std::memory_order_acq_rel)) {
return;
}
FTL_PAUSE();
Expand All @@ -128,15 +130,15 @@ class Fibtex {
*/
// ReSharper disable once CppInconsistentNaming
bool try_lock() {
return m_atomicCounter.CompareExchange(0, 1, std::memory_order_acq_rel);
return m_atomicCounter.Set(std::memory_order_acq_rel);
}

/**
* Unlock the mutex.
*/
// ReSharper disable once CppInconsistentNaming
void unlock() {
if (!m_atomicCounter.CompareExchange(1, 0, std::memory_order_acq_rel)) {
if (!m_atomicCounter.Reset(std::memory_order_acq_rel)) {
FTL_ASSERT("Error: Mutex was unlocked by another fiber or was double unlocked.", false);
}
}
Expand Down