Removed the AtomicInt class
Deleted Thread.hpp and replaced AtomicInt with std::atomic<int>.
Should be noop.
Note: AtomicInt's operator++(int) and operator--(int) were
Post-increment operators implemented as Pre-increment
operators, so expressions <AtomicInt object>++ have
been replaced with ++<std::atomic object> (same for
decrement operators).
Bug b/129403963
Change-Id: I907b8c91581fed4b76647327f316b87369c6c82f
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/33668
Tested-by: Alexis Hétu <sugoi@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 23d9310..70596df 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1749,7 +1749,6 @@
${SOURCE_DIR}/System/Socket.cpp
${SOURCE_DIR}/System/Socket.hpp
${SOURCE_DIR}/System/Synchronization.hpp
- ${SOURCE_DIR}/System/Thread.hpp
${SOURCE_DIR}/System/Timer.cpp
${SOURCE_DIR}/System/Timer.hpp
${SOURCE_DIR}/Device/*.cpp
diff --git a/src/Device/Renderer.cpp b/src/Device/Renderer.cpp
index d97c803..ddba431 100644
--- a/src/Device/Renderer.cpp
+++ b/src/Device/Renderer.cpp
@@ -42,9 +42,9 @@
namespace sw
{
static const int batchSize = 128;
- AtomicInt threadCount(1);
- AtomicInt Renderer::unitCount(1);
- AtomicInt Renderer::clusterCount(1);
+ std::atomic<int> threadCount(1);
+ std::atomic<int> Renderer::unitCount(1);
+ std::atomic<int> Renderer::clusterCount(1);
template<typename T>
inline bool setBatchIndices(unsigned int batch[128][3], VkPrimitiveTopology topology, T indices, unsigned int start, unsigned int triangleCount)
@@ -572,7 +572,7 @@
// Commit to the task queue
qHead = (qHead + 1) & TASK_COUNT_BITS;
- qSize++;
+ ++qSize; // Atomic
break;
}
@@ -613,7 +613,7 @@
count = draw->count;
int batch = draw->batchSize;
- primitiveProgress[unit].drawCall = currentDraw;
+ primitiveProgress[unit].drawCall = currentDraw.load();
primitiveProgress[unit].firstPrimitive = primitive;
primitiveProgress[unit].primitiveCount = count - primitive >= batch ? batch : count - primitive;
@@ -627,7 +627,7 @@
// Commit to the task queue
qHead = (qHead + 1) & TASK_COUNT_BITS;
- qSize++;
+ ++qSize; // Atomic
}
}
}
@@ -646,7 +646,7 @@
if(qSize != 0)
{
task[threadIndex] = taskQueue[(qHead - qSize) & TASK_COUNT_BITS];
- qSize--;
+ --qSize; // Atomic
if(curThreadsAwake != threadCount)
{
@@ -678,7 +678,7 @@
void Renderer::executeTask(int threadIndex)
{
- switch(task[threadIndex].type)
+ switch(task[threadIndex].type.load())
{
case Task::PRIMITIVES:
{
@@ -699,7 +699,7 @@
}
primitiveProgress[unit].visible = visible;
- primitiveProgress[unit].references = clusterCount;
+ primitiveProgress[unit].references = clusterCount.load();
}
break;
case Task::PIXELS:
@@ -754,11 +754,11 @@
pixelProgress[cluster].processedPrimitives = 0;
}
- int ref = primitiveProgress[unit].references--; // Atomic
+ int ref = --primitiveProgress[unit].references; // Atomic
if(ref == 0)
{
- ref = draw.references--; // Atomic
+ ref = --draw.references; // Atomic
if(ref == 0)
{
@@ -839,7 +839,7 @@
}
else
{
- switch(draw->indexType)
+ switch(draw->indexType.load())
{
case VK_INDEX_TYPE_UINT16:
if(!setBatchIndices(batch, topology, static_cast<const uint16_t*>(indices), start, triangleCount))
diff --git a/src/Device/Renderer.hpp b/src/Device/Renderer.hpp
index bfd4bb0..85367f1 100644
--- a/src/Device/Renderer.hpp
+++ b/src/Device/Renderer.hpp
@@ -22,9 +22,9 @@
#include "Blitter.hpp"
#include "Device/Config.hpp"
#include "System/Synchronization.hpp"
-#include "System/Thread.hpp"
#include "Vulkan/VkDescriptorSet.hpp"
+#include <atomic>
#include <list>
#include <mutex>
#include <thread>
@@ -111,9 +111,16 @@
SUSPEND
};
- AtomicInt type;
- AtomicInt primitiveUnit;
- AtomicInt pixelCluster;
+ void operator=(const Task& task)
+ {
+ type = task.type.load();
+ primitiveUnit = task.primitiveUnit.load();
+ pixelCluster = task.pixelCluster.load();
+ }
+
+ std::atomic<int> type;
+ std::atomic<int> primitiveUnit;
+ std::atomic<int> pixelCluster;
};
struct PrimitiveProgress
@@ -127,11 +134,11 @@
references = 0;
}
- AtomicInt drawCall;
- AtomicInt firstPrimitive;
- AtomicInt primitiveCount;
- AtomicInt visible;
- AtomicInt references;
+ std::atomic<int> drawCall;
+ std::atomic<int> firstPrimitive;
+ std::atomic<int> primitiveCount;
+ std::atomic<int> visible;
+ std::atomic<int> references;
};
struct PixelProgress
@@ -143,9 +150,9 @@
executing = false;
}
- AtomicInt drawCall;
- AtomicInt processedPrimitives;
- AtomicInt executing;
+ std::atomic<int> drawCall;
+ std::atomic<int> processedPrimitives;
+ std::atomic<int> executing;
};
public:
@@ -201,8 +208,8 @@
Triangle *triangleBatch[16];
Primitive *primitiveBatch[16];
- AtomicInt exitThreads;
- AtomicInt threadsAwake;
+ std::atomic<int> exitThreads;
+ std::atomic<int> threadsAwake;
std::thread *worker[16];
Event *resume[16]; // Events for resuming threads
Event *suspend[16]; // Events for suspending threads
@@ -219,19 +226,19 @@
DrawCall *drawCall[DRAW_COUNT];
DrawCall *drawList[DRAW_COUNT];
- AtomicInt currentDraw;
- AtomicInt nextDraw;
+ std::atomic<int> currentDraw;
+ std::atomic<int> nextDraw;
enum {
TASK_COUNT = 32, // Size of the task queue (must be power of 2)
TASK_COUNT_BITS = TASK_COUNT - 1,
};
Task taskQueue[TASK_COUNT];
- AtomicInt qHead;
- AtomicInt qSize;
+ std::atomic<int> qHead;
+ std::atomic<int> qSize;
- static AtomicInt unitCount;
- static AtomicInt clusterCount;
+ static std::atomic<int> unitCount;
+ static std::atomic<int> clusterCount;
std::mutex schedulerMutex;
@@ -255,9 +262,9 @@
~DrawCall();
- AtomicInt topology;
- AtomicInt indexType;
- AtomicInt batchSize;
+ std::atomic<int> topology;
+ std::atomic<int> indexType;
+ std::atomic<int> batchSize;
Routine *vertexRoutine;
Routine *setupRoutine;
@@ -277,9 +284,9 @@
std::list<vk::Query*> *queries;
- AtomicInt primitive; // Current primitive to enter pipeline
- AtomicInt count; // Number of primitives to render
- AtomicInt references; // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
+ std::atomic<int> primitive; // Current primitive to enter pipeline
+ std::atomic<int> count; // Number of primitives to render
+ std::atomic<int> references; // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
DrawData *data;
};
diff --git a/src/System/BUILD.gn b/src/System/BUILD.gn
index 7599617..38256f7 100644
--- a/src/System/BUILD.gn
+++ b/src/System/BUILD.gn
@@ -30,7 +30,6 @@
"Memory.hpp",
"Socket.cpp",
"Socket.hpp",
- "Thread.hpp",
"Timer.cpp",
"Timer.hpp",
]
diff --git a/src/System/Thread.hpp b/src/System/Thread.hpp
deleted file mode 100644
index 3b6770d..0000000
--- a/src/System/Thread.hpp
+++ /dev/null
@@ -1,42 +0,0 @@
-// 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_Thread_hpp
-#define sw_Thread_hpp
-
-#include <atomic>
-
-namespace sw
-{
- class AtomicInt
- {
- public:
- AtomicInt() : ai() {}
- AtomicInt(int i) : ai(i) {}
-
- inline operator int() const { return ai.load(std::memory_order_acquire); }
- inline void operator=(const AtomicInt& i) { ai.store(i.ai.load(std::memory_order_acquire), std::memory_order_release); }
- inline void operator=(int i) { ai.store(i, std::memory_order_release); }
- inline void operator--() { ai.fetch_sub(1, std::memory_order_acq_rel); }
- inline void operator++() { ai.fetch_add(1, std::memory_order_acq_rel); }
- inline int operator--(int) { return ai.fetch_sub(1, std::memory_order_acq_rel) - 1; }
- inline int operator++(int) { return ai.fetch_add(1, std::memory_order_acq_rel) + 1; }
- inline void operator-=(int i) { ai.fetch_sub(i, std::memory_order_acq_rel); }
- inline void operator+=(int i) { ai.fetch_add(i, std::memory_order_acq_rel); }
- private:
- std::atomic<int> ai;
- };
-}
-
-#endif // sw_Thread_hpp
diff --git a/src/Vulkan/vulkan.vcxproj b/src/Vulkan/vulkan.vcxproj
index fcc808d..9da32ff 100644
--- a/src/Vulkan/vulkan.vcxproj
+++ b/src/Vulkan/vulkan.vcxproj
@@ -274,7 +274,6 @@
<ClInclude Include="..\System\SharedLibrary.hpp" />
<ClInclude Include="..\System\Socket.hpp" />
<ClInclude Include="..\System\Synchronization.hpp" />
- <ClInclude Include="..\System\Thread.hpp" />
<ClInclude Include="..\System\Timer.hpp" />
<ClInclude Include="..\System\Types.hpp" />
<ClInclude Include="..\WSI\VkSurfaceKHR.hpp" />
diff --git a/src/Vulkan/vulkan.vcxproj.filters b/src/Vulkan/vulkan.vcxproj.filters
index 3181b58..4f325dc 100644
--- a/src/Vulkan/vulkan.vcxproj.filters
+++ b/src/Vulkan/vulkan.vcxproj.filters
@@ -488,9 +488,6 @@
<ClInclude Include="..\System\Synchronization.hpp">
<Filter>Header Files\System</Filter>
</ClInclude>
- <ClInclude Include="..\System\Thread.hpp">
- <Filter>Header Files\System</Filter>
- </ClInclude>
<ClInclude Include="..\System\Timer.hpp">
<Filter>Header Files\System</Filter>
</ClInclude>