Commit 2926e966 by Maxime Grégoire

Use of std::mutex instead of Resources in Devices

Change-Id: Ib276cae0b196949a91dd13f436dfe853e1cf1b25 Reviewed-on: https://swiftshader-review.googlesource.com/3712Reviewed-by: 's avatarMaxime Grégoire <mgregoire@google.com> Tested-by: 's avatarMaxime Grégoire <mgregoire@google.com>
parent bc26e7ce
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
using namespace Devices; using namespace Devices;
CPUDevice::CPUDevice() CPUDevice::CPUDevice()
: DeviceInterface(), p_num_events(0), p_stop(false) : DeviceInterface(), p_num_events(0), p_stop(false), initialized(false)
{ {
} }
...@@ -33,7 +33,6 @@ void CPUDevice::init() ...@@ -33,7 +33,6 @@ void CPUDevice::init()
{ {
// Initialize the locking machinery // Initialize the locking machinery
p_events_cond = new sw::Event(); p_events_cond = new sw::Event();
eventListResource = new sw::Resource(0);
//TODO CHANGE pcore value to real //TODO CHANGE pcore value to real
p_cpu_mhz = 3200; p_cpu_mhz = 3200;
...@@ -43,12 +42,21 @@ void CPUDevice::init() ...@@ -43,12 +42,21 @@ void CPUDevice::init()
{ {
p_workers[i] = new sw::Thread(worker, this); p_workers[i] = new sw::Thread(worker, this);
} }
initialized = true;
} }
CPUDevice::~CPUDevice() CPUDevice::~CPUDevice()
{ {
if(!initialized)
return;
p_events_cond->signal();
p_stop = true; p_stop = true;
delete p_events_cond;
for(int thread = 0; thread < sw::CPUID::coreCount(); thread++) for(int thread = 0; thread < sw::CPUID::coreCount(); thread++)
{ {
if(p_workers[thread]) if(p_workers[thread])
...@@ -59,13 +67,6 @@ CPUDevice::~CPUDevice() ...@@ -59,13 +67,6 @@ CPUDevice::~CPUDevice()
p_workers[thread] = 0; p_workers[thread] = 0;
} }
} }
p_events_cond->signal();
delete p_events_cond;
eventListResource->lock(sw::DESTRUCT);
eventListResource->unlock();
eventListResource->destruct();
} }
DeviceBuffer *CPUDevice::createDeviceBuffer(MemObject *buffer, cl_int *rs) DeviceBuffer *CPUDevice::createDeviceBuffer(MemObject *buffer, cl_int *rs)
...@@ -167,27 +168,27 @@ void CPUDevice::freeEventDeviceData(Event *event) ...@@ -167,27 +168,27 @@ void CPUDevice::freeEventDeviceData(Event *event)
void CPUDevice::pushEvent(Event *event) void CPUDevice::pushEvent(Event *event)
{ {
eventListResource->lock(sw::PRIVATE); eventListResource.lock();
p_events.push_back(event); p_events.push_back(event);
p_num_events++; p_num_events++;
p_events_cond->signal(); p_events_cond->signal();
eventListResource->unlock(); eventListResource.unlock();
} }
Event *CPUDevice::getEvent(bool &stop) Event *CPUDevice::getEvent(bool &stop)
{ {
eventListResource->lock(sw::PRIVATE);
// Return the first event in the list, if any. Remove it if it is a // Return the first event in the list, if any. Remove it if it is a
// single-shot event. // single-shot event.
while(p_num_events == 0 && !p_stop) while(p_num_events == 0 && !p_stop)
p_events_cond->wait(); p_events_cond->wait();
eventListResource.lock();
if(p_stop) if(p_stop)
{ {
eventListResource->unlock(); eventListResource.unlock();
stop = true; stop = true;
return 0; return 0;
} }
...@@ -210,7 +211,7 @@ Event *CPUDevice::getEvent(bool &stop) ...@@ -210,7 +211,7 @@ Event *CPUDevice::getEvent(bool &stop)
p_events.pop_front(); p_events.pop_front();
} }
eventListResource->unlock(); eventListResource.unlock();
return event; return event;
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#define MAX_THREAD_AMOUNT 16 #define MAX_THREAD_AMOUNT 16
#include <list> #include <list>
#include <mutex>
#include "opencl.h" #include "opencl.h"
#include "device_interface.h" #include "device_interface.h"
...@@ -51,10 +52,10 @@ private: ...@@ -51,10 +52,10 @@ private:
unsigned int p_num_events; unsigned int p_num_events;
float p_cpu_mhz; float p_cpu_mhz;
sw::Thread *p_workers[MAX_THREAD_AMOUNT]; sw::Thread *p_workers[MAX_THREAD_AMOUNT];
sw::Resource *eventListResource; std::mutex eventListResource;
sw::Event *p_events_cond; sw::Event *p_events_cond;
std::list<Event *> p_events; std::list<Event *> p_events;
bool p_stop; bool p_stop, initialized;
}; };
//class GPUDevice : public DeviceInterface //class GPUDevice : public DeviceInterface
......
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