diff --git a/Makefile b/Makefile index eeaa5cd..2aec4a6 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ LIB_SOURCE_DIRS += src/hal/filesystem/linux LIB_SOURCE_DIRS += src/hal/time/unix else ifeq ($(HAL_IMPL), BSD) LIB_SOURCE_DIRS += src/hal/socket/bsd -LIB_SOURCE_DIRS += src/hal/thread/linux +LIB_SOURCE_DIRS += src/hal/thread/bsd LIB_SOURCE_DIRS += src/hal/ethernet/bsd LIB_SOURCE_DIRS += src/hal/filesystem/linux LIB_SOURCE_DIRS += src/hal/time/unix diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c126643..49938c3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -203,7 +203,7 @@ set (lib_windows_SRCS set (lib_bsd_SRCS ./hal/socket/bsd/socket_bsd.c ./hal/ethernet/bsd/ethernet_bsd.c -./hal/thread/linux/thread_linux.c +./hal/thread/bsd/thread_bsd.c ./hal/filesystem/linux/file_provider_linux.c ./hal/time/unix/time.c ) diff --git a/src/hal/thread/bsd/thread_bsd.c b/src/hal/thread/bsd/thread_bsd.c new file mode 100644 index 0000000..df16fc9 --- /dev/null +++ b/src/hal/thread/bsd/thread_bsd.c @@ -0,0 +1,122 @@ +/** + * thread_bsd.c + * + * Copyright 2016 Michael Zillgith + * + * This file is part of libIEC61850. + * + * libIEC61850 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libIEC61850 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with libIEC61850. If not, see . + * + * See COPYING file for the complete license text. + */ + +#include +#include +#include +#include "hal_thread.h" + +#include "libiec61850_platform_includes.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 = sem_open(tmpname, O_CREAT, 0666, initialValue); + + 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); +}