From 8aa988068c42bb88687fbfb3b8c87700b1384cf6 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Thu, 10 Mar 2022 20:29:36 +0000 Subject: [PATCH] - MacOS thread layer: replaced semaphore by mutex --- hal/thread/macos/thread_macos.c | 53 ++++++++++++++------------------- hal/tls/mbedtls/tls_mbedtls.c | 8 ++++- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/hal/thread/macos/thread_macos.c b/hal/thread/macos/thread_macos.c index 2ba58b06..8acbaa84 100644 --- a/hal/thread/macos/thread_macos.c +++ b/hal/thread/macos/thread_macos.c @@ -9,10 +9,10 @@ /* * NOTE: MacOS needs own thread layer because it doesn't support unnamed semaphores! + * NOTE: named semaphores were replaced by POSIX mutex */ #include -#include #include #include #include @@ -32,56 +32,52 @@ typedef struct sSemaphore* mSemaphore; struct sSemaphore { - sem_t* sem; + pthread_mutex_t mutex; }; +/* + * NOTE: initialValue is ignored because semaphore was replaced by mutex + */ Semaphore Semaphore_create(int initialValue) { mSemaphore self = NULL; - char tmpname[] = {"/tmp/libiec61850.XXXXXX"}; - char* res = mktemp(tmpname); - - if (res) { - self = (mSemaphore) GLOBAL_CALLOC(1, sizeof(struct sSemaphore)); - - if (self) { - self->sem = sem_open(tmpname, O_CREAT, 0666, initialValue); - - if (self->sem == SEM_FAILED) { - printf("ERROR: Failed to create semaphore (errno = %i)\n", errno); - - GLOBAL_FREEMEM(self); - self = NULL; - } - else { - int ret = sem_unlink(tmpname); + self = (mSemaphore) GLOBAL_CALLOC(1, sizeof(struct sSemaphore)); - if (ret == -1) - printf("ERROR: Failed to unlink semaphore %s\n", tmpname); - } - } + if (self) { + pthread_mutex_init(&(self->mutex), NULL); } return (Semaphore)self; } -/* Wait until semaphore value is more than zero. Then decrease the semaphore value. */ +/* lock mutex */ void Semaphore_wait(Semaphore self) { mSemaphore mSelf = (mSemaphore) self; - sem_wait(mSelf->sem); + int retVal = pthread_mutex_lock(&(mSelf->mutex)); + + if (retVal) { + printf("FATAL ERROR: pthread_mutex_lock failed (err=%i)\n", retVal); + exit(-1); + } } +/* unlock mutex */ void Semaphore_post(Semaphore self) { mSemaphore mSelf = (mSemaphore) self; - sem_post(mSelf->sem); + int retVal = pthread_mutex_unlock(&(mSelf->mutex)); + + if (retVal) { + printf("FATAL ERROR: pthread_mutex_unlock failed (err=%i)\n", retVal); + exit(-1); + } } void @@ -90,10 +86,7 @@ Semaphore_destroy(Semaphore self) if (self) { mSemaphore mSelf = (mSemaphore) self; - int ret = sem_close(mSelf->sem); - - if (ret == -1) - printf("ERROR: Failed to close semaphore (errno = %i)\n", errno); + pthread_mutex_destroy(&(mSelf->mutex)); GLOBAL_FREEMEM(mSelf); } diff --git a/hal/tls/mbedtls/tls_mbedtls.c b/hal/tls/mbedtls/tls_mbedtls.c index 4e3af470..8acd533e 100644 --- a/hal/tls/mbedtls/tls_mbedtls.c +++ b/hal/tls/mbedtls/tls_mbedtls.c @@ -423,6 +423,12 @@ readFunction(void* ctx, unsigned char* buf, size_t len) return ret; } +static int +writeFunction(void* ctx, unsigned char* buf, size_t len) +{ + return Socket_write((Socket)ctx, buf, (int)len); +} + static int getMajorVersion(TLSConfigVersion version) { @@ -512,7 +518,7 @@ TLSSocket_create(Socket socket, TLSConfiguration configuration, bool storeClient if (ret != 0) DEBUG_PRINT("TLS", "mbedtls_ssl_setup returned %d\n", ret); - mbedtls_ssl_set_bio(&(self->ssl), socket, (mbedtls_ssl_send_t*) Socket_write, + mbedtls_ssl_set_bio(&(self->ssl), socket, (mbedtls_ssl_send_t*) writeFunction, (mbedtls_ssl_recv_t*) readFunction, NULL); while( (ret = mbedtls_ssl_handshake(&(self->ssl)) ) != 0 )