- MacOS thread layer: replaced semaphore by mutex

pull/375/head
Michael Zillgith 4 years ago
parent d40b359292
commit 8aa988068c

@ -9,10 +9,10 @@
/* /*
* NOTE: MacOS needs own thread layer because it doesn't support unnamed semaphores! * NOTE: MacOS needs own thread layer because it doesn't support unnamed semaphores!
* NOTE: named semaphores were replaced by POSIX mutex
*/ */
#include <pthread.h> #include <pthread.h>
#include <semaphore.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
@ -32,56 +32,52 @@ typedef struct sSemaphore* mSemaphore;
struct sSemaphore struct sSemaphore
{ {
sem_t* sem; pthread_mutex_t mutex;
}; };
/*
* NOTE: initialValue is ignored because semaphore was replaced by mutex
*/
Semaphore Semaphore
Semaphore_create(int initialValue) Semaphore_create(int initialValue)
{ {
mSemaphore self = NULL; mSemaphore self = NULL;
char tmpname[] = {"/tmp/libiec61850.XXXXXX"}; self = (mSemaphore) GLOBAL_CALLOC(1, sizeof(struct sSemaphore));
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);
if (ret == -1) if (self) {
printf("ERROR: Failed to unlink semaphore %s\n", tmpname); pthread_mutex_init(&(self->mutex), NULL);
}
}
} }
return (Semaphore)self; return (Semaphore)self;
} }
/* Wait until semaphore value is more than zero. Then decrease the semaphore value. */ /* lock mutex */
void void
Semaphore_wait(Semaphore self) Semaphore_wait(Semaphore self)
{ {
mSemaphore mSelf = (mSemaphore) 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 void
Semaphore_post(Semaphore self) Semaphore_post(Semaphore self)
{ {
mSemaphore mSelf = (mSemaphore) 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 void
@ -90,10 +86,7 @@ Semaphore_destroy(Semaphore self)
if (self) { if (self) {
mSemaphore mSelf = (mSemaphore) self; mSemaphore mSelf = (mSemaphore) self;
int ret = sem_close(mSelf->sem); pthread_mutex_destroy(&(mSelf->mutex));
if (ret == -1)
printf("ERROR: Failed to close semaphore (errno = %i)\n", errno);
GLOBAL_FREEMEM(mSelf); GLOBAL_FREEMEM(mSelf);
} }

@ -423,6 +423,12 @@ readFunction(void* ctx, unsigned char* buf, size_t len)
return ret; return ret;
} }
static int
writeFunction(void* ctx, unsigned char* buf, size_t len)
{
return Socket_write((Socket)ctx, buf, (int)len);
}
static int static int
getMajorVersion(TLSConfigVersion version) getMajorVersion(TLSConfigVersion version)
{ {
@ -512,7 +518,7 @@ TLSSocket_create(Socket socket, TLSConfiguration configuration, bool storeClient
if (ret != 0) if (ret != 0)
DEBUG_PRINT("TLS", "mbedtls_ssl_setup returned %d\n", ret); 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); (mbedtls_ssl_recv_t*) readFunction, NULL);
while( (ret = mbedtls_ssl_handshake(&(self->ssl)) ) != 0 ) while( (ret = mbedtls_ssl_handshake(&(self->ssl)) ) != 0 )

Loading…
Cancel
Save