add FreeRTOS support for PIThread PIMutex PIConditionVariable
This commit is contained in:
@@ -36,8 +36,10 @@
|
||||
|
||||
#include "pimutex.h"
|
||||
#include "piincludes_p.h"
|
||||
#ifdef WINDOWS
|
||||
#if defined(WINDOWS)
|
||||
# include <synchapi.h>
|
||||
#elif defined(FREERTOS)
|
||||
# include <semphr.h>
|
||||
#else
|
||||
# include <pthread.h>
|
||||
#endif
|
||||
@@ -45,8 +47,10 @@
|
||||
|
||||
|
||||
PRIVATE_DEFINITION_START(PIMutex)
|
||||
#ifdef WINDOWS
|
||||
#if defined(WINDOWS)
|
||||
CRITICAL_SECTION
|
||||
#elif defined(FREERTOS)
|
||||
SemaphoreHandle_t
|
||||
#else
|
||||
pthread_mutex_t
|
||||
#endif
|
||||
@@ -65,8 +69,10 @@ PIMutex::~PIMutex() {
|
||||
|
||||
|
||||
void PIMutex::lock() {
|
||||
#ifdef WINDOWS
|
||||
#if defined(WINDOWS)
|
||||
EnterCriticalSection(&(PRIVATE->mutex));
|
||||
#elif defined(FREERTOS)
|
||||
xSemaphoreTake(PRIVATE->mutex, portMAX_DELAY);
|
||||
#else
|
||||
pthread_mutex_lock(&(PRIVATE->mutex));
|
||||
#endif
|
||||
@@ -74,8 +80,10 @@ void PIMutex::lock() {
|
||||
|
||||
|
||||
void PIMutex::unlock() {
|
||||
#ifdef WINDOWS
|
||||
#if defined(WINDOWS)
|
||||
LeaveCriticalSection(&(PRIVATE->mutex));
|
||||
#elif defined(FREERTOS)
|
||||
xSemaphoreGive(PRIVATE->mutex);
|
||||
#else
|
||||
pthread_mutex_unlock(&(PRIVATE->mutex));
|
||||
#endif
|
||||
@@ -84,8 +92,10 @@ void PIMutex::unlock() {
|
||||
|
||||
bool PIMutex::tryLock() {
|
||||
bool ret =
|
||||
#ifdef WINDOWS
|
||||
#if defined(WINDOWS)
|
||||
(TryEnterCriticalSection(&(PRIVATE->mutex)) != 0);
|
||||
#elif defined(FREERTOS)
|
||||
xSemaphoreTake(PRIVATE->mutex, 0);
|
||||
#else
|
||||
(pthread_mutex_trylock(&(PRIVATE->mutex)) == 0);
|
||||
#endif
|
||||
@@ -94,13 +104,19 @@ bool PIMutex::tryLock() {
|
||||
|
||||
|
||||
void * PIMutex::handle() {
|
||||
#ifdef FREERTOS
|
||||
return PRIVATE->mutex;
|
||||
#else
|
||||
return (void*)&(PRIVATE->mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void PIMutex::init() {
|
||||
#ifdef WINDOWS
|
||||
#if defined(WINDOWS)
|
||||
InitializeCriticalSection(&(PRIVATE->mutex));
|
||||
#elif defined(FREERTOS)
|
||||
PRIVATE->mutex = xSemaphoreCreateMutex();
|
||||
#else
|
||||
pthread_mutexattr_t attr;
|
||||
memset(&attr, 0, sizeof(attr));
|
||||
@@ -114,8 +130,10 @@ void PIMutex::init() {
|
||||
|
||||
|
||||
void PIMutex::destroy() {
|
||||
#ifdef WINDOWS
|
||||
#if defined(WINDOWS)
|
||||
DeleteCriticalSection(&(PRIVATE->mutex));
|
||||
#elif defined(FREERTOS)
|
||||
vSemaphoreDelete(PRIVATE->mutex);
|
||||
#else
|
||||
pthread_mutex_destroy(&(PRIVATE->mutex));
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user