-
Notifications
You must be signed in to change notification settings - Fork 288
Compat: pthread.h shim for FreeRTOS #978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a58a525
a5d536b
7c75b1e
c60781e
dd2cdd8
84aea1d
c47d69c
bb69805
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| #ifndef LIBCRYPTOCOMPAT_FREERTOSTHREADCOMPAT_H | ||
| #define LIBCRYPTOCOMPAT_FREERTOSTHREADCOMPAT_H | ||
|
|
||
| #ifdef FREERTOS | ||
|
|
||
| #include <sys/types.h> | ||
|
|
||
| #include <FreeRTOS/FreeRTOSConfig.h> | ||
| #include <FreeRTOS/FreeRTOS.h> | ||
| #include <FreeRTOS/semphr.h> | ||
| #include <FreeRTOS/task.h> | ||
|
|
||
|
|
||
| // 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think all return |
||
|
|
||
| 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) | ||
|
projectgoav marked this conversation as resolved.
|
||
| { | ||
| return xTaskGetCurrentTaskHandle(); | ||
| } | ||
|
|
||
| #define pthread_equal libressl_pthread_equal | ||
| static inline int | ||
| pthread_equal(pthread_t t1, pthread_t t2) | ||
|
projectgoav marked this conversation as resolved.
|
||
| { | ||
| 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) | ||
|
projectgoav marked this conversation as resolved.
|
||
| { | ||
| 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) | ||
|
projectgoav marked this conversation as resolved.
|
||
| { | ||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,9 @@ | |
| * syslog.h compatibility shim | ||
| */ | ||
|
|
||
| #ifndef _WIN32 | ||
| #if defined(_WIN32) || defined(FREERTOS) | ||
| #define NO_SYSLOG | ||
| #else | ||
| #include_next <syslog.h> | ||
| #endif | ||
|
|
||
|
|
@@ -14,7 +16,7 @@ | |
|
|
||
| #include <stdarg.h> | ||
|
|
||
| #ifdef _WIN32 | ||
| #ifdef NO_SYSLOG | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems fine if we fix autotools. |
||
| #define LOG_CONS LOG_INFO | ||
| #define LOG_INFO 6 /* informational */ | ||
| #define LOG_USER (1<<3) /* random user-level messages */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.