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: Alexis Hétu <sugoi@google.com>
Presubmit-Ready: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 90ca0eb..7d3f3aa 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,4 +1,4 @@
-cmake_minimum_required(VERSION 2.8)
+cmake_minimum_required(VERSION 2.8)
 
 project(SwiftShader C CXX)
 
@@ -1653,8 +1653,6 @@
     ${SOURCE_DIR}/System/Math.hpp
     ${SOURCE_DIR}/System/Memory.cpp
     ${SOURCE_DIR}/System/Memory.hpp
-    ${SOURCE_DIR}/System/Resource.cpp
-    ${SOURCE_DIR}/System/Resource.hpp
     ${SOURCE_DIR}/System/Socket.cpp
     ${SOURCE_DIR}/System/Socket.hpp
     ${SOURCE_DIR}/System/Synchronization.hpp
diff --git a/src/Android.bp b/src/Android.bp
index edb927a..d9ff88d 100644
--- a/src/Android.bp
+++ b/src/Android.bp
@@ -652,7 +652,6 @@
         "System/Half.cpp",
         "System/Math.cpp",
         "System/Memory.cpp",
-        "System/Resource.cpp",
         "System/Socket.cpp",
         "System/Timer.cpp",
         "System/DebugAndroid.cpp",
diff --git a/src/Device/Renderer.cpp b/src/Device/Renderer.cpp
index 6ed66ff..5e41029 100644
--- a/src/Device/Renderer.cpp
+++ b/src/Device/Renderer.cpp
@@ -22,7 +22,6 @@
 #include "Pipeline/Constants.hpp"
 #include "System/CPUID.hpp"
 #include "System/Memory.hpp"
-#include "System/Resource.hpp"
 #include "System/Half.hpp"
 #include "System/Math.hpp"
 #include "System/Timer.hpp"
@@ -249,16 +248,12 @@
 
 		swiftConfig = new SwiftConfig(disableServer);
 		updateConfiguration(true);
-
-		sync = new Resource(0);
 	}
 
 	Renderer::~Renderer()
 	{
-		sync->lock(EXCLUSIVE);
-		sync->destruct();
+		sync.wait();
 		terminateThreads();
-		sync->unlock();
 
 		delete resumeApp;
 		resumeApp = nullptr;
@@ -318,7 +313,7 @@
 			return;
 		}
 
-		sync->lock(sw::PRIVATE);
+		sync.add();
 
 		if(update)
 		{
@@ -810,8 +805,7 @@
 
 	void Renderer::synchronize()
 	{
-		sync->lock(sw::PUBLIC);
-		sync->unlock();
+		sync.wait();
 	}
 
 	void Renderer::finishRendering(Task &pixelTask)
@@ -884,7 +878,7 @@
 					draw.events = nullptr;
 				}
 
-				sync->unlock();
+				sync.done();
 
 				draw.references = -1;
 				resumeApp->signal();
diff --git a/src/Device/Renderer.hpp b/src/Device/Renderer.hpp
index e8242e0..3f02609 100644
--- a/src/Device/Renderer.hpp
+++ b/src/Device/Renderer.hpp
@@ -300,7 +300,7 @@
 		SwiftConfig *swiftConfig;
 
 		std::list<vk::Query*> queries;
-		Resource *sync;
+		WaitGroup sync;
 
 		VertexProcessor::State vertexState;
 		SetupProcessor::State setupState;
diff --git a/src/System/BUILD.gn b/src/System/BUILD.gn
index d72bcab..7599617 100644
--- a/src/System/BUILD.gn
+++ b/src/System/BUILD.gn
@@ -28,8 +28,6 @@
     "Math.hpp",
     "Memory.cpp",
     "Memory.hpp",
-    "Resource.cpp",
-    "Resource.hpp",
     "Socket.cpp",
     "Socket.hpp",
     "Thread.hpp",
diff --git a/src/System/Resource.cpp b/src/System/Resource.cpp
deleted file mode 100644
index d0bd655..0000000
--- a/src/System/Resource.cpp
+++ /dev/null
@@ -1,108 +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.
-
-#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;
-	}
-}
diff --git a/src/System/Resource.hpp b/src/System/Resource.hpp
deleted file mode 100644
index bff79a1..0000000
--- a/src/System/Resource.hpp
+++ /dev/null
@@ -1,107 +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_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
diff --git a/src/Vulkan/vulkan.vcxproj b/src/Vulkan/vulkan.vcxproj
index 5d5b8cc..e249529 100644
--- a/src/Vulkan/vulkan.vcxproj
+++ b/src/Vulkan/vulkan.vcxproj
@@ -178,7 +178,6 @@
     <ClCompile Include="..\System\Half.cpp" />

     <ClCompile Include="..\System\Math.cpp" />

     <ClCompile Include="..\System\Memory.cpp" />

-    <ClCompile Include="..\System\Resource.cpp" />

     <ClCompile Include="..\System\Socket.cpp" />

     <ClCompile Include="..\System\Timer.cpp" />

     <ClCompile Include="..\WSI\VkSurfaceKHR.cpp" />

@@ -274,7 +273,6 @@
     <ClInclude Include="..\System\Half.hpp" />

     <ClInclude Include="..\System\Math.hpp" />

     <ClInclude Include="..\System\Memory.hpp" />

-    <ClInclude Include="..\System\Resource.hpp" />

     <ClInclude Include="..\System\SharedLibrary.hpp" />

     <ClInclude Include="..\System\Socket.hpp" />

     <ClInclude Include="..\System\Synchronization.hpp" />

diff --git a/src/Vulkan/vulkan.vcxproj.filters b/src/Vulkan/vulkan.vcxproj.filters
index 30c24be..c25de97 100644
--- a/src/Vulkan/vulkan.vcxproj.filters
+++ b/src/Vulkan/vulkan.vcxproj.filters
@@ -150,9 +150,6 @@
     <ClCompile Include="..\System\Memory.cpp">

       <Filter>Source Files\System</Filter>

     </ClCompile>

-    <ClCompile Include="..\System\Resource.cpp">

-      <Filter>Source Files\System</Filter>

-    </ClCompile>

     <ClCompile Include="..\System\Socket.cpp">

       <Filter>Source Files\System</Filter>

     </ClCompile>

@@ -488,9 +485,6 @@
     <ClInclude Include="..\System\Memory.hpp">

       <Filter>Header Files\System</Filter>

     </ClInclude>

-    <ClInclude Include="..\System\Resource.hpp">

-      <Filter>Header Files\System</Filter>

-    </ClInclude>

     <ClInclude Include="..\System\SharedLibrary.hpp">

       <Filter>Header Files\System</Filter>

     </ClInclude>