Skip to content
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

Compat: pthread.h shim for FreeRTOS #978

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
139 changes: 139 additions & 0 deletions include/compat/freertosthreadcompat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#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>

static inline uid_t getuid() {
return 0;
}


// PTHREAD ONCE
#define pthread_once libressl_pthread_once
struct pthread_once {
int is_initialized;
int init_executed;
};
#define pthread_once_t libressl_pthread_once_t
typedef struct pthread_once pthread_once_t;

#define PTHREAD_ONCE_INIT { 1, 0 }

static inline int
pthread_once(pthread_once_t *once, void (*cb) (void))
{
if (!once->is_initialized) {
return -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all return -1 should actually be an errno, such as EINVAL. This seems also incorrect in the windows pthread shim. I have not corrected that in my suggestions below.

}

if (!once->init_executed) {
once->init_executed = 1;
cb();
return 0;
}

return -1;

}

// PTHREAD Support
#define pthread_t libressl_pthread_t
typedef TaskHandle_t pthread_t;

static inline pthread_t
pthread_self(void)
{
return xTaskGetCurrentTaskHandle();
}

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_mutexattr
struct pthread_mutexattr {

};
#define pthread_mutexattr_t libressl_mutexattr_t
typedef struct pthread_mutexattr pthread_mutexattr_t;


#define PTHREAD_MUTEX_INITIALIZER { NULL }

static inline int
pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
{
xSemaphoreHandle x = xSemaphoreCreateMutex();
if (x) {
mutex->handle = x;
return 0;
}

return -1;
}

static inline int
pthread_mutex_lock(pthread_mutex_t *mutex)
{
xSemaphoreHandle x = mutex->handle;
if (x == NULL) {
x = xSemaphoreCreateMutex();
mutex->handle = x;
}

if ( xSemaphoreTake(x, portMAX_DELAY) == pdTRUE ) {
return 0;
}
else {
return -2;
}
}

static inline int
pthread_mutex_unlock(pthread_mutex_t *mutex)
{
xSemaphoreHandle x = mutex->handle;
if (x) {
if ( xSemaphoreGive(x) == pdTRUE ) {
return 0;
}
else {
return -1;
}
}

return 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
xSemaphoreHandle x = mutex->handle;
if (x) {
if ( xSemaphoreGive(x) == pdTRUE ) {
return 0;
}
else {
return -1;
}
}
return 0;
if (mutex->handle == NULL)
return 0;
if (xSemaphoreGive(mutex->handle) == pdTRUE)
return 0;
return -1;

Copy link
Author

@projectgoav projectgoav Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In mutex_lock we:

  • Return -1 if the mutex handle is NULL, here we return 0. Should we be consistent here and return -1 as well?
  • We compare != pdTRUE and final return is the happy path. Here, we compare == pdTRUE and the final return is a non-happy path. Should we be consistent and != pdTRUE with happy path the final return?

}

static inline int
pthread_mutex_destroy(pthread_mutex_t *mutex)
{
xSemaphoreHandle x = mutex->handle;
if (x) {
vQueueDelete(x);
}
mutex->handle = NULL;
return 0;
}

#endif

#endif
4 changes: 3 additions & 1 deletion include/compat/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#ifndef LIBCRYPTOCOMPAT_PTHREAD_H
#define LIBCRYPTOCOMPAT_PTHREAD_H

#ifdef _WIN32
#if defined(FREERTOS)
#include <freertosthreadcompat.h>
#elif defined(_WIN32)

#include <malloc.h>
#include <stdlib.h>
Expand Down
8 changes: 6 additions & 2 deletions include/compat/syslog.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
* syslog.h compatibility shim
*/

#ifndef _WIN32
#if defined(_WIN32)
#define NO_SYSLOG
#elif defined(FREERTOS)
#define NO_SYSLOG
#else
#include_next <syslog.h>
#endif

Expand All @@ -14,7 +18,7 @@

#include <stdarg.h>

#ifdef _WIN32
#ifdef NO_SYSLOG
Copy link
Contributor

Choose a reason for hiding this comment

The 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 */
Expand Down