2015-03-03 09:01:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
* http://www.gabest.org
|
|
|
|
*
|
|
|
|
* 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, 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 GNU Make; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "GSdx.h"
|
2015-03-20 22:19:44 +00:00
|
|
|
#include "boost_spsc_queue.hpp"
|
2015-03-03 09:01:22 +00:00
|
|
|
|
|
|
|
class IGSThread
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
virtual void ThreadProc() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// let us use std::thread for now, comment out the definition to go back to pthread
|
|
|
|
// There are currently some bugs/limitations to std::thread (see various comment)
|
|
|
|
// For the moment let's keep pthread but uses new std object (mutex, cond_var)
|
|
|
|
//#define _STD_THREAD_
|
|
|
|
|
2016-02-04 19:09:57 +00:00
|
|
|
#ifdef _WIN32
|
2015-03-03 09:01:22 +00:00
|
|
|
|
|
|
|
class GSThread : public IGSThread
|
|
|
|
{
|
|
|
|
DWORD m_ThreadId;
|
|
|
|
HANDLE m_hThread;
|
|
|
|
|
|
|
|
static DWORD WINAPI StaticThreadProc(void* lpParam);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void CreateThread();
|
|
|
|
void CloseThread();
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSThread();
|
|
|
|
virtual ~GSThread();
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#ifdef _STD_THREAD_
|
|
|
|
#include <thread>
|
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class GSThread : public IGSThread
|
|
|
|
{
|
|
|
|
#ifdef _STD_THREAD_
|
|
|
|
std::thread *t;
|
|
|
|
#else
|
|
|
|
pthread_attr_t m_thread_attr;
|
|
|
|
pthread_t m_thread;
|
|
|
|
#endif
|
|
|
|
static void* StaticThreadProc(void* param);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void CreateThread();
|
|
|
|
void CloseThread();
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSThread();
|
|
|
|
virtual ~GSThread();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-03-13 18:52:04 +00:00
|
|
|
template<class T> class IGSJobQueue : public GSThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IGSJobQueue() {}
|
|
|
|
virtual ~IGSJobQueue() {}
|
2015-03-03 09:01:22 +00:00
|
|
|
|
2015-03-13 18:52:04 +00:00
|
|
|
virtual bool IsEmpty() const = 0;
|
|
|
|
virtual void Push(const T& item) = 0;
|
|
|
|
virtual void Wait() = 0;
|
2015-03-03 09:01:22 +00:00
|
|
|
|
2015-03-13 18:52:04 +00:00
|
|
|
virtual void Process(T& item) = 0;
|
|
|
|
virtual int GetPixels(bool reset) = 0;
|
|
|
|
};
|
2015-03-03 09:01:22 +00:00
|
|
|
|
2015-05-18 15:01:12 +00:00
|
|
|
template<class T, int CAPACITY> class GSJobQueue : public IGSJobQueue<T>
|
2015-03-03 09:01:22 +00:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
std::atomic<int16_t> m_count;
|
|
|
|
std::atomic<bool> m_exit;
|
2015-05-18 15:01:12 +00:00
|
|
|
ringbuffer_base<T, CAPACITY> m_queue;
|
2015-03-03 09:01:22 +00:00
|
|
|
|
|
|
|
std::mutex m_lock;
|
|
|
|
std::condition_variable m_empty;
|
|
|
|
std::condition_variable m_notempty;
|
|
|
|
|
|
|
|
void ThreadProc() {
|
|
|
|
std::unique_lock<std::mutex> l(m_lock);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
while (m_count == 0) {
|
2016-07-14 08:19:54 +00:00
|
|
|
if (m_exit.load(memory_order_relaxed)) {
|
|
|
|
m_exit = false;
|
|
|
|
return;
|
|
|
|
}
|
2015-03-03 09:01:22 +00:00
|
|
|
m_notempty.wait(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
l.unlock();
|
|
|
|
|
|
|
|
int16_t consumed = 0;
|
|
|
|
for (int16_t nb = m_count; nb >= 0; nb--) {
|
|
|
|
if (m_queue.consume_one(*this))
|
|
|
|
consumed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
l.lock();
|
|
|
|
|
|
|
|
m_count -= consumed;
|
|
|
|
|
|
|
|
if (m_count <= 0)
|
|
|
|
m_empty.notify_one();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSJobQueue() :
|
|
|
|
m_count(0),
|
|
|
|
m_exit(false)
|
|
|
|
{
|
2015-03-13 18:52:04 +00:00
|
|
|
this->CreateThread();
|
|
|
|
}
|
2015-03-03 09:01:22 +00:00
|
|
|
|
|
|
|
virtual ~GSJobQueue() {
|
2016-07-14 08:19:54 +00:00
|
|
|
m_exit = true;
|
|
|
|
do {
|
|
|
|
m_notempty.notify_one();
|
|
|
|
} while (m_exit);
|
2015-03-13 18:52:04 +00:00
|
|
|
this->CloseThread();
|
2015-03-03 09:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty() const {
|
|
|
|
ASSERT(m_count >= 0);
|
|
|
|
|
|
|
|
return m_count == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Push(const T& item) {
|
|
|
|
while(!m_queue.push(item))
|
|
|
|
std::this_thread::yield();
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> l(m_lock);
|
|
|
|
|
|
|
|
m_count++;
|
|
|
|
|
|
|
|
l.unlock();
|
|
|
|
|
|
|
|
m_notempty.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Wait() {
|
|
|
|
if (m_count > 0) {
|
|
|
|
std::unique_lock<std::mutex> l(m_lock);
|
|
|
|
while (m_count > 0) {
|
|
|
|
m_empty.wait(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(m_count == 0);
|
|
|
|
}
|
|
|
|
|
2015-03-13 18:52:04 +00:00
|
|
|
void operator() (T& item) {
|
|
|
|
this->Process(item);
|
2015-03-03 09:01:22 +00:00
|
|
|
}
|
|
|
|
};
|