mirror of
https://github.com/86Box/86Box.git
synced 2025-01-23 01:31:51 -05:00
Switched threads to pthread for all platforms (on Windows, you can compile with Win32 threads using PTHREAD=n).
This commit is contained in:
parent
0e8348f0e2
commit
3c2ac29c68
6 changed files with 223 additions and 180 deletions
|
@ -16,7 +16,7 @@
|
|||
# WIN32 marks us as a GUI app on Windows
|
||||
add_executable(86Box WIN32 86box.c config.c random.c timer.c io.c acpi.c apm.c
|
||||
dma.c ddma.c nmi.c pic.c pit.c port_92.c ppi.c pci.c mca.c usb.c
|
||||
device.c nvr.c nvr_at.c nvr_ps2.c)
|
||||
device.c nvr.c nvr_at.c nvr_ps2.c thread.c)
|
||||
|
||||
if(NEW_DYNAREC)
|
||||
add_compile_definitions(USE_NEW_DYNAREC)
|
||||
|
|
195
src/thread.c
Normal file
195
src/thread.c
Normal file
|
@ -0,0 +1,195 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/param.h>
|
||||
#include <unistd.h>
|
||||
#include <inttypes.h>
|
||||
#include <86box/86box.h>
|
||||
#include <86box/plat.h>
|
||||
|
||||
|
||||
typedef struct event_pthread_t
|
||||
{
|
||||
pthread_cond_t cond;
|
||||
pthread_mutex_t mutex;
|
||||
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* (*)(void*))thread_run_wrapper, thrparam);
|
||||
|
||||
return thread;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
thread_wait(thread_t *arg, int timeout)
|
||||
{
|
||||
return pthread_join(*(pthread_t*)(arg), NULL) != 0;
|
||||
}
|
||||
|
||||
|
||||
event_t *
|
||||
thread_create_event()
|
||||
{
|
||||
event_pthread_t *event = malloc(sizeof(event_pthread_t));
|
||||
|
||||
pthread_cond_init(&event->cond, NULL);
|
||||
pthread_mutex_init(&event->mutex, NULL);
|
||||
event->state = 0;
|
||||
|
||||
return (event_t *)event;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
thread_set_event(event_t *handle)
|
||||
{
|
||||
event_pthread_t *event = (event_pthread_t *)handle;
|
||||
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
event->state = 1;
|
||||
pthread_cond_broadcast(&event->cond);
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
thread_reset_event(event_t *handle)
|
||||
{
|
||||
event_pthread_t *event = (event_pthread_t *)handle;
|
||||
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
event->state = 0;
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
thread_wait_event(event_t *handle, int timeout)
|
||||
{
|
||||
event_pthread_t *event = (event_pthread_t *)handle;
|
||||
struct timespec abstime;
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &abstime);
|
||||
abstime.tv_nsec += (timeout % 1000) * 1000000;
|
||||
abstime.tv_sec += (timeout / 1000);
|
||||
if (abstime.tv_nsec > 1000000000) {
|
||||
abstime.tv_nsec -= 1000000000;
|
||||
abstime.tv_sec++;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
if (timeout == -1) {
|
||||
while (!event->state)
|
||||
pthread_cond_wait(&event->cond, &event->mutex);
|
||||
} else if (!event->state)
|
||||
pthread_cond_timedwait(&event->cond, &event->mutex, &abstime);
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
thread_destroy_event(event_t *handle)
|
||||
{
|
||||
event_pthread_t *event = (event_pthread_t *)handle;
|
||||
|
||||
pthread_cond_destroy(&event->cond);
|
||||
pthread_mutex_destroy(&event->mutex);
|
||||
|
||||
free(event);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
thread_sleep(int t)
|
||||
{
|
||||
usleep(t * 1000);
|
||||
}
|
||||
|
||||
|
||||
mutex_t *
|
||||
thread_create_mutex(void)
|
||||
{
|
||||
pt_mutex_t *mutex = malloc(sizeof(pt_mutex_t));
|
||||
|
||||
pthread_mutex_init(&mutex->mutex, NULL);
|
||||
|
||||
return mutex;
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
if (_mutex == NULL)
|
||||
return(0);
|
||||
pt_mutex_t *mutex = (pt_mutex_t *)_mutex;
|
||||
|
||||
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;
|
||||
|
||||
return pthread_mutex_unlock(&mutex->mutex) != 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
thread_close_mutex(mutex_t *_mutex)
|
||||
{
|
||||
pt_mutex_t *mutex = (pt_mutex_t *)_mutex;
|
||||
|
||||
pthread_mutex_destroy(&mutex->mutex);
|
||||
|
||||
free(mutex);
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
set(PLAT_SOURCES unix_thread.c)
|
||||
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
|
||||
find_package(ALSA)
|
||||
if (ALSA_FOUND)
|
||||
set(PLAT_SOURCES ${PLAT_SOURCES} linux_midi_alsa.c)
|
||||
set(PLAT_SOURCES linux_midi_alsa.c)
|
||||
else()
|
||||
set(PLAT_SOURCES ${PLAT_SOURCES} unix_midi.c)
|
||||
set(PLAT_SOURCES unix_midi.c)
|
||||
endif()
|
||||
else()
|
||||
set(PLAT_SOURCES ${PLAT_SOURCES} unix_midi.c)
|
||||
set(PLAT_SOURCES unix_midi.c)
|
||||
endif()
|
||||
add_library(plat STATIC ${PLAT_SOURCES})
|
||||
add_library(ui STATIC unix.c unix_sdl.c unix_cdrom.c)
|
||||
|
|
|
@ -1,174 +0,0 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/param.h>
|
||||
#include <unistd.h>
|
||||
#include <inttypes.h>
|
||||
#include <86box/86box.h>
|
||||
#include <86box/plat.h>
|
||||
|
||||
typedef struct event_pthread_t
|
||||
{
|
||||
pthread_cond_t cond;
|
||||
pthread_mutex_t mutex;
|
||||
int state;
|
||||
} event_pthread_t;
|
||||
|
||||
typedef struct thread_param
|
||||
{
|
||||
void (*thread_rout)(void*);
|
||||
void* param;
|
||||
} thread_param;
|
||||
|
||||
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* (*)(void*))thread_run_wrapper, thrparam);
|
||||
|
||||
return thread;
|
||||
}
|
||||
|
||||
int
|
||||
thread_wait(thread_t *arg, int timeout)
|
||||
{
|
||||
return pthread_join(*(pthread_t*)(arg), NULL) != 0;
|
||||
}
|
||||
|
||||
event_t *thread_create_event()
|
||||
{
|
||||
event_pthread_t *event = malloc(sizeof(event_pthread_t));
|
||||
|
||||
pthread_cond_init(&event->cond, NULL);
|
||||
pthread_mutex_init(&event->mutex, NULL);
|
||||
event->state = 0;
|
||||
|
||||
return (event_t *)event;
|
||||
}
|
||||
|
||||
void thread_set_event(event_t *handle)
|
||||
{
|
||||
event_pthread_t *event = (event_pthread_t *)handle;
|
||||
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
event->state = 1;
|
||||
pthread_cond_broadcast(&event->cond);
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
}
|
||||
|
||||
void thread_reset_event(event_t *handle)
|
||||
{
|
||||
event_pthread_t *event = (event_pthread_t *)handle;
|
||||
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
event->state = 0;
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
}
|
||||
|
||||
int thread_wait_event(event_t *handle, int timeout)
|
||||
{
|
||||
event_pthread_t *event = (event_pthread_t *)handle;
|
||||
struct timespec abstime;
|
||||
|
||||
#if defined __linux__ || defined BSD
|
||||
clock_gettime(CLOCK_REALTIME, &abstime);
|
||||
#else
|
||||
struct timeval now;
|
||||
gettimeofday(&now, 0);
|
||||
abstime.tv_sec = now.tv_sec;
|
||||
abstime.tv_nsec = now.tv_usec*1000UL;
|
||||
#endif
|
||||
abstime.tv_nsec += (timeout % 1000) * 1000000;
|
||||
abstime.tv_sec += (timeout / 1000);
|
||||
if (abstime.tv_nsec > 1000000000)
|
||||
{
|
||||
abstime.tv_nsec -= 1000000000;
|
||||
abstime.tv_sec++;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
if (timeout == -1)
|
||||
{
|
||||
while (!event->state)
|
||||
pthread_cond_wait(&event->cond, &event->mutex);
|
||||
}
|
||||
else if (!event->state)
|
||||
pthread_cond_timedwait(&event->cond, &event->mutex, &abstime);
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void thread_destroy_event(event_t *handle)
|
||||
{
|
||||
event_pthread_t *event = (event_pthread_t *)handle;
|
||||
|
||||
pthread_cond_destroy(&event->cond);
|
||||
pthread_mutex_destroy(&event->mutex);
|
||||
|
||||
free(event);
|
||||
}
|
||||
|
||||
void thread_sleep(int t)
|
||||
{
|
||||
usleep(t * 1000);
|
||||
}
|
||||
|
||||
|
||||
typedef struct pt_mutex_t
|
||||
{
|
||||
pthread_mutex_t mutex;
|
||||
} pt_mutex_t;
|
||||
|
||||
mutex_t *thread_create_mutex(void)
|
||||
{
|
||||
pt_mutex_t *mutex = malloc(sizeof(pt_mutex_t));
|
||||
|
||||
pthread_mutex_init(&mutex->mutex, NULL);
|
||||
|
||||
return mutex;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (_mutex == NULL) return(0);
|
||||
pt_mutex_t *mutex = (pt_mutex_t *)_mutex;
|
||||
|
||||
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;
|
||||
|
||||
return pthread_mutex_unlock(&mutex->mutex) != 0;
|
||||
}
|
||||
|
||||
void thread_close_mutex(mutex_t *_mutex)
|
||||
{
|
||||
pt_mutex_t *mutex = (pt_mutex_t *)_mutex;
|
||||
|
||||
pthread_mutex_destroy(&mutex->mutex);
|
||||
|
||||
free(mutex);
|
||||
}
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
enable_language(RC)
|
||||
|
||||
add_library(plat OBJECT win.c win_dynld.c win_thread.c win_cdrom.c
|
||||
add_library(plat OBJECT win.c win_dynld.c win_cdrom.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
|
||||
|
|
|
@ -26,6 +26,10 @@ ifndef DEV_BUILD
|
|||
DEV_BUILD := n
|
||||
endif
|
||||
|
||||
ifneq ($(PTHREAD), n)
|
||||
PTHREAD := y
|
||||
endif
|
||||
|
||||
ifeq ($(DEV_BUILD), y)
|
||||
ifndef DEBUG
|
||||
DEBUG := y
|
||||
|
@ -607,10 +611,17 @@ CXXFLAGS := $(CFLAGS)
|
|||
#########################################################################
|
||||
# Create the (final) list of objects to build. #
|
||||
#########################################################################
|
||||
ifeq ($(PTHREAD), y)
|
||||
MAINOBJ := 86box.o config.o random.o timer.o io.o acpi.o apm.o dma.o ddma.o \
|
||||
nmi.o pic.o pit.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 random.o timer.o io.o acpi.o apm.o dma.o ddma.o \
|
||||
nmi.o pic.o pit.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
|
||||
|
||||
|
@ -791,11 +802,19 @@ 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
|
||||
|
@ -821,7 +840,11 @@ 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
|
||||
|
|
Loading…
Reference in a new issue