From ad9f1c3417cf8fd882ce4718984f1b82f1a75907 Mon Sep 17 00:00:00 2001 From: Volker Ruppert Date: Sun, 10 Sep 2017 06:50:07 +0000 Subject: [PATCH] Moved event / wait stuff from the Voodoo code to osdep.cc fot the usage in other parts of Bochs. --- bochs/iodev/display/voodoo.cc | 33 ++++++------------ bochs/iodev/display/voodoo_data.h | 56 +++++-------------------------- bochs/iodev/display/voodoo_func.h | 8 ++--- bochs/osdep.cc | 50 ++++++++++++++++++++++++++- bochs/osdep.h | 21 +++++++++++- 5 files changed, 91 insertions(+), 77 deletions(-) diff --git a/bochs/iodev/display/voodoo.cc b/bochs/iodev/display/voodoo.cc index fed31b87b..de1d36c59 100644 --- a/bochs/iodev/display/voodoo.cc +++ b/bochs/iodev/display/voodoo.cc @@ -162,7 +162,7 @@ BX_THREAD_FUNC(fifo_thread, indata) UNUSED(indata); while (1) { - if (fifo_wait_for_event(&fifo_wakeup)) { + if (bx_wait_for_event(&fifo_wakeup)) { BX_LOCK(fifo_mutex); while (1) { if (!fifo_empty(&v->fbi.fifo)) { @@ -235,15 +235,9 @@ bx_voodoo_c::~bx_voodoo_c() if (BX_VOODOO_THIS s.model == VOODOO_2) { BX_FINI_MUTEX(cmdfifo_mutex); } -#ifdef WIN32 - CloseHandle(fifo_wakeup.event); - CloseHandle(fifo_not_full.event); -#else - pthread_cond_destroy(&fifo_wakeup.cond); - pthread_mutex_destroy(&fifo_wakeup.mutex); - pthread_cond_destroy(&fifo_not_full.cond); - pthread_mutex_destroy(&fifo_not_full.mutex); -#endif + bx_destroy_event(&fifo_wakeup); + bx_destroy_event(&fifo_not_full); + if (v != NULL) { free(v->fbi.ram); free(v->tmu[0].ram); @@ -303,16 +297,9 @@ void bx_voodoo_c::init(void) voodoo_init(BX_VOODOO_THIS s.model); -#ifdef WIN32 - fifo_wakeup.event = CreateEvent(NULL, FALSE, FALSE, "fifo_wakeup"); - fifo_not_full.event = CreateEvent(NULL, FALSE, FALSE, "fifo_not_full"); -#else - pthread_cond_init(&fifo_wakeup.cond, NULL); - pthread_mutex_init(&fifo_wakeup.mutex, NULL); - pthread_cond_init(&fifo_not_full.cond, NULL); - pthread_mutex_init(&fifo_not_full.mutex, NULL); -#endif - fifo_set_event(&fifo_not_full); + bx_create_event(&fifo_wakeup); + bx_create_event(&fifo_not_full); + bx_set_event(&fifo_not_full); BX_THREAD_CREATE(fifo_thread, this, fifo_thread_var); BX_INFO(("3dfx Voodoo Graphics adapter (model=%s) initialized", @@ -660,11 +647,11 @@ void bx_voodoo_c::vertical_timer_handler(void *this_ptr) BX_LOCK(fifo_mutex); if (!fifo_empty(&v->pci.fifo) || !fifo_empty(&v->fbi.fifo)) { - fifo_set_event(&fifo_wakeup); + bx_set_event(&fifo_wakeup); } BX_UNLOCK(fifo_mutex); if (v->fbi.cmdfifo[0].cmd_ready) { - fifo_set_event(&fifo_wakeup); + bx_set_event(&fifo_wakeup); } if (v->fbi.vblank_swap_pending) { @@ -830,7 +817,7 @@ void bx_voodoo_c::pci_write_handler(Bit8u address, Bit32u value, unsigned io_len if (((address+i) == 0x40) && ((value8 ^ oldval) & 0x02)) { v->pci.fifo.enabled = ((value8 & 0x02) > 0); if (!v->pci.fifo.enabled && !fifo_empty(&v->pci.fifo)) { - fifo_set_event(&fifo_wakeup); + bx_set_event(&fifo_wakeup); } BX_DEBUG(("PCI FIFO now %sabled (not implemented)", v->pci.fifo.enabled ? "en":"dis")); } diff --git a/bochs/iodev/display/voodoo_data.h b/bochs/iodev/display/voodoo_data.h index 41c35b44d..1fb7b6ea8 100644 --- a/bochs/iodev/display/voodoo_data.h +++ b/bochs/iodev/display/voodoo_data.h @@ -1416,17 +1416,6 @@ struct _stats_block }; -typedef struct -{ -#ifdef WIN32 - HANDLE event; -#else - pthread_cond_t cond; - pthread_mutex_t mutex; -#endif -} fifo_event_t; - - typedef struct _fifo_state fifo_state; struct _fifo_state { @@ -1734,37 +1723,8 @@ struct _voodoo_state // FIFO event handling BX_MUTEX(fifo_mutex); -fifo_event_t fifo_wakeup; -fifo_event_t fifo_not_full; - - -void fifo_set_event(fifo_event_t *fifo_ev) -{ -#ifdef WIN32 - SetEvent(fifo_ev->event); -#else - pthread_mutex_lock(&fifo_ev->mutex); - pthread_cond_signal(&fifo_ev->cond); - pthread_mutex_unlock(&fifo_ev->mutex); -#endif -} - - -bx_bool fifo_wait_for_event(fifo_event_t *fifo_ev) -{ -#ifdef WIN32 - if (WaitForSingleObject(fifo_ev->event, 1) == WAIT_OBJECT_0) { - return 1; - } else { - return 0; - } -#else - pthread_mutex_lock(&fifo_ev->mutex); - pthread_cond_wait(&fifo_ev->cond, &fifo_ev->mutex); - pthread_mutex_unlock(&fifo_ev->mutex); - return 1; -#endif -} +bx_thread_event_t fifo_wakeup; +bx_thread_event_t fifo_not_full; /************************************* @@ -1785,7 +1745,7 @@ BX_CPP_INLINE void fifo_reset(fifo_state *f) { BX_LOCK(fifo_mutex); f->in = f->out = 0; - fifo_set_event(&fifo_not_full); + bx_set_event(&fifo_not_full); BX_UNLOCK(fifo_mutex); } @@ -1820,9 +1780,9 @@ BX_CPP_INLINE void fifo_add(fifo_state *f, Bit32u offset, Bit32u data) Bit32s next_in; if (fifo_full(f)) { - fifo_set_event(&fifo_wakeup); + bx_set_event(&fifo_wakeup); BX_UNLOCK(fifo_mutex); - fifo_wait_for_event(&fifo_not_full); + bx_wait_for_event(&fifo_not_full); BX_LOCK(fifo_mutex); } /* compute the value of 'in' after we add this item */ @@ -1862,7 +1822,7 @@ BX_CPP_INLINE Bit32u fifo_remove(fifo_state *f, Bit32u *offset, Bit32u *data) f->out = next_out; } if (fifo_space(f) > 15) { - fifo_set_event(&fifo_not_full); + bx_set_event(&fifo_not_full); } return type; } @@ -1886,9 +1846,9 @@ BX_CPP_INLINE bx_bool fifo_empty_locked(fifo_state *f) BX_CPP_INLINE void fifo_move(fifo_state *f1, fifo_state *f2) { if (fifo_full(f2)) { - fifo_set_event(&fifo_wakeup); + bx_set_event(&fifo_wakeup); BX_UNLOCK(fifo_mutex); - fifo_wait_for_event(&fifo_not_full); + bx_wait_for_event(&fifo_not_full); BX_LOCK(fifo_mutex); } Bit32s items1 = fifo_items(f1); diff --git a/bochs/iodev/display/voodoo_func.h b/bochs/iodev/display/voodoo_func.h index 376381602..7b559a439 100644 --- a/bochs/iodev/display/voodoo_func.h +++ b/bochs/iodev/display/voodoo_func.h @@ -2478,7 +2478,7 @@ void cmdfifo_w(Bit32u fbi_offset, Bit32u data) if (FBIINIT0_VGA_PASSTHRU(v->reg[fbiInit0].u)) { v->fbi.cmdfifo[0].cmd_ready = 1; } else { - fifo_set_event(&fifo_wakeup); + bx_set_event(&fifo_wakeup); } } BX_UNLOCK(cmdfifo_mutex); @@ -2693,11 +2693,11 @@ bx_bool fifo_add_common(Bit32u type_offset, Bit32u data) fifo_move(&v->pci.fifo, &v->fbi.fifo); } if ((fifo_space(&v->fbi.fifo)/2) <= 0xe000) { - fifo_set_event(&fifo_wakeup); + bx_set_event(&fifo_wakeup); } } else { if ((fifo_space(&v->pci.fifo)/2) <= 16) { - fifo_set_event(&fifo_wakeup); + bx_set_event(&fifo_wakeup); } } } @@ -2915,7 +2915,7 @@ void register_w_common(Bit32u offset, Bit32u data) if (regnum == swapbufferCMD) { v->fbi.swaps_pending++; } - fifo_set_event(&fifo_wakeup); + bx_set_event(&fifo_wakeup); } BX_UNLOCK(fifo_mutex); } else { diff --git a/bochs/osdep.cc b/bochs/osdep.cc index 512961441..c72783034 100644 --- a/bochs/osdep.cc +++ b/bochs/osdep.cc @@ -2,7 +2,7 @@ // $Id$ ///////////////////////////////////////////////////////////////////////// // -// Copyright (C) 2001-2013 The Bochs Project +// Copyright (C) 2001-2017 The Bochs Project // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -369,3 +369,51 @@ Bit64u bx_get_realtime64_usec(void) } #endif #endif + + +void bx_create_event(bx_thread_event_t *thread_ev) +{ +#ifdef WIN32 + thread_ev->event = CreateEvent(NULL, FALSE, FALSE, "event"); +#else + pthread_cond_init(&thread_ev->cond, NULL); + pthread_mutex_init(&thread_ev->mutex, NULL); +#endif +} + +void bx_destroy_event(bx_thread_event_t *thread_ev) +{ +#ifdef WIN32 + CloseHandle(thread_ev->event); +#else + pthread_cond_destroy(&thread_ev->cond); + pthread_mutex_destroy(&thread_ev->mutex); +#endif +} + +void bx_set_event(bx_thread_event_t *thread_ev) +{ +#ifdef WIN32 + SetEvent(thread_ev->event); +#else + pthread_mutex_lock(&thread_ev->mutex); + pthread_cond_signal(&thread_ev->cond); + pthread_mutex_unlock(&thread_ev->mutex); +#endif +} + +bx_bool bx_wait_for_event(bx_thread_event_t *thread_ev) +{ +#ifdef WIN32 + if (WaitForSingleObject(thread_ev->event, 1) == WAIT_OBJECT_0) { + return 1; + } else { + return 0; + } +#else + pthread_mutex_lock(&thread_ev->mutex); + pthread_cond_wait(&thread_ev->cond, &thread_ev->mutex); + pthread_mutex_unlock(&thread_ev->mutex); + return 1; +#endif +} diff --git a/bochs/osdep.h b/bochs/osdep.h index cc3be5587..0ee16d37a 100644 --- a/bochs/osdep.h +++ b/bochs/osdep.h @@ -2,7 +2,7 @@ // $Id$ ///////////////////////////////////////////////////////////////////////// // -// Copyright (C) 2001-2014 The Bochs Project +// Copyright (C) 2001-2017 The Bochs Project // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -379,4 +379,23 @@ extern Bit64u bx_get_realtime64_usec (void); #endif // BX_LARGE_RAMFILE +#ifndef WIN32 +#include +#endif + +typedef struct +{ +#ifdef WIN32 + HANDLE event; +#else + pthread_cond_t cond; + pthread_mutex_t mutex; +#endif +} bx_thread_event_t; + +void bx_create_event(bx_thread_event_t *thread_ev); +void bx_destroy_event(bx_thread_event_t *thread_ev); +void bx_set_event(bx_thread_event_t *thread_ev); +bx_bool bx_wait_for_event(bx_thread_event_t *thread_ev); + #endif /* ifdef BX_OSDEP_H */