xref: /aosp_15_r20/external/swiftshader/src/Vulkan/VkTimelineSemaphore.hpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2021 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef VK_TIMELINE_SEMAPHORE_HPP_
16 #define VK_TIMELINE_SEMAPHORE_HPP_
17 
18 #include "VkConfig.hpp"
19 #include "VkObject.hpp"
20 #include "VkSemaphore.hpp"
21 
22 #include "marl/conditionvariable.h"
23 #include "marl/mutex.h"
24 
25 #include "System/Synchronization.hpp"
26 
27 #include <chrono>
28 
29 namespace vk {
30 
31 // Timeline Semaphores track a 64-bit payload instead of a binary payload.
32 //
33 // A timeline does not have a "signaled" and "unsignalled" state. Threads instead wait
34 // for the payload to become a certain value. When a thread signals the timeline, it provides
35 // a new payload that is greater than the current payload.
36 //
37 // There is no way to reset a timeline or to decrease the payload's value. A user must instead
38 // create a new timeline with a new initial payload if they desire this behavior.
39 class TimelineSemaphore : public Semaphore, public Object<TimelineSemaphore, VkSemaphore>
40 {
41 public:
42 	// WaitForAny represents a single vkWaitSemaphores() call with the
43 	// VK_SEMAPHORE_WAIT_ANY_BIT set.
44 	class WaitForAny
45 	{
46 	public:
47 		// Creates a WaitForAny object and populates it with the contents of a VkSemaphoreWaitInfo.
48 		WaitForAny(const VkSemaphoreWaitInfo *pWaitInfo);
49 		~WaitForAny();
50 
51 		void wait();
52 		template<class CLOCK, class DURATION>
53 		VkResult wait(std::chrono::time_point<CLOCK, DURATION> end_ns);
54 		void signal();
55 
56 	private:
57 		marl::mutex mutex;
58 		marl::ConditionVariable cv;
59 		// TODO(b/181683382) -- Add Thread Safety Analysis instrumentation when it can properly
60 		// analyze lambdas.
61 		bool is_signaled = false;
62 		marl::containers::vector<TimelineSemaphore *, 16> semaphores;
63 	};
64 
65 	TimelineSemaphore(const VkSemaphoreCreateInfo *pCreateInfo, void *mem, const VkAllocationCallbacks *pAllocator);
66 	TimelineSemaphore();
67 
68 	static size_t ComputeRequiredAllocationSize(const VkSemaphoreCreateInfo *pCreateInfo);
69 
70 	// Block until this semaphore is signaled with the specified value;
71 	void wait(uint64_t value);
72 
73 	// Wait until a certain amount of time has passed or until the specified value is signaled.
74 	template<class CLOCK, class DURATION>
75 	VkResult wait(uint64_t value, std::chrono::time_point<CLOCK, DURATION> end_ns);
76 
77 	// Set the payload to the specified value and signal all waiting threads.
78 	void signal(uint64_t value);
79 
80 	// Retrieve the current payload. This should not be used to make thread execution decisions as
81 	// there's no guarantee that the value returned here matches the actual payload's value.
82 	uint64_t getCounterValue();
83 
84 	// Clean up any allocated resources
85 	void destroy(const VkAllocationCallbacks *pAllocator);
86 
87 private:
88 	enum class AddWaitResult
89 	{
90 		kWaitAdded = 0,
91 		kWaitUpdated,
92 		kValueAlreadySignaled
93 	};
94 
95 	AddWaitResult addWait(WaitForAny *waitObject, uint64_t waitValue);
96 	void removeWait(WaitForAny *waitObject);
97 
98 	// Guards access to all the resources that may be accessed by other threads.
99 	// No clang Thread Safety Analysis is used on variables guarded by mutex
100 	// as there is an issue with TSA. Despite instrumenting everything properly,
101 	// compilation will fail when a lambda function uses a guarded resource.
102 	marl::mutex mutex;
103 
104 	// Entry point to the marl threading library that handles blocking and unblocking.
105 	marl::ConditionVariable cv;
106 
107 	// TODO(b/181683382) -- Add Thread Safety Analysis instrumentation when it can properly
108 	// analyze lambdas.
109 	// The 64-bit payload.
110 	uint64_t counter;
111 
112 	// All the WaitForAny objects waiting on this semaphore to reach specific values.
113 	std::map<WaitForAny *, uint64_t> any_waits;
114 };
115 
116 template<typename Clock, typename Duration>
wait(uint64_t value,const std::chrono::time_point<Clock,Duration> timeout)117 VkResult TimelineSemaphore::wait(uint64_t value,
118                                  const std::chrono::time_point<Clock, Duration> timeout)
119 {
120 	marl::lock lock(mutex);
121 	if(!cv.wait_until(lock, timeout, [&]() { return counter >= value; }))
122 	{
123 		return VK_TIMEOUT;
124 	}
125 	return VK_SUCCESS;
126 }
127 
128 template<typename Clock, typename Duration>
wait(const std::chrono::time_point<Clock,Duration> timeout)129 VkResult TimelineSemaphore::WaitForAny::wait(const std::chrono::time_point<Clock, Duration> timeout)
130 {
131 	marl::lock lock(mutex);
132 	if(!cv.wait_until(lock, timeout, [&]() { return is_signaled; }))
133 	{
134 		return VK_TIMEOUT;
135 	}
136 	return VK_SUCCESS;
137 }
138 
139 }  // namespace vk
140 
141 #endif  // VK_TIMELINE_SEMAPHORE_HPP_
142