Create os specific thread implementation

Change-Id: I267d6cb021a75713c28a7bbf29384da13d2a7217
This commit is contained in:
Mateusz Jablonski
2018-05-22 17:23:39 +02:00
committed by sys_ocldev
parent c104db1d5e
commit 09e4dab4f6
19 changed files with 256 additions and 59 deletions

View File

@@ -22,6 +22,7 @@
#include "runtime/event/async_events_handler.h"
#include "runtime/event/event.h"
#include "runtime/os_interface/os_thread.h"
#include <iterator>
namespace OCLRT {
@@ -68,29 +69,31 @@ Event *AsyncEventsHandler::processList() {
return sleepCandidate;
}
void AsyncEventsHandler::asyncProcess() {
std::unique_lock<std::mutex> lock(asyncMtx, std::defer_lock);
void *AsyncEventsHandler::asyncProcess(void *arg) {
auto self = reinterpret_cast<AsyncEventsHandler *>(arg);
std::unique_lock<std::mutex> lock(self->asyncMtx, std::defer_lock);
Event *sleepCandidate = nullptr;
while (true) {
lock.lock();
transferRegisterList();
if (!allowAsyncProcess) {
processList();
releaseEvents();
self->transferRegisterList();
if (!self->allowAsyncProcess) {
self->processList();
self->releaseEvents();
break;
}
if (list.empty()) {
asyncCond.wait(lock);
if (self->list.empty()) {
self->asyncCond.wait(lock);
}
lock.unlock();
sleepCandidate = processList();
sleepCandidate = self->processList();
if (sleepCandidate) {
sleepCandidate->wait(true, true);
}
std::this_thread::yield();
}
return nullptr;
}
void AsyncEventsHandler::closeThread() {
@@ -108,7 +111,7 @@ void AsyncEventsHandler::openThread() {
if (!thread.get()) {
DEBUG_BREAK_IF(allowAsyncProcess);
allowAsyncProcess = true;
thread.reset(new std::thread([this] { asyncProcess(); }));
thread = Thread::create(asyncProcess, reinterpret_cast<void *>(this));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Intel Corporation
* Copyright (c) 2017 - 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@@ -23,13 +23,13 @@
#pragma once
#include <vector>
#include <memory>
#include <thread>
#include <atomic>
#include <mutex>
#include <condition_variable>
namespace OCLRT {
class Event;
class Thread;
class AsyncEventsHandler {
public:
@@ -40,7 +40,7 @@ class AsyncEventsHandler {
protected:
Event *processList();
void asyncProcess();
static void *asyncProcess(void *arg);
void releaseEvents();
MOCKABLE_VIRTUAL void openThread();
MOCKABLE_VIRTUAL void transferRegisterList();
@@ -48,7 +48,7 @@ class AsyncEventsHandler {
std::vector<Event *> list;
std::vector<Event *> pendingList;
std::unique_ptr<std::thread> thread;
std::unique_ptr<Thread> thread;
std::mutex asyncMtx;
std::condition_variable asyncCond;
std::atomic<bool> allowAsyncProcess;