diff options
author | Jack Lloyd <[email protected]> | 2017-10-12 11:43:28 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-10-12 11:43:28 -0400 |
commit | 39d9e995f49c92d6737d5e641ca306be174a271f (patch) | |
tree | add121cd9834b7dd56c4b059159491d15741bd75 /src/lib/utils | |
parent | f60b816469ebc3b46aa64dcba0dcdcdb92e87301 (diff) | |
parent | 2e24a69581709366084d40348232cca1a5758438 (diff) |
Merge GH #1245 Restructure Barrier/Semaphore to avoid signed overflow warnings
Diffstat (limited to 'src/lib/utils')
-rw-r--r-- | src/lib/utils/barrier.cpp | 5 | ||||
-rw-r--r-- | src/lib/utils/semaphore.cpp | 15 |
2 files changed, 9 insertions, 11 deletions
diff --git a/src/lib/utils/barrier.cpp b/src/lib/utils/barrier.cpp index 3c721d905..ed3878d18 100644 --- a/src/lib/utils/barrier.cpp +++ b/src/lib/utils/barrier.cpp @@ -20,9 +20,10 @@ void Barrier::wait(size_t delta) void Barrier::sync() { std::unique_lock<mutex_type> lock(m_mutex); - --m_value; - if(m_value > 0) + + if(m_value > 1) { + --m_value; const size_t current_syncs = m_syncs; m_cond.wait(lock, [this, ¤t_syncs] { return m_syncs != current_syncs; }); } diff --git a/src/lib/utils/semaphore.cpp b/src/lib/utils/semaphore.cpp index 62c31d3e3..e990ded41 100644 --- a/src/lib/utils/semaphore.cpp +++ b/src/lib/utils/semaphore.cpp @@ -19,9 +19,7 @@ void Semaphore::release(size_t n) { lock_guard_type<mutex_type> lock(m_mutex); - ++m_value; - - if(m_value <= 0) + if(m_value++ < 0) { ++m_wakeups; m_cond.notify_one(); @@ -32,12 +30,11 @@ void Semaphore::release(size_t n) void Semaphore::acquire() { std::unique_lock<mutex_type> lock(m_mutex); - --m_value; - if(m_value < 0) - { - m_cond.wait(lock, [this] { return m_wakeups > 0; }); - --m_wakeups; - } + if(m_value-- <= 0) + { + m_cond.wait(lock, [this] { return m_wakeups > 0; }); + --m_wakeups; + } } } |