Skip to content

Commit

Permalink
Posix-port: switch from pthreads to C11 threading
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed May 17, 2021
1 parent 0f6afc0 commit b28283e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 43 deletions.
1 change: 1 addition & 0 deletions examples/simlin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ INCLUDE= -I . -I ../../inc -I ../../flsemu
CPPFLAGS += $(INCLUDE)

CFLAGS += -std=gnu11 -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -ffunction-sections
#CFLAGS += -std=c11 -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -ffunction-sections
LDFLAGS= -Wl,-Map=$(xcp_sim_TARGET).map -Wl,--cref -Wl,-gc-sections -lpthread -lrt -lncurses
ifeq ($(PROFILE),1)
CFLAGS += -pg -fprofile-arcs -ftest-coverage --coverage
Expand Down
13 changes: 7 additions & 6 deletions examples/simlin/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include <pthread.h>
#include <threads.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -21,7 +21,7 @@ void * TlTask(void * param);

void * AppTask(void * param);

extern pthread_t XcpHw_ThreadID[4];
extern thrd_t XcpHw_ThreadID[4];

#define XCP_THREAD (0)
#define UI_THREAD (1)
Expand All @@ -43,6 +43,7 @@ Xcp_OptionsType Xcp_Options = {0};
int main(int argc, char **argv)
{
int opt;
int res;

#if defined(ETHER)
int p_assigned = 0;
Expand Down Expand Up @@ -121,11 +122,11 @@ int main(int argc, char **argv)
FlsEmu_Init(&FlsEmu_Config);
Xcp_Init();

pthread_create(&XcpHw_ThreadID[1], NULL, &AppTask, NULL);
pthread_create(&XcpHw_ThreadID[2], NULL, &XcpTui_MainFunction, NULL);
pthread_create(&XcpHw_ThreadID[3], NULL, &TlTask, NULL);
res = thrd_create(&XcpHw_ThreadID[1], &AppTask, NULL);
res = thrd_create(&XcpHw_ThreadID[2], &XcpTui_MainFunction, NULL);
res = thrd_create(&XcpHw_ThreadID[3], &TlTask, NULL);

pthread_join(XcpHw_ThreadID[2], NULL);
thrd_join(XcpHw_ThreadID[2], &res);

XcpHw_Deinit();
XcpTl_DeInit();
Expand Down
82 changes: 48 additions & 34 deletions src/hw/linux/hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
#include <threads.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
Expand Down Expand Up @@ -98,7 +98,7 @@ void exitFunc(void);
/*
** Global Variables.
*/
pthread_t XcpHw_ThreadID[4];
thrd_t XcpHw_ThreadID[4];


/*
Expand All @@ -107,8 +107,8 @@ pthread_t XcpHw_ThreadID[4];
#define XCPHW_APPLICATION_STATES (32)

typedef struct tagXcpHw_ApplicationStateType {
pthread_mutex_t stateMutex;
pthread_cond_t stateCond;
mtx_t stateMutex;
cnd_t stateCond;
volatile uint32_t stateBitmap;
volatile uint8_t counter[XCPHW_APPLICATION_STATES];
} XcpHw_ApplicationStateType;
Expand All @@ -118,17 +118,17 @@ typedef struct tagXcpHw_ApplicationStateType {
** Local Variables.
*/
static HwStateType HwState = {0};
static pthread_mutex_t XcpHw_Locks[XCP_HW_LOCK_COUNT] = {PTHREAD_MUTEX_INITIALIZER};
static mtx_t XcpHw_Locks[XCP_HW_LOCK_COUNT];


static struct timespec XcpHw_TimerResolution = {0};
static timer_t XcpHw_AppMsTimer;
static unsigned long long XcpHw_FreeRunningCounter = 0ULL;

static XcpHw_ApplicationStateType XcpHw_ApplicationState = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, {0}};
static XcpHw_ApplicationStateType XcpHw_ApplicationState = {0};

static pthread_cond_t XcpHw_TransmissionEvent;
static pthread_mutex_t XcpHw_TransmissionMutex = {PTHREAD_MUTEX_INITIALIZER};
static cnd_t XcpHw_TransmissionEvent;
static mtx_t XcpHw_TransmissionMutex;

/*
** Global Functions.
Expand Down Expand Up @@ -258,52 +258,66 @@ void XcpHw_Init(void)
XcpHw_ErrorMsg("XcpHw_Init::sigprocmask()", errno);
}
XcpTui_Init();
pthread_cond_init(&XcpHw_TransmissionEvent, NULL);

cnd_init(&XcpHw_TransmissionEvent);
cnd_init(&XcpHw_ApplicationState.stateCond);
mtx_init(&XcpHw_ApplicationState.stateMutex, mtx_recursive);
mtx_init(&XcpHw_TransmissionMutex, mtx_recursive);
for (size_t idx = 0; idx < XCP_HW_LOCK_COUNT; ++idx) {
mtx_init(&XcpHw_Locks[idx], mtx_recursive);
}

}

void XcpHw_Deinit(void)
{
XcpTui_Deinit();
XcpHw_DeinitLocks();
pthread_cond_destroy(&XcpHw_TransmissionEvent);
cnd_destroy(&XcpHw_TransmissionEvent);
cnd_destroy(&XcpHw_ApplicationState.stateCond);
mtx_destroy(&XcpHw_ApplicationState.stateMutex);
mtx_destroy(&XcpHw_TransmissionMutex);
for (size_t idx = 0; idx < XCP_HW_LOCK_COUNT; ++idx) {
mtx_destroy(&XcpHw_Locks[idx]);
}
}

void XcpHw_SignalApplicationState(uint32_t state, uint8_t signal_all)
{
int status = 0;

status = pthread_mutex_lock(&XcpHw_ApplicationState.stateMutex);
status = mtx_lock(&XcpHw_ApplicationState.stateMutex);
if (status != 0) {
XcpHw_ErrorMsg("XcpHw_SignalApplicationState::pthread_mutex_lock()", status);
XcpHw_ErrorMsg("XcpHw_SignalApplicationState::mtx_lock()", status);
}
XcpHw_ApplicationState.stateBitmap = state;

if (signal_all) {
status = pthread_cond_broadcast(&XcpHw_ApplicationState.stateCond);
status = cnd_broadcast(&XcpHw_ApplicationState.stateCond);
} else {
status = pthread_cond_signal(&XcpHw_ApplicationState.stateCond);
status = cnd_signal(&XcpHw_ApplicationState.stateCond);
}
if (status != 0) {
XcpHw_ErrorMsg("XcpHw_SignalApplicationState::pthread_cond_signal()", status);
XcpHw_ErrorMsg("XcpHw_SignalApplicationState::cnd_signal()", status);
}
status = pthread_mutex_unlock(&XcpHw_ApplicationState.stateMutex);
status = mtx_unlock(&XcpHw_ApplicationState.stateMutex);
if (status != 0) {
XcpHw_ErrorMsg("XcpHw_SignalApplicationState::pthread_mutex_unlock()", status);
XcpHw_ErrorMsg("XcpHw_SignalApplicationState::mtx_unlock()", status);
}
}

void XcpHw_ResetApplicationState(uint32_t mask)
{
int status = 0;

status = pthread_mutex_lock(&XcpHw_ApplicationState.stateMutex);
status = mtx_lock(&XcpHw_ApplicationState.stateMutex);
if (status != 0) {
XcpHw_ErrorMsg("XcpHw_ResetApplicationState::pthread_mutex_lock()", status);
XcpHw_ErrorMsg("XcpHw_ResetApplicationState::mtx_lock()", status);
}
XcpHw_ApplicationState.stateBitmap &= ~mask;
status = pthread_mutex_unlock(&XcpHw_ApplicationState.stateMutex);
status = mtx_unlock(&XcpHw_ApplicationState.stateMutex);
if (status != 0) {
XcpHw_ErrorMsg("XcpHw_ResetApplicationState::pthread_mutex_unlock()", status);
XcpHw_ErrorMsg("XcpHw_ResetApplicationState::mtx_unlock()", status);
}
}

Expand All @@ -330,22 +344,22 @@ uint32_t XcpHw_WaitApplicationState(uint32_t mask)
timeout.tv_sec = time(NULL) + 2;
timeout.tv_nsec = 0;
#endif
status = pthread_mutex_lock(&XcpHw_ApplicationState.stateMutex);
status = mtx_lock(&XcpHw_ApplicationState.stateMutex);
if (status != 0) {
XcpHw_ErrorMsg("XcpHw_WaitApplicationState::pthread_mutex_lock()", status);
XcpHw_ErrorMsg("XcpHw_WaitApplicationState::mtx_lock()", status);
}

while ((XcpHw_ApplicationState.stateBitmap == 0) && (!match)) {
status = pthread_cond_wait(&XcpHw_ApplicationState.stateCond, &XcpHw_ApplicationState.stateMutex);
status = cnd_wait(&XcpHw_ApplicationState.stateCond, &XcpHw_ApplicationState.stateMutex);
if (status != 0) {
XcpHw_ErrorMsg("XcpHw_WaitApplicationState::pthread_cond_wait()", status);
XcpHw_ErrorMsg("XcpHw_WaitApplicationState::cnd_wait()", status);
}
//match = (XcpHw_ApplicationState.stateBitmap & mask) != 0x00;
match = (XcpHw_ApplicationState.stateBitmap & mask) == mask;
}
status = pthread_mutex_unlock(&XcpHw_ApplicationState.stateMutex);
status = mtx_unlock(&XcpHw_ApplicationState.stateMutex);
if (status != 0) {
XcpHw_ErrorMsg("XcpHw_WaitApplicationState::pthread_mutex_unlock()", status);
XcpHw_ErrorMsg("XcpHw_WaitApplicationState::mtx_unlock()", status);
}
return XcpHw_ApplicationState.stateBitmap;
}
Expand Down Expand Up @@ -437,7 +451,7 @@ static void XcpHw_InitLocks(void)
uint8_t idx = UINT8(0);

for (idx = UINT8(0); idx < XCP_HW_LOCK_COUNT; ++idx) {
pthread_mutex_init(&XcpHw_Locks[idx], NULL);
mtx_init(&XcpHw_Locks[idx], mtx_recursive);
}
}

Expand All @@ -446,7 +460,7 @@ static void XcpHw_DeinitLocks(void)
uint8_t idx = UINT8(0);

for (idx = UINT8(0); idx < XCP_HW_LOCK_COUNT; ++idx) {
pthread_mutex_destroy(&XcpHw_Locks[idx]);
mtx_destroy(&XcpHw_Locks[idx]);
}
}

Expand All @@ -455,26 +469,26 @@ void XcpHw_AcquireLock(uint8_t lockIdx)
if (lockIdx >= XCP_HW_LOCK_COUNT) {
return;
}
pthread_mutex_lock(&XcpHw_Locks[lockIdx]);
mtx_lock(&XcpHw_Locks[lockIdx]);
}

void XcpHw_ReleaseLock(uint8_t lockIdx)
{
if (lockIdx >= XCP_HW_LOCK_COUNT) {
return;
}
pthread_mutex_unlock(&XcpHw_Locks[lockIdx]);
mtx_unlock(&XcpHw_Locks[lockIdx]);
}

void XcpHw_SignalTransmitRequest(void)
{
pthread_mutex_lock(&XcpHw_TransmissionMutex);
pthread_cond_signal(&XcpHw_TransmissionEvent);
mtx_lock(&XcpHw_TransmissionMutex);
cnd_signal(&XcpHw_TransmissionEvent);
}

void XcpHw_WaitTransmitRequest(void)
{
pthread_cond_wait(&XcpHw_TransmissionEvent, &XcpHw_TransmissionMutex);
cnd_wait(&XcpHw_TransmissionEvent, &XcpHw_TransmissionMutex);
}

#if 0
Expand Down
6 changes: 3 additions & 3 deletions src/tl/eth/linuxeth.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#include <threads.h>

#include <ncurses.h>

Expand Down Expand Up @@ -75,7 +75,7 @@ typedef struct tagXcpTl_ConnectionType {
} XcpTl_ConnectionType;


extern pthread_t XcpHw_ThreadID[4];
extern thrd_t XcpHw_ThreadID[4];

unsigned char buf[XCP_COMM_BUFLEN];
socklen_t addrSize = sizeof(struct sockaddr_storage);
Expand Down Expand Up @@ -167,7 +167,7 @@ void XcpTl_Init(void)
XcpHw_ErrorMsg("XcpTl_Init:setsockopt(SO_REUSEADDR)", errno);
}

ret = pthread_create(&XcpHw_ThreadID[0], NULL, &XcpTl_WorkerThread, NULL);
ret = thrd_create(&XcpHw_ThreadID[0], &XcpTl_WorkerThread, NULL);
if (ret != 0) {
err_abort(ret, "Create worker thread");
}
Expand Down

0 comments on commit b28283e

Please sign in to comment.