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

Commit

Permalink
Thread lib: #44 Missing binary semaphore in the common library
Browse files Browse the repository at this point in the history
Semaphore implemented and simple cleanup is made
  • Loading branch information
forGGe committed May 27, 2016
1 parent c87a1d7 commit 85312f8
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/thread/common/export/ecl/thread/common/mutex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace common
class mutex
{
public:
constexpr mutex();
constexpr mutex() { }

void lock();
void unlock();
Expand Down
20 changes: 20 additions & 0 deletions lib/thread/common/export/ecl/thread/common/semaphore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ class semaphore
std::atomic_int m_counter;
};

// Empty binary semaphore, acting like spinlock of some kind that can have
// only two values - 0 or 1
class binary_semaphore
{
public:
constexpr binary_semaphore()
:m_flag{0} { }

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

binary_semaphore(const binary_semaphore&) = delete;
binary_semaphore& operator=(const binary_semaphore&) = delete;

private:
std::atomic_bool m_flag;
};


}

}
Expand Down
4 changes: 0 additions & 4 deletions lib/thread/common/mutex.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include <ecl/thread/common/mutex.hpp>

constexpr ecl::common::mutex::mutex()
{
}

void ecl::common::mutex::lock()
{
}
Expand Down
21 changes: 21 additions & 0 deletions lib/thread/common/semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,24 @@ ecl::err ecl::common::semaphore::try_wait()

return rc;
}

//------------------------------------------------------------------------------

void ecl::common::binary_semaphore::signal()
{
m_flag = true;
}

void ecl::common::binary_semaphore::wait()
{
while (!m_flag) { }
}

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

return err::again;
}
1 change: 1 addition & 0 deletions lib/thread/default/export/ecl/thread/semaphore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace ecl
{
using semaphore = common::semaphore;
using binary_semaphore = common::binary_semaphore;
}

#endif //LIB_THREAD_DEFAULT_SEMAPHORE_
6 changes: 0 additions & 6 deletions lib/thread/posix/export/ecl/thread/future.hpp

This file was deleted.

Empty file removed lib/thread/posix/future.cpp
Empty file.

0 comments on commit 85312f8

Please sign in to comment.