1// Copyright 2018 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_FENCE_HPP_
16#define VK_FENCE_HPP_
17
18#include "VkObject.hpp"
19#include "System/Synchronization.hpp"
20
21#include "marl/containers.h"
22#include "marl/event.h"
23#include "marl/waitgroup.h"
24
25namespace vk
26{
27
28class Fence : public Object<Fence, VkFence>, public sw::TaskEvents
29{
30public:
31 Fence(const VkFenceCreateInfo* pCreateInfo, void* mem) :
32 event(marl::Event::Mode::Manual, (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) != 0) {}
33
34 static size_t ComputeRequiredAllocationSize(const VkFenceCreateInfo* pCreateInfo)
35 {
36 return 0;
37 }
38
39 void reset()
40 {
41 event.clear();
42 }
43
44 VkResult getStatus()
45 {
46 return event.isSignalled() ? VK_SUCCESS : VK_NOT_READY;
47 }
48
49 VkResult wait()
50 {
51 event.wait();
52 return VK_SUCCESS;
53 }
54
55 template <class CLOCK, class DURATION>
56 VkResult wait(const std::chrono::time_point<CLOCK, DURATION>& timeout)
57 {
58 return event.wait_until(timeout) ? VK_SUCCESS : VK_TIMEOUT;
59 }
60
61 const marl::Event& getEvent() const { return event; }
62
63 // TaskEvents compliance
64 void start() override
65 {
66 ASSERT(!event.isSignalled());
67 wg.add();
68 }
69
70 void finish() override
71 {
72 ASSERT(!event.isSignalled());
73 if (wg.done())
74 {
75 event.signal();
76 }
77 }
78
79private:
80 Fence(const Fence&) = delete;
81
82 marl::WaitGroup wg;
83 const marl::Event event;
84};
85
86static inline Fence* Cast(VkFence object)
87{
88 return Fence::Cast(object);
89}
90
91} // namespace vk
92
93#endif // VK_FENCE_HPP_
94