diff --git a/include/compat/freertosthreadcompat.h b/include/compat/freertosthreadcompat.h new file mode 100644 index 0000000000..30b3e1668d --- /dev/null +++ b/include/compat/freertosthreadcompat.h @@ -0,0 +1,126 @@ +#ifndef LIBCRYPTOCOMPAT_FREERTOSTHREADCOMPAT_H +#define LIBCRYPTOCOMPAT_FREERTOSTHREADCOMPAT_H + +#ifdef FREERTOS + +#include + +#include +#include +#include +#include + + +// PTHREAD ONCE +#define pthread_once libressl_pthread_once +struct pthread_once { + xSemaphoreHandle lock; + int init_executed; +}; +#define pthread_once_t libressl_pthread_once_t +typedef struct pthread_once pthread_once_t; + +#define PTHREAD_ONCE_INIT { .lock = NULL, .init_executed = 0 } + +static inline int +pthread_once(pthread_once_t *once, void (*cb) (void)) +{ + if (once->lock == NULL) + once->lock = xSemaphoreCreateMutex(); + if (once->lock == NULL) + return -1; + if (xSemaphoreTake(once->lock, portMAX_DELAY) != pdTRUE) + return -1; + + if (!once->init_executed) { + once->init_executed = 1; + cb(); + } + + xSemaphoreGive(once->lock); + return 0; +} + +// PTHREAD Support +#define pthread_t libressl_pthread_t +typedef TaskHandle_t pthread_t; + +#define pthread_self libressl_pthread_self +static inline pthread_t +pthread_self(void) +{ + return xTaskGetCurrentTaskHandle(); +} + +#define pthread_equal libressl_pthread_equal +static inline int +pthread_equal(pthread_t t1, pthread_t t2) +{ + return t1 == t2; +} + + +// PTHREAD MUTEX Support +#define pthread_mutex libressl_pthread_mutex +struct pthread_mutex { + xSemaphoreHandle handle; +}; +#define pthread_mutex_t libressl_pthread_mutex_t +typedef struct pthread_mutex pthread_mutex_t; + +#define pthread_mutexattr libressl_pthread_mutexattr +struct pthread_mutexattr { + +}; +#define pthread_mutexattr_t libressl_pthread_mutexattr_t +typedef struct pthread_mutexattr pthread_mutexattr_t; + + +#define PTHREAD_MUTEX_INITIALIZER { NULL } + +#define pthread_mutex_init libressl_pthread_mutex_init +static inline int +pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) +{ + if ((mutex->handle = xSemaphoreCreateMutex()) == NULL) + return -1; + return 0; +} + +#define pthread_mutex_lock libressl_pthread_mutex_lock +static inline int +pthread_mutex_lock(pthread_mutex_t *mutex) +{ + if (mutex->handle == NULL) + mutex->handle = xSemaphoreCreateMutex(); + if (mutex->handle == NULL) + return -1; + if (xSemaphoreTake(mutex->handle, portMAX_DELAY) != pdTRUE) + return -1; + return 0; +} + +#define pthread_mutex_unlock libressl_pthread_mutex_unlock +static inline int +pthread_mutex_unlock(pthread_mutex_t *mutex) +{ + if (mutex->handle == NULL) + return 0; + if (xSemaphoreGive(mutex->handle) == pdTRUE) + return 0; + return -1; +} + +#define pthread_mutex_destroy libressl_pthread_mutex_destroy +static inline int +pthread_mutex_destroy(pthread_mutex_t *mutex) +{ + if (mutex->handle != NULL) + vQueueDelete(mutex->handle); + mutex->handle = NULL; + return 0; +} + +#endif + +#endif diff --git a/include/compat/pthread.h b/include/compat/pthread.h index ed1b9dc51a..0663737eff 100644 --- a/include/compat/pthread.h +++ b/include/compat/pthread.h @@ -6,7 +6,9 @@ #ifndef LIBCRYPTOCOMPAT_PTHREAD_H #define LIBCRYPTOCOMPAT_PTHREAD_H -#ifdef _WIN32 +#if defined(FREERTOS) +#include +#elif defined(_WIN32) #include #include diff --git a/include/compat/syslog.h b/include/compat/syslog.h index c7a2608bdc..683a60602c 100644 --- a/include/compat/syslog.h +++ b/include/compat/syslog.h @@ -3,7 +3,9 @@ * syslog.h compatibility shim */ -#ifndef _WIN32 +#if defined(_WIN32) || defined(FREERTOS) +#define NO_SYSLOG +#else #include_next #endif @@ -14,7 +16,7 @@ #include -#ifdef _WIN32 +#ifdef NO_SYSLOG #define LOG_CONS LOG_INFO #define LOG_INFO 6 /* informational */ #define LOG_USER (1<<3) /* random user-level messages */ diff --git a/include/compat/unistd.h b/include/compat/unistd.h index 63c07fc3dc..6198615b98 100644 --- a/include/compat/unistd.h +++ b/include/compat/unistd.h @@ -17,6 +17,10 @@ ssize_t pread(int d, void *buf, size_t nbytes, off_t offset); ssize_t pwrite(int d, const void *buf, size_t nbytes, off_t offset); #endif +#elif defined(FREERTOS) +static inline uid_t getuid(void) { + return 0; +} #else #include