Move platform shutdown to destructor.

Change-Id: Ifeca1c5fe11d8e562b9f4641737b6bf1ac70aac6
This commit is contained in:
Mrozek, Michal
2018-06-28 08:51:44 +02:00
committed by sys_ocldev
parent b7545f1401
commit b20bcb8b31
7 changed files with 65 additions and 65 deletions

View File

@ -62,8 +62,28 @@ Platform::Platform() {
}
Platform::~Platform() {
shutdown();
asyncEventsHandler->closeThread();
TakeOwnershipWrapper<Platform> platformOwnership(*this);
executionEnvironment->decRefInternal();
if (state == StateNone) {
return;
}
for (auto dev : this->devices) {
if (dev) {
dev->decRefInternal();
}
}
devices.clear();
state = StateNone;
delete platformInfo;
platformInfo = nullptr;
std::string().swap(compilerExtensions);
gtpinNotifyPlatformShutdown();
}
cl_int Platform::getInfo(cl_platform_info paramName,
@ -183,30 +203,6 @@ bool Platform::isInitialized() {
return ret;
}
void Platform::shutdown() {
asyncEventsHandler->closeThread();
TakeOwnershipWrapper<Platform> platformOwnership(*this);
if (state == StateNone) {
return;
}
for (auto dev : this->devices) {
if (dev) {
dev->decRefInternal();
}
}
devices.clear();
state = StateNone;
delete platformInfo;
platformInfo = nullptr;
std::string().swap(compilerExtensions);
gtpinNotifyPlatformShutdown();
}
Device *Platform::getDevice(size_t deviceOrdinal) {
TakeOwnershipWrapper<Platform> platformOwnership(*this);

View File

@ -48,6 +48,9 @@ class Platform : public BaseObject<_cl_platform_id> {
Platform();
~Platform() override;
Platform(const Platform &) = delete;
Platform &operator=(Platform const &) = delete;
cl_int getInfo(cl_platform_info paramName,
size_t paramValueSize,
void *paramValue,
@ -57,7 +60,6 @@ class Platform : public BaseObject<_cl_platform_id> {
bool initialize();
bool isInitialized();
void shutdown();
size_t getNumDevices() const;
Device **getDevices();

View File

@ -21,6 +21,7 @@
set(IGDRCL_SRCS_tests_mocks
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/mock_32bitAllocator.h
${CMAKE_CURRENT_SOURCE_DIR}/mock_async_event_handler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mock_async_event_handler.h
${CMAKE_CURRENT_SOURCE_DIR}/mock_aub_subcapture_manager.h
${CMAKE_CURRENT_SOURCE_DIR}/mock_block_kernel_manager.h

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 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"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "unit_tests/mocks/mock_async_event_handler.h"
namespace MockAsyncEventHandlerGlobals {
bool destructorCalled = false;
}

View File

@ -30,6 +30,9 @@
#include <iterator>
using namespace OCLRT;
namespace MockAsyncEventHandlerGlobals {
extern bool destructorCalled;
}
class MockHandler : public AsyncEventsHandler {
public:
@ -43,11 +46,13 @@ class MockHandler : public AsyncEventsHandler {
if (!allowThreadCreating) {
asyncProcess(this); // process once for cleanup
}
MockAsyncEventHandlerGlobals::destructorCalled = true;
}
MockHandler(bool allowAsync = false) : AsyncEventsHandler() {
allowThreadCreating = allowAsync;
transferCounter.store(0);
MockAsyncEventHandlerGlobals::destructorCalled = false;
}
Event *process() {
@ -70,7 +75,6 @@ class MockHandler : public AsyncEventsHandler {
bool peekIsListEmpty() { return list.size() == 0; }
bool peekIsRegisterListEmpty() { return registerList.size() == 0; }
std::atomic<int> transferCounter;
bool openThreadCalled = false;
bool allowThreadCreating = false;

View File

@ -62,13 +62,6 @@ TEST_F(PlatformTest, getDevices) {
auto allDevices = pPlatform->getDevices();
EXPECT_NE(nullptr, allDevices);
pPlatform->shutdown();
devNum = pPlatform->getNumDevices();
EXPECT_EQ(0u, devNum);
allDevices = pPlatform->getDevices();
EXPECT_EQ(nullptr, allDevices);
}
TEST_F(PlatformTest, PlatformgetAsCompilerEnabledExtensionsString) {
@ -82,16 +75,6 @@ TEST_F(PlatformTest, PlatformgetAsCompilerEnabledExtensionsString) {
if (std::string(pPlatform->getDevice(0)->getDeviceInfo().clVersion).find("OpenCL 2.1") != std::string::npos) {
EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_khr_subgroups")));
}
pPlatform->shutdown();
compilerExtensions.shrink_to_fit();
}
TEST_F(PlatformTest, destructorCallsShutdownAndReleasesAllResources) {
Platform *platform = new Platform;
ASSERT_NE(nullptr, platform);
platform->initialize();
delete platform;
}
TEST_F(PlatformTest, hasAsyncEventsHandler) {
@ -107,10 +90,8 @@ TEST(PlatformTestSimple, shutdownClosesAsyncEventHandlerThread) {
EXPECT_EQ(mockAsyncHandler, platform->getAsyncEventsHandler());
mockAsyncHandler->openThread();
platform->shutdown();
EXPECT_EQ(nullptr, mockAsyncHandler->thread);
delete platform;
EXPECT_TRUE(MockAsyncEventHandlerGlobals::destructorCalled);
}
namespace OCLRT {
@ -220,3 +201,9 @@ TEST(PlatformConstructionTest, givenPlatformConstructorWhenItIsCalledAfterResetT
EXPECT_NE(platform2, nullptr);
platformImpl.reset(nullptr);
}
TEST(PlatformConstructionTest, givenPlatformThatIsNotInitializedWhenGetDevicesIsCalledThenNullptrIsReturned) {
Platform platform;
auto devices = platform.getDevices();
EXPECT_EQ(nullptr, devices);
}

View File

@ -33,10 +33,6 @@ struct PlatformTestMt : public ::testing::Test {
pP->initialize();
}
static void shutdownThreadFunc(Platform *pP) {
pP->shutdown();
}
cl_int retVal = CL_SUCCESS;
std::unique_ptr<Platform> pPlatform;
};
@ -60,7 +56,7 @@ TEST_F(PlatformTestMt, initialize) {
EXPECT_TRUE(pPlatform->isInitialized());
pPlatform->shutdown();
pPlatform.reset(new Platform());
for (int i = 0; i < 10; ++i) {
threads[i] = std::thread(callinitPlatform, pPlatform.get(), &ret[i]);
@ -72,30 +68,18 @@ TEST_F(PlatformTestMt, initialize) {
for (int i = 0; i < 10; ++i)
EXPECT_TRUE(ret[i]);
pPlatform->shutdown();
}
TEST_F(PlatformTestMt, mtSafeTest) {
size_t devNum = pPlatform->getNumDevices();
EXPECT_EQ(0u, devNum);
bool ret = pPlatform->initialize();
std::thread t1(PlatformTestMt::initThreadFunc, pPlatform.get());
std::thread t2(PlatformTestMt::shutdownThreadFunc, pPlatform.get());
EXPECT_TRUE(ret);
std::thread t2(PlatformTestMt::initThreadFunc, pPlatform.get());
t1.join();
t2.join();
std::thread t3(PlatformTestMt::initThreadFunc, pPlatform.get());
std::thread t4(PlatformTestMt::initThreadFunc, pPlatform.get());
t3.join();
t4.join();
devNum = pPlatform->getNumDevices();
EXPECT_EQ(numPlatformDevices, devNum);
pPlatform->shutdown();
devNum = pPlatform->getNumDevices();
EXPECT_EQ(0u, devNum);
}