- PAL: added option to prefix thread and semaphore functions to avoid naming conflicts with other libraries (LIB61850-399)

v1.5_develop_399
Michael Zillgith 2 years ago
parent 097c055f96
commit 077e4f51c3

@ -25,6 +25,12 @@ check_library_exists(rt clock_gettime "time.h" CONFIG_SYSTEM_HAS_CLOCK_GETTIME)
include (TestBigEndian) include (TestBigEndian)
test_big_endian(PLATFORM_IS_BIGENDIAN) test_big_endian(PLATFORM_IS_BIGENDIAN)
option(CONFIG_HAL_PREFIX_ENABLED "Enable function prefixes for HAL" OFF)
if (CONFIG_HAL_PREFIX_ENABLED)
ADD_DEFINITIONS("-DCONFIG_LIBGCOS_PREFIX=1")
endif (CONFIG_HAL_PREFIX_ENABLED)
set(CONFIG_MMS_MAXIMUM_PDU_SIZE "65000" CACHE STRING "Configure the maximum size of an MMS PDU (default 65000)" ) set(CONFIG_MMS_MAXIMUM_PDU_SIZE "65000" CACHE STRING "Configure the maximum size of an MMS PDU (default 65000)" )
set(CONFIG_MAXIMUM_TCP_CLIENT_CONNECTIONS 5 CACHE STRING "Configure the maximum number of clients allowed to connect to the server") set(CONFIG_MAXIMUM_TCP_CLIENT_CONNECTIONS 5 CACHE STRING "Configure the maximum number of clients allowed to connect to the server")

@ -30,6 +30,16 @@
/* Maximum MMS PDU SIZE - default is 65000 */ /* Maximum MMS PDU SIZE - default is 65000 */
#define CONFIG_MMS_MAXIMUM_PDU_SIZE 65000 #define CONFIG_MMS_MAXIMUM_PDU_SIZE 65000
// /* Prefix hal/common library names with libgcos_ to avoid naming conflics with other libraries */
// #ifndef CONFIG_LIBGCOS_PREFIX
// #define CONFIG_LIBGCOS_PREFIX 0
// #endif
// /* Define legacy hal/common library names to avoid using the libgcos_ prefix in application code */
// #ifndef CONFIG_LIBGCOS_LEGACY_NAMES
// #define CONFIG_LIBGCOS_LEGACY_NAMES 1
// #endif
/* /*
* Enable single threaded mode * Enable single threaded mode
* *

@ -3,14 +3,14 @@
* *
* Multi-threading abstraction layer * Multi-threading abstraction layer
* *
* Copyright 2013-2021 Michael Zillgith * Copyright 2013-2023 Michael Zillgith
* *
* This file is part of Platform Abstraction Layer (libpal) * This file is part of Platform Abstraction Layer (libpal)
* for libiec61850, libmms, and lib60870. * for libiec61850, libmms, and lib60870.
*/ */
#ifndef THREAD_HAL_H_ #ifndef LIBGCOS_THREAD_HAL_H_
#define THREAD_HAL_H_ #define LIBGCOS_THREAD_HAL_H_
#include "hal_base.h" #include "hal_base.h"
@ -18,6 +18,51 @@
extern "C" { extern "C" {
#endif #endif
#ifndef CONFIG_LIBGCOS_LEGACY_NAMES
#define CONFIG_LIBGCOS_LEGACY_NAMES 1
#endif
#ifndef CONFIG_LIBGCOS_PREFIX
#define CONFIG_LIBGCOS_PREFIX 0
#endif
#if (CONFIG_LIBGCOS_PREFIX == 1)
#define THREAD_CREATE libgcos_Thread_create
#define THREAD_START libgcos_Thread_start
#define THREAD_DESTROY libgcos_Thread_destroy
#define THREAD_SLEEP libgcos_Thread_sleep
#define SEMAPHORE_CREATE libgcos_Semaphore_create
#define SEMAPHORE_WAIT libgcos_Semaphore_wait
#define SEMAPHORE_POST libgcos_Semaphore_post
#define SEMAPHORE_DESTROY libgcos_Semaphore_destroy
#if (CONFIG_LIBGCOS_LEGACY_NAMES == 1)
#define Thread_create THREAD_CREATE
#define Thread_start THREAD_START
#define Thread_destroy THREAD_DESTROY
#define Thread_sleep THREAD_SLEEP
#define Semaphore_create SEMAPHORE_CREATE
#define Semaphore_wait SEMAPHORE_WAIT
#define Semaphore_post SEMAPHORE_POST
#define Semaphore_destroy SEMAPHORE_DESTROY
#endif /* (CONFIG_LIBGCOS_LEGACY_NAMES == 1) */
#else
#define THREAD_CREATE Thread_create
#define THREAD_START Thread_start
#define THREAD_DESTROY Thread_destroy
#define THREAD_SLEEP Thread_sleep
#define SEMAPHORE_CREATE Semaphore_create
#define SEMAPHORE_WAIT Semaphore_wait
#define SEMAPHORE_POST Semaphore_post
#define SEMAPHORE_DESTROY Semaphore_destroy
#endif /* (CONFIG_LIBGCOS_PREFIX == 1) */
/** /**
* \file hal_thread.h * \file hal_thread.h
* \brief Abstraction layer for threading and synchronization * \brief Abstraction layer for threading and synchronization
@ -53,7 +98,7 @@ typedef void* (*ThreadExecutionFunction) (void*);
* \return the newly created Thread instance * \return the newly created Thread instance
*/ */
PAL_API Thread PAL_API Thread
Thread_create(ThreadExecutionFunction function, void* parameter, bool autodestroy); THREAD_CREATE(ThreadExecutionFunction function, void* parameter, bool autodestroy);
/** /**
* \brief Start a Thread. * \brief Start a Thread.
@ -64,7 +109,7 @@ Thread_create(ThreadExecutionFunction function, void* parameter, bool autodestro
* \param thread the Thread instance to start * \param thread the Thread instance to start
*/ */
PAL_API void PAL_API void
Thread_start(Thread thread); THREAD_START(Thread thread);
/** /**
* \brief Destroy a Thread and free all related resources. * \brief Destroy a Thread and free all related resources.
@ -72,26 +117,26 @@ Thread_start(Thread thread);
* \param thread the Thread instance to destroy * \param thread the Thread instance to destroy
*/ */
PAL_API void PAL_API void
Thread_destroy(Thread thread); THREAD_DESTROY(Thread thread);
/** /**
* \brief Suspend execution of the Thread for the specified number of milliseconds * \brief Suspend execution of the Thread for the specified number of milliseconds
*/ */
PAL_API void PAL_API void
Thread_sleep(int millies); THREAD_SLEEP(int millies);
PAL_API Semaphore PAL_API Semaphore
Semaphore_create(int initialValue); SEMAPHORE_CREATE(int initialValue);
/* Wait until semaphore value is greater than zero. Then decrease the semaphore value. */ /* Wait until semaphore value is greater than zero. Then decrease the semaphore value. */
PAL_API void PAL_API void
Semaphore_wait(Semaphore self); SEMAPHORE_WAIT(Semaphore self);
PAL_API void PAL_API void
Semaphore_post(Semaphore self); SEMAPHORE_POST(Semaphore self);
PAL_API void PAL_API void
Semaphore_destroy(Semaphore self); SEMAPHORE_DESTROY(Semaphore self);
/*! @} */ /*! @} */
@ -101,5 +146,4 @@ Semaphore_destroy(Semaphore self);
} }
#endif #endif
#endif /* LIBGCOS_THREAD_HAL_H_ */
#endif /* THREAD_HAL_H_ */

@ -1,7 +1,7 @@
/** /**
* thread_bsd.c * thread_bsd.c
* *
* Copyright 2013-2021 Michael Zillgith * Copyright 2013-2023 Michael Zillgith
* *
* This file is part of Platform Abstraction Layer (libpal) * This file is part of Platform Abstraction Layer (libpal)
* for libiec61850, libmms, and lib60870. * for libiec61850, libmms, and lib60870.
@ -22,7 +22,7 @@ struct sThread {
}; };
Semaphore Semaphore
Semaphore_create(int initialValue) SEMAPHORE_CREATE(int initialValue)
{ {
Semaphore self = GLOBAL_MALLOC(sizeof(sem_t)); Semaphore self = GLOBAL_MALLOC(sizeof(sem_t));
@ -33,26 +33,26 @@ Semaphore_create(int initialValue)
/* Wait until semaphore value is more than zero. Then decrease the semaphore value. */ /* Wait until semaphore value is more than zero. Then decrease the semaphore value. */
void void
Semaphore_wait(Semaphore self) SEMAPHORE_WAIT(Semaphore self)
{ {
sem_wait((sem_t*) self); sem_wait((sem_t*) self);
} }
void void
Semaphore_post(Semaphore self) SEMAPHORE_POST(Semaphore self)
{ {
sem_post((sem_t*) self); sem_post((sem_t*) self);
} }
void void
Semaphore_destroy(Semaphore self) SEMAPHORE_DESTROY(Semaphore self)
{ {
sem_destroy((sem_t*) self); sem_destroy((sem_t*) self);
GLOBAL_FREEMEM(self); GLOBAL_FREEMEM(self);
} }
Thread Thread
Thread_create(ThreadExecutionFunction function, void* parameter, bool autodestroy) THREAD_CREATE(ThreadExecutionFunction function, void* parameter, bool autodestroy)
{ {
Thread thread = (Thread) GLOBAL_MALLOC(sizeof(struct sThread)); Thread thread = (Thread) GLOBAL_MALLOC(sizeof(struct sThread));
@ -79,7 +79,7 @@ destroyAutomaticThread(void* parameter)
} }
void void
Thread_start(Thread thread) THREAD_START(Thread thread)
{ {
if (thread->autodestroy == true) { if (thread->autodestroy == true) {
pthread_create(&thread->pthread, NULL, destroyAutomaticThread, thread); pthread_create(&thread->pthread, NULL, destroyAutomaticThread, thread);
@ -92,7 +92,7 @@ Thread_start(Thread thread)
} }
void void
Thread_destroy(Thread thread) THREAD_DESTROY(Thread thread)
{ {
if (thread->state == 1) { if (thread->state == 1) {
pthread_join(thread->pthread, NULL); pthread_join(thread->pthread, NULL);
@ -102,7 +102,7 @@ Thread_destroy(Thread thread)
} }
void void
Thread_sleep(int millies) THREAD_SLEEP(int millies)
{ {
usleep(millies * 1000); usleep(millies * 1000);
} }

@ -1,7 +1,7 @@
/* /*
* thread_linux.c * thread_linux.c
* *
* Copyright 2013-2021 Michael Zillgith * Copyright 2013-2023 Michael Zillgith
* *
* This file is part of Platform Abstraction Layer (libpal) * This file is part of Platform Abstraction Layer (libpal)
* for libiec61850, libmms, and lib60870. * for libiec61850, libmms, and lib60870.
@ -22,7 +22,7 @@ struct sThread {
}; };
Semaphore Semaphore
Semaphore_create(int initialValue) SEMAPHORE_CREATE(int initialValue)
{ {
Semaphore self = GLOBAL_MALLOC(sizeof(sem_t)); Semaphore self = GLOBAL_MALLOC(sizeof(sem_t));
@ -33,26 +33,26 @@ Semaphore_create(int initialValue)
/* Wait until semaphore value is more than zero. Then decrease the semaphore value. */ /* Wait until semaphore value is more than zero. Then decrease the semaphore value. */
void void
Semaphore_wait(Semaphore self) SEMAPHORE_WAIT(Semaphore self)
{ {
sem_wait((sem_t*) self); sem_wait((sem_t*) self);
} }
void void
Semaphore_post(Semaphore self) SEMAPHORE_POST(Semaphore self)
{ {
sem_post((sem_t*) self); sem_post((sem_t*) self);
} }
void void
Semaphore_destroy(Semaphore self) SEMAPHORE_DESTROY(Semaphore self)
{ {
sem_destroy((sem_t*) self); sem_destroy((sem_t*) self);
GLOBAL_FREEMEM(self); GLOBAL_FREEMEM(self);
} }
Thread Thread
Thread_create(ThreadExecutionFunction function, void* parameter, bool autodestroy) THREAD_CREATE(ThreadExecutionFunction function, void* parameter, bool autodestroy)
{ {
Thread thread = (Thread) GLOBAL_MALLOC(sizeof(struct sThread)); Thread thread = (Thread) GLOBAL_MALLOC(sizeof(struct sThread));
@ -79,7 +79,7 @@ destroyAutomaticThread(void* parameter)
} }
void void
Thread_start(Thread thread) THREAD_START(Thread thread)
{ {
if (thread->autodestroy == true) { if (thread->autodestroy == true) {
pthread_create(&thread->pthread, NULL, destroyAutomaticThread, thread); pthread_create(&thread->pthread, NULL, destroyAutomaticThread, thread);
@ -92,7 +92,7 @@ Thread_start(Thread thread)
} }
void void
Thread_destroy(Thread thread) THREAD_DESTROY(Thread thread)
{ {
if (thread->state == 1) { if (thread->state == 1) {
pthread_join(thread->pthread, NULL); pthread_join(thread->pthread, NULL);
@ -102,7 +102,7 @@ Thread_destroy(Thread thread)
} }
void void
Thread_sleep(int millies) THREAD_SLEEP(int millies)
{ {
usleep(millies * 1000); usleep(millies * 1000);
} }

@ -39,7 +39,7 @@ struct sSemaphore
* NOTE: initialValue is ignored because semaphore was replaced by 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;
@ -54,7 +54,7 @@ Semaphore_create(int initialValue)
/* lock mutex */ /* lock mutex */
void void
Semaphore_wait(Semaphore self) SEMAPHORE_WAIT(Semaphore self)
{ {
mSemaphore mSelf = (mSemaphore) self; mSemaphore mSelf = (mSemaphore) self;
@ -68,7 +68,7 @@ Semaphore_wait(Semaphore self)
/* unlock mutex */ /* unlock mutex */
void void
Semaphore_post(Semaphore self) SEMAPHORE_POST(Semaphore self)
{ {
mSemaphore mSelf = (mSemaphore) self; mSemaphore mSelf = (mSemaphore) self;
@ -81,7 +81,7 @@ Semaphore_post(Semaphore self)
} }
void void
Semaphore_destroy(Semaphore self) SEMAPHORE_DESTROY(Semaphore self)
{ {
if (self) { if (self) {
mSemaphore mSelf = (mSemaphore) self; mSemaphore mSelf = (mSemaphore) self;
@ -93,7 +93,7 @@ Semaphore_destroy(Semaphore self)
} }
Thread Thread
Thread_create(ThreadExecutionFunction function, void* parameter, bool autodestroy) THREAD_CREATE(ThreadExecutionFunction function, void* parameter, bool autodestroy)
{ {
Thread thread = (Thread) GLOBAL_MALLOC(sizeof(struct sThread)); Thread thread = (Thread) GLOBAL_MALLOC(sizeof(struct sThread));
@ -120,7 +120,7 @@ destroyAutomaticThread(void* parameter)
} }
void void
Thread_start(Thread thread) THREAD_START(Thread thread)
{ {
if (thread->autodestroy == true) { if (thread->autodestroy == true) {
pthread_create(&thread->pthread, NULL, destroyAutomaticThread, thread); pthread_create(&thread->pthread, NULL, destroyAutomaticThread, thread);
@ -133,7 +133,7 @@ Thread_start(Thread thread)
} }
void void
Thread_destroy(Thread thread) THREAD_DESTROY(Thread thread)
{ {
if (thread->state == 1) { if (thread->state == 1) {
pthread_join(thread->pthread, NULL); pthread_join(thread->pthread, NULL);
@ -143,7 +143,7 @@ Thread_destroy(Thread thread)
} }
void void
Thread_sleep(int millies) THREAD_SLEEP(int millies)
{ {
usleep(millies * 1000); usleep(millies * 1000);
} }

@ -42,7 +42,7 @@ threadRunner(LPVOID parameter)
} }
Thread Thread
Thread_create(ThreadExecutionFunction function, void* parameter, bool autodestroy) THREAD_CREATE(ThreadExecutionFunction function, void* parameter, bool autodestroy)
{ {
DWORD threadId; DWORD threadId;
Thread thread = (Thread) GLOBAL_MALLOC(sizeof(struct sThread)); Thread thread = (Thread) GLOBAL_MALLOC(sizeof(struct sThread));
@ -61,14 +61,14 @@ Thread_create(ThreadExecutionFunction function, void* parameter, bool autodestro
} }
void void
Thread_start(Thread thread) THREAD_START(Thread thread)
{ {
thread->state = 1; thread->state = 1;
ResumeThread(thread->handle); ResumeThread(thread->handle);
} }
void void
Thread_destroy(Thread thread) THREAD_DESTROY(Thread thread)
{ {
if (thread->state == 1) if (thread->state == 1)
WaitForSingleObject(thread->handle, INFINITE); WaitForSingleObject(thread->handle, INFINITE);
@ -79,7 +79,7 @@ Thread_destroy(Thread thread)
} }
void void
Thread_sleep(int millies) THREAD_SLEEP(int millies)
{ {
Sleep(millies); Sleep(millies);
} }

Loading…
Cancel
Save