12 #ifndef ECL_THREADS_MUTEX_POS_HPP_ 13 #define ECL_THREADS_MUTEX_POS_HPP_ 19 #include <ecl/config/ecl.hpp> 20 #if defined(ECL_IS_POSIX) 31 #include <ecl/config/macros.hpp> 32 #include <ecl/exceptions/macros.hpp> 33 #include <ecl/exceptions/standard_exception.hpp> 34 #include <ecl/time/duration.hpp> 47 typedef pthread_mutex_t RawMutex;
146 unsigned int locks() {
return number_locks; }
154 RawMutex& rawType() {
return mutex; }
158 unsigned int number_locks;
168 #if defined(ECL_HAS_EXCEPTIONS) 184 inline StandardException
ECL_LOCAL throwMutexAttrException(
const char* loc,
int error_result) {
185 switch (error_result) {
186 case ( EINVAL ) :
return StandardException(loc,
InvalidInputError,
"The specified mutex attribute was invalid.");
187 case ( ENOMEM ) :
return StandardException(loc,
MemoryError,
"There is insufficient memory for initialisation of the mutex attribute.");
190 std::ostringstream ostream;
191 ostream <<
"Unknown posix error " << error_result <<
": " << strerror(error_result) <<
".";
192 return StandardException(loc,
UnknownError, ostream.str());
204 inline StandardException
ECL_LOCAL throwMutexInitException(
const char* loc,
int error_result) {
205 switch (error_result) {
206 case ( EINVAL ) :
return StandardException(loc,
InvalidInputError,
"The specified mutex was invalid.");
207 case ( EBUSY ) :
return StandardException(loc,
InvalidInputError,
"The mutex object has already been initialised and not yet destroyed.");
208 case ( EAGAIN ) :
return StandardException(loc,
MemoryError,
"The mutex object has already been initialised and not yet destroyed.");
209 case ( ENOMEM ) :
return StandardException(loc,
MemoryError,
"There is insufficient memory for initialisation of the mutex.");
210 case ( EPERM ) :
return StandardException(loc,
PermissionsError,
"The user does not have the privilege to perform the operation.");
213 std::ostringstream ostream;
214 ostream <<
"Unknown posix error " << error_result <<
": " << strerror(error_result) <<
".";
215 return StandardException(loc,
UnknownError, ostream.str());
227 inline StandardException
ECL_LOCAL throwMutexDestroyException(
const char* loc,
int error_result) {
228 switch (error_result) {
229 case ( EINVAL ) :
return StandardException(loc,
DestructorError,
"The specified mutex is invalid (for some reason or other).");
230 case ( EBUSY ) :
return StandardException(loc,
DestructorError,
"Attempted to destroy the mutex while it was locked.");
231 default :
return StandardException(loc,
UnknownError,
"Unknown error.");
242 inline StandardException
ECL_LOCAL throwMutexTimedLockException(
const char* loc,
int error_result) {
243 switch (error_result) {
244 case ( EDEADLK ):
return StandardException(loc,
UsageError,
"DEADLOCK! The current thread already owns the mutex.");
246 case ( EINVAL ) :
return StandardException(loc,
UsageError,
"The mutex is not initialised or it is priority protected and the calling thread's priority is higher than the mutex' current priority ceiling.");
247 case ( EAGAIN ) :
return StandardException(loc,
OutOfRangeError,
"The mutex could not be acquired because the maximum number of recursive locks for the mutex has been exceeded.");
248 default :
return StandardException(loc,
UnknownError,
"Unknown error.");
259 inline StandardException
ECL_LOCAL throwMutexLockException(
const char* loc,
int error_result) {
260 switch (error_result) {
261 case ( EDEADLK ):
return StandardException(loc,
UsageError,
"DEADLOCK! The mutex has already been locked by this thread, it now has to wait on itself.");
263 case ( EBUSY ) :
return StandardException(loc,
ConfigurationError,
"The try lock failed because it was already locked (normal operation really, not really an error).");
264 case ( EINVAL ) :
return StandardException(loc,
InvalidInputError,
"The mutex does not refer to an initialised mutex.");
265 case ( EAGAIN ) :
return StandardException(loc,
OutOfRangeError,
"The mutex could not be acquired because the maximum number of recursive locks for the mutex has been exceeded.");
266 default :
return StandardException(loc,
PosixError,
"Unknown error.");
277 inline StandardException
ECL_LOCAL throwMutexUnLockException(
const char* loc,
int error_result) {
278 switch (error_result) {
279 case ( EINVAL ) :
return StandardException(loc,
InvalidInputError,
"The mutex does not refer to an initialised mutex.");
280 case ( EAGAIN ) :
return StandardException(loc,
OutOfRangeError,
"The mutex could not be acquired because the maximum number of recursive locks for the mutex has been exceeded.");
281 case ( EPERM ) :
return StandardException(loc,
PermissionsError,
"The user does not have the privilege to perform the operation.");
282 default :
return StandardException(loc,
UnknownError,
"Unknown error.");
#define ecl_assert_throw_decl(exception)
Assure throw exception declaration.