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
|
|
|
|
gsdx: Make GSJobQueue non-inheritable
In the previous code, the threads were created and destroyed in the base
class constructor and destructor, so the threads could potentially be
active while the object is in a partially constructed or destroyed state.
The thread however, relies on a virtual function to process the queue
items, and the vtable might not be in the desired state when the object
is partially constructed or destroyed.
This probably only matters during object destruction - no items are in
the queue during object construction so the virtual function won't be
called, but items may still be queued up when the destructor is called,
so the virtual function can be called. It wasn't an issue because all
uses of the thread explicitly waited for the queues to be empty before
invoking the destructor.
Adjust the constructor to take a std::function parameter, which the
thread will use instead to process queue items, and avoid inheriting
from the GSJobQueue class. This will also eliminate the need to
explicitly wait for all jobs to finish (unless there are other external
factors, of course), which would probably make future code safer.
2016-11-09 01:34:48 +00:00
|
|
|
template<class T, int CAPACITY> class GSJobQueue final
|
2015-03-03 09:01:22 +00:00
|
|
|
{
|
2016-10-31 19:47:21 +00:00
|
|
|
private:
|
|
|
|
std::thread m_thread;
|
gsdx: Make GSJobQueue non-inheritable
In the previous code, the threads were created and destroyed in the base
class constructor and destructor, so the threads could potentially be
active while the object is in a partially constructed or destroyed state.
The thread however, relies on a virtual function to process the queue
items, and the vtable might not be in the desired state when the object
is partially constructed or destroyed.
This probably only matters during object destruction - no items are in
the queue during object construction so the virtual function won't be
called, but items may still be queued up when the destructor is called,
so the virtual function can be called. It wasn't an issue because all
uses of the thread explicitly waited for the queues to be empty before
invoking the destructor.
Adjust the constructor to take a std::function parameter, which the
thread will use instead to process queue items, and avoid inheriting
from the GSJobQueue class. This will also eliminate the need to
explicitly wait for all jobs to finish (unless there are other external
factors, of course), which would probably make future code safer.
2016-11-09 01:34:48 +00:00
|
|
|
std::function<void(T&)> m_func;
|
2017-01-02 13:52:54 +00:00
|
|
|
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;
|
2017-01-01 21:31:24 +00:00
|
|
|
std::mutex m_wait_lock;
|
2015-03-03 09:01:22 +00:00
|
|
|
std::condition_variable m_empty;
|
|
|
|
std::condition_variable m_notempty;
|
|
|
|
|
|
|
|
void ThreadProc() {
|
|
|
|
std::unique_lock<std::mutex> l(m_lock);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
2017-01-02 12:10:06 +00:00
|
|
|
while (m_queue.empty()) {
|
2017-01-02 13:52:54 +00:00
|
|
|
if (m_exit)
|
2016-07-14 08:19:54 +00:00
|
|
|
return;
|
2017-01-02 12:11:14 +00:00
|
|
|
|
2015-03-03 09:01:22 +00:00
|
|
|
m_notempty.wait(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
l.unlock();
|
|
|
|
|
2017-01-02 12:10:06 +00:00
|
|
|
while (m_queue.consume_one(*this))
|
|
|
|
;
|
2015-03-03 09:01:22 +00:00
|
|
|
|
2017-01-02 12:10:06 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> wait_guard(m_wait_lock);
|
2017-01-01 21:31:24 +00:00
|
|
|
}
|
2017-01-02 12:10:06 +00:00
|
|
|
m_empty.notify_one();
|
2015-03-03 09:01:22 +00:00
|
|
|
|
2017-01-02 12:10:06 +00:00
|
|
|
l.lock();
|
2015-03-03 09:01:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
gsdx: Make GSJobQueue non-inheritable
In the previous code, the threads were created and destroyed in the base
class constructor and destructor, so the threads could potentially be
active while the object is in a partially constructed or destroyed state.
The thread however, relies on a virtual function to process the queue
items, and the vtable might not be in the desired state when the object
is partially constructed or destroyed.
This probably only matters during object destruction - no items are in
the queue during object construction so the virtual function won't be
called, but items may still be queued up when the destructor is called,
so the virtual function can be called. It wasn't an issue because all
uses of the thread explicitly waited for the queues to be empty before
invoking the destructor.
Adjust the constructor to take a std::function parameter, which the
thread will use instead to process queue items, and avoid inheriting
from the GSJobQueue class. This will also eliminate the need to
explicitly wait for all jobs to finish (unless there are other external
factors, of course), which would probably make future code safer.
2016-11-09 01:34:48 +00:00
|
|
|
GSJobQueue(std::function<void(T&)> func) :
|
|
|
|
m_func(func),
|
2015-03-03 09:01:22 +00:00
|
|
|
m_exit(false)
|
|
|
|
{
|
2016-10-31 19:47:21 +00:00
|
|
|
m_thread = std::thread(&GSJobQueue::ThreadProc, this);
|
2015-03-13 18:52:04 +00:00
|
|
|
}
|
2015-03-03 09:01:22 +00:00
|
|
|
|
gsdx: Make GSJobQueue non-inheritable
In the previous code, the threads were created and destroyed in the base
class constructor and destructor, so the threads could potentially be
active while the object is in a partially constructed or destroyed state.
The thread however, relies on a virtual function to process the queue
items, and the vtable might not be in the desired state when the object
is partially constructed or destroyed.
This probably only matters during object destruction - no items are in
the queue during object construction so the virtual function won't be
called, but items may still be queued up when the destructor is called,
so the virtual function can be called. It wasn't an issue because all
uses of the thread explicitly waited for the queues to be empty before
invoking the destructor.
Adjust the constructor to take a std::function parameter, which the
thread will use instead to process queue items, and avoid inheriting
from the GSJobQueue class. This will also eliminate the need to
explicitly wait for all jobs to finish (unless there are other external
factors, of course), which would probably make future code safer.
2016-11-09 01:34:48 +00:00
|
|
|
~GSJobQueue()
|
2016-10-31 19:47:21 +00:00
|
|
|
{
|
2017-01-02 12:11:14 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(m_lock);
|
|
|
|
m_exit = true;
|
|
|
|
}
|
|
|
|
m_notempty.notify_one();
|
2016-10-31 19:47:21 +00:00
|
|
|
|
|
|
|
m_thread.join();
|
2015-03-03 09:01:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 12:10:06 +00:00
|
|
|
bool IsEmpty()
|
|
|
|
{
|
|
|
|
return m_queue.empty();
|
2015-03-03 09:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Push(const T& item) {
|
|
|
|
while(!m_queue.push(item))
|
|
|
|
std::this_thread::yield();
|
|
|
|
|
2017-01-02 12:10:06 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(m_lock);
|
|
|
|
}
|
2015-03-03 09:01:22 +00:00
|
|
|
m_notempty.notify_one();
|
|
|
|
}
|
|
|
|
|
2017-01-02 12:10:06 +00:00
|
|
|
void Wait()
|
|
|
|
{
|
|
|
|
if (IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> l(m_wait_lock);
|
|
|
|
while (!IsEmpty())
|
|
|
|
m_empty.wait(l);
|
2015-03-03 09:01:22 +00:00
|
|
|
|
2017-01-02 12:10:06 +00:00
|
|
|
assert(IsEmpty());
|
2015-03-03 09:01:22 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 18:52:04 +00:00
|
|
|
void operator() (T& item) {
|
gsdx: Make GSJobQueue non-inheritable
In the previous code, the threads were created and destroyed in the base
class constructor and destructor, so the threads could potentially be
active while the object is in a partially constructed or destroyed state.
The thread however, relies on a virtual function to process the queue
items, and the vtable might not be in the desired state when the object
is partially constructed or destroyed.
This probably only matters during object destruction - no items are in
the queue during object construction so the virtual function won't be
called, but items may still be queued up when the destructor is called,
so the virtual function can be called. It wasn't an issue because all
uses of the thread explicitly waited for the queues to be empty before
invoking the destructor.
Adjust the constructor to take a std::function parameter, which the
thread will use instead to process queue items, and avoid inheriting
from the GSJobQueue class. This will also eliminate the need to
explicitly wait for all jobs to finish (unless there are other external
factors, of course), which would probably make future code safer.
2016-11-09 01:34:48 +00:00
|
|
|
m_func(item);
|
2015-03-03 09:01:22 +00:00
|
|
|
}
|
|
|
|
};
|