Skip to content
This repository has been archived by the owner on Dec 6, 2018. It is now read-only.

[#135] changed semaphore try_wait() API #147

Merged
merged 1 commit into from
Oct 5, 2016
Merged
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
4 changes: 2 additions & 2 deletions lib/thread/common/export/ecl/thread/common/semaphore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class semaphore

void signal();
void wait();
ecl::err try_wait();
bool try_wait();

semaphore(const semaphore&) = delete;
semaphore& operator=(const semaphore&) = delete;
Expand All @@ -39,7 +39,7 @@ class binary_semaphore

void signal();
void wait();
ecl::err try_wait();
bool try_wait();

binary_semaphore(const binary_semaphore&) = delete;
binary_semaphore& operator=(const binary_semaphore&) = delete;
Expand Down
12 changes: 6 additions & 6 deletions lib/thread/common/semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ void ecl::common::semaphore::wait()
}
}

ecl::err ecl::common::semaphore::try_wait()
bool ecl::common::semaphore::try_wait()
{
err rc = err::ok;
bool rc = true;

if (m_counter.fetch_sub(1) <= 0) {
m_counter++;
rc = err::again;
rc = false;
}

return rc;
Expand All @@ -40,11 +40,11 @@ void ecl::common::binary_semaphore::wait()
while (!m_flag);
}

ecl::err ecl::common::binary_semaphore::try_wait()
bool ecl::common::binary_semaphore::try_wait()
{
if (m_flag.exchange(false)) {
return err::ok;
return true;
}

return err::again;
return false;
}
12 changes: 6 additions & 6 deletions lib/thread/freertos/export/ecl/thread/semaphore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class binary_semaphore

//!
//! \brief Tries to wait on semaphore without lock.
//! \retval err::again Someone already waits a semaphore.
//! \retval err::ok Semaphore counter decremented.
//! \retval false Someone already waits a semaphore.
//! \retval true Semaphore counter decremented.
//!
ecl::err try_wait();
bool try_wait();

binary_semaphore(const binary_semaphore&) = delete;
binary_semaphore& operator=(const binary_semaphore&) = delete;
Expand Down Expand Up @@ -72,10 +72,10 @@ class semaphore

//!
//! \brief Tries to wait on semaphore without lock.
//! \retval err::again Wait will result in lock.
//! \retval err::ok Semaphore counter decremented.
//! \retval false Wait will result in lock.
//! \retval true Semaphore counter decremented.
//!
ecl::err try_wait();
bool try_wait();

semaphore(const semaphore&) = delete;
semaphore& operator=(const semaphore&) = delete;
Expand Down
10 changes: 5 additions & 5 deletions lib/thread/freertos/semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ void ecl::semaphore::wait()
}
}

ecl::err ecl::semaphore::try_wait()
bool ecl::semaphore::try_wait()
{
if (m_cnt.fetch_sub(1) > 0) {
return err::ok; // Got semaphore.
return true; // Got semaphore.
}

// Bring counter back
++m_cnt;

return err::again;
return false;
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -74,8 +74,8 @@ void ecl::binary_semaphore::wait()
ecl_assert(rc == pdTRUE);
}

ecl::err ecl::binary_semaphore::try_wait()
bool ecl::binary_semaphore::try_wait()
{
auto rc = xSemaphoreTake(m_semaphore, 0);
return rc == pdTRUE ? ecl::err::ok : ecl::err::again;
return rc == pdTRUE ? true : false;
}
4 changes: 2 additions & 2 deletions lib/thread/posix/export/ecl/thread/semaphore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class semaphore

void signal();
void wait();
ecl::err try_wait();
bool try_wait();

semaphore(const semaphore&) = delete;
semaphore& operator=(const semaphore&) = delete;
Expand All @@ -37,7 +37,7 @@ class binary_semaphore

void signal();
void wait();
ecl::err try_wait();
bool try_wait();

binary_semaphore(const binary_semaphore&) = delete;
binary_semaphore& operator=(const binary_semaphore&) = delete;
Expand Down
10 changes: 5 additions & 5 deletions lib/thread/posix/semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ void ecl::semaphore::signal()
m_cond.notify_one();
}

ecl::err ecl::semaphore::try_wait()
bool ecl::semaphore::try_wait()
{
std::unique_lock <std::mutex> lock(m_mutex);

if (m_cnt) {
m_cnt--;
return ecl::err::ok;
return true;
}

return ecl::err::again;
return false;
}

void ecl::semaphore::wait()
Expand All @@ -51,11 +51,11 @@ void ecl::binary_semaphore::signal()
m_cond.notify_one();
}

ecl::err ecl::binary_semaphore::try_wait()
bool ecl::binary_semaphore::try_wait()
{
std::unique_lock <std::mutex> lock(m_mutex);

auto rc = m_flag ? ecl::err::ok : ecl::err::again;
auto rc = m_flag ? true : false;
m_flag = false;
return rc;
}
Expand Down
26 changes: 9 additions & 17 deletions lib/thread/tests/semaphore_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@

// TODO: remove ugly sleep-programming

// Error code helper
// TODO: move it to 'utils' headers and protect with check of
// current test state (enabled or disabled)
static SimpleString StringFrom(ecl::err err)
{
return SimpleString{ecl::err_to_str(err)};
}

// Helpers
template< class Container, class Func >
static void test_for_each(Container &cont, Func fn)
Expand Down Expand Up @@ -61,23 +53,23 @@ TEST(semaphore, try_wait)
sem.signal();

auto rc = sem.try_wait();
CHECK_EQUAL(ecl::err::ok, rc);
CHECK_EQUAL(true, rc);

rc = sem.try_wait();
CHECK_EQUAL(ecl::err::again, rc);
CHECK_EQUAL(false, rc);

rc = sem.try_wait();
CHECK_EQUAL(ecl::err::again, rc);
CHECK_EQUAL(false, rc);

// Signal same semaphore again
// to make sure counter is in valid state
sem.signal();

rc = sem.try_wait();
CHECK_EQUAL(ecl::err::ok, rc);
CHECK_EQUAL(true, rc);

rc = sem.try_wait();
CHECK_EQUAL(ecl::err::again, rc);
CHECK_EQUAL(false, rc);
}

TEST(semaphore, one_semaphore_few_threads)
Expand All @@ -96,8 +88,8 @@ TEST(semaphore, one_semaphore_few_threads)
auto single_thread = [&counter, &entry_semaphore, &exit_semaphore]() {
std::cout << "entry wait!" << std::endl;

ecl::err ret;
while ((ret = entry_semaphore.try_wait()) == ecl::err::again) {
bool ret;
while ((ret = entry_semaphore.try_wait()) == false) {
std::this_thread::yield();
}

Expand Down Expand Up @@ -125,8 +117,8 @@ TEST(semaphore, one_semaphore_few_threads)
entry_semaphore.signal();
signalled++;

ecl::err ret;
while ((ret = exit_semaphore.try_wait()) == ecl::err::again) {
bool ret;
while ((ret = exit_semaphore.try_wait()) == false) {
std::this_thread::yield();
}

Expand Down