Semaphore.cpp

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #include <fesa-core/Utilities/Semaphore.h>
00004 
00005 #include <fesa-core/Utilities/StringUtilities.h>
00006 #include <fesa-core/Exception/FesaException.h>
00007 
00008 #include <iostream>
00009 #include <cerrno>
00010 #include <sys/stat.h>
00011 #include <fcntl.h>
00012 
00013 
00014 namespace fesa
00015 {
00016 
00017 Semaphore::Semaphore(const std::string& semaphoreName, int32_t value)
00018 {
00019     std::string errorNumber;
00020     semaphoreName_ = semaphoreName;
00021     int32_t org_mask = umask(0); // set the user mask to 0 in order to create the semaphore with the wanted rights
00022     semaphore_ = sem_open(semaphoreName_.c_str(), O_CREAT, 0666, value);
00023     umask(org_mask);   // restore the user mask
00024     if (semaphore_ == SEM_FAILED)
00025     {
00026         errorNumber = StringUtilities::toString(errno);
00027         throw FesaException(__FILE__, __LINE__, FesaErrorOpeningSemaphore.c_str(), errorNumber.c_str());
00028     }
00029 }
00030 
00031 Semaphore::~Semaphore()
00032 {
00033     std::string errorNumber;
00034     if (sem_close(semaphore_) == 0)
00035         sem_unlink(semaphoreName_.c_str());
00036 
00037 }
00038 
00039 void Semaphore::wait()
00040 {
00041     std::string errorNumber;
00042 
00043     if (sem_wait(semaphore_) == -1)
00044     {
00045         errorNumber = StringUtilities::toString(errno);
00046         throw FesaException(__FILE__, __LINE__, FesaErrorWaitingForSemaphore.c_str(), errorNumber.c_str());
00047     }
00048 }
00049 
00050 int32_t Semaphore::tryWait()
00051 {
00052     std::string errorNumber;
00053     if (sem_trywait(semaphore_) == -1)
00054     {
00055         if (errno != EAGAIN)
00056         {
00057             errorNumber = StringUtilities::toString(errno);
00058             throw FesaException(__FILE__, __LINE__, FesaErrorTryWaitingForSemaphore.c_str(), errorNumber.c_str());
00059         }
00060         return -1;
00061     }
00062     return 0;
00063 }
00064 
00065 int32_t Semaphore::timedWait(const struct timespec* abs_timeout)
00066 {
00067     std::string errorNumber;
00068     int32_t err;
00069     //      std::cout << "Sem value " << this->getValue() << std::endl;
00070     err = sem_timedwait(semaphore_, abs_timeout);
00071     if (err != 0)
00072     {
00073         throw FesaException(__FILE__, __LINE__, FesaErrorTryWaitingForSemaphore.c_str(), errorNumber.c_str());
00074         return -1;
00075     }
00076     return 0;
00077 }
00078 
00079 void Semaphore::post()
00080 {
00081     std::string errorNumber;
00082     if (sem_post(semaphore_) == -1)
00083     {
00084         errorNumber = StringUtilities::toString(errno);
00085         throw FesaException(__FILE__, __LINE__, FesaErrorPostingSemaphore.c_str(), errorNumber.c_str());
00086     }
00087 }
00088 
00089 int32_t Semaphore::getValue()
00090 {
00091     std::string errorNumber;
00092     int32_t value;
00093     if (sem_getvalue(semaphore_, &value) == 0)
00094     {
00095         return value;
00096     }
00097     else
00098     {
00099         errorNumber = StringUtilities::toString(errno);
00100         throw FesaException(__FILE__, __LINE__, FesaErrorGettingValueOfSemaphore.c_str(), errorNumber.c_str());
00101     }
00102     return -1;
00103 }
00104 
00105 } // fesa

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1