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_EVENT_HPP_ |
16 | #define VK_EVENT_HPP_ |
17 | |
18 | #include "VkObject.hpp" |
19 | #include <condition_variable> |
20 | #include <mutex> |
21 | |
22 | namespace vk |
23 | { |
24 | |
25 | class Event : public Object<Event, VkEvent> |
26 | { |
27 | public: |
28 | Event(const VkEventCreateInfo* pCreateInfo, void* mem) |
29 | { |
30 | } |
31 | |
32 | static size_t ComputeRequiredAllocationSize(const VkEventCreateInfo* pCreateInfo) |
33 | { |
34 | return 0; |
35 | } |
36 | |
37 | void signal() |
38 | { |
39 | std::unique_lock<std::mutex> lock(mutex); |
40 | status = VK_EVENT_SET; |
41 | lock.unlock(); |
42 | condition.notify_all(); |
43 | } |
44 | |
45 | void reset() |
46 | { |
47 | std::unique_lock<std::mutex> lock(mutex); |
48 | status = VK_EVENT_RESET; |
49 | } |
50 | |
51 | VkResult getStatus() |
52 | { |
53 | std::unique_lock<std::mutex> lock(mutex); |
54 | auto result = status; |
55 | lock.unlock(); |
56 | return result; |
57 | } |
58 | |
59 | void wait() |
60 | { |
61 | std::unique_lock<std::mutex> lock(mutex); |
62 | condition.wait(lock, [this] { return status == VK_EVENT_SET; }); |
63 | } |
64 | |
65 | private: |
66 | VkResult status = VK_EVENT_RESET; // guarded by mutex |
67 | std::mutex mutex; |
68 | std::condition_variable condition; |
69 | }; |
70 | |
71 | static inline Event* Cast(VkEvent object) |
72 | { |
73 | return Event::Cast(object); |
74 | } |
75 | |
76 | } // namespace vk |
77 | |
78 | #endif // VK_EVENT_HPP_ |
79 |