diff --git a/CMakeLists.txt b/CMakeLists.txt index 59a3b34f8..10f14a967 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,10 +88,6 @@ CMAKE_DEPENDENT_OPTION(VNC "VNC renderer" ON "DEV_BRANCH" OFF) CMAKE_DEPENDENT_OPTION(XL24 "ATI VGA Wonder XL24 (ATI-28800-6)" ON "DEV_BRANCH" OFF) CMAKE_DEPENDENT_OPTION(VECT486VL "HP Vectra 486VL" ON "DEV_BRANCH" OFF) -if(WIN32) - option(PTHREAD "Use POSIX threads (pthreads) instead of Win32 threads" ON) -endif() - # HACK: Avoid a MSVC2019 compiler bug on ARM64 Debug builds if(MSVC_TOOLSET_VERSION GREATER_EQUAL 142 AND ARCH STREQUAL "arm64") # Define a cache option in case somebody wants to disable this workaround diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a400eaeb6..1d13c7145 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -47,22 +47,6 @@ if(VNC) endif() endif() -if(NOT WIN32 OR PTHREAD) - target_sources(86Box PRIVATE thread.c) - if(WIN32 AND VCPKG_TOOLCHAIN) - find_package(pthreads REQUIRED) - if (PThreads4W_FOUND) - target_link_libraries(86Box PThreads4W::PThreads4W) - else() - target_link_libraries(86Box pthreads) - endif() - else() - set(THREADS_PREFER_PTHREAD_FLAG TRUE) - find_package(Threads REQUIRED) - target_link_libraries(86Box Threads::Threads) - endif() -endif() - target_link_libraries(86Box cpu chipset mch dev mem fdd game cdrom zip mo hdd net print scsi sio snd vid voodoo plat ui) diff --git a/src/unix/CMakeLists.txt b/src/unix/CMakeLists.txt index 193de96c0..d0524e38b 100644 --- a/src/unix/CMakeLists.txt +++ b/src/unix/CMakeLists.txt @@ -8,10 +8,14 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Linux") else() set(PLAT_SOURCES unix_midi.c) endif() -add_library(plat STATIC ${PLAT_SOURCES}) +add_library(plat STATIC ${PLAT_SOURCES} unix_thread.c) add_library(ui STATIC unix.c unix_sdl.c unix_cdrom.c) target_compile_definitions(ui PUBLIC _FILE_OFFSET_BITS=64) target_link_libraries(ui dl) if (ALSA_FOUND) target_link_libraries(plat ALSA::ALSA) -endif() \ No newline at end of file +endif() + +set(THREADS_PREFER_PTHREAD_FLAG TRUE) +find_package(Threads REQUIRED) +target_link_libraries(86Box Threads::Threads) \ No newline at end of file diff --git a/src/thread.c b/src/unix/unix_thread.c similarity index 50% rename from src/thread.c rename to src/unix/unix_thread.c index c6d3a6edd..d95c337d1 100644 --- a/src/thread.c +++ b/src/unix/unix_thread.c @@ -1,191 +1,52 @@ -#include -#include #include +#include #include -#ifdef __APPLE__ -#include -#endif +#include +#include #include <86box/86box.h> #include <86box/plat.h> -#if (defined WIN32) || (defined _WIN32) || (defined _WIN32) -#include -#include - -typedef struct { - HANDLE handle; -} win_event_t; - - -thread_t * -thread_create(void (*func)(void *param), void *param) -{ - uintptr_t bt = _beginthread(func, 0, param); - return((thread_t *)bt); -} - - -int -thread_wait(thread_t *arg, int timeout) -{ - if (arg == NULL) return(0); - - if (timeout == -1) - timeout = INFINITE; - - if (WaitForSingleObject(arg, timeout)) return(1); - - return(0); -} - - -event_t * -thread_create_event(void) -{ - win_event_t *ev = malloc(sizeof(win_event_t)); - - ev->handle = CreateEvent(NULL, FALSE, FALSE, NULL); - - return((event_t *)ev); -} - - -void -thread_set_event(event_t *arg) -{ - win_event_t *ev = (win_event_t *)arg; - - if (arg == NULL) return; - - SetEvent(ev->handle); -} - - -void -thread_reset_event(event_t *arg) -{ - win_event_t *ev = (win_event_t *)arg; - - if (arg == NULL) return; - - ResetEvent(ev->handle); -} - - -int -thread_wait_event(event_t *arg, int timeout) -{ - win_event_t *ev = (win_event_t *)arg; - - if (arg == NULL) return(0); - - if (ev->handle == NULL) return(0); - - if (timeout == -1) - timeout = INFINITE; - - if (WaitForSingleObject(ev->handle, timeout)) return(1); - - return(0); -} - - -void -thread_destroy_event(event_t *arg) -{ - win_event_t *ev = (win_event_t *)arg; - - if (arg == NULL) return; - - CloseHandle(ev->handle); - - free(ev); -} - - -mutex_t * -thread_create_mutex(void) -{ - mutex_t *mutex = malloc(sizeof(CRITICAL_SECTION)); - - InitializeCriticalSection(mutex); - - return mutex; -} - - -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) -{ - if (mutex == NULL) return(0); - - LPCRITICAL_SECTION critsec = (LPCRITICAL_SECTION)mutex; - - EnterCriticalSection(critsec); - - return 1; -} - - -int -thread_release_mutex(mutex_t *mutex) -{ - if (mutex == NULL) return(0); - - LPCRITICAL_SECTION critsec = (LPCRITICAL_SECTION)mutex; - - LeaveCriticalSection(critsec); - - return 1; -} - - -void -thread_close_mutex(mutex_t *mutex) -{ - if (mutex == NULL) return; - - LPCRITICAL_SECTION critsec = (LPCRITICAL_SECTION)mutex; - - DeleteCriticalSection(critsec); - - free(critsec); -} -#else -#include -#include typedef struct event_pthread_t { pthread_cond_t cond; pthread_mutex_t mutex; - int state; + int state; } event_pthread_t; +typedef struct thread_param +{ + void (*thread_rout)(void*); + void * param; +} thread_param; + + typedef struct pt_mutex_t { pthread_mutex_t mutex; } pt_mutex_t; +void * +thread_run_wrapper(thread_param* arg) +{ + thread_param localparam = *arg; + free(arg); + localparam.thread_rout(localparam.param); + return NULL; +} + + thread_t * thread_create(void (*thread_rout)(void *param), void *param) { pthread_t *thread = malloc(sizeof(pthread_t)); + thread_param *thrparam = malloc(sizeof(thread_param)); + thrparam->thread_rout = thread_rout; + thrparam->param = param; - pthread_create(thread, NULL, (void*)thread_rout, param); + pthread_create(thread, NULL, (void* (*)(void*))thread_run_wrapper, thrparam); return thread; } @@ -240,13 +101,10 @@ thread_wait_event(event_t *handle, int timeout) event_pthread_t *event = (event_pthread_t *)handle; struct timespec abstime; -#ifdef __linux__ - clock_gettime(CLOCK_REALTIME, &abstime); +#ifdef HAS_TIMESPEC_GET + timespec_get(&abstime, TIME_UTC); #else - struct timeval now; - gettimeofday(&now, 0); - abstime.tv_sec = now.tv_sec; - abstime.tv_nsec = now.tv_usec*1000UL; + clock_gettime(CLOCK_REALTIME, &abstime); #endif abstime.tv_nsec += (timeout % 1000) * 1000000; abstime.tv_sec += (timeout / 1000); @@ -301,22 +159,23 @@ thread_create_mutex_with_spin_count(unsigned int spin_count) int thread_wait_mutex(mutex_t *_mutex) { + if (_mutex == NULL) + return(0); pt_mutex_t *mutex = (pt_mutex_t *)_mutex; - pthread_mutex_lock(&mutex->mutex); - - return 1; + return + pthread_mutex_lock(&mutex->mutex) != 0; } int thread_release_mutex(mutex_t *_mutex) { + if (_mutex == NULL) + return(0); pt_mutex_t *mutex = (pt_mutex_t *)_mutex; - pthread_mutex_unlock(&mutex->mutex); - - return 1; + return pthread_mutex_unlock(&mutex->mutex) != 0; } @@ -329,4 +188,3 @@ thread_close_mutex(mutex_t *_mutex) free(mutex); } -#endif diff --git a/src/video/vid_s3.c b/src/video/vid_s3.c index f13fbf980..f9684a731 100644 --- a/src/video/vid_s3.c +++ b/src/video/vid_s3.c @@ -1847,11 +1847,11 @@ s3_hwcursor_draw(svga_t *svga, int displine) if (svga->crtc[0x55] & 0x10) { /*X11*/ for (xx = 0; xx < 16; xx++) { - if (offset >= svga->hwcursor_latch.x) { + if (offset >= 0) { if (dat[0] & 0x8000) buffer32->line[displine][offset + svga->x_add] = (dat[1] & 0x8000) ? fg : bg; } - + offset++; dat[0] <<= shift; dat[1] <<= shift; @@ -1859,16 +1859,16 @@ s3_hwcursor_draw(svga_t *svga, int displine) } else { /*Windows*/ for (xx = 0; xx < width; xx++) { - if (offset >= svga->hwcursor_latch.x) { + if (offset >= 0) { if (!(dat[0] & 0x8000)) buffer32->line[displine][offset + svga->x_add] = (dat[1] & 0x8000) ? fg : bg; else if (dat[1] & 0x8000) buffer32->line[displine][offset + svga->x_add] ^= 0xffffff; - } - - offset++; - dat[0] <<= shift; - dat[1] <<= shift; + } + + offset++; + dat[0] <<= shift; + dat[1] <<= shift; } } svga->hwcursor_latch.addr += 4; diff --git a/src/video/vid_s3_virge.c b/src/video/vid_s3_virge.c index eaaa55ebd..db12879ec 100644 --- a/src/video/vid_s3_virge.c +++ b/src/video/vid_s3_virge.c @@ -3316,7 +3316,7 @@ static void s3_virge_hwcursor_draw(svga_t *svga, int displine) /*X11*/ for (xx = 0; xx < 16; xx++) { - if (offset >= svga->hwcursor_latch.x) + if (offset >= 0) { if (dat[0] & 0x8000) buffer32->line[displine][offset + svga->x_add] = (dat[1] & 0x8000) ? fg : bg; @@ -3332,7 +3332,7 @@ static void s3_virge_hwcursor_draw(svga_t *svga, int displine) /*Windows*/ for (xx = 0; xx < 16; xx++) { - if (offset >= svga->hwcursor_latch.x) + if (offset >= 0) { if (!(dat[0] & 0x8000)) buffer32->line[displine][offset + svga->x_add] = (dat[1] & 0x8000) ? fg : bg; diff --git a/src/win/CMakeLists.txt b/src/win/CMakeLists.txt index 527f64f51..e1b3fd942 100644 --- a/src/win/CMakeLists.txt +++ b/src/win/CMakeLists.txt @@ -15,7 +15,7 @@ enable_language(RC) -add_library(plat OBJECT win.c win_dynld.c win_cdrom.c +add_library(plat OBJECT win.c win_dynld.c win_cdrom.c win_thread.c win_keyboard.c win_crashdump.c win_midi.c win_mouse.c) add_library(ui OBJECT win_ui.c win_stbar.c win_sdl.c win_dialog.c win_about.c @@ -58,9 +58,5 @@ if(OPENGL) target_sources(ui PRIVATE glad.c win_opengl.c win_opengl_glslp.c) endif() -if(NOT PTHREAD) - target_sources(plat PRIVATE win_thread.c) -endif() - target_link_libraries(86Box advapi32 comctl32 comdlg32 gdi32 shell32 iphlpapi dxguid imm32 hid setupapi uxtheme version winmm psapi) diff --git a/src/win/Makefile.mingw b/src/win/Makefile.mingw index 90f7ab19e..ba4ecef21 100644 --- a/src/win/Makefile.mingw +++ b/src/win/Makefile.mingw @@ -26,10 +26,6 @@ ifndef DEV_BUILD DEV_BUILD := n endif -ifneq ($(PTHREAD), n) - PTHREAD := y -endif - ifeq ($(DEV_BUILD), y) ifndef DEBUG DEBUG := y @@ -589,17 +585,10 @@ CXXFLAGS := $(CFLAGS) ######################################################################### # Create the (final) list of objects to build. # ######################################################################### -ifeq ($(PTHREAD), y) -MAINOBJ := 86box.o config.o log.o random.o timer.o io.o acpi.o apm.o dma.o ddma.o \ - nmi.o pic.o pit.o port_6x.o port_92.o ppi.o pci.o mca.o \ - usb.o device.o nvr.o nvr_at.o nvr_ps2.o thread.o \ - $(VNCOBJ) -else MAINOBJ := 86box.o config.o log.o random.o timer.o io.o acpi.o apm.o dma.o ddma.o \ nmi.o pic.o pit.o port_6x.o port_92.o ppi.o pci.o mca.o \ usb.o device.o nvr.o nvr_at.o nvr_ps2.o \ $(VNCOBJ) -endif MEMOBJ := catalyst_flash.o i2c_eeprom.o intel_flash.o mem.o rom.o smram.o spd.o sst_flash.o @@ -801,19 +790,11 @@ VOODOOOBJ := vid_voodoo.o vid_voodoo_banshee.o \ vid_voodoo_render.o vid_voodoo_setup.o \ vid_voodoo_texture.o -ifeq ($(PTHREAD), y) -PLATOBJ := win.o \ - win_dynld.o \ - win_cdrom.o win_keyboard.o \ - win_crashdump.o win_midi.o \ - win_mouse.o -else PLATOBJ := win.o \ win_dynld.o win_thread.o \ win_cdrom.o win_keyboard.o \ win_crashdump.o win_midi.o \ win_mouse.o -endif ifeq ($(DINPUT), y) PLATOBJ += win_joystick.o @@ -839,11 +820,7 @@ endif ifneq ($(WX), n) LIBS += $(WX_LIBS) -lm endif -ifeq ($(PTHREAD), y) -LIBS += -lpng -lz -lwsock32 -lshell32 -liphlpapi -lpsapi -lSDL2 -limm32 -lhid -lsetupapi -loleaut32 -luxtheme -lversion -lwinmm -static -lstdc++ -lpthread -else LIBS += -lpng -lz -lwsock32 -lshell32 -liphlpapi -lpsapi -lSDL2 -limm32 -lhid -lsetupapi -loleaut32 -luxtheme -lversion -lwinmm -static -lstdc++ -endif ifneq ($(X64), y) ifneq ($(ARM64), y) LIBS += -Wl,--large-address-aware