targets/OSX/plc_OSX_main.c
branchpython3
changeset 3781 25195da82745
parent 3778 296e459efdfb
child 3782 39480d9d3d3f
equal deleted inserted replaced
3780:fdd7f9938e59 3781:25195da82745
    10 #include <pthread.h>
    10 #include <pthread.h>
    11 #include <locale.h>
    11 #include <locale.h>
    12 #include <semaphore.h>
    12 #include <semaphore.h>
    13 #include <dispatch/dispatch.h>
    13 #include <dispatch/dispatch.h>
    14 
    14 
    15 static sem_t Run_PLC;
    15 static dispatch_semaphore_t Run_PLC;
    16 
    16 
    17 long AtomicCompareExchange(long *atomicvar, long compared, long exchange)
    17 long AtomicCompareExchange(long *atomicvar, long compared, long exchange)
    18 {
    18 {
    19     return __sync_val_compare_and_swap(atomicvar, compared, exchange);
    19     return __sync_val_compare_and_swap(atomicvar, compared, exchange);
    20 }
    20 }
    44 }
    44 }
    45 
    45 
    46 static inline void PLC_timer_notify(void *arg)
    46 static inline void PLC_timer_notify(void *arg)
    47 {
    47 {
    48     PLC_GetTime(&__CURRENT_TIME);
    48     PLC_GetTime(&__CURRENT_TIME);
    49     sem_post(&Run_PLC);
    49     dispatch_semaphore_signal(Run_PLC);
    50 }
    50 }
    51 
    51 
    52 void PLC_SetTimer(unsigned long long next, unsigned long long period)
    52 void PLC_SetTimer(unsigned long long next, unsigned long long period)
    53 {
    53 {
    54     if (next == period && next == 0) {
    54     if (next == period && next == 0) {
    85 }
    85 }
    86 
    86 
    87 void PLC_thread_proc(void *arg)
    87 void PLC_thread_proc(void *arg)
    88 {
    88 {
    89     while (!PLC_shutdown) {
    89     while (!PLC_shutdown) {
    90         sem_wait(&Run_PLC);
    90         dispatch_semaphore_wait(Run_PLC, DISPATCH_TIME_FOREVER);
    91         __run();
    91         __run();
    92     }
    92     }
    93     pthread_exit(0);
    93     pthread_exit(0);
    94 }
    94 }
    95 
    95 
    98 {
    98 {
    99     setlocale(LC_NUMERIC, "C");
    99     setlocale(LC_NUMERIC, "C");
   100 
   100 
   101     PLC_shutdown = 0;
   101     PLC_shutdown = 0;
   102 
   102 
   103     sem_init(&Run_PLC, 0, 0);
   103     Run_PLC = dispatch_semaphore_create(0);
   104 
   104 
   105     pthread_create(&PLC_thread, NULL, (void *)&PLC_thread_proc, NULL);
   105     pthread_create(&PLC_thread, NULL, (void *)&PLC_thread_proc, NULL);
   106 
   106 
   107     pthread_mutex_init(&debug_wait_mutex, NULL);
   107     pthread_mutex_init(&debug_wait_mutex, NULL);
   108     pthread_mutex_init(&debug_mutex, NULL);
   108     pthread_mutex_init(&debug_mutex, NULL);
   149 
   149 
   150 int stopPLC()
   150 int stopPLC()
   151 {
   151 {
   152     /* Stop the PLC */
   152     /* Stop the PLC */
   153     PLC_shutdown = 1;
   153     PLC_shutdown = 1;
   154     sem_post(&Run_PLC);
   154     dispatch_semaphore_signal(Run_PLC);
   155     PLC_SetTimer(0, 0);
   155     PLC_SetTimer(0, 0);
   156     pthread_join(PLC_thread, NULL);
   156     pthread_join(PLC_thread, NULL);
   157     sem_destroy(&Run_PLC);
   157     dispatch_release(Run_PLC);
       
   158     Run_PLC = NULL;
   158     dispatch_source_cancel(PLC_timer);
   159     dispatch_source_cancel(PLC_timer);
   159     __cleanup();
   160     __cleanup();
   160     pthread_mutex_destroy(&debug_wait_mutex);
   161     pthread_mutex_destroy(&debug_wait_mutex);
   161     pthread_mutex_destroy(&debug_mutex);
   162     pthread_mutex_destroy(&debug_mutex);
   162     pthread_mutex_destroy(&python_wait_mutex);
   163     pthread_mutex_destroy(&python_wait_mutex);