Commit ed01f2c3 by Ben Clayton

Move Chan out of VkQueue.hpp and into System/Synchronization.hpp

This is a pure move with no changes. The goal here is to centralize the concurrent synchronization primitives, and not have them scattered around the codebase. Bug: b/133127573 Change-Id: I6abdfc34bf13ce455983610ff0c686778e0e9e4c Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/31677 Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarBen Clayton <headlessclayton@gmail.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent f448d8e1
...@@ -1603,6 +1603,7 @@ file(GLOB_RECURSE VULKAN_LIST ...@@ -1603,6 +1603,7 @@ file(GLOB_RECURSE VULKAN_LIST
${SOURCE_DIR}/System/Resource.hpp ${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/Thread.cpp ${SOURCE_DIR}/System/Thread.cpp
${SOURCE_DIR}/System/Thread.hpp ${SOURCE_DIR}/System/Thread.hpp
${SOURCE_DIR}/System/Timer.cpp ${SOURCE_DIR}/System/Timer.cpp
......
...@@ -263,6 +263,7 @@ if %errorlevel% neq 0 goto :VCEnd</Command> ...@@ -263,6 +263,7 @@ if %errorlevel% neq 0 goto :VCEnd</Command>
<ClInclude Include="$(SolutionDir)src\System\Resource.hpp" /> <ClInclude Include="$(SolutionDir)src\System\Resource.hpp" />
<ClCompile Include="$(SolutionDir)src\System\Socket.cpp" /> <ClCompile Include="$(SolutionDir)src\System\Socket.cpp" />
<ClInclude Include="$(SolutionDir)src\System\Socket.hpp" /> <ClInclude Include="$(SolutionDir)src\System\Socket.hpp" />
<ClInclude Include="$(SolutionDir)src\System\Synchronization.hpp" />
<ClCompile Include="$(SolutionDir)src\System\Thread.cpp" /> <ClCompile Include="$(SolutionDir)src\System\Thread.cpp" />
<ClInclude Include="$(SolutionDir)src\System\Thread.hpp" /> <ClInclude Include="$(SolutionDir)src\System\Thread.hpp" />
<ClCompile Include="$(SolutionDir)src\System\Timer.cpp" /> <ClCompile Include="$(SolutionDir)src\System\Timer.cpp" />
......
...@@ -342,6 +342,9 @@ ...@@ -342,6 +342,9 @@
<ClInclude Include="$(SolutionDir)src\System\Socket.hpp"> <ClInclude Include="$(SolutionDir)src\System\Socket.hpp">
<Filter>src\System</Filter> <Filter>src\System</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="$(SolutionDir)src\System\Synchronization.hpp">
<Filter>src\System</Filter>
</ClInclude>
<ClInclude Include="$(SolutionDir)src\System\Thread.hpp"> <ClInclude Include="$(SolutionDir)src\System\Thread.hpp">
<Filter>src\System</Filter> <Filter>src\System</Filter>
</ClInclude> </ClInclude>
......
// Copyright 2019 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_Synchronization_hpp
#define sw_Synchronization_hpp
#include <condition_variable>
#include <mutex>
#include <queue>
namespace sw
{
// Chan is a thread-safe FIFO queue of type T.
// Chan takes its name after Golang's chan.
template <typename T>
class Chan
{
public:
Chan();
// take returns the next item in the chan, blocking until an item is
// available.
T take();
// tryTake returns a <T, bool> pair.
// If the chan is not empty, then the next item and true are returned.
// If the chan is empty, then a default-initialized T and false are returned.
std::pair<T, bool> tryTake();
// put places an item into the chan, blocking if the chan is bounded and
// full.
void put(const T &v);
// Returns the number of items in the chan.
// Note: that this may change as soon as the function returns, so should
// only be used for debugging.
size_t count();
private:
std::queue<T> queue;
std::mutex mutex;
std::condition_variable added;
std::condition_variable removed;
};
template <typename T>
Chan<T>::Chan() {}
template <typename T>
T Chan<T>::take()
{
std::unique_lock<std::mutex> lock(mutex);
if (queue.size() == 0)
{
// Chan empty. Wait for item to be added.
added.wait(lock, [this] { return queue.size() > 0; });
}
T out = queue.front();
queue.pop();
lock.unlock();
removed.notify_one();
return out;
}
template <typename T>
std::pair<T, bool> Chan<T>::tryTake()
{
std::unique_lock<std::mutex> lock(mutex);
if (queue.size() == 0)
{
return std::make_pair(T{}, false);
}
T out = queue.front();
queue.pop();
lock.unlock();
removed.notify_one();
return std::make_pair(out, true);
}
template <typename T>
void Chan<T>::put(const T &item)
{
std::unique_lock<std::mutex> lock(mutex);
queue.push(item);
lock.unlock();
added.notify_one();
}
template <typename T>
size_t Chan<T>::count()
{
std::unique_lock<std::mutex> lock(mutex);
auto out = queue.size();
lock.unlock();
return out;
}
} // namespace sw
#endif // sw_Synchronization_hpp
...@@ -17,10 +17,11 @@ ...@@ -17,10 +17,11 @@
#include "VkObject.hpp" #include "VkObject.hpp"
#include "Device/Renderer.hpp" #include "Device/Renderer.hpp"
#include <queue>
#include <thread> #include <thread>
#include <vulkan/vk_icd.h> #include <vulkan/vk_icd.h>
#include "System/Synchronization.hpp"
namespace sw namespace sw
{ {
class Context; class Context;
...@@ -30,91 +31,6 @@ namespace sw ...@@ -30,91 +31,6 @@ namespace sw
namespace vk namespace vk
{ {
// Chan is a thread-safe FIFO queue of type T.
// Chan takes its name after Golang's chan.
template <typename T>
class Chan
{
public:
Chan();
// take returns the next item in the chan, blocking until an item is
// available.
T take();
// tryTake returns a <T, bool> pair.
// If the chan is not empty, then the next item and true are returned.
// If the chan is empty, then a default-initialized T and false are returned.
std::pair<T, bool> tryTake();
// put places an item into the chan, blocking if the chan is bounded and
// full.
void put(const T &v);
// Returns the number of items in the chan.
// Note: that this may change as soon as the function returns, so should
// only be used for debugging.
size_t count();
private:
std::queue<T> queue;
std::mutex mutex;
std::condition_variable added;
std::condition_variable removed;
};
template <typename T>
Chan<T>::Chan() {}
template <typename T>
T Chan<T>::take()
{
std::unique_lock<std::mutex> lock(mutex);
if (queue.size() == 0)
{
// Chan empty. Wait for item to be added.
added.wait(lock, [this] { return queue.size() > 0; });
}
T out = queue.front();
queue.pop();
lock.unlock();
removed.notify_one();
return out;
}
template <typename T>
std::pair<T, bool> Chan<T>::tryTake()
{
std::unique_lock<std::mutex> lock(mutex);
if (queue.size() == 0)
{
return std::make_pair(T{}, false);
}
T out = queue.front();
queue.pop();
lock.unlock();
removed.notify_one();
return std::make_pair(out, true);
}
template <typename T>
void Chan<T>::put(const T &item)
{
std::unique_lock<std::mutex> lock(mutex);
queue.push(item);
lock.unlock();
added.notify_one();
}
template <typename T>
size_t Chan<T>::count()
{
std::unique_lock<std::mutex> lock(mutex);
auto out = queue.size();
lock.unlock();
return out;
}
class Queue class Queue
{ {
VK_LOADER_DATA loaderData = { ICD_LOADER_MAGIC }; VK_LOADER_DATA loaderData = { ICD_LOADER_MAGIC };
...@@ -152,8 +68,8 @@ private: ...@@ -152,8 +68,8 @@ private:
void submitQueue(const Task& task); void submitQueue(const Task& task);
std::unique_ptr<sw::Renderer> renderer; std::unique_ptr<sw::Renderer> renderer;
Chan<Task> pending; sw::Chan<Task> pending;
Chan<VkSubmitInfo*> toDelete; sw::Chan<VkSubmitInfo*> toDelete;
std::thread queueThread; std::thread queueThread;
}; };
......
...@@ -279,6 +279,7 @@ IF EXIST "$(SolutionDir)..\deqp\build\external\vulkancts\modules\vulkan\" (copy ...@@ -279,6 +279,7 @@ IF EXIST "$(SolutionDir)..\deqp\build\external\vulkancts\modules\vulkan\" (copy
<ClInclude Include="..\System\Resource.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\Thread.hpp" /> <ClInclude Include="..\System\Thread.hpp" />
<ClInclude Include="..\System\Timer.hpp" /> <ClInclude Include="..\System\Timer.hpp" />
<ClInclude Include="..\System\Types.hpp" /> <ClInclude Include="..\System\Types.hpp" />
......
...@@ -503,6 +503,9 @@ ...@@ -503,6 +503,9 @@
<ClInclude Include="..\System\Socket.hpp"> <ClInclude Include="..\System\Socket.hpp">
<Filter>Header Files\System</Filter> <Filter>Header Files\System</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\System\Synchronization.hpp">
<Filter>Header Files\System</Filter>
</ClInclude>
<ClInclude Include="..\System\Thread.hpp"> <ClInclude Include="..\System\Thread.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