13 #include "../../include/ecl/ipc/semaphore_pos.hpp" 15 #ifdef ECL_HAS_POSIX_SEMAPHORES 34 using std::ostringstream;
40 Semaphore::Semaphore() throw(StandardException) {
46 name(string(
"/"+string_id)),
58 static const int open_flags = O_CREAT;
82 static const int permissions = S_IRWXU|S_IRWXG|S_IRWXO;
87 static const int initial_value_unlocked = 1;
92 semaphore = sem_open(name.c_str(),open_flags,permissions,initial_value_unlocked);
96 Semaphore::~Semaphore()
101 sem_unlink(name.c_str());
104 void Semaphore::lock()
109 int Semaphore::count()
113 sem_getvalue(semaphore,&semaphore_count);
114 return semaphore_count;
117 void Semaphore::unlock()
121 if ( count() == 1 ) {
128 bool Semaphore::trylock()
131 if ( sem_trywait( semaphore ) == 0 ) {
142 #if defined(_POSIX_TIMEOUTS) && (_POSIX_TIMEOUTS - 200112L) >= 0L 146 if ( epoch_time(ctime).flag() !=
NoError ) {
return false; }
148 ctime.tv_sec += timeout.sec();
150 tnsec = ctime.tv_nsec + timeout.nsec();
151 if(tnsec >= 999999999 ) { ctime.tv_sec += 1; }
153 ctime.tv_nsec = tnsec;
158 int result = sem_timedwait( semaphore, &ctime );
160 if ( errno == ETIMEDOUT ) {
180 #ifdef ECL_HAS_EXCEPTIONS 185 int error_result = errno;
186 switch ( error_result ) {
188 return StandardException(LOC,
PermissionsError,
"The semaphore exists, but permission to open has been denied.");
191 return StandardException(LOC,
PermissionsError,
"The semaphore already exists, so your request to explicitly create was denied.");
194 return StandardException(LOC,
ConfigurationError,
"The semaphore requested doesn't already exist (you specifically requested it to just open, not create).");
197 return StandardException(LOC,
MemoryError,
"Insufficient memory.");
200 return StandardException(LOC,
InvalidArgError,
"Name was empty (i.e. '/'). Can also be the maximum number of semaphores has already been exceeded.");
203 return StandardException(LOC,
OutOfResourcesError,
"This process has already exceeded the number of files/pseudofiles it is permitted to open.");
206 return StandardException(LOC,
OutOfResourcesError,
"This system has already exceeded the number of files/pseudofiles it is permitted to open.");
208 case ( ENAMETOOLONG ) : {
209 return StandardException(LOC,
InvalidArgError,
"The semaphore name was too long.");
213 ostringstream ostream;
214 ostream <<
"Unknown posix error " << error_result <<
": " << strerror(error_result) <<
".";
215 return StandardException(loc,
UnknownError, ostream.str());
221 int error_result = errno;
222 switch ( error_result ) {
224 return StandardException(LOC,
InterruptedError,
"Waiting for the semaphore lock was interrupted by a system signal.");
227 return StandardException(LOC,
InvalidArgError,
"The semaphore was invalid or the timeout structure specified was invalid.");
230 return StandardException(LOC,
BlockingError,
"The waiting operation could not be performed without blocking???");
234 ostringstream ostream;
235 ostream <<
"Posix error " << error_result <<
": " << strerror(error_result) <<
".";
236 return StandardException(loc,
UnknownError, ostream.str());
Embedded control libraries.
#define ecl_assert_throw(expression, exception)
#define ecl_assert_throw_decl(exception)
#define ecl_debug_throw_decl(exception)
#define ecl_debug_throw(exception)