targets/OSX/plc_OSX_main.c
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 20 Aug 2024 01:13:14 +0200
changeset 3973 42730c1cb879
parent 3947 9b5cb90297e4
permissions -rw-r--r--
IDE: fix completion

uses ctrl key on Mac
update internal variable list before listing them
avoid exception if empty list
apply selection when pressing return or enter
3778
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
     1
/**
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
     2
 * Mac OSX specific code
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
     3
 **/
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
     4
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
     5
#include <stdio.h>
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
     6
#include <string.h>
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
     7
#include <time.h>
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
     8
#include <signal.h>
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
     9
#include <stdlib.h>
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    10
#include <pthread.h>
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    11
#include <locale.h>
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    12
#include <semaphore.h>
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    13
#include <dispatch/dispatch.h>
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    14
3781
25195da82745 replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents: 3778
diff changeset
    15
static dispatch_semaphore_t Run_PLC;
3778
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    16
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    17
long AtomicCompareExchange(long *atomicvar, long compared, long exchange)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    18
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    19
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    20
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    21
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    22
long long AtomicCompareExchange64(long long *atomicvar, long long compared,
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    23
                                  long long exchange)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    24
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    25
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    26
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    27
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    28
void PLC_GetTime(IEC_TIME * CURRENT_TIME)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    29
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    30
    struct timespec tmp;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    31
    clock_gettime(CLOCK_REALTIME, &tmp);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    32
    CURRENT_TIME->tv_sec = tmp.tv_sec;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    33
    CURRENT_TIME->tv_nsec = tmp.tv_nsec;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    34
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    35
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    36
dispatch_queue_t queue;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    37
dispatch_source_t PLC_timer;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    38
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    39
static inline void PLC_timer_cancel(void *arg)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    40
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    41
    dispatch_release(PLC_timer);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    42
    dispatch_release(queue);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    43
    exit(0);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    44
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    45
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    46
static inline void PLC_timer_notify(void *arg)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    47
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    48
    PLC_GetTime(&__CURRENT_TIME);
3781
25195da82745 replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents: 3778
diff changeset
    49
    dispatch_semaphore_signal(Run_PLC);
3778
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    50
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    51
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    52
void PLC_SetTimer(unsigned long long next, unsigned long long period)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    53
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    54
    if (next == period && next == 0) {
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    55
        dispatch_suspend(PLC_timer);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    56
    } else {
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    57
        dispatch_time_t start;
3782
39480d9d3d3f OSX: use wall time instead of system clock
GP Orcullo <kinsamanka@gmail.com>
parents: 3781
diff changeset
    58
        start = dispatch_walltime(NULL, next);
3778
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    59
        dispatch_source_set_timer(PLC_timer, start, period, 0);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    60
        dispatch_resume(PLC_timer);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    61
    }
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    62
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    63
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    64
void catch_signal(int sig)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    65
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    66
    signal(SIGINT, catch_signal);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    67
    printf("Got Signal %d\n", sig);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    68
    dispatch_source_cancel(PLC_timer);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    69
    exit(0);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    70
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    71
3947
9b5cb90297e4 All Runtimes: Now sizeof(tick)=4 on all platforms (unsigned int).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3782
diff changeset
    72
static unsigned int __debug_tick;
3778
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    73
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    74
pthread_t PLC_thread;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    75
static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    76
static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    77
static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    78
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    79
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    80
int PLC_shutdown = 0;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    81
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    82
int ForceSaveRetainReq(void)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    83
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    84
    return PLC_shutdown;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    85
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    86
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    87
void PLC_thread_proc(void *arg)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    88
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    89
    while (!PLC_shutdown) {
3781
25195da82745 replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents: 3778
diff changeset
    90
        dispatch_semaphore_wait(Run_PLC, DISPATCH_TIME_FOREVER);
3778
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    91
        __run();
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    92
    }
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    93
    pthread_exit(0);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    94
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    95
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    96
#define maxval(a,b) ((a>b)?a:b)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    97
int startPLC(int argc, char **argv)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    98
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
    99
    setlocale(LC_NUMERIC, "C");
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   100
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   101
    PLC_shutdown = 0;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   102
3781
25195da82745 replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents: 3778
diff changeset
   103
    Run_PLC = dispatch_semaphore_create(0);
3778
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   104
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   105
    pthread_create(&PLC_thread, NULL, (void *)&PLC_thread_proc, NULL);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   106
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   107
    pthread_mutex_init(&debug_wait_mutex, NULL);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   108
    pthread_mutex_init(&debug_mutex, NULL);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   109
    pthread_mutex_init(&python_wait_mutex, NULL);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   110
    pthread_mutex_init(&python_mutex, NULL);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   111
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   112
    pthread_mutex_lock(&debug_wait_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   113
    pthread_mutex_lock(&python_wait_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   114
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   115
    queue = dispatch_queue_create("timerQueue", 0);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   116
    PLC_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   117
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   118
    dispatch_set_context(PLC_timer, &PLC_timer);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   119
    dispatch_source_set_event_handler_f(PLC_timer, PLC_timer_notify);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   120
    dispatch_source_set_cancel_handler_f(PLC_timer, PLC_timer_cancel);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   121
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   122
    if (__init(argc, argv) == 0) {
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   123
        PLC_SetTimer(common_ticktime__, common_ticktime__);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   124
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   125
        /* install signal handler for manual break */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   126
        signal(SIGINT, catch_signal);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   127
    } else {
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   128
        return 1;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   129
    }
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   130
    return 0;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   131
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   132
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   133
int TryEnterDebugSection(void)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   134
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   135
    if (pthread_mutex_trylock(&debug_mutex) == 0) {
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   136
        /* Only enter if debug active */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   137
        if (__DEBUG) {
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   138
            return 1;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   139
        }
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   140
        pthread_mutex_unlock(&debug_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   141
    }
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   142
    return 0;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   143
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   144
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   145
void LeaveDebugSection(void)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   146
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   147
    pthread_mutex_unlock(&debug_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   148
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   149
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   150
int stopPLC()
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   151
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   152
    /* Stop the PLC */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   153
    PLC_shutdown = 1;
3781
25195da82745 replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents: 3778
diff changeset
   154
    dispatch_semaphore_signal(Run_PLC);
3778
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   155
    PLC_SetTimer(0, 0);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   156
    pthread_join(PLC_thread, NULL);
3781
25195da82745 replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents: 3778
diff changeset
   157
    dispatch_release(Run_PLC);
25195da82745 replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents: 3778
diff changeset
   158
    Run_PLC = NULL;
3778
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   159
    dispatch_source_cancel(PLC_timer);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   160
    __cleanup();
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   161
    pthread_mutex_destroy(&debug_wait_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   162
    pthread_mutex_destroy(&debug_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   163
    pthread_mutex_destroy(&python_wait_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   164
    pthread_mutex_destroy(&python_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   165
    return 0;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   166
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   167
3947
9b5cb90297e4 All Runtimes: Now sizeof(tick)=4 on all platforms (unsigned int).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3782
diff changeset
   168
extern unsigned int __tick;
9b5cb90297e4 All Runtimes: Now sizeof(tick)=4 on all platforms (unsigned int).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3782
diff changeset
   169
9b5cb90297e4 All Runtimes: Now sizeof(tick)=4 on all platforms (unsigned int).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3782
diff changeset
   170
int WaitDebugData(unsigned int *tick)
3778
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   171
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   172
    int res;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   173
    if (PLC_shutdown)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   174
        return 1;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   175
    /* Wait signal from PLC thread */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   176
    res = pthread_mutex_lock(&debug_wait_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   177
    *tick = __debug_tick;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   178
    return res;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   179
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   180
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   181
/* Called by PLC thread when debug_publish finished
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   182
 * This is supposed to unlock debugger thread in WaitDebugData*/
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   183
void InitiateDebugTransfer()
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   184
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   185
    /* remember tick */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   186
    __debug_tick = __tick;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   187
    /* signal debugger thread it can read data */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   188
    pthread_mutex_unlock(&debug_wait_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   189
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   190
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   191
int suspendDebug(int disable)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   192
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   193
    /* Prevent PLC to enter debug code */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   194
    pthread_mutex_lock(&debug_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   195
    /*__DEBUG is protected by this mutex */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   196
    __DEBUG = !disable;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   197
    if (disable)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   198
        pthread_mutex_unlock(&debug_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   199
    return 0;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   200
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   201
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   202
void resumeDebug(void)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   203
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   204
    __DEBUG = 1;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   205
    /* Let PLC enter debug code */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   206
    pthread_mutex_unlock(&debug_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   207
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   208
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   209
/* from plc_python.c */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   210
int WaitPythonCommands(void)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   211
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   212
    /* Wait signal from PLC thread */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   213
    return pthread_mutex_lock(&python_wait_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   214
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   215
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   216
/* Called by PLC thread on each new python command*/
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   217
void UnBlockPythonCommands(void)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   218
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   219
    /* signal python thread it can read data */
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   220
    pthread_mutex_unlock(&python_wait_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   221
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   222
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   223
int TryLockPython(void)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   224
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   225
    return pthread_mutex_trylock(&python_mutex) == 0;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   226
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   227
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   228
void UnLockPython(void)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   229
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   230
    pthread_mutex_unlock(&python_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   231
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   232
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   233
void LockPython(void)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   234
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   235
    pthread_mutex_lock(&python_mutex);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   236
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   237
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   238
struct RT_to_nRT_signal_s {
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   239
    pthread_cond_t WakeCond;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   240
    pthread_mutex_t WakeCondLock;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   241
};
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   242
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   243
typedef struct RT_to_nRT_signal_s RT_to_nRT_signal_t;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   244
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   245
#define _LogAndReturnNull(text) \
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   246
    {\
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   247
    	char mstr[256] = text " for ";\
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   248
        strncat(mstr, name, 255);\
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   249
        LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   250
        return NULL;\
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   251
    }
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   252
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   253
void *create_RT_to_nRT_signal(char *name)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   254
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   255
    RT_to_nRT_signal_t *sig =
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   256
        (RT_to_nRT_signal_t *) malloc(sizeof(RT_to_nRT_signal_t));
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   257
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   258
    if (!sig)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   259
        _LogAndReturnNull("Failed allocating memory for RT_to_nRT signal");
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   260
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   261
    pthread_cond_init(&sig->WakeCond, NULL);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   262
    pthread_mutex_init(&sig->WakeCondLock, NULL);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   263
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   264
    return (void *)sig;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   265
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   266
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   267
void delete_RT_to_nRT_signal(void *handle)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   268
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   269
    RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t *) handle;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   270
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   271
    pthread_cond_destroy(&sig->WakeCond);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   272
    pthread_mutex_destroy(&sig->WakeCondLock);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   273
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   274
    free(sig);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   275
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   276
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   277
int wait_RT_to_nRT_signal(void *handle)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   278
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   279
    int ret;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   280
    RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t *) handle;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   281
    pthread_mutex_lock(&sig->WakeCondLock);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   282
    ret = pthread_cond_wait(&sig->WakeCond, &sig->WakeCondLock);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   283
    pthread_mutex_unlock(&sig->WakeCondLock);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   284
    return ret;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   285
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   286
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   287
int unblock_RT_to_nRT_signal(void *handle)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   288
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   289
    RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t *) handle;
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   290
    return pthread_cond_signal(&sig->WakeCond);
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   291
}
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   292
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   293
void nRT_reschedule(void)
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   294
{
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   295
    sched_yield();
296e459efdfb Preliminary OSX support
GP Orcullo <kinsamanka@gmail.com>
parents:
diff changeset
   296
}