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
126 changes: 126 additions & 0 deletions include/compat/freertosthreadcompat.h
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))
{
projectgoav marked this conversation as resolved.
Show resolved Hide resolved
if (once->lock == NULL)
once->lock = xSemaphoreCreateMutex();
if (once->lock == NULL)
return -1;
if (xSemaphoreTake(once->lock, portMAX_DELAY) != pdTRUE)
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();
}

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.
Show resolved Hide 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.
Show resolved Hide 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.
Show resolved Hide 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.
Show resolved Hide 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
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
6 changes: 4 additions & 2 deletions include/compat/syslog.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* syslog.h compatibility shim
*/

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

Expand All @@ -14,7 +16,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
4 changes: 4 additions & 0 deletions include/compat/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h>
Expand Down