From 0777e9bdf001bf07bba393c44b079e29b62582ef Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Fri, 16 Nov 2018 08:38:05 +0000 Subject: [PATCH] some code cleanup --- moo/lib/moo-utl.h | 1 - moo/lib/std.c | 183 ++++++++++++++++++++++++---------------------- 2 files changed, 94 insertions(+), 90 deletions(-) diff --git a/moo/lib/moo-utl.h b/moo/lib/moo-utl.h index 264bd26..3330af4 100644 --- a/moo/lib/moo-utl.h +++ b/moo/lib/moo-utl.h @@ -602,7 +602,6 @@ MOO_EXPORT moo_uint128_t moo_hton128 ( ); #endif - #if defined(__cplusplus) } #endif diff --git a/moo/lib/std.c b/moo/lib/std.c index ded7799..d97d8b0 100644 --- a/moo/lib/std.c +++ b/moo/lib/std.c @@ -359,6 +359,7 @@ struct xtn_t #define GET_XTN(moo) ((xtn_t*)moo_getxtn(moo)) static moo_t* g_moo = MOO_NULL; +static int g_moo_tick_active = 0; /* ========================================================================= */ @@ -2619,19 +2620,20 @@ static MOO_INLINE void swproc_all (void) /* TODO: make this atomic */ } + #if defined(__DOS__) && (defined(_INTELC32_) || defined(__WATCOMC__)) #if defined(_INTELC32_) -static void (*prev_timer_intr_handler) (void); +static void (*dos_prev_timer_intr_handler) (void); #else -static void (__interrupt *prev_timer_intr_handler) (void); +static void (__interrupt *dos_prev_timer_intr_handler) (void); #endif #if defined(_INTELC32_) #pragma interrupt(timer_intr_handler) -static void timer_intr_handler (void) +static void dos_timer_intr_handler (void) #else -static void __interrupt timer_intr_handler (void) +static void __interrupt dos_timer_intr_handler (void) #endif { /* @@ -2642,111 +2644,142 @@ static void __interrupt timer_intr_handler (void) */ /* The timer interrupt (normally) occurs 18.2 times per second. */ - if (g_moo) moo_switchprocess (g_moo); - _chain_intr (prev_timer_intr_handler); + swproc_moo (); + _chain_intr (dos_prev_timer_intr_handler); +} + +static void setup_tick (void) +{ + dos_prev_timer_intr_handler = _dos_getvect(0x1C); + _dos_setvect (0x1C, dos_timer_intr_handler); +} + +static void cancel_tick (void) +{ + _dos_setvect (0x1C, dos_prev_timer_intr_handler); } #elif defined(_WIN32) -static HANDLE g_tick_timer = MOO_NULL; /*INVALID_HANDLE_VALUE;*/ + +static HANDLE win_tick_timer = MOO_NULL; /*INVALID_HANDLE_VALUE;*/ static VOID CALLBACK arrange_process_switching (LPVOID arg, DWORD timeLow, DWORD timeHigh) { - swproc_all (); + swproc_moo (); +} + +static void setup_tick (void) +{ + LARGE_INTEGER li; + win_tick_timer = CreateWaitableTimer(MOO_NULL, TRUE, MOO_NULL); + if (win_tick_timer) + { + li.QuadPart = -MOO_SECNSEC_TO_NSEC(0, 20000); /* 20000 microseconds. 0.02 seconds */ + SetWaitableTimer (win_tick_timer, &li, 0, arrange_process_switching, MOO_NULL, FALSE); + } +} + +static void cancel_tick (void) +{ + if (win_tick_timer) + { + CancelWaitableTimer (win_tick_timer); + CloseHandle (win_tick_timer); + win_tick_timer = MOO_NULL; + } } #elif defined(__OS2__) -static TID g_tick_tid; -static HEV g_tick_sem; -static HTIMER g_tick_timer; -static int g_tick_done = 0; +static TID os2_tick_tid; +static HEV os2_tick_sem; +static HTIMER os2_tick_timer; +static int os2_tick_done = 0; static void EXPENTRY os2_wait_for_timer_event (ULONG x) { APIRET rc; ULONG count; - rc = DosCreateEventSem (NULL, &g_tick_sem, DC_SEM_SHARED, FALSE); + rc = DosCreateEventSem (NULL, &os2_tick_sem, DC_SEM_SHARED, FALSE); if (rc != NO_ERROR) { /* xxxx */ } - rc = DosStartTimer (1L, (HSEM)g_tick_sem, &g_tick_timer); + rc = DosStartTimer (1L, (HSEM)os2_tick_sem, &os2_tick_timer); if (rc != NO_ERROR) { } - while (!g_tick_done) + while (!os2_tick_done) { - rc = DosWaitEventSem((HSEM)g_tick_sem, 5000L); - DosResetEventSem((HSEM)g_tick_sem, &count); - swproc_all (); + rc = DosWaitEventSem((HSEM)os2_tick_sem, 5000L); + DosResetEventSem((HSEM)os2_tick_sem, &count); + swproc_moo (); } - DosStopTimer (g_tick_timer); - DosCloseEventSem ((HSEM)g_tick_sem); + DosStopTimer (os2_tick_timer); + DosCloseEventSem ((HSEM)os2_tick_sem); - g_tick_timer = NULL; - g_tick_sem = NULL; + os2_tick_timer = NULL; + os2_tick_sem = NULL; DosExit (EXIT_THREAD, 0); } +static void setup_tick (void) +{ + /* TODO: Error check */ + DosCreateThread (&os2_tick_tid, os2_wait_for_timer_event, 0, 0, 4096); +} + +static void cancel_tick (void) +{ + if (os2_tick_sem) DosPostEventSem (os2_tick_sem); + os2_tick_done = 1; +} #elif defined(macintosh) -static TMTask g_tmtask; -static ProcessSerialNumber g_psn; +static TMTask mac_tmtask; +static ProcessSerialNumber mac_psn; #define TMTASK_DELAY 50 /* milliseconds if positive, microseconds(after negation) if negative */ static pascal void timer_intr_handler (TMTask* task) { - swproc_all (); - WakeUpProcess (&g_psn); - PrimeTime ((QElem*)&g_tmtask, TMTASK_DELAY); + swproc_moo (); + WakeUpProcess (&mac_psn); + PrimeTime ((QElem*)&mac_tmtask, TMTASK_DELAY); } -#else -static void arrange_process_switching (int sig) -{ - swproc_all (); -} -#endif - static void setup_tick (void) { -#if defined(__DOS__) && (defined(_INTELC32_) || defined(__WATCOMC__)) + GetCurrentProcess (&mac_psn); + memset (&mac_tmtask, 0, MOO_SIZEOF(mac_tmtask)); + mac_tmtask.tmAddr = NewTimerProc (timer_intr_handler); + InsXTime ((QElem*)&mac_tmtask); + PrimeTime ((QElem*)&mac_tmtask, TMTASK_DELAY); +} - prev_timer_intr_handler = _dos_getvect (0x1C); - _dos_setvect (0x1C, timer_intr_handler); - -#elif defined(_WIN32) - - LARGE_INTEGER li; - g_tick_timer = CreateWaitableTimer(MOO_NULL, TRUE, MOO_NULL); - if (g_tick_timer) - { - li.QuadPart = -MOO_SECNSEC_TO_NSEC(0, 20000); /* 20000 microseconds. 0.02 seconds */ - SetWaitableTimer (g_tick_timer, &li, 0, arrange_process_switching, MOO_NULL, FALSE); - } - -#elif defined(__OS2__) - /* TODO: Error check */ - DosCreateThread (&g_tick_tid, os2_wait_for_timer_event, 0, 0, 4096); - -#elif defined(macintosh) - - GetCurrentProcess (&g_psn); - MOO_MEMSET (&g_tmtask, 0, MOO_SIZEOF(g_tmtask)); - g_tmtask.tmAddr = NewTimerProc (timer_intr_handler); - InsXTime ((QElem*)&g_tmtask); - PrimeTime ((QElem*)&g_tmtask, TMTASK_DELAY); +static void cancel_tick (void) +{ + RmvTime ((QElem*)&mac_tmtask); + /*DisposeTimerProc (mac_tmtask.tmAddr);*/ +} #elif defined(HAVE_SETITIMER) && defined(SIGVTALRM) && defined(ITIMER_VIRTUAL) + +static void arrange_process_switching (int sig) +{ + swproc_moo (); +} + +static void setup_tick (void) +{ struct itimerval itv; struct sigaction act; - MOO_MEMSET (&act, 0, sizeof(act)); + memset (&act, 0, sizeof(act)); sigemptyset (&act.sa_mask); act.sa_handler = arrange_process_switching; act.sa_flags = SA_RESTART; @@ -2759,36 +2792,10 @@ static void setup_tick (void) itv.it_value.tv_sec = 0; itv.it_value.tv_usec = MOO_ITIMER_TICK; setitimer (ITIMER_VIRTUAL, &itv, MOO_NULL); -#else - -# error UNSUPPORTED -#endif } static void cancel_tick (void) { -#if defined(__DOS__) && (defined(_INTELC32_) || defined(__WATCOMC__)) - - _dos_setvect (0x1C, prev_timer_intr_handler); - -#elif defined(_WIN32) - - if (g_tick_timer) - { - CancelWaitableTimer (g_tick_timer); - CloseHandle (g_tick_timer); - g_tick_timer = MOO_NULL; - } - -#elif defined(__OS2__) - if (g_tick_sem) DosPostEventSem (g_tick_sem); - g_tick_done = 1; - -#elif defined(macintosh) - RmvTime ((QElem*)&g_tmtask); - /*DisposeTimerProc (g_tmtask.tmAddr);*/ - -#elif defined(HAVE_SETITIMER) && defined(SIGVTALRM) && defined(ITIMER_VIRTUAL) struct itimerval itv; struct sigaction act; @@ -2802,12 +2809,10 @@ static void cancel_tick (void) act.sa_handler = SIG_IGN; /* ignore the signal potentially fired by the one-shot arrange above */ act.sa_flags = 0; sigaction (SIGVTALRM, &act, MOO_NULL); - +} #else # error UNSUPPORTED #endif -} - /* ========================================================================= */