long AtomicCompareExchange(long* atomicvar, long compared, long exchange)
return InterlockedCompareExchange(atomicvar, exchange, compared);
CRITICAL_SECTION Atomic64CS;
long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange)
EnterCriticalSection(&Atomic64CS);
if(*atomicvar == compared){
LeaveCriticalSection(&Atomic64CS);
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
(*CURRENT_TIME).tv_sec = timetmp.time;
(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
void PLC_SetTimer(unsigned long long next, unsigned long long period)
/* arg 2 of SetWaitableTimer take 100 ns interval*/
liDueTime.QuadPart = next / (-100);
if (!SetWaitableTimer(PLC_timer, &liDueTime, period<1000000?1:period/1000000, NULL, NULL, 0))
printf("SetWaitableTimer failed (%d)\n", GetLastError());
int ForceSaveRetainReq(void) {
/* Variable used to stop plcloop thread */
if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0){
PLC_GetTime(&__CURRENT_TIME);
#define maxval(a,b) ((a>b)?a:b)
int startPLC(int argc,char **argv)
unsigned long thread_id = 0;
debug_sem = CreateSemaphore(
NULL, // default security attributes
NULL); // unnamed semaphore
printf("startPLC CreateSemaphore debug_sem error: %d\n", GetLastError());
debug_wait_sem = CreateSemaphore(
NULL, // default security attributes
NULL); // unnamed semaphore
if (debug_wait_sem == NULL)
printf("startPLC CreateSemaphore debug_wait_sem error: %d\n", GetLastError());
python_sem = CreateSemaphore(
NULL, // default security attributes
NULL); // unnamed semaphore
printf("startPLC CreateSemaphore python_sem error: %d\n", GetLastError());
python_wait_sem = CreateSemaphore(
NULL, // default security attributes
NULL); // unnamed semaphore
if (python_wait_sem == NULL)
printf("startPLC CreateSemaphore python_wait_sem error: %d\n", GetLastError());
/* Create a waitable timer */
PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
if( __init(argc,argv) == 0 )
PLC_SetTimer(common_ticktime__,common_ticktime__);
PLC_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlcLoop, NULL, 0, &thread_id);
static unsigned int __debug_tick;
int TryEnterDebugSection(void)
//printf("TryEnterDebugSection\n");
if(WaitForSingleObject(debug_sem, 0) == WAIT_OBJECT_0){
/* Only enter if debug active */
ReleaseSemaphore(debug_sem, 1, NULL);
void LeaveDebugSection(void)
ReleaseSemaphore(debug_sem, 1, NULL);
//printf("LeaveDebugSection\n");
// force last wakeup of PLC thread
SetWaitableTimer(PLC_timer, 0, 0, NULL, NULL, 0);
// wait end of PLC thread
WaitForSingleObject(PLC_thread, INFINITE);
CloseHandle(debug_wait_sem);
CloseHandle(python_wait_sem);
/* from plc_debugger.c */
int WaitDebugData(unsigned int *tick)
res = WaitForSingleObject(debug_wait_sem, INFINITE);
/* Wait signal from PLC thread */
return res != WAIT_OBJECT_0;
/* Called by PLC thread when debug_publish finished
* This is supposed to unlock debugger thread in WaitDebugData*/
void InitiateDebugTransfer()
/* signal debugger thread it can read data */
ReleaseSemaphore(debug_wait_sem, 1, NULL);
int suspendDebug(int disable)
/* Prevent PLC to enter debug code */
WaitForSingleObject(debug_sem, INFINITE);
ReleaseSemaphore(debug_sem, 1, NULL);
/* Let PLC enter debug code */
ReleaseSemaphore(debug_sem, 1, NULL);
int WaitPythonCommands(void)
/* Wait signal from PLC thread */
return WaitForSingleObject(python_wait_sem, INFINITE);
/* Called by PLC thread on each new python command*/
void UnBlockPythonCommands(void)
/* signal debugger thread it can read data */
ReleaseSemaphore(python_wait_sem, 1, NULL);
return WaitForSingleObject(python_sem, 0) == WAIT_OBJECT_0;
ReleaseSemaphore(python_sem, 1, NULL);
WaitForSingleObject(python_sem, INFINITE);
static void __attribute__((constructor))
InitializeCriticalSection(&Atomic64CS);
static void __attribute__((destructor))
beremiz_dll_destroy(void)
DeleteCriticalSection(&Atomic64CS);
struct RT_to_nRT_signal_s {
typedef struct RT_to_nRT_signal_s RT_to_nRT_signal_t;
#define _LogAndReturnNull(text) \
char mstr[256] = text " for ";\
strncat(mstr, name, 255);\
LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\
void *create_RT_to_nRT_signal(char* name){
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)malloc(sizeof(RT_to_nRT_signal_t));
_LogAndReturnNull("Failed allocating memory for RT_to_nRT signal");
sig->sem = CreateSemaphore(
NULL, // default security attributes
NULL); // unnamed semaphore
snprintf(mstr, 255, "startPLC CreateSemaphore %s error: %d\n", name, GetLastError());
LogMessage(LOG_CRITICAL, mstr, strlen(mstr));
void delete_RT_to_nRT_signal(void* handle){
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
int wait_RT_to_nRT_signal(void* handle){
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
return WaitForSingleObject(sig->sem, INFINITE);
int unblock_RT_to_nRT_signal(void* handle){
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
return ReleaseSemaphore(sig->sem, 1, NULL);
void nRT_reschedule(void){