Minor changes to threading

- Renamed `cpp11_thread.cpp` to `thread.cpp`
- Removed features that are only supported by Win32 threads (`thread_wait` with timeout and mutex with spinlock)
- Fixed formatting in `thread.cpp`
This commit is contained in:
David Hrdlička
2021-12-17 19:59:25 +01:00
parent 149c67335a
commit 7381ab6928
16 changed files with 29 additions and 58 deletions

View File

@@ -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}) device.c nvr.c nvr_at.c nvr_ps2.c ${APP_ICON_MACOSX})
if(CPPTHREADS) if(CPPTHREADS)
target_sources(86Box PRIVATE cpp11_thread.cpp) target_sources(86Box PRIVATE thread.cpp)
endif() endif()
if(APPLE) if(APPLE)

View File

@@ -165,7 +165,7 @@ typedef void event_t;
typedef void mutex_t; typedef void mutex_t;
extern thread_t *thread_create(void (*thread_func)(void *param), void *param); 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 event_t *thread_create_event(void);
extern void thread_set_event(event_t *arg); extern void thread_set_event(event_t *arg);
extern void thread_reset_event(event_t *arg); extern void thread_reset_event(event_t *arg);
@@ -175,7 +175,6 @@ extern void thread_destroy_event(event_t *arg);
#define MUTEX_DEFAULT_SPIN_COUNT 1024 #define MUTEX_DEFAULT_SPIN_COUNT 1024
extern mutex_t *thread_create_mutex(void); 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 void thread_close_mutex(mutex_t *arg);
extern int thread_wait_mutex(mutex_t *arg); extern int thread_wait_mutex(mutex_t *arg);
extern int thread_release_mutex(mutex_t *mutex); extern int thread_release_mutex(mutex_t *mutex);

View File

@@ -346,7 +346,7 @@ void fluidsynth_close(void* p)
data->on = 0; data->on = 0;
thread_set_event(data->event); thread_set_event(data->event);
thread_wait(data->thread_h, -1); thread_wait(data->thread_h);
if (data->synth) { if (data->synth) {
f_delete_fluid_synth(data->synth); f_delete_fluid_synth(data->synth);

View File

@@ -240,7 +240,7 @@ void mt32_close(void* p)
mt32_on = 0; mt32_on = 0;
thread_set_event(event); thread_set_event(event);
thread_wait(thread_h, -1); thread_wait(thread_h);
event = NULL; event = NULL;
start_event = NULL; start_event = NULL;

View File

@@ -506,7 +506,7 @@ sound_cd_thread_end(void)
sound_log("Waiting for CD Audio thread to terminate...\n"); sound_log("Waiting for CD Audio thread to terminate...\n");
thread_set_event(sound_cd_event); 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"); sound_log("CD Audio thread terminated...\n");
if (sound_cd_event) { if (sound_cd_event) {

View File

@@ -22,17 +22,9 @@ thread_create(void (*thread_rout)(void *param), void *param)
return thread; 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 int
thread_wait(thread_t *arg, int timeout) thread_wait(thread_t *arg)
{ {
(void) timeout;
auto thread = reinterpret_cast<std::thread*>(arg); auto thread = reinterpret_cast<std::thread*>(arg);
thread->join(); thread->join();
return 0; return 0;
@@ -49,7 +41,8 @@ int
thread_wait_mutex(mutex_t *_mutex) thread_wait_mutex(mutex_t *_mutex)
{ {
if (_mutex == nullptr) if (_mutex == nullptr)
return(0); return 0;
auto mutex = reinterpret_cast<std::mutex*>(_mutex); auto mutex = reinterpret_cast<std::mutex*>(_mutex);
mutex->lock(); mutex->lock();
return 1; return 1;
@@ -60,7 +53,8 @@ int
thread_release_mutex(mutex_t *_mutex) thread_release_mutex(mutex_t *_mutex)
{ {
if (_mutex == nullptr) if (_mutex == nullptr)
return(0); return 0;
auto mutex = reinterpret_cast<std::mutex*>(_mutex); auto mutex = reinterpret_cast<std::mutex*>(_mutex);
mutex->unlock(); mutex->unlock();
return 1; return 1;

View File

@@ -53,9 +53,9 @@ thread_create(void (*thread_rout)(void *param), void *param)
int 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 int
thread_wait_mutex(mutex_t *_mutex) thread_wait_mutex(mutex_t *_mutex)
{ {

View File

@@ -3459,7 +3459,7 @@ void mach64_close(void *p)
mach64->thread_run = 0; mach64->thread_run = 0;
thread_set_event(mach64->wake_fifo_thread); 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->wake_fifo_thread);
thread_destroy_event(mach64->fifo_not_full_event); thread_destroy_event(mach64->fifo_not_full_event);

View File

@@ -5036,7 +5036,7 @@ mystique_close(void *p)
mystique->thread_run = 0; mystique->thread_run = 0;
thread_set_event(mystique->wake_fifo_thread); 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->wake_fifo_thread);
thread_destroy_event(mystique->fifo_not_full_event); thread_destroy_event(mystique->fifo_not_full_event);
thread_close_mutex(mystique->dma.lock); thread_close_mutex(mystique->dma.lock);

View File

@@ -2624,7 +2624,7 @@ pgc_close(void *priv)
pgc_log("PGC: waiting for thread to stop...\n"); pgc_log("PGC: waiting for thread to stop...\n");
#endif #endif
// while (dev->stopped); // while (dev->stopped);
thread_wait(dev->pgc_thread, -1); thread_wait(dev->pgc_thread);
#ifdef ENABLE_PGC_LOG #ifdef ENABLE_PGC_LOG
pgc_log("PGC: thread stopped, closing up.\n"); pgc_log("PGC: thread stopped, closing up.\n");
#endif #endif

View File

@@ -4097,7 +4097,7 @@ static void s3_virge_close(void *p)
virge->render_thread_run = 0; virge->render_thread_run = 0;
thread_set_event(virge->wake_render_thread); 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->not_full_event);
thread_destroy_event(virge->wake_main_thread); thread_destroy_event(virge->wake_main_thread);
thread_destroy_event(virge->wake_render_thread); thread_destroy_event(virge->wake_render_thread);

View File

@@ -1048,7 +1048,7 @@ void *voodoo_card_init()
voodoo->force_blit_count = 0; voodoo->force_blit_count = 0;
voodoo->can_blit = 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; return voodoo;
} }
@@ -1172,7 +1172,7 @@ void *voodoo_2d3d_card_init(int type)
voodoo->force_blit_count = 0; voodoo->force_blit_count = 0;
voodoo->can_blit = 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; return voodoo;
} }
@@ -1241,22 +1241,22 @@ void voodoo_card_close(voodoo_t *voodoo)
voodoo->fifo_thread_run = 0; voodoo->fifo_thread_run = 0;
thread_set_event(voodoo->wake_fifo_thread); thread_set_event(voodoo->wake_fifo_thread);
thread_wait(voodoo->fifo_thread, -1); thread_wait(voodoo->fifo_thread);
voodoo->render_thread_run[0] = 0; voodoo->render_thread_run[0] = 0;
thread_set_event(voodoo->wake_render_thread[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) { if (voodoo->render_threads >= 2) {
voodoo->render_thread_run[1] = 0; voodoo->render_thread_run[1] = 0;
thread_set_event(voodoo->wake_render_thread[1]); 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) { if (voodoo->render_threads == 4) {
voodoo->render_thread_run[2] = 0; voodoo->render_thread_run[2] = 0;
thread_set_event(voodoo->wake_render_thread[2]); 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; voodoo->render_thread_run[3] = 0;
thread_set_event(voodoo->wake_render_thread[3]); 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->fifo_not_full_event);
thread_destroy_event(voodoo->wake_main_thread); thread_destroy_event(voodoo->wake_main_thread);

View File

@@ -889,7 +889,7 @@ video_close(void)
{ {
thread_run = 0; thread_run = 0;
thread_set_event(blit_data.wake_blit_thread); 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.buffer_not_in_use);
thread_destroy_event(blit_data.blit_complete); thread_destroy_event(blit_data.blit_complete);
thread_destroy_event(blit_data.wake_blit_thread); thread_destroy_event(blit_data.wake_blit_thread);

View File

@@ -374,7 +374,7 @@ MUNTOBJ := midi_mt32.o \
endif endif
ifeq ($(CPPTHREADS), y) ifeq ($(CPPTHREADS), y)
THREADOBJ := cpp11_thread.o THREADOBJ := thread.o
else else
THREADOBJ := win_thread.o THREADOBJ := win_thread.o
endif endif

View File

@@ -947,7 +947,7 @@ void opengl_close(void)
SetEvent(sync_objects.closing); SetEvent(sync_objects.closing);
thread_wait(thread, -1); thread_wait(thread);
thread_close_mutex(resize_info.mutex); thread_close_mutex(resize_info.mutex);
thread_close_mutex(options.mutex); thread_close_mutex(options.mutex);

View File

@@ -45,14 +45,11 @@ thread_create(void (*func)(void *param), void *param)
int int
thread_wait(thread_t *arg, int timeout) thread_wait(thread_t *arg)
{ {
if (arg == NULL) return(0); if (arg == NULL) return(0);
if (timeout == -1) if (WaitForSingleObject(arg, INFINITE)) return(1);
timeout = INFINITE;
if (WaitForSingleObject(arg, timeout)) return(1);
return(0); 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 int
thread_wait_mutex(mutex_t *mutex) thread_wait_mutex(mutex_t *mutex)
{ {