2018-07-12 16:29:32 +02:00
|
|
|
#include "Mutex.h"
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
2018-07-12 16:29:32 +02:00
|
|
|
|
|
|
|
uint8_t Mutex::count = 0;
|
|
|
|
|
|
|
|
Mutex::Mutex() :
|
|
|
|
mutexId(0) {
|
|
|
|
rtems_name mutexName = ('M' << 24) + ('T' << 16) + ('X' << 8) + count++;
|
|
|
|
rtems_status_code status = rtems_semaphore_create(mutexName, 1,
|
|
|
|
RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY | RTEMS_INHERIT_PRIORITY, 0,
|
|
|
|
&mutexId);
|
|
|
|
if (status != RTEMS_SUCCESSFUL) {
|
2021-01-03 13:58:18 +01:00
|
|
|
#if CPP_OSTREAM_ENABLED == 1
|
2020-11-17 19:25:57 +01:00
|
|
|
sif::error << "Mutex: creation with name, id " << mutexName << ", " << mutexId
|
2018-07-12 16:29:32 +02:00
|
|
|
<< " failed with " << status << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::~Mutex() {
|
|
|
|
rtems_status_code status = rtems_semaphore_delete(mutexId);
|
|
|
|
if (status != RTEMS_SUCCESSFUL) {
|
2021-01-03 13:58:18 +01:00
|
|
|
#if CPP_OSTREAM_ENABLED == 1
|
2020-11-17 19:25:57 +01:00
|
|
|
sif::error << "Mutex: deletion for id " << mutexId
|
2018-07-12 16:29:32 +02:00
|
|
|
<< " failed with " << status << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 12:04:59 +02:00
|
|
|
ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType =
|
|
|
|
TimeoutType::BLOCKING, uint32_t timeoutMs) {
|
2020-11-17 19:25:57 +01:00
|
|
|
rtems_status_code status = RTEMS_INVALID_ID;
|
2020-08-25 12:04:59 +02:00
|
|
|
if(timeoutMs == MutexIF::TimeoutType::BLOCKING) {
|
2020-11-17 19:25:57 +01:00
|
|
|
status = rtems_semaphore_obtain(mutexId,
|
2020-08-25 12:10:28 +02:00
|
|
|
RTEMS_WAIT, RTEMS_NO_TIMEOUT);
|
2020-06-09 13:26:27 +02:00
|
|
|
}
|
2020-08-25 12:04:59 +02:00
|
|
|
else if(timeoutMs == MutexIF::TimeoutType::POLLING) {
|
2020-06-09 13:26:27 +02:00
|
|
|
timeoutMs = RTEMS_NO_TIMEOUT;
|
2020-11-17 19:25:57 +01:00
|
|
|
status = rtems_semaphore_obtain(mutexId,
|
2020-08-25 12:10:28 +02:00
|
|
|
RTEMS_NO_WAIT, 0);
|
2020-06-09 13:26:27 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-11-17 19:25:57 +01:00
|
|
|
status = rtems_semaphore_obtain(mutexId,
|
2020-08-25 12:10:28 +02:00
|
|
|
RTEMS_WAIT, timeoutMs);
|
2020-06-09 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 18:28:26 +02:00
|
|
|
switch(status){
|
|
|
|
case RTEMS_SUCCESSFUL:
|
|
|
|
//semaphore obtained successfully
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
case RTEMS_UNSATISFIED:
|
|
|
|
//semaphore not available
|
|
|
|
return MUTEX_NOT_FOUND;
|
|
|
|
case RTEMS_TIMEOUT:
|
|
|
|
//timed out waiting for semaphore
|
|
|
|
return MUTEX_TIMEOUT;
|
|
|
|
case RTEMS_OBJECT_WAS_DELETED:
|
|
|
|
//semaphore deleted while waiting
|
|
|
|
return MUTEX_DESTROYED_WHILE_WAITING;
|
|
|
|
case RTEMS_INVALID_ID:
|
|
|
|
//invalid semaphore id
|
|
|
|
return MUTEX_INVALID_ID;
|
|
|
|
default:
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t Mutex::unlockMutex() {
|
|
|
|
rtems_status_code status = rtems_semaphore_release(mutexId);
|
2018-07-13 18:28:26 +02:00
|
|
|
switch(status){
|
|
|
|
case RTEMS_SUCCESSFUL:
|
|
|
|
//semaphore obtained successfully
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
case RTEMS_NOT_OWNER_OF_RESOURCE:
|
|
|
|
//semaphore not available
|
|
|
|
return CURR_THREAD_DOES_NOT_OWN_MUTEX;
|
|
|
|
case RTEMS_INVALID_ID:
|
|
|
|
//invalid semaphore id
|
|
|
|
return MUTEX_INVALID_ID;
|
|
|
|
default:
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|