22 |
|
|
23 |
#include <iostream> |
#include <iostream> |
24 |
|
|
25 |
|
#include <errno.h> |
26 |
|
|
27 |
#include "Mutex.h" |
#include "Mutex.h" |
28 |
|
|
29 |
Mutex::Mutex() { |
Mutex::Mutex() { |
30 |
|
// the following function call only works on UNIX98 compatible systems |
31 |
if (pthread_mutexattr_settype(&__posix_mutexattr, PTHREAD_MUTEX_ERRORCHECK)) { |
if (pthread_mutexattr_settype(&__posix_mutexattr, PTHREAD_MUTEX_ERRORCHECK)) { |
32 |
std::cout << "Mutex Constructor: Fatal error - unable to pthread_mutexattr_settype(PTHREAD_MUTEX_ERRORCHECK)\n" << std::flush; |
std::cout << "Mutex Constructor: Fatal error - unable to pthread_mutexattr_settype(PTHREAD_MUTEX_ERRORCHECK)\n" << std::flush; |
33 |
exit(-1); |
exit(-1); |
43 |
pthread_mutex_lock(&__posix_mutex); |
pthread_mutex_lock(&__posix_mutex); |
44 |
} |
} |
45 |
|
|
46 |
|
bool Mutex::Trylock() { |
47 |
|
if (pthread_mutex_trylock(&__posix_mutex) == EBUSY) |
48 |
|
return false; |
49 |
|
return true; |
50 |
|
} |
51 |
|
|
52 |
void Mutex::Unlock() { |
void Mutex::Unlock() { |
53 |
pthread_mutex_unlock(&__posix_mutex); |
pthread_mutex_unlock(&__posix_mutex); |
54 |
} |
} |