- HAL: added thread/semaphore support for MacOS

pull/244/head
Michael Zillgith 5 years ago
parent 7e1dd825a6
commit 93921b1c81

@ -65,7 +65,16 @@ endif(WITH_WPCAP)
set (libhal_bsd_SRCS set (libhal_bsd_SRCS
${CMAKE_CURRENT_LIST_DIR}/socket/bsd/socket_bsd.c ${CMAKE_CURRENT_LIST_DIR}/socket/bsd/socket_bsd.c
${CMAKE_CURRENT_LIST_DIR}/ethernet/bsd/ethernet_bsd.c ${CMAKE_CURRENT_LIST_DIR}/ethernet/bsd/ethernet_bsd.c
${CMAKE_CURRENT_LIST_DIR}/thread/bsd/thread_bsd.c ${CMAKE_CURRENT_LIST_DIR}/thread/bsd/thread_macos.c
${CMAKE_CURRENT_LIST_DIR}/filesystem/linux/file_provider_linux.c
${CMAKE_CURRENT_LIST_DIR}/time/unix/time.c
${CMAKE_CURRENT_LIST_DIR}/memory/lib_memory.c
)
set (libhal_macos_SRCS
${CMAKE_CURRENT_LIST_DIR}/socket/bsd/socket_bsd.c
${CMAKE_CURRENT_LIST_DIR}/ethernet/bsd/ethernet_bsd.c
${CMAKE_CURRENT_LIST_DIR}/thread/macos/thread_macos.c
${CMAKE_CURRENT_LIST_DIR}/filesystem/linux/file_provider_linux.c ${CMAKE_CURRENT_LIST_DIR}/filesystem/linux/file_provider_linux.c
${CMAKE_CURRENT_LIST_DIR}/time/unix/time.c ${CMAKE_CURRENT_LIST_DIR}/time/unix/time.c
${CMAKE_CURRENT_LIST_DIR}/memory/lib_memory.c ${CMAKE_CURRENT_LIST_DIR}/memory/lib_memory.c
@ -90,7 +99,7 @@ ENDIF()
ELSEIF(UNIX) ELSEIF(UNIX)
IF(APPLE) IF(APPLE)
set (libhal_SRCS set (libhal_SRCS
${libhal_bsd_SRCS} ${libhal_macos_SRCS}
) )
ELSEIF(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") ELSEIF(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
set (libhal_SRCS set (libhal_SRCS

@ -34,6 +34,7 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include "lib_memory.h" #include "lib_memory.h"
#include "hal_ethernet.h" #include "hal_ethernet.h"
@ -390,7 +391,7 @@ Ethernet_receivePacket(EthernetSocket self, uint8_t* buffer, int bufferSize)
/* Check if the target buffer is big enough to hold the received ethernet frame. */ /* Check if the target buffer is big enough to hold the received ethernet frame. */
if ((unsigned int) bufferSize >= header->bh_caplen) if ((unsigned int) bufferSize >= header->bh_caplen)
{ {
/* Copy the frame to the target buffer. /* Copy the frame to the target buffer. */
memcpy(buffer, self->bpfPositon + header->bh_hdrlen, header->bh_caplen); memcpy(buffer, self->bpfPositon + header->bh_hdrlen, header->bh_caplen);
/* Move the read pointer to the next ethernet frame header WORD ALIGNED (Took me a while to find that out). */ /* Move the read pointer to the next ethernet frame header WORD ALIGNED (Took me a while to find that out). */

@ -0,0 +1,124 @@
/**
* thread_macos.c
*
* Copyright 2013-2019 MZ Automation GmbH
*
* This file is part of Platform Abstraction Layer (libpal)
* for libiec61850, libmms, and lib60870.
*/
/*
* NOTE: MacOS needs own thread layer because it doesn't support unnamed semaphores!
*/
#include <pthread.h>
#include <semaphore.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include "hal_thread.h"
#include "lib_memory.h"
struct sThread {
ThreadExecutionFunction function;
void* parameter;
pthread_t pthread;
int state;
bool autodestroy;
};
Semaphore
Semaphore_create(int initialValue)
{
char tmpname[] = {"/tmp/libiec61850.XXXXXX"};
mktemp(tmpname);
Semaphore self = (Semaphore) sem_open(tmpname, O_CREAT, 0666, initialValue);
if (self == SEM_FAILED) {
printf("ERROR: Failed to create semaphore (errno = %i)\n", errno);
}
int ret = sem_unlink(tmpname);
if (ret == -1)
printf("ERROR: Failed to unlink semaphore %s\n", tmpname);
return self;
}
/* Wait until semaphore value is more than zero. Then decrease the semaphore value. */
void
Semaphore_wait(Semaphore self)
{
sem_wait((sem_t*) self);
}
void
Semaphore_post(Semaphore self)
{
sem_post((sem_t*) self);
}
void
Semaphore_destroy(Semaphore self)
{
sem_close(self);
}
Thread
Thread_create(ThreadExecutionFunction function, void* parameter, bool autodestroy)
{
Thread thread = (Thread) GLOBAL_MALLOC(sizeof(struct sThread));
if (thread != NULL) {
thread->parameter = parameter;
thread->function = function;
thread->state = 0;
thread->autodestroy = autodestroy;
}
return thread;
}
static void*
destroyAutomaticThread(void* parameter)
{
Thread thread = (Thread) parameter;
thread->function(thread->parameter);
GLOBAL_FREEMEM(thread);
pthread_exit(NULL);
}
void
Thread_start(Thread thread)
{
if (thread->autodestroy == true) {
pthread_create(&thread->pthread, NULL, destroyAutomaticThread, thread);
pthread_detach(thread->pthread);
}
else
pthread_create(&thread->pthread, NULL, thread->function, thread->parameter);
thread->state = 1;
}
void
Thread_destroy(Thread thread)
{
if (thread->state == 1) {
pthread_join(thread->pthread, NULL);
}
GLOBAL_FREEMEM(thread);
}
void
Thread_sleep(int millies)
{
usleep(millies * 1000);
}
Loading…
Cancel
Save