Commit 7b4fc281 by Alexis Hetu Committed by Alexis Hétu

Replace sw::Resource with sw::WaitGroup

sw::Resource is only used by the Renderer class and the functionality used is simply one of a WaitGroup. The sync variables has been changed from a Resource to a WaitGroup and the Resource class has been removed. Bug b/133127573 Change-Id: Ic6843be44fcf57f7ac7bef6f3a14726f94761863 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/31689 Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarAlexis Hétu <sugoi@google.com> Presubmit-Ready: Alexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent 8a2d0116
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8)
project(SwiftShader C CXX) project(SwiftShader C CXX)
...@@ -1653,8 +1653,6 @@ file(GLOB_RECURSE VULKAN_LIST ...@@ -1653,8 +1653,6 @@ file(GLOB_RECURSE VULKAN_LIST
${SOURCE_DIR}/System/Math.hpp ${SOURCE_DIR}/System/Math.hpp
${SOURCE_DIR}/System/Memory.cpp ${SOURCE_DIR}/System/Memory.cpp
${SOURCE_DIR}/System/Memory.hpp ${SOURCE_DIR}/System/Memory.hpp
${SOURCE_DIR}/System/Resource.cpp
${SOURCE_DIR}/System/Resource.hpp
${SOURCE_DIR}/System/Socket.cpp ${SOURCE_DIR}/System/Socket.cpp
${SOURCE_DIR}/System/Socket.hpp ${SOURCE_DIR}/System/Socket.hpp
${SOURCE_DIR}/System/Synchronization.hpp ${SOURCE_DIR}/System/Synchronization.hpp
......
...@@ -652,7 +652,6 @@ cc_defaults { ...@@ -652,7 +652,6 @@ cc_defaults {
"System/Half.cpp", "System/Half.cpp",
"System/Math.cpp", "System/Math.cpp",
"System/Memory.cpp", "System/Memory.cpp",
"System/Resource.cpp",
"System/Socket.cpp", "System/Socket.cpp",
"System/Timer.cpp", "System/Timer.cpp",
"System/DebugAndroid.cpp", "System/DebugAndroid.cpp",
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include "Pipeline/Constants.hpp" #include "Pipeline/Constants.hpp"
#include "System/CPUID.hpp" #include "System/CPUID.hpp"
#include "System/Memory.hpp" #include "System/Memory.hpp"
#include "System/Resource.hpp"
#include "System/Half.hpp" #include "System/Half.hpp"
#include "System/Math.hpp" #include "System/Math.hpp"
#include "System/Timer.hpp" #include "System/Timer.hpp"
...@@ -249,16 +248,12 @@ namespace sw ...@@ -249,16 +248,12 @@ namespace sw
swiftConfig = new SwiftConfig(disableServer); swiftConfig = new SwiftConfig(disableServer);
updateConfiguration(true); updateConfiguration(true);
sync = new Resource(0);
} }
Renderer::~Renderer() Renderer::~Renderer()
{ {
sync->lock(EXCLUSIVE); sync.wait();
sync->destruct();
terminateThreads(); terminateThreads();
sync->unlock();
delete resumeApp; delete resumeApp;
resumeApp = nullptr; resumeApp = nullptr;
...@@ -318,7 +313,7 @@ namespace sw ...@@ -318,7 +313,7 @@ namespace sw
return; return;
} }
sync->lock(sw::PRIVATE); sync.add();
if(update) if(update)
{ {
...@@ -810,8 +805,7 @@ namespace sw ...@@ -810,8 +805,7 @@ namespace sw
void Renderer::synchronize() void Renderer::synchronize()
{ {
sync->lock(sw::PUBLIC); sync.wait();
sync->unlock();
} }
void Renderer::finishRendering(Task &pixelTask) void Renderer::finishRendering(Task &pixelTask)
...@@ -884,7 +878,7 @@ namespace sw ...@@ -884,7 +878,7 @@ namespace sw
draw.events = nullptr; draw.events = nullptr;
} }
sync->unlock(); sync.done();
draw.references = -1; draw.references = -1;
resumeApp->signal(); resumeApp->signal();
......
...@@ -300,7 +300,7 @@ namespace sw ...@@ -300,7 +300,7 @@ namespace sw
SwiftConfig *swiftConfig; SwiftConfig *swiftConfig;
std::list<vk::Query*> queries; std::list<vk::Query*> queries;
Resource *sync; WaitGroup sync;
VertexProcessor::State vertexState; VertexProcessor::State vertexState;
SetupProcessor::State setupState; SetupProcessor::State setupState;
......
...@@ -28,8 +28,6 @@ swiftshader_source_set("System") { ...@@ -28,8 +28,6 @@ swiftshader_source_set("System") {
"Math.hpp", "Math.hpp",
"Memory.cpp", "Memory.cpp",
"Memory.hpp", "Memory.hpp",
"Resource.cpp",
"Resource.hpp",
"Socket.cpp", "Socket.cpp",
"Socket.hpp", "Socket.hpp",
"Thread.hpp", "Thread.hpp",
......
// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "Resource.hpp"
#include "Memory.hpp"
#include "Debug.hpp"
namespace sw
{
Resource::Resource(size_t bytes) : size(bytes)
{
buffer = allocate(bytes);
}
Resource::~Resource()
{
deallocate(buffer);
}
void *Resource::lock(Accessor claimer)
{
std::unique_lock<std::mutex> lock(mutex);
return acquire(lock, claimer);
}
void *Resource::lock(Accessor relinquisher, Accessor claimer)
{
std::unique_lock<std::mutex> lock(mutex);
// Release
if (count > 0 && accessor == relinquisher)
{
release(lock);
}
// Acquire
acquire(lock, claimer);
return buffer;
}
void Resource::unlock()
{
std::unique_lock<std::mutex> lock(mutex);
release(lock);
}
void *Resource::acquire(std::unique_lock<std::mutex> &lock, Accessor claimer)
{
while (count > 0 && accessor != claimer)
{
blocked++;
released.wait(lock, [&] { return count == 0 || accessor == claimer; });
blocked--;
}
accessor = claimer;
count++;
return buffer;
}
void Resource::release(std::unique_lock<std::mutex> &lock)
{
ASSERT(count > 0);
count--;
if(count == 0)
{
if(orphaned)
{
lock.unlock();
delete this;
return;
}
released.notify_one();
}
}
void Resource::destruct()
{
std::unique_lock<std::mutex> lock(mutex);
if(count == 0 && blocked == 0)
{
lock.unlock();
delete this;
return;
}
orphaned = true;
}
const void *Resource::data() const
{
return buffer;
}
}
// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef sw_Resource_hpp
#define sw_Resource_hpp
#include <condition_variable>
#include <mutex>
namespace sw
{
enum Accessor
{
PUBLIC, // Application/API access
PRIVATE, // Renderer access, shared by multiple threads if read-only
MANAGED, // Renderer access, shared read/write access if partitioned
EXCLUSIVE
};
// Resource is a form of shared mutex that guards an internally allocated
// buffer. Resource has an exclusive lock mode (sw::Accessor) and lock
// count, defaulting to sw::Accessor::PUBLIC and 0, respectively.
// Resource doesn't treat any of the sw::Accessor enumerator lock modes
// differently, all semantic meaning comes from the usage of Resource.
// You can have multiple locks in mode sw::Accessor::EXCLUSIVE.
class Resource
{
public:
Resource(size_t bytes);
// destruct() is an asynchronous destructor, that will atomically:
// When the resource is unlocked:
// * Delete itself.
// When the resource is locked:
// * Flag itself for deletion when next fully unlocked.
void destruct();
// lock() will atomically:
// When the resource is unlocked OR the lock mode equals claimer:
// * Increment the lock count.
// * Return a pointer to the buffer.
// When the resource is locked AND the lock mode does not equal claimer:
// * Block until all existing locks are released (lock count equals 0).
// * Switch lock mode to claimer.
// * Increment the lock count.
// * Return a pointer to the buffer.
void *lock(Accessor claimer);
// lock() will atomically:
// When the resource is unlocked OR the lock mode equals claimer:
// * Increment the lock count.
// * Return a pointer to the buffer.
// When the resource is locked AND the lock mode equals relinquisher:
// * Release *all* existing locks (regardless of prior count).
// * Delete itself and return nullptr if Resource::destruct() had been called while locked.
// * Switch lock mode to claimer.
// * Increment the lock count to 1.
// * Return a pointer to the buffer.
// When the resource is locked AND the lock mode does not equal relinquisher:
// * Block until all existing locks are released (lock count equals 0)
// * Switch lock mode to claimer
// * Increment the lock count to 1.
// * Return a pointer to the buffer.
void *lock(Accessor relinquisher, Accessor claimer);
// unlock() will atomically:
// * Assert if there are no locks.
// * Release a single lock.
// * Delete itself if Resource::destruct() had been called while locked.
void unlock();
// data() will return the Resource's buffer pointer regardless of lock
// state.
const void *data() const;
// size is the size in bytes of the Resource's buffer.
const size_t size;
private:
void *acquire(std::unique_lock<std::mutex> &lock, Accessor claimer);
void release(std::unique_lock<std::mutex> &lock);
~Resource(); // Always call destruct() instead
std::mutex mutex;
std::condition_variable released;
Accessor accessor = PUBLIC; // guarded by mutex
int blocked = 0; // guarded by mutex
int count = 0; // guarded by mutex
bool orphaned = false; // guarded by mutex
void *buffer;
};
}
#endif // sw_Resource_hpp
...@@ -178,7 +178,6 @@ IF EXIST "$(SolutionDir)..\deqp\build\external\vulkancts\modules\vulkan\" (copy ...@@ -178,7 +178,6 @@ IF EXIST "$(SolutionDir)..\deqp\build\external\vulkancts\modules\vulkan\" (copy
<ClCompile Include="..\System\Half.cpp" /> <ClCompile Include="..\System\Half.cpp" />
<ClCompile Include="..\System\Math.cpp" /> <ClCompile Include="..\System\Math.cpp" />
<ClCompile Include="..\System\Memory.cpp" /> <ClCompile Include="..\System\Memory.cpp" />
<ClCompile Include="..\System\Resource.cpp" />
<ClCompile Include="..\System\Socket.cpp" /> <ClCompile Include="..\System\Socket.cpp" />
<ClCompile Include="..\System\Timer.cpp" /> <ClCompile Include="..\System\Timer.cpp" />
<ClCompile Include="..\WSI\VkSurfaceKHR.cpp" /> <ClCompile Include="..\WSI\VkSurfaceKHR.cpp" />
...@@ -274,7 +273,6 @@ IF EXIST "$(SolutionDir)..\deqp\build\external\vulkancts\modules\vulkan\" (copy ...@@ -274,7 +273,6 @@ IF EXIST "$(SolutionDir)..\deqp\build\external\vulkancts\modules\vulkan\" (copy
<ClInclude Include="..\System\Half.hpp" /> <ClInclude Include="..\System\Half.hpp" />
<ClInclude Include="..\System\Math.hpp" /> <ClInclude Include="..\System\Math.hpp" />
<ClInclude Include="..\System\Memory.hpp" /> <ClInclude Include="..\System\Memory.hpp" />
<ClInclude Include="..\System\Resource.hpp" />
<ClInclude Include="..\System\SharedLibrary.hpp" /> <ClInclude Include="..\System\SharedLibrary.hpp" />
<ClInclude Include="..\System\Socket.hpp" /> <ClInclude Include="..\System\Socket.hpp" />
<ClInclude Include="..\System\Synchronization.hpp" /> <ClInclude Include="..\System\Synchronization.hpp" />
......
...@@ -150,9 +150,6 @@ ...@@ -150,9 +150,6 @@
<ClCompile Include="..\System\Memory.cpp"> <ClCompile Include="..\System\Memory.cpp">
<Filter>Source Files\System</Filter> <Filter>Source Files\System</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\System\Resource.cpp">
<Filter>Source Files\System</Filter>
</ClCompile>
<ClCompile Include="..\System\Socket.cpp"> <ClCompile Include="..\System\Socket.cpp">
<Filter>Source Files\System</Filter> <Filter>Source Files\System</Filter>
</ClCompile> </ClCompile>
...@@ -488,9 +485,6 @@ ...@@ -488,9 +485,6 @@
<ClInclude Include="..\System\Memory.hpp"> <ClInclude Include="..\System\Memory.hpp">
<Filter>Header Files\System</Filter> <Filter>Header Files\System</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\System\Resource.hpp">
<Filter>Header Files\System</Filter>
</ClInclude>
<ClInclude Include="..\System\SharedLibrary.hpp"> <ClInclude Include="..\System\SharedLibrary.hpp">
<Filter>Header Files\System</Filter> <Filter>Header Files\System</Filter>
</ClInclude> </ClInclude>
......
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