diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f18e0dea6..34118fb4f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -39,7 +39,7 @@ add_executable(86Box WIN32 MACOSX_BUNDLE 86box.c config.c log.c random.c timer.c device.c nvr.c nvr_at.c nvr_ps2.c ${APP_ICON_MACOSX}) if(CPPTHREADS) - target_sources(86Box PRIVATE cpp11_thread.cpp) + target_sources(86Box PRIVATE thread.cpp) endif() if(APPLE) diff --git a/src/include/86box/plat.h b/src/include/86box/plat.h index de5daf1ff..0a2fa72f9 100644 --- a/src/include/86box/plat.h +++ b/src/include/86box/plat.h @@ -186,7 +186,7 @@ typedef void event_t; typedef void mutex_t; extern thread_t *thread_create(void (*thread_func)(void *param), void *param); -extern int thread_wait(thread_t *arg, int timeout); +extern int thread_wait(thread_t *arg); extern event_t *thread_create_event(void); extern void thread_set_event(event_t *arg); extern void thread_reset_event(event_t *arg); @@ -196,7 +196,6 @@ extern void thread_destroy_event(event_t *arg); #define MUTEX_DEFAULT_SPIN_COUNT 1024 extern mutex_t *thread_create_mutex(void); -extern mutex_t *thread_create_mutex_with_spin_count(unsigned int spin_count); extern void thread_close_mutex(mutex_t *arg); extern int thread_wait_mutex(mutex_t *arg); extern int thread_release_mutex(mutex_t *mutex); diff --git a/src/sound/midi_fluidsynth.c b/src/sound/midi_fluidsynth.c index 283188fef..a93618cd4 100644 --- a/src/sound/midi_fluidsynth.c +++ b/src/sound/midi_fluidsynth.c @@ -346,7 +346,7 @@ void fluidsynth_close(void* p) data->on = 0; thread_set_event(data->event); - thread_wait(data->thread_h, -1); + thread_wait(data->thread_h); if (data->synth) { f_delete_fluid_synth(data->synth); diff --git a/src/sound/midi_mt32.c b/src/sound/midi_mt32.c index c41942574..8ade99b51 100644 --- a/src/sound/midi_mt32.c +++ b/src/sound/midi_mt32.c @@ -240,7 +240,7 @@ void mt32_close(void* p) mt32_on = 0; thread_set_event(event); - thread_wait(thread_h, -1); + thread_wait(thread_h); event = NULL; start_event = NULL; diff --git a/src/sound/sound.c b/src/sound/sound.c index b223e706a..4ab9cf58e 100644 --- a/src/sound/sound.c +++ b/src/sound/sound.c @@ -506,7 +506,7 @@ sound_cd_thread_end(void) sound_log("Waiting for CD Audio thread to terminate...\n"); thread_set_event(sound_cd_event); - thread_wait(sound_cd_thread_h, -1); + thread_wait(sound_cd_thread_h); sound_log("CD Audio thread terminated...\n"); if (sound_cd_event) { diff --git a/src/cpp11_thread.cpp b/src/thread.cpp similarity index 88% rename from src/cpp11_thread.cpp rename to src/thread.cpp index 96eb4a2e1..4134befba 100644 --- a/src/cpp11_thread.cpp +++ b/src/thread.cpp @@ -17,22 +17,14 @@ thread_t * thread_create(void (*thread_rout)(void *param), void *param) { auto thread = new std::thread([thread_rout, param] { - thread_rout(param); + thread_rout(param); }); return thread; } -mutex_t * -thread_create_mutex_with_spin_count(unsigned int spin_count) -{ - /* Setting spin count of a mutex is not possible with pthreads. */ - return thread_create_mutex(); -} - int -thread_wait(thread_t *arg, int timeout) +thread_wait(thread_t *arg) { - (void) timeout; auto thread = reinterpret_cast(arg); thread->join(); return 0; @@ -49,7 +41,8 @@ int thread_wait_mutex(mutex_t *_mutex) { if (_mutex == nullptr) - return(0); + return 0; + auto mutex = reinterpret_cast(_mutex); mutex->lock(); return 1; @@ -60,7 +53,8 @@ int thread_release_mutex(mutex_t *_mutex) { if (_mutex == nullptr) - return(0); + return 0; + auto mutex = reinterpret_cast(_mutex); mutex->unlock(); return 1; diff --git a/src/unix/unix_thread.c b/src/unix/unix_thread.c index f045d2820..564b1943b 100644 --- a/src/unix/unix_thread.c +++ b/src/unix/unix_thread.c @@ -53,9 +53,9 @@ thread_create(void (*thread_rout)(void *param), void *param) int -thread_wait(thread_t *arg, int timeout) +thread_wait(thread_t *arg) { - return pthread_join(*(pthread_t*)(arg), NULL) != 0; + return pthread_join(*(pthread_t*)(arg), NULL); } @@ -144,14 +144,6 @@ thread_create_mutex(void) } -mutex_t * -thread_create_mutex_with_spin_count(unsigned int spin_count) -{ - /* Setting spin count of a mutex is not possible with pthreads. */ - return thread_create_mutex(); -} - - int thread_wait_mutex(mutex_t *_mutex) { diff --git a/src/video/vid_ati_mach64.c b/src/video/vid_ati_mach64.c index 0701df498..1e19adf84 100644 --- a/src/video/vid_ati_mach64.c +++ b/src/video/vid_ati_mach64.c @@ -3459,7 +3459,7 @@ void mach64_close(void *p) mach64->thread_run = 0; thread_set_event(mach64->wake_fifo_thread); - thread_wait(mach64->fifo_thread, -1); + thread_wait(mach64->fifo_thread); thread_destroy_event(mach64->wake_fifo_thread); thread_destroy_event(mach64->fifo_not_full_event); diff --git a/src/video/vid_mga.c b/src/video/vid_mga.c index 3d9102b7c..c3f1f3593 100644 --- a/src/video/vid_mga.c +++ b/src/video/vid_mga.c @@ -5036,7 +5036,7 @@ mystique_close(void *p) mystique->thread_run = 0; thread_set_event(mystique->wake_fifo_thread); - thread_wait(mystique->fifo_thread, -1); + thread_wait(mystique->fifo_thread); thread_destroy_event(mystique->wake_fifo_thread); thread_destroy_event(mystique->fifo_not_full_event); thread_close_mutex(mystique->dma.lock); diff --git a/src/video/vid_pgc.c b/src/video/vid_pgc.c index 34e5c2d99..32e8b31ac 100644 --- a/src/video/vid_pgc.c +++ b/src/video/vid_pgc.c @@ -2624,7 +2624,7 @@ pgc_close(void *priv) pgc_log("PGC: waiting for thread to stop...\n"); #endif // while (dev->stopped); - thread_wait(dev->pgc_thread, -1); + thread_wait(dev->pgc_thread); #ifdef ENABLE_PGC_LOG pgc_log("PGC: thread stopped, closing up.\n"); #endif diff --git a/src/video/vid_s3_virge.c b/src/video/vid_s3_virge.c index 23033b5b3..7119e36b3 100644 --- a/src/video/vid_s3_virge.c +++ b/src/video/vid_s3_virge.c @@ -4097,7 +4097,7 @@ static void s3_virge_close(void *p) virge->render_thread_run = 0; thread_set_event(virge->wake_render_thread); - thread_wait(virge->render_thread, -1); + thread_wait(virge->render_thread); thread_destroy_event(virge->not_full_event); thread_destroy_event(virge->wake_main_thread); thread_destroy_event(virge->wake_render_thread); diff --git a/src/video/vid_voodoo.c b/src/video/vid_voodoo.c index a9b0e52c9..a8697f0e8 100644 --- a/src/video/vid_voodoo.c +++ b/src/video/vid_voodoo.c @@ -1048,7 +1048,7 @@ void *voodoo_card_init() voodoo->force_blit_count = 0; voodoo->can_blit = 0; - voodoo->force_blit_mutex = thread_create_mutex_with_spin_count(MUTEX_DEFAULT_SPIN_COUNT); + voodoo->force_blit_mutex = thread_create_mutex(); return voodoo; } @@ -1172,7 +1172,7 @@ void *voodoo_2d3d_card_init(int type) voodoo->force_blit_count = 0; voodoo->can_blit = 0; - voodoo->force_blit_mutex = thread_create_mutex_with_spin_count(MUTEX_DEFAULT_SPIN_COUNT); + voodoo->force_blit_mutex = thread_create_mutex(); return voodoo; } @@ -1241,22 +1241,22 @@ void voodoo_card_close(voodoo_t *voodoo) voodoo->fifo_thread_run = 0; thread_set_event(voodoo->wake_fifo_thread); - thread_wait(voodoo->fifo_thread, -1); + thread_wait(voodoo->fifo_thread); voodoo->render_thread_run[0] = 0; thread_set_event(voodoo->wake_render_thread[0]); - thread_wait(voodoo->render_thread[0], -1); + thread_wait(voodoo->render_thread[0]); if (voodoo->render_threads >= 2) { voodoo->render_thread_run[1] = 0; thread_set_event(voodoo->wake_render_thread[1]); - thread_wait(voodoo->render_thread[1], -1); + thread_wait(voodoo->render_thread[1]); } if (voodoo->render_threads == 4) { voodoo->render_thread_run[2] = 0; thread_set_event(voodoo->wake_render_thread[2]); - thread_wait(voodoo->render_thread[2], -1); + thread_wait(voodoo->render_thread[2]); voodoo->render_thread_run[3] = 0; thread_set_event(voodoo->wake_render_thread[3]); - thread_wait(voodoo->render_thread[3], -1); + thread_wait(voodoo->render_thread[3]); } thread_destroy_event(voodoo->fifo_not_full_event); thread_destroy_event(voodoo->wake_main_thread); diff --git a/src/video/video.c b/src/video/video.c index e3ff2200c..532995cb3 100644 --- a/src/video/video.c +++ b/src/video/video.c @@ -889,7 +889,7 @@ video_close(void) { thread_run = 0; thread_set_event(blit_data.wake_blit_thread); - thread_wait(blit_data.blit_thread, -1); + thread_wait(blit_data.blit_thread); thread_destroy_event(blit_data.buffer_not_in_use); thread_destroy_event(blit_data.blit_complete); thread_destroy_event(blit_data.wake_blit_thread); diff --git a/src/win/Makefile.mingw b/src/win/Makefile.mingw index 6a596beae..c3d7bb834 100644 --- a/src/win/Makefile.mingw +++ b/src/win/Makefile.mingw @@ -374,7 +374,7 @@ MUNTOBJ := midi_mt32.o \ endif ifeq ($(CPPTHREADS), y) -THREADOBJ := cpp11_thread.o +THREADOBJ := thread.o else THREADOBJ := win_thread.o endif diff --git a/src/win/win_opengl.c b/src/win/win_opengl.c index bb9afd7be..4f328c097 100644 --- a/src/win/win_opengl.c +++ b/src/win/win_opengl.c @@ -947,7 +947,7 @@ void opengl_close(void) SetEvent(sync_objects.closing); - thread_wait(thread, -1); + thread_wait(thread); thread_close_mutex(resize_info.mutex); thread_close_mutex(options.mutex); diff --git a/src/win/win_thread.c b/src/win/win_thread.c index f8d81fa86..5a0fc4be8 100644 --- a/src/win/win_thread.c +++ b/src/win/win_thread.c @@ -45,14 +45,11 @@ thread_create(void (*func)(void *param), void *param) int -thread_wait(thread_t *arg, int timeout) +thread_wait(thread_t *arg) { if (arg == NULL) return(0); - if (timeout == -1) - timeout = INFINITE; - - if (WaitForSingleObject(arg, timeout)) return(1); + if (WaitForSingleObject(arg, INFINITE)) return(1); return(0); } @@ -133,17 +130,6 @@ thread_create_mutex(void) } -mutex_t * -thread_create_mutex_with_spin_count(unsigned int spin_count) -{ - mutex_t *mutex = malloc(sizeof(CRITICAL_SECTION)); - - InitializeCriticalSectionAndSpinCount(mutex, spin_count); - - return mutex; -} - - int thread_wait_mutex(mutex_t *mutex) {