2020-10-13 08:22:43 +00:00
|
|
|
#include "valuemutex.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
bool init_mutex(ValueMutex* valuemutex, void* value, size_t size) {
|
|
|
|
// mutex without name,
|
|
|
|
// no attributes (unfortunatly robust mutex is not supported by FreeRTOS),
|
|
|
|
// with dynamic memory allocation
|
|
|
|
const osMutexAttr_t value_mutext_attr = {
|
|
|
|
.name = NULL, .attr_bits = 0, .cb_mem = NULL, .cb_size = 0U};
|
|
|
|
|
|
|
|
valuemutex->mutex = osMutexNew(&value_mutext_attr);
|
|
|
|
if(valuemutex->mutex == NULL) return false;
|
|
|
|
|
|
|
|
valuemutex->value = value;
|
|
|
|
valuemutex->size = size;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-26 09:26:15 +00:00
|
|
|
bool delete_mutex(ValueMutex* valuemutex) {
|
|
|
|
if(osMutexAcquire(valuemutex->mutex, osWaitForever) == osOK) {
|
|
|
|
return osMutexDelete(valuemutex->mutex) == osOK;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 08:22:43 +00:00
|
|
|
void* acquire_mutex(ValueMutex* valuemutex, uint32_t timeout) {
|
|
|
|
if(osMutexAcquire(valuemutex->mutex, timeout) == osOK) {
|
|
|
|
return valuemutex->value;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 12:25:32 +00:00
|
|
|
bool release_mutex(ValueMutex* valuemutex, const void* value) {
|
2020-10-13 08:22:43 +00:00
|
|
|
if(value != valuemutex->value) return false;
|
|
|
|
|
|
|
|
if(osMutexRelease(valuemutex->mutex) != osOK) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool read_mutex(ValueMutex* valuemutex, void* data, size_t len, uint32_t timeout) {
|
|
|
|
void* value = acquire_mutex(valuemutex, timeout);
|
|
|
|
if(value == NULL || len > valuemutex->size) return false;
|
|
|
|
memcpy(data, value, len > 0 ? len : valuemutex->size);
|
|
|
|
if(!release_mutex(valuemutex, value)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_mutex(ValueMutex* valuemutex, void* data, size_t len, uint32_t timeout) {
|
|
|
|
void* value = acquire_mutex(valuemutex, timeout);
|
|
|
|
if(value == NULL || len > valuemutex->size) return false;
|
|
|
|
memcpy(value, data, len > 0 ? len : valuemutex->size);
|
|
|
|
if(!release_mutex(valuemutex, value)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|