45 #if !defined(__STDC_NO_ATOMICS__) 48 typedef bool atomic_bool;
50 #include <stdatomic.h> 58 #include "xcp_threads.h" 61 #define XCP_THREAD (0) 63 #define APP_THREAD (2) 66 #define NUM_THREADS (4) 68 typedef void (*XcpThrd_ThreadFuncType)(
void *);
71 typedef HANDLE XcpThrd_ThreadType;
73 typedef pthread_t XcpThrd_ThreadType;
76 XcpThrd_ThreadType threads[NUM_THREADS];
78 static void XcpThrd_CreateThread(XcpThrd_ThreadType *thrd,
79 XcpThrd_ThreadFuncType func);
81 static void XcpThrd_SetAffinity(XcpThrd_ThreadType thrd,
int cpu);
83 #if defined(__STDC_NO_ATOMICS__) 84 static bool XcpThrd_ShuttingDown;
86 static atomic_bool XcpThrd_ShuttingDown;
91 static void XcpThrd_CreateThread(XcpThrd_ThreadType *thrd,
92 XcpThrd_ThreadFuncType func) {
94 XcpThrd_ThreadType res;
95 res = (HANDLE)_beginthread(func, 0, NULL);
96 CopyMemory(thrd, &res,
sizeof(XcpThrd_ThreadType));
97 XcpThrd_SetAffinity(res, 1);
99 pthread_create(thrd, NULL, func, NULL);
100 XcpThrd_SetAffinity(thrd, 1);
104 void XcpThrd_RunThreads(
void) {
107 XcpThrd_CreateThread(&threads[UI_THREAD], XcpTerm_Thread);
108 XcpThrd_CreateThread(&threads[TL_THREAD], XcpTl_Thread);
109 XcpThrd_CreateThread(&threads[XCP_THREAD], Xcp_Thread);
111 WaitForSingleObject(threads[UI_THREAD], INFINITE);
114 pthread_join(threads[UI_THREAD], NULL);
116 pthread_kill(threads[TL_THREAD], SIGINT);
117 pthread_kill(threads[XCP_THREAD], SIGINT);
121 void XcpThrd_Exit(
void) {
129 void *Xcp_Thread(
void *param) {
130 XCP_UNREFERENCED_PARAMETER(param);
131 XCP_FOREVER { Xcp_MainFunction(); }
135 void XcpThrd_EnableAsyncCancellation(
void) {
141 res = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
143 XcpHw_ErrorMsg(
"pthread_setcancelstate()", errno);
148 void XcpThrd_ShutDown(
void) {
151 printf(
"Shutdown RQ.\n");
152 if (XcpThrd_IsShuttingDown()) {
157 res = TerminateThread(threads[TL_THREAD], 0);
159 XcpHw_ErrorMsg(
"TerminateThread", GetLastError());
162 res = pthread_cancel(threads[TL_THREAD]);
164 XcpHw_ErrorMsg(
"pthread_cancel()", errno);
167 XcpThrd_ShuttingDown =
true;
170 bool XcpThrd_IsShuttingDown(
void) {
return XcpThrd_ShuttingDown; }
172 void bye(
void) { printf(
"Exiting program.\n"); }
174 static void XcpThrd_SetAffinity(XcpThrd_ThreadType thrd,
int cpu) {
176 if (SetThreadAffinityMask(thrd, cpu) == 0) {
177 XcpHw_ErrorMsg(
"SetThreadAffinityMask()", GetLastError());
187 res = pthread_setaffinity_np(thrd,
sizeof(cpu_set_t), &cpuset);
189 XcpHw_ErrorMsg(
"pthread_setaffinity_np()", errno);
192 res = pthread_getaffinity_np(thrd,
sizeof(cpu_set_t), &cpuset);
194 XcpHw_ErrorMsg(
"pthread_getaffinity_np()", errno);