1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
11 #include <anna/core/mt/Semaphore.hpp>
12 #include <anna/core/RuntimeException.hpp>
13 #include <anna/core/functions.hpp>
14 #include <anna/config/defines.hpp>
18 Semaphore::Semaphore(unsigned int count) :
21 sem_init(&a_id, 0, count);
25 Semaphore::~Semaphore() {
31 void Semaphore::wait()
32 throw(RuntimeException) {
35 anna_signal_shield(errorCode, sem_wait(&a_id));
38 throw RuntimeException(std::string("Error doing wait"), errno, ANNA_FILE_LOCATION);
43 bool Semaphore::tryWait()
44 throw(RuntimeException) {
49 if((errorCode = sem_trywait(&a_id)) != 0) {
50 if(errorCode != EBUSY)
51 throw RuntimeException(std::string("Error trying wait"), errno, ANNA_FILE_LOCATION);
60 void Semaphore::signal()
61 throw(RuntimeException) {
63 int errorCode = sem_post(&a_id);
66 throw RuntimeException(std::string("Error doing post"), errno, ANNA_FILE_LOCATION);
71 std::string Semaphore::asString() const
73 std::string msg("anna::Semaphone { Id: ");
74 msg += functions::asHexString(anna_ptrnumber_cast(&a_id));
78 sem_getvalue(&(const_cast <Semaphore*>(this)->a_id), &value);
79 msg += functions::asString(value);