From d315dfb5b1a0852d8665ef88042a9272d0b14ac5 Mon Sep 17 00:00:00 2001 From: Magnus Holmgren Date: Tue, 19 Jul 2005 20:43:21 +0000 Subject: Made Win32 thread management similar to the X11 one (previously yield() could make a thread stop, seemingly forever). git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7202 a1c6a512-1295-4272-9138-f99709370657 --- uisimulator/win32/kernel.c | 12 ------------ uisimulator/win32/thread-win32.c | 25 +++++++++++++++++++++++++ uisimulator/win32/thread-win32.h | 1 + uisimulator/win32/uisw32.c | 10 +--------- uisimulator/win32/uisw32.h | 1 - 5 files changed, 27 insertions(+), 22 deletions(-) (limited to 'uisimulator') diff --git a/uisimulator/win32/kernel.c b/uisimulator/win32/kernel.c index 5de436007b..8e7bb88546 100644 --- a/uisimulator/win32/kernel.c +++ b/uisimulator/win32/kernel.c @@ -34,18 +34,6 @@ int set_irq_level (int level) return (_lv = level); } -void sim_sleep(int ticks) -{ - Sleep (1000 / HZ * ticks); -} - - -void yield (void) -{ - Sleep (1); /* prevent busy loop */ - PostThreadMessage (GetWindowThreadProcessId (hGUIWnd,NULL), TM_YIELD, 0, 0); -} - void queue_init(struct event_queue *q) { q->read = 0; diff --git a/uisimulator/win32/thread-win32.c b/uisimulator/win32/thread-win32.c index ca973b6662..5c452d7735 100644 --- a/uisimulator/win32/thread-win32.c +++ b/uisimulator/win32/thread-win32.c @@ -17,18 +17,41 @@ * ****************************************************************************/ +#define WINDOWS_LEAN_AND_MEAN #include #include "thread-win32.h" +#include "kernel.h" HANDLE lpThreads[256]; int nThreads = 0, nPos = 0; long current_tick = 0; +CRITICAL_SECTION CriticalSection; +void yield(void) +{ + LeaveCriticalSection(&CriticalSection); + /* Don't need a sleep here, and it can be bad, e.g., for audio playback. + * Increases CPU usage a lot though. Only sleep if CPU isn't boosted + * could be a solution. + */ + /* Sleep(1); */ + EnterCriticalSection(&CriticalSection); +} + +void sim_sleep(int ticks) +{ + LeaveCriticalSection(&CriticalSection); + Sleep((1000/HZ) * ticks); + EnterCriticalSection(&CriticalSection); +} + DWORD WINAPI runthread (LPVOID lpParameter) { + EnterCriticalSection(&CriticalSection); ((void(*)())lpParameter) (); + LeaveCriticalSection(&CriticalSection); return 0; } @@ -54,4 +77,6 @@ int create_thread(void (*fp)(void), void* sp, int stk_size) void init_threads(void) { + InitializeCriticalSection(&CriticalSection); + EnterCriticalSection(&CriticalSection); } diff --git a/uisimulator/win32/thread-win32.h b/uisimulator/win32/thread-win32.h index 2d26b8db21..467c47ee39 100644 --- a/uisimulator/win32/thread-win32.h +++ b/uisimulator/win32/thread-win32.h @@ -20,3 +20,4 @@ extern HANDLE lpThreads[256]; extern int nPos, nThreads; +extern CRITICAL_SECTION CriticalSection; diff --git a/uisimulator/win32/uisw32.c b/uisimulator/win32/uisw32.c index 02697be443..f492b9de36 100644 --- a/uisimulator/win32/uisw32.c +++ b/uisimulator/win32/uisw32.c @@ -262,10 +262,9 @@ int GUIDown () DestroyWindow (hGUIWnd); CloseHandle (hGUIThread); + for (i = 0; i < nThreads; i++) { - ResumeThread (lpThreads[i]); - WaitForSingleObject (lpThreads[i], 1); CloseHandle (lpThreads[i]); } return 0; @@ -280,13 +279,6 @@ void GUIMessageLoop () { TranslateMessage (&msg); DispatchMessage (&msg); - if (msg.message == TM_YIELD) - { - SuspendThread (lpThreads[nPos]); - if (++nPos >= nThreads) - nPos = 0; - ResumeThread (lpThreads[nPos]); - } } } diff --git a/uisimulator/win32/uisw32.h b/uisimulator/win32/uisw32.h index 7d46067c60..c4ce761c80 100644 --- a/uisimulator/win32/uisw32.h +++ b/uisimulator/win32/uisw32.h @@ -107,7 +107,6 @@ typedef unsigned short wchar_t; #endif -#define TM_YIELD WM_USER + 101 // thread message for yield #define TIMER_EVENT 0x34928340 extern HWND hGUIWnd; // the GUI window handle -- cgit v1.2.3