diff --git a/bochs/bochs.h b/bochs/bochs.h index 06ac95cb9..be850559a 100644 --- a/bochs/bochs.h +++ b/bochs/bochs.h @@ -631,10 +631,11 @@ BX_CPP_INLINE Bit64u bx_bswap64(Bit64u val64) // multithreading support #ifdef WIN32 -#define BX_THREAD_ID(id) DWORD (id) +#define BX_THREAD_VAR(name) HANDLE (name) #define BX_THREAD_FUNC(name,arg) DWORD WINAPI name(LPVOID arg) #define BX_THREAD_EXIT return 0 -#define BX_THREAD_CREATE(name,arg,id) CreateThread(NULL, 0, name, arg, 0, &(id)) +#define BX_THREAD_CREATE(name,arg,var) do { var = CreateThread(NULL, 0, name, arg, 0, NULL); } while (0) +#define BX_THREAD_KILL(var) TerminateThread(var, 0) #define BX_LOCK(mutex) EnterCriticalSection(&(mutex)) #define BX_UNLOCK(mutex) LeaveCriticalSection(&(mutex)) #define BX_MUTEX(mutex) CRITICAL_SECTION (mutex) @@ -642,11 +643,12 @@ BX_CPP_INLINE Bit64u bx_bswap64(Bit64u val64) #define BX_FINI_MUTEX(mutex) DeleteCriticalSection(&(mutex)) #define BX_MSLEEP(val) Sleep(val) #else -#define BX_THREAD_ID(id) pthread_t (id) +#define BX_THREAD_VAR(name) pthread_t (name) #define BX_THREAD_FUNC(name,arg) void name(void* arg) #define BX_THREAD_EXIT pthread_exit(NULL) -#define BX_THREAD_CREATE(name,arg,id) \ - pthread_create(&(id), NULL, (void *(*)(void *))&(name), arg) +#define BX_THREAD_CREATE(name,arg,var) \ + pthread_create(&(var), NULL, (void *(*)(void *))&(name), arg) +#define BX_THREAD_KILL(var) pthread_cancel(var); pthread_join(var, NULL) #define BX_LOCK(mutex) pthread_mutex_lock(&(mutex)); #define BX_UNLOCK(mutex) pthread_mutex_unlock(&(mutex)); #define BX_MUTEX(mutex) pthread_mutex_t (mutex) diff --git a/bochs/gui/rfb.cc b/bochs/gui/rfb.cc index fdfe90329..feff2ea9c 100644 --- a/bochs/gui/rfb.cc +++ b/bochs/gui/rfb.cc @@ -1254,9 +1254,9 @@ end_of_thread: void rfbStartThread() { - BX_THREAD_ID(threadID); + BX_THREAD_VAR(thread_var); - BX_THREAD_CREATE(rfbServerThreadInit, NULL, threadID); + BX_THREAD_CREATE(rfbServerThreadInit, NULL, thread_var); } void HandleRfbClient(SOCKET sClient) diff --git a/bochs/gui/vncsrv.cc b/bochs/gui/vncsrv.cc index 3fae29e73..2a8340633 100644 --- a/bochs/gui/vncsrv.cc +++ b/bochs/gui/vncsrv.cc @@ -1131,9 +1131,9 @@ BX_THREAD_FUNC(vncServerThreadInit, indata) void vncStartThread() { - BX_THREAD_ID(threadID); + BX_THREAD_VAR(thread_var); - BX_THREAD_CREATE(vncServerThreadInit, NULL, threadID); + BX_THREAD_CREATE(vncServerThreadInit, NULL, thread_var); } void DrawBitmap(int x, int y, int width, int height, char *bmap, diff --git a/bochs/iodev/display/voodoo.cc b/bochs/iodev/display/voodoo.cc index 5b1d5fdf2..664859647 100644 --- a/bochs/iodev/display/voodoo.cc +++ b/bochs/iodev/display/voodoo.cc @@ -163,14 +163,12 @@ void CDECL libvoodoo_LTX_plugin_fini(void) BX_THREAD_FUNC(cmdfifo_thread, indata) { UNUSED(indata); - cmdfifo_control = 1; - while (cmdfifo_control > 0) { - while ((cmdfifo_control > 0) && (v->fbi.cmdfifo[0].depth == 0)) { + while (1) { + while (!v->fbi.cmdfifo[0].enable || (v->fbi.cmdfifo[0].depth == 0)) { BX_MSLEEP(1); } cmdfifo_process(); } - cmdfifo_control = -1; BX_THREAD_EXIT; } @@ -186,11 +184,8 @@ bx_voodoo_c::bx_voodoo_c() bx_voodoo_c::~bx_voodoo_c() { - if (cmdfifo_control > 0) { - cmdfifo_control = 0; - while (cmdfifo_control >= 0) { - BX_MSLEEP(1); - } + if (BX_VOODOO_THIS s.model == VOODOO_2) { + BX_THREAD_KILL(cmdfifo_thread_var); BX_FINI_MUTEX(cmdfifo_mutex); } if (v != NULL) { @@ -249,7 +244,7 @@ void bx_voodoo_c::init(void) if (BX_VOODOO_THIS s.model == VOODOO_2) { BX_INIT_MUTEX(cmdfifo_mutex); - BX_THREAD_CREATE(cmdfifo_thread, this, cmdfifo_threadID); + BX_THREAD_CREATE(cmdfifo_thread, this, cmdfifo_thread_var); } BX_INFO(("3dfx Voodoo Graphics adapter (model=%s) initialized", diff --git a/bochs/iodev/display/voodoo_func.h b/bochs/iodev/display/voodoo_func.h index a1850e196..f4d6a4f39 100644 --- a/bochs/iodev/display/voodoo_func.h +++ b/bochs/iodev/display/voodoo_func.h @@ -61,8 +61,7 @@ Bit32u voodoo_last_msg = 255; #define MODIFY_PIXEL(VV) /* cmdfifo thread (Voodoo2) */ -BX_THREAD_ID(cmdfifo_threadID); -int cmdfifo_control = 0; +BX_THREAD_VAR(cmdfifo_thread_var); BX_MUTEX(cmdfifo_mutex); /* fast dither lookup */ @@ -2523,7 +2522,7 @@ Bit32u cmdfifo_r(void) { Bit32u data; - while ((cmdfifo_control > 0) && (v->fbi.cmdfifo[0].depth == 0)) { + while (v->fbi.cmdfifo[0].depth == 0) { BX_MSLEEP(1); } BX_LOCK(cmdfifo_mutex); diff --git a/bochs/iodev/sound/soundlow.cc b/bochs/iodev/sound/soundlow.cc index 3716658eb..e75a6665f 100644 --- a/bochs/iodev/sound/soundlow.cc +++ b/bochs/iodev/sound/soundlow.cc @@ -485,18 +485,18 @@ Bit32u bx_soundlow_waveout_c::resampler_common(audio_buffer_t *inbuffer, float * void bx_soundlow_waveout_c::start_resampler_thread() { - BX_THREAD_ID(threadID); + BX_THREAD_VAR(thread_var); BX_INIT_MUTEX(resampler_mutex); - BX_THREAD_CREATE(resampler_thread, this, threadID); + BX_THREAD_CREATE(resampler_thread, this, thread_var); } void bx_soundlow_waveout_c::start_mixer_thread() { - BX_THREAD_ID(threadID); + BX_THREAD_VAR(thread_var); BX_INIT_MUTEX(mixer_mutex); - BX_THREAD_CREATE(mixer_thread, this, threadID); + BX_THREAD_CREATE(mixer_thread, this, thread_var); } // bx_soundlow_wavein_c class implementation