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