From 864be6b36d09b79f31318a204673fcab273b91a4 Mon Sep 17 00:00:00 2001 From: Vincent Wei Date: Fri, 21 Feb 2020 12:54:24 +0800 Subject: [PATCH] tune code for no timer thread for MiniGUI-Threads runmode; remove mg_InstallIntervalTimer and mg_UninstallIntervalTimer, always use mg_InitTimer and mg_TerminateTimer instead --- cleanup-code.sh | 2 +- src/include/internals.h | 13 +-- src/include/timer.h | 3 +- src/kernel/desktop-comm.c | 45 ++------ src/kernel/desktop-ths.c | 12 ++- src/kernel/desktop.c | 15 +++ src/kernel/init.c | 16 ++- src/kernel/message.c | 2 +- src/kernel/timer.c | 208 +++++++++++++++++++----------------- src/server/server.c | 4 +- src/standalone/standalone.c | 58 ++++------ 11 files changed, 187 insertions(+), 191 deletions(-) diff --git a/cleanup-code.sh b/cleanup-code.sh index 36a4ef05..b49f4f04 100755 --- a/cleanup-code.sh +++ b/cleanup-code.sh @@ -135,6 +135,6 @@ tab2space() { # sed -i 's/\<__ZOF_TYPE_HIGHER\>/ZOF_TYPE_HIGHER/g' `grep __ZOF_TYPE_HIGHER -rl include/ src/` -sed -i 's/\/AlertDesktopTimerEvent/g' `grep SetDesktopTimerFlag -rl include/ src/` +# sed -i 's/\/AlertDesktopTimerEvent/g' `grep SetDesktopTimerFlag -rl include/ src/` exit 0 diff --git a/src/include/internals.h b/src/include/internals.h index b079d36a..f58b62a1 100644 --- a/src/include/internals.h +++ b/src/include/internals.h @@ -767,7 +767,7 @@ static inline MSGQUEUE* getMsgQueueIfWindowInThisThread (HWND hWnd) #endif /* defined _MGHAVE_VIRTUAL_WINDOW */ -#if 0 // def _MGHAVE_VIRTUAL_WINDOW +#if 0 /* always use QS_DESKTIMER for desktop timer */ static inline void AlertDesktopTimerEvent (void) { if (__mg_dsk_msg_queue) { @@ -775,15 +775,17 @@ static inline void AlertDesktopTimerEvent (void) POST_MSGQ (__mg_dsk_msg_queue); } } -#endif /* defined _MGHAVE_VIRTUAL_WINDOW */ +#endif /* deprecated code */ static inline void AlertDesktopTimerEvent (void) { __mg_dsk_msg_queue->dwState |= QS_DESKTIMER; +#if 0 /* since 5.0.0, we no longer use the timer thread */ #ifdef _MGHAVE_VIRTUAL_WINDOW if (getMsgQueueForThisThread() != __mg_dsk_msg_queue) POST_MSGQ (__mg_dsk_msg_queue); -#endif +#endif /* defined _MGHAVE_VIRTUAL_WINDOW */ +#endif /* deprecated code */ } static inline void setMsgQueueTimerFlag (PMSGQUEUE pMsgQueue, int slot) @@ -797,9 +799,8 @@ static inline void removeMsgQueueTimerFlag (PMSGQUEUE pMsgQueue, int slot) pMsgQueue->expired_timer_mask &= ~(0x01 << slot); } -BOOL mg_InitTimer (void); -BOOL mg_InstallIntervalTimer (void); -BOOL mg_UninstallIntervalTimer (void); +BOOL mg_InitTimer (BOOL use_sys_timer); +void mg_TerminateTimer (BOOL use_sys_timer); /*window element renderer manager interface*/ extern WINDOW_ELEMENT_RENDERER * __mg_def_renderer; diff --git a/src/include/timer.h b/src/include/timer.h index d53bfe09..45541828 100644 --- a/src/include/timer.h +++ b/src/include/timer.h @@ -75,8 +75,7 @@ typedef struct _MSGQUEUE MSGQUEUE; extern "C" { #endif /* __cplusplus */ -BOOL mg_InitTimer (void); -void mg_TerminateTimer (void); +void __mg_update_timer_count (void* data); int __mg_check_expired_timers (MSGQUEUE* msg_queue, DWORD inter); void __mg_remove_timers_by_msg_queue (MSGQUEUE* msg_queue); void __mg_remove_timer (MSGQUEUE* msg_queue, int slot); diff --git a/src/kernel/desktop-comm.c b/src/kernel/desktop-comm.c index 9f98bd1a..ad29fc62 100644 --- a/src/kernel/desktop-comm.c +++ b/src/kernel/desktop-comm.c @@ -2532,6 +2532,7 @@ static LRESULT DesktopWinProc (HWND hWnd, UINT message, static DWORD sg_old_counter = 0; if (__mg_quiting_stage < 0) { +#if 0 /* use post_quit_to_all_message_threads instead */ int level, slot; PMSGQUEUE pMsgQueue; ZORDERNODE* nodes = GET_ZORDERNODE(__mg_zorder_info); @@ -2549,45 +2550,9 @@ static LRESULT DesktopWinProc (HWND hWnd, UINT message, } } } +#endif /* deprecated code */ -#if 0 /* deprecated code */ - static int fixed_slots [] = { ZNIDX_SCREENLOCK, ZNIDX_DOCKER, - ZNIDX_LAUNCHER }; - - /* Since 5.0.0 */ - for (slot = 0; slot < TABLESIZE(fixed_slots); slot++) { - pWin = (PMAINWIN)(nodes[fixed_slots[slot]].hwnd); - if (pWin && (pWin->WinType != TYPE_CONTROL) && - (pWin->pHosting == NULL)) { - if ((pMsgQueue = getMsgQueue((HWND)pWin))) { - POST_MSGQ(pMsgQueue); - } - } - } - - /* XXX: wake up other theads */ - for (slot=__mg_zorder_info->first_topmost; - slot > 0; slot = nodes[slot].next) { - pWin = (PMAINWIN)(nodes[slot].hwnd); - if (pWin && (pWin->WinType != TYPE_CONTROL) && - (pWin->pHosting == NULL)) { - if ((pMsgQueue = getMsgQueue((HWND)pWin))) { - POST_MSGQ(pMsgQueue); - } - } - } - - for (slot = __mg_zorder_info->first_normal; - slot > 0; slot = nodes[slot].next) { - pWin = (PMAINWIN)(nodes[slot].hwnd); - if (pWin && (pWin->WinType == TYPE_MAINWIN) && - (pWin->pHosting == NULL)){ - if ((pMsgQueue = getMsgQueue((HWND)pWin))) { - POST_MSGQ(pMsgQueue); - } - } - } -#endif /* deprecated code */ + post_quit_to_all_message_threads (); if (__mg_quiting_stage > _MG_QUITING_STAGE_FORCE && __mg_quiting_stage <= _MG_QUITING_STAGE_START) { @@ -2602,13 +2567,15 @@ static LRESULT DesktopWinProc (HWND hWnd, UINT message, && __mg_enter_terminategui) { /* Let Desktop wait for MiniGUIMain() */ __mg_quiting_stage = _MG_QUITING_STAGE_DESKTOP; - } else if (__mg_quiting_stage <= _MG_QUITING_STAGE_DESKTOP) { + } + else if (__mg_quiting_stage <= _MG_QUITING_STAGE_DESKTOP) { PostMessage (HWND_DESKTOP, MSG_ENDSESSION, 0, 0); } } if (MG_UNLIKELY(sg_old_counter == 0)) sg_old_counter = __mg_timer_counter; + /* XXX: no need to check expired timers in desktop thread */ __mg_check_expired_timers (NULL, __mg_timer_counter - sg_old_counter); sg_old_counter = __mg_timer_counter; diff --git a/src/kernel/desktop-ths.c b/src/kernel/desktop-ths.c index 86ff4551..20b6ecfa 100644 --- a/src/kernel/desktop-ths.c +++ b/src/kernel/desktop-ths.c @@ -142,6 +142,16 @@ BOOL mg_InitDesktop (void) #include "debug.h" +static BOOL desktop_idle_handler (MSGQUEUE* msg_queue, BOOL wait) +{ + if (wait) { + __mg_os_time_delay (10); + __mg_update_timer_count (NULL); + } + + return FALSE; +} + void* __kernel_desktop_main (void* data) { MSG Msg; @@ -153,7 +163,7 @@ void* __kernel_desktop_main (void* data) } /* for threads mode, the idle handler for desktop thread is NULL */ - __mg_dsk_msg_queue->OnIdle = NULL; + __mg_dsk_msg_queue->OnIdle = desktop_idle_handler; /* init desktop window */ init_desktop_win (); diff --git a/src/kernel/desktop.c b/src/kernel/desktop.c index 05c41308..d4f1a3f4 100644 --- a/src/kernel/desktop.c +++ b/src/kernel/desktop.c @@ -4833,4 +4833,19 @@ int __mg_join_all_message_threads (void) return nr; } +static inline int post_quit_to_all_message_threads (void) +{ + int nr = 0; + struct list_head *l; + + list_for_each (l, &msg_queue_list) { + MSGQUEUE *msg_queue = (MSGQUEUE*)l; + dump_message_queue (msg_queue, __func__); + POST_MSGQ (msg_queue); + nr++; + } + + return nr; +} + #endif /* defined _MGHAVE_VIRTUAL_WINDOW */ diff --git a/src/kernel/init.c b/src/kernel/init.c index 15d0b3c0..93e90f47 100644 --- a/src/kernel/init.c +++ b/src/kernel/init.c @@ -286,9 +286,6 @@ MSGQUEUE* mg_GetMsgQueueForThisThread (void) */ /************************** System Initialization ****************************/ - -int __mg_timer_init (void); - static BOOL SystemThreads(void) { sem_t wait; @@ -316,7 +313,10 @@ static BOOL SystemThreads(void) sem_wait (&wait); +#if 0 /* deprecated code */ + /* Since 5.0.0, we no longer use the timer thread */ __mg_timer_init (); +#endif /* deprecated code */ // this thread collect low level event from outside, // if there is no event, this thread will suspend to wait a event. @@ -574,6 +574,13 @@ int GUIAPI InitGUI (int args, const char *agr[]) goto failure; } + /* init message queue of main GUI thread */ + step++; + if (!mg_InitTimer (FALSE)) { + _ERR_PRINTF ("failed to start time counter\n"); + goto failure; + } + SetKeyboardLayout ("default"); SetCursor (GetSystemCursor (IDC_ARROW)); @@ -597,11 +604,12 @@ void GUIAPI TerminateGUI (int not_used) { __mg_enter_terminategui = 1; + mg_TerminateTimer (FALSE); + /* Since 5.0.0 */ __mg_join_all_message_threads (); __mg_quiting_stage = _MG_QUITING_STAGE_TIMER; - mg_TerminateTimer (); pthread_join (__mg_desktop, NULL); diff --git a/src/kernel/message.c b/src/kernel/message.c index 0969e39e..c7628aa3 100644 --- a/src/kernel/message.c +++ b/src/kernel/message.c @@ -242,7 +242,7 @@ static BOOL std_idle_handler (MSGQUEUE* msg_queue, BOOL wait) } else { sel_timeout.tv_sec = 0; - sel_timeout.tv_usec = USEC_TIMEOUT; + sel_timeout.tv_usec = USEC_10MS; // USEC_TIMEOUT; } } else { diff --git a/src/kernel/timer.c b/src/kernel/timer.c index 96b66b0a..7f92cf87 100644 --- a/src/kernel/timer.c +++ b/src/kernel/timer.c @@ -69,7 +69,7 @@ #include "ourhdr.h" #include "client.h" #include "sharedres.h" -#endif +#endif /* defined _MGRM_PROCESSES */ #if defined(_DEBUG) && defined(_MGRM_PROCESSES) #define TIMER_ERR_SYS(text) __mg_err_sys (text) @@ -79,78 +79,27 @@ DWORD __mg_timer_counter = 0; -/* timer action for minigui timers */ -static void __mg_timer_action (void *data) +/* update timer count for desktop thread */ +void __mg_update_timer_count (void *data) { #if defined(_MGRM_PROCESSES) SHAREDRES_TIMER_COUNTER = __mg_os_get_time_ms()/10; -#elif defined(_MGRM_THREADS) +#else __mg_timer_counter = __mg_os_get_time_ms()/10; - /* alert desktop */ - AlertDesktopTimerEvent (); -#else /* _MGRM_STANDALONE */ - // do nothing #endif + + /* alert desktop */ + __mg_dsk_msg_queue->dwState |= QS_DESKTIMER; } -/* timer entry for thread version */ -#ifdef _MGRM_THREADS - -#ifdef __AOS__ - -#include "os_api.h" - -static OS_TIMER_ID __mg_os_timer = 0; - -#else /* __AOS__ */ -static void* TimerEntry (void* data) -{ - sem_post ((sem_t*)data); - - while (__mg_quiting_stage > _MG_QUITING_STAGE_TIMER) { - __mg_os_time_delay (10); - __mg_timer_action (NULL); - } - - /* printf("quit from TimerEntry()\n"); */ - return NULL; -} -#endif /* !__AOS__ */ - -int __mg_timer_init (void) -{ - if (!mg_InitTimer ()) { - fprintf (stderr, "KERNEL>timer: Init Timer failure.\n"); - return -1; - } - -#ifdef __AOS__ - __mg_os_timer = tp_os_timer_create ("mgtimer", __mg_timer_action, - NULL, AOS_TIMER_TICKT, - OS_AUTO_ACTIVATE | OS_AUTO_LOAD); -#else /* __AOS__ */ - { - sem_t wait; - sem_init (&wait, 0, 0); - pthread_create (&__mg_timer, NULL, TimerEntry, &wait); - sem_wait (&wait); - sem_destroy (&wait); - } -#endif /* !__AOS__ */ - - return 0; -} - -#else /* defined _MGRM_THREADS */ - #ifdef __NOUNIX__ -BOOL mg_InstallIntervalTimer (void) +static BOOL install_system_timer (void) { return TRUE; } -BOOL mg_UninstallIntervalTimer (void) +static BOOL unintall_system_timer (void) { return TRUE; } @@ -164,7 +113,7 @@ BOOL mg_UninstallIntervalTimer (void) static struct sigaction old_alarm_handler; static struct itimerval old_timer; -BOOL mg_InstallIntervalTimer (void) +static BOOL install_system_timer (void) { struct itimerval timerv; struct sigaction siga; @@ -172,7 +121,7 @@ BOOL mg_InstallIntervalTimer (void) sigaction (SIGALRM, NULL, &old_alarm_handler); siga = old_alarm_handler; - siga.sa_handler = (void(*)(int))__mg_timer_action; + siga.sa_handler = (void(*)(int))__mg_update_timer_count; #ifndef _MGRM_STANDALONE siga.sa_flags = 0; #else @@ -196,7 +145,7 @@ BOOL mg_InstallIntervalTimer (void) return TRUE; } -BOOL mg_UninstallIntervalTimer (void) +static BOOL unintall_system_timer (void) { if (setitimer (ITIMER_REAL, &old_timer, 0) == -1) { TIMER_ERR_SYS ("setitimer call failed!\n"); @@ -212,51 +161,24 @@ BOOL mg_UninstallIntervalTimer (void) } #endif /* not defined __NOUNIX__ */ -#endif /* defined _MGRM_THREADS */ -BOOL mg_InitTimer (void) +BOOL mg_InitTimer (BOOL use_sys_timer) { + __mg_timer_counter = 0; + __mg_os_start_time_ms(); -#ifdef _MGRM_THREADS - __mg_timer_counter = 0; -#else - mg_InstallIntervalTimer (); -#endif + if (use_sys_timer) { + install_system_timer (); + } return TRUE; } -void mg_TerminateTimer (void) +void mg_TerminateTimer (BOOL use_sys_timer) { -#ifdef __AOS__ - tp_os_timer_delete (__mg_os_timer); -#endif - -#ifndef _MGRM_THREADS - mg_UninstallIntervalTimer (); -#else -# ifdef __WINBOND_SWLINUX__ - pthread_detach (__mg_timer); /* XXX: Can we pthread_join()? */ -# else - pthread_join (__mg_timer, NULL); -# endif /* __WINBOND_SWLINUX__ */ -#endif - - /* Since 5.0.0, we allocate timer slots per thread, and manage the time slots - in message queue */ -#if 0 - for (i = 0; i < DEF_NR_TIMERS; i++) { - if (timerstr[i] != NULL) - free ( timerstr[i] ); - timerstr[i] = NULL; - } - -#ifdef _MGHAVE_VIRTUAL_WINDOW - pthread_mutex_destroy (&timerLock); -#endif - -#endif /* deprecated code */ + if (use_sys_timer) + unintall_system_timer (); } /************************* Functions run in message thread *******************/ @@ -550,7 +472,93 @@ DWORD GUIAPI GetTickCount (void) #endif } -#if 0 +#if 0 /* deprecated code */ + +/* Since 5.0.0, we no longer use the timer thread for MiniGUI-Threads runmode */ + in message queue */ + +/* timer entry for thread version */ +#ifdef _MGRM_THREADS + +#ifdef __AOS__ + +#include "os_api.h" + +static OS_TIMER_ID __mg_os_timer = 0; + +#else /* __AOS__ */ +static void* TimerEntry (void* data) +{ + sem_post ((sem_t*)data); + + while (__mg_quiting_stage > _MG_QUITING_STAGE_TIMER) { + __mg_os_time_delay (10); + __mg_update_timer_count (NULL); + } + + /* printf("quit from TimerEntry()\n"); */ + return NULL; +} +#endif /* !__AOS__ */ + +int __mg_timer_init (void) +{ + if (!mg_InitTimer ()) { + fprintf (stderr, "KERNEL>timer: Init Timer failure.\n"); + return -1; + } + +#ifdef __AOS__ + __mg_os_timer = tp_os_timer_create ("mgtimer", __mg_update_timer_count, + NULL, AOS_TIMER_TICKT, + OS_AUTO_ACTIVATE | OS_AUTO_LOAD); +#else /* __AOS__ */ + { + sem_t wait; + sem_init (&wait, 0, 0); + pthread_create (&__mg_timer, NULL, TimerEntry, &wait); + sem_wait (&wait); + sem_destroy (&wait); + } +#endif /* !__AOS__ */ + + return 0; +} + +void mg_TerminateTimer (void) +{ +#ifdef __AOS__ + tp_os_timer_delete (__mg_os_timer); +#endif + +#ifdef __WINBOND_SWLINUX__ + pthread_detach (__mg_timer); /* XXX: Can we pthread_join()? */ +#else + pthread_join (__mg_timer, NULL); +#endif /* __WINBOND_SWLINUX__ */ + +#ifdef _MGHAVE_VIRTUAL_WINDOW + pthread_mutex_destroy (&timerLock); +#endif + +#if 0 /* deprecated code */ + /* Since 5.0.0, we allocate timer slots per thread, and manage the time slots + in message queue */ + for (i = 0; i < DEF_NR_TIMERS; i++) { + if (timerstr[i] != NULL) + free ( timerstr[i] ); + timerstr[i] = NULL; + } + +#ifdef _MGHAVE_VIRTUAL_WINDOW + pthread_mutex_destroy (&timerLock); +#endif +#endif /* deprecated code */ +} + +#else /* defined _MGRM_THREADS */ +#endif /* defined _MGRM_THREADS */ + /* Since 5.0.0, we use timer slots per thread, and manage the time slots in message queue */ static TIMER *timerstr[DEF_NR_TIMERS]; diff --git a/src/server/server.c b/src/server/server.c index 9718c26d..58940bda 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -311,7 +311,7 @@ BOOL GUIAPI ServerStartup (int nr_globals, __mg_dsk_msg_queue->maxfd = listenfd; maxi = -1; - mg_InitTimer (); + mg_InitTimer (TRUE); __mg_start_server_desktop (); @@ -336,7 +336,7 @@ void server_ServerCleanup (void) { __mg_cleanup_layers (); - mg_UninstallIntervalTimer (); + mg_TerminateTimer (TRUE); unlink (CS_PATH); } diff --git a/src/standalone/standalone.c b/src/standalone/standalone.c index 1cf46ede..21f14088 100644 --- a/src/standalone/standalone.c +++ b/src/standalone/standalone.c @@ -79,20 +79,6 @@ extern DWORD __mg_timer_counter; static DWORD old_timer_counter = 0; -#if 0 -/* Since 5.0.0, use RegisterEventHookFunc to implement SetServerEventHook */ -static SRVEVTHOOK srv_evt_hook = NULL; - -SRVEVTHOOK GUIAPI SetServerEventHook (SRVEVTHOOK SrvEvtHook) -{ - SRVEVTHOOK old_hook = srv_evt_hook; - - srv_evt_hook = SrvEvtHook; - - return old_hook; -} -#endif /* deprecated code; moved to window.c */ - static void ParseEvent (PMSGQUEUE msg_que, int event) { LWEVENT lwe; @@ -192,34 +178,17 @@ extern DWORD __mg_os_get_time_ms(void); BOOL GUIAPI salone_StandAloneStartup (void) { -#if 0 - mg_maxfd = 0; - mg_fd_zero (&mg_rfdset); -#endif /* deprecated code since 5.0.0 */ - -#if 0 /* VW: do not use signal based interval timer; since 4.0 */ - mg_InstallIntervalTimer (); -#endif - - __mg_os_start_time_ms (); - + /* VW: do not use signal based interval timer; since 4.0 */ + mg_InitTimer (FALSE); return TRUE; } void salone_StandAloneCleanup (void) { -#if 0 /* VW: do not use signal based interval timer; since 4.0 */ - mg_UninstallIntervalTimer (); -#endif + /* VW: do not use signal based interval timer; since 4.0 */ + mg_TerminateTimer (FALSE); } -#if 0 -BOOL minigui_idle (void) -{ - return salone_IdleHandler4StandAlone (__mg_dsk_msg_queue, TRUE); -} -#endif /* deprecated code */ - BOOL salone_IdleHandler4StandAlone (PMSGQUEUE msg_queue, BOOL wait) { int n; @@ -340,3 +309,22 @@ BOOL salone_IdleHandler4StandAlone (PMSGQUEUE msg_queue, BOOL wait) return (n > 0); } +#if 0 /* deprecated code */ +/* Since 5.0.0, use RegisterEventHookFunc to implement SetServerEventHook */ +static SRVEVTHOOK srv_evt_hook = NULL; + +SRVEVTHOOK GUIAPI SetServerEventHook (SRVEVTHOOK SrvEvtHook) +{ + SRVEVTHOOK old_hook = srv_evt_hook; + + srv_evt_hook = SrvEvtHook; + + return old_hook; +} + +BOOL minigui_idle (void) +{ + return salone_IdleHandler4StandAlone (__mg_dsk_msg_queue, TRUE); +} +#endif /* deprecated code */ +