From e408b3ab021e62c7d399f90f5482c82f25c1a73c Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Mon, 17 Apr 2017 09:09:32 +0200 Subject: [PATCH] onepad: replace pthred/queue with std::mutex/mt_queue v2: based on turtleli feedback rename m_q into m_queue add includes --- plugins/onepad/keyboard.cpp | 10 ++---- plugins/onepad/mt_queue.h | 67 +++++++++++++++++++++++++++++++++++++ plugins/onepad/onepad.cpp | 21 ++---------- plugins/onepad/onepad.h | 8 ++--- 4 files changed, 77 insertions(+), 29 deletions(-) create mode 100644 plugins/onepad/mt_queue.h diff --git a/plugins/onepad/keyboard.cpp b/plugins/onepad/keyboard.cpp index accf2ce80d..154a2d8d7e 100644 --- a/plugins/onepad/keyboard.cpp +++ b/plugins/onepad/keyboard.cpp @@ -55,7 +55,8 @@ static bool s_grab_input = false; static bool s_Shift = false; static unsigned int s_previous_mouse_x = 0; static unsigned int s_previous_mouse_y = 0; -void AnalyzeKeyEvent(keyEvent &evt) + +static void AnalyzeKeyEvent(keyEvent &evt) { KeySym key = (KeySym)evt.key; int pad = 0; @@ -207,12 +208,7 @@ void PollForX11KeyboardInput() XEvent E = {0}; // Keyboard input send by PCSX2 - while (!ev_fifo.empty()) { - AnalyzeKeyEvent(ev_fifo.front()); - pthread_spin_lock(&mutex_KeyEvent); - ev_fifo.pop(); - pthread_spin_unlock(&mutex_KeyEvent); - } + g_ev_fifo.consume_all(AnalyzeKeyEvent); // keyboard input while (XPending(GSdsp) > 0) { diff --git a/plugins/onepad/mt_queue.h b/plugins/onepad/mt_queue.h new file mode 100644 index 0000000000..3fc7937129 --- /dev/null +++ b/plugins/onepad/mt_queue.h @@ -0,0 +1,67 @@ +/* OnePAD - PCSX2 dev + * Copyright (C) 2017-2017 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#pragma once + +#include +#include + +template +class MtQueue +{ + std::queue m_queue; + std::mutex m_mtx; + +public: + MtQueue() + { + } + + ~MtQueue() + { + } + + void push(const T &e) + { + std::lock_guard guard(m_mtx); + m_queue.push(e); + } + + size_t size() + { + std::lock_guard guard(m_mtx); + return m_queue.size(); + } + + template + void consume_all(F f) + { + std::lock_guard guard(m_mtx); + while (!m_queue.empty()) { + f(m_queue.front()); + m_queue.pop(); + } + } + + void reset() + { + std::lock_guard guard(m_mtx); + while (!m_queue.empty()) + m_queue.pop(); + } +}; diff --git a/plugins/onepad/onepad.cpp b/plugins/onepad/onepad.cpp index e0acdc69ec..0cc0226594 100644 --- a/plugins/onepad/onepad.cpp +++ b/plugins/onepad/onepad.cpp @@ -53,11 +53,9 @@ const u32 build = 0; // increase that with each version FILE *padLog = NULL; -pthread_spinlock_t mutex_KeyEvent; -bool mutex_WasInit = false; KeyStatus *key_status = NULL; -queue ev_fifo; +MtQueue g_ev_fifo; static void InitLibraryName() { @@ -197,10 +195,7 @@ PADopen(void *pDsp) memset(&event, 0, sizeof(event)); key_status->Init(); - while (!ev_fifo.empty()) - ev_fifo.pop(); - pthread_spin_init(&mutex_KeyEvent, PTHREAD_PROCESS_PRIVATE); - mutex_WasInit = true; + g_ev_fifo.reset(); #if defined(__unix__) GamePad::EnumerateGamePads(s_vgamePad); @@ -229,10 +224,6 @@ PADsetLogDir(const char *dir) EXPORT_C_(void) PADclose() { - while (!ev_fifo.empty()) - ev_fifo.pop(); - mutex_WasInit = false; - pthread_spin_destroy(&mutex_KeyEvent); _PADclose(); } @@ -366,12 +357,6 @@ PADkeyEvent() EXPORT_C_(void) PADWriteEvent(keyEvent &evt) { - // This function call be called before PADopen. Therefore we cann't - // guarantee that the spin lock was initialized - if (mutex_WasInit) { - pthread_spin_lock(&mutex_KeyEvent); - ev_fifo.push(evt); - pthread_spin_unlock(&mutex_KeyEvent); - } + g_ev_fifo.push(evt); } #endif diff --git a/plugins/onepad/onepad.h b/plugins/onepad/onepad.h index 6c1afabe6d..ed6e668c30 100644 --- a/plugins/onepad/onepad.h +++ b/plugins/onepad/onepad.h @@ -26,7 +26,6 @@ #include #include -#include #ifdef _WIN32 #include @@ -44,8 +43,9 @@ #include #include #include -#include #include +#include +#include using namespace std; #define PADdefs @@ -108,6 +108,7 @@ enum gamePadValues { #include "bitwise.h" #include "controller.h" #include "KeyStatus.h" +#include "mt_queue.h" #ifdef _MSC_VER #define EXPORT_C_(type) extern "C" __declspec(dllexport) type CALLBACK @@ -123,8 +124,7 @@ extern bool toggleAutoRepeat; //#define PAD_LOG __LogToConsole extern keyEvent event; -extern queue ev_fifo; -extern pthread_spinlock_t mutex_KeyEvent; +extern MtQueue g_ev_fifo; void clearPAD(int pad); s32 _PADopen(void *pDsp);