Bochs thread handling improvements:

- Modified win32 thread macros to use the handle instead of the id.
- Added new macro BX_THREAD_KILL to terminate threads.
Voodoo thread changes:
- Let the cmdfifo_thread sleep while the CMDFIFO is disabled.
- Kill the cmdfifo_thread in destructor and removed old thread control code.
This commit is contained in:
Volker Ruppert 2017-05-18 18:34:00 +00:00
parent 495a8afeac
commit c160fd4830
6 changed files with 22 additions and 26 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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,

View File

@ -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",

View File

@ -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);

View File

@ -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