onepad: replace pthred/queue with std::mutex/mt_queue

v2: based on turtleli feedback
rename m_q into m_queue
add includes
This commit is contained in:
Gregory Hainaut 2017-04-17 09:09:32 +02:00
parent 7d771229e2
commit e408b3ab02
4 changed files with 77 additions and 29 deletions

View File

@ -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) {

67
plugins/onepad/mt_queue.h Normal file
View File

@ -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 <mutex>
#include <queue>
template <typename T>
class MtQueue
{
std::queue<T> m_queue;
std::mutex m_mtx;
public:
MtQueue()
{
}
~MtQueue()
{
}
void push(const T &e)
{
std::lock_guard<std::mutex> guard(m_mtx);
m_queue.push(e);
}
size_t size()
{
std::lock_guard<std::mutex> guard(m_mtx);
return m_queue.size();
}
template <typename F>
void consume_all(F f)
{
std::lock_guard<std::mutex> guard(m_mtx);
while (!m_queue.empty()) {
f(m_queue.front());
m_queue.pop();
}
}
void reset()
{
std::lock_guard<std::mutex> guard(m_mtx);
while (!m_queue.empty())
m_queue.pop();
}
};

View File

@ -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<keyEvent> ev_fifo;
MtQueue<keyEvent> 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

View File

@ -26,7 +26,6 @@
#include <stdio.h>
#include <assert.h>
#include <queue>
#ifdef _WIN32
#include <windows.h>
@ -44,8 +43,9 @@
#include <vector>
#include <map>
#include <string>
#include <pthread.h>
#include <memory>
#include <mutex>
#include <queue>
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<keyEvent> ev_fifo;
extern pthread_spinlock_t mutex_KeyEvent;
extern MtQueue<keyEvent> g_ev_fifo;
void clearPAD(int pad);
s32 _PADopen(void *pDsp);