Commit 026f9321 authored by bbguimaraes's avatar bbguimaraes
Browse files

hcwocl: use the standard threading library

parent 08a9cbe9
......@@ -6,8 +6,6 @@
ThreadPool::ThreadPool(int nThreads)
: m_nThreads( nThreads ), m_deleteSignal( false ), m_taskHead( 0 ), m_taskTail( 0 )
{
InitializeCriticalSection(&m_cs);
m_threads = new Thread[nThreads];
for(int i=0; i<nThreads; i++)
m_threads[i].init(i, this);
......@@ -20,9 +18,8 @@ ThreadPool::~ThreadPool()
m_deleteSignal = true;
start();
wait();
DeleteCriticalSection(&m_cs);
for(int i = 0; i < m_nThreads; ++i)
m_threads[i].m_thread.join();
delete [] m_threads;
m_threads = 0;
......@@ -33,9 +30,7 @@ ThreadPool::~ThreadPool()
void ThreadPool::resetThreadTimer()
{
QueryPerformanceFrequency( &m_frequency );
QueryPerformanceCounter( &m_startTime );
m_startTime = std::chrono::system_clock::now();
for(int i=0; i<m_nThreads; i++)
{
m_threads[i].timestampReset();
......@@ -55,41 +50,29 @@ void ThreadPool::start(bool resetTimestamp)
void ThreadPool::wait()
{
HANDLE* finSignals = new HANDLE[m_nThreads];
for(int i=0; i<m_nThreads; i++) finSignals[i] = m_threads[i].m_finSignal;
WaitForMultipleObjects( m_nThreads, finSignals, true, INFINITE );
for(int i = 0; i < m_nThreads; ++i)
m_threads[i].m_finSignal.get_future().wait();
for(int i=0; i<m_nThreads; i++)
{
ResetEvent( m_threads[i].m_finSignal );
}
delete [] finSignals;
m_threads[i].m_finSignal = {};
}
void ThreadPool::pushBack(Task* task)
{
EnterCriticalSection(&m_cs);
std::lock_guard<std::mutex> guard{m_mutex};
CLASSERT( m_taskHead != ((m_taskTail+1)&TASK_MASK) ); // full
m_tasks[m_taskTail] = task;
m_taskTail = (m_taskTail+1) & TASK_MASK;
LeaveCriticalSection(&m_cs);
}
ThreadPool::Task* ThreadPool::pop()
{
Task* task = NULL;
EnterCriticalSection(&m_cs);
std::lock_guard<std::mutex> guard{m_mutex};
if( m_taskHead != m_taskTail )
{
task = m_tasks[ m_taskHead ];
m_taskHead = (m_taskHead+1)&TASK_MASK;
}
LeaveCriticalSection(&m_cs);
return task;
}
......@@ -102,11 +85,7 @@ void ThreadPool::Thread::init(int idx, ThreadPool* threadPool)
{
m_args.m_threadPool = threadPool;
m_args.m_idx = idx;
_beginthreadex(NULL, 0, run, &m_args, 0, &m_threadIdx);
m_runSignal = CreateEvent(NULL, TRUE, FALSE, NULL);
m_finSignal = CreateEvent(NULL, TRUE, FALSE, NULL);
m_thread = std::thread(run, &m_args);
}
ThreadPool::Thread::~Thread()
......@@ -116,7 +95,7 @@ ThreadPool::Thread::~Thread()
void ThreadPool::Thread::start()
{
SetEvent( m_runSignal );
m_runSignal.set_value();
}
void ThreadPool::Thread::timestampReset()
......@@ -124,7 +103,7 @@ void ThreadPool::Thread::timestampReset()
m_nTimestamps = 0;
}
u32 __stdcall ThreadPool::Thread::run(void* args)
u32 ThreadPool::Thread::run(void* args)
{
ThreadArgs* tArgs = (ThreadArgs*)args;
ThreadPool* threadPool = tArgs->m_threadPool;
......@@ -134,9 +113,8 @@ u32 __stdcall ThreadPool::Thread::run(void* args)
volatile bool& deleteSignal = threadPool->m_deleteSignal;
while(!deleteSignal)
{
WaitForSingleObject(th->m_runSignal, INFINITE);
ResetEvent( th->m_runSignal );
th->m_runSignal.get_future().wait();
th->m_runSignal = {};
if( deleteSignal )
{
break;
......@@ -146,16 +124,14 @@ u32 __stdcall ThreadPool::Thread::run(void* args)
Task* task = threadPool->pop();
while( task )
{
LARGE_INTEGER s,e;
QueryPerformanceCounter( &s );
std::chrono::system_clock::time_point s,e;
s = std::chrono::system_clock::now();
task->run( idx );
QueryPerformanceCounter( &e );
e = std::chrono::system_clock::now();
using T = std::chrono::duration<float, std::milli>;
float start, end;
start = (float)(1000*(s.QuadPart - threadPool->m_startTime.QuadPart))/threadPool->m_frequency.QuadPart;
end = (float)(1000*(e.QuadPart - threadPool->m_startTime.QuadPart))/threadPool->m_frequency.QuadPart;
start = std::chrono::duration_cast<T>(s - threadPool->m_startTime).count();
end = std::chrono::duration_cast<T>(e - threadPool->m_startTime).count();
th->pushBackTimeStamp( task->getType(), start, end );
delete task;
......@@ -163,12 +139,9 @@ u32 __stdcall ThreadPool::Thread::run(void* args)
task = threadPool->pop();
}
}
SetEvent( th->m_finSignal );
th->m_finSignal.set_value();
}
SetEvent( th->m_finSignal );
_endthreadex(0);
th->m_finSignal.set_value();
return 0;
}
......
......@@ -4,7 +4,10 @@
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#pragma warning( disable : 4996 )
#include <future>
#include <mutex>
#include <thread>
#include <Common/Math/Math.h>
class ThreadPool
......@@ -12,6 +15,7 @@ class ThreadPool
public:
struct Task
{
virtual ~Task() = default;
virtual u16 getType() = 0;
virtual void run(int tIdx) = 0;
};
......@@ -42,7 +46,7 @@ class ThreadPool
void timestampReset();
static
u32 __stdcall run(void* args);
u32 run(void* args);
struct Timestamp
{
......@@ -54,9 +58,9 @@ class ThreadPool
void pushBackTimeStamp(u16 type, float s, float e);
public:
u32 m_threadIdx;
HANDLE m_runSignal;
HANDLE m_finSignal;
std::thread m_thread;
std::promise<void> m_runSignal;
std::promise<void> m_finSignal;
ThreadArgs m_args;
enum
......@@ -71,11 +75,9 @@ class ThreadPool
int m_nThreads;
bool m_deleteSignal;
CRITICAL_SECTION m_cs;
std::mutex m_mutex;
Thread* m_threads;
LARGE_INTEGER m_startTime;
LARGE_INTEGER m_frequency;
std::chrono::system_clock::time_point m_startTime;
enum
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment