Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 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" |
Ben Clayton | 377573c | 2020-04-03 20:36:40 +0100 | [diff] [blame] | 19 | |
| 20 | #include "marl/mutex.h" |
| 21 | #include "marl/tsa.h" |
| 22 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 23 | #include <condition_variable> |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 24 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 25 | namespace vk { |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 26 | |
| 27 | class Event : public Object<Event, VkEvent> |
| 28 | { |
| 29 | public: |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 30 | Event(const VkEventCreateInfo *pCreateInfo, void *mem) |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 31 | { |
| 32 | } |
| 33 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 34 | static size_t ComputeRequiredAllocationSize(const VkEventCreateInfo *pCreateInfo) |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 35 | { |
| 36 | return 0; |
| 37 | } |
| 38 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 39 | void signal() |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 40 | { |
Ben Clayton | 377573c | 2020-04-03 20:36:40 +0100 | [diff] [blame] | 41 | { |
| 42 | marl::lock lock(mutex); |
| 43 | status = VK_EVENT_SET; |
| 44 | } |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 45 | condition.notify_all(); |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void reset() |
| 49 | { |
Ben Clayton | 377573c | 2020-04-03 20:36:40 +0100 | [diff] [blame] | 50 | marl::lock lock(mutex); |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 51 | status = VK_EVENT_RESET; |
| 52 | } |
| 53 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 54 | VkResult getStatus() |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 55 | { |
Ben Clayton | 377573c | 2020-04-03 20:36:40 +0100 | [diff] [blame] | 56 | marl::lock lock(mutex); |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 57 | auto result = status; |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 58 | return result; |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 61 | void wait() |
Alexis Hetu | 9041bb7 | 2019-03-15 14:45:45 -0400 | [diff] [blame] | 62 | { |
Ben Clayton | 377573c | 2020-04-03 20:36:40 +0100 | [diff] [blame] | 63 | marl::lock lock(mutex); |
| 64 | lock.wait(condition, [this]() REQUIRES(mutex) { return status == VK_EVENT_SET; }); |
Alexis Hetu | 9041bb7 | 2019-03-15 14:45:45 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 67 | private: |
Ben Clayton | 377573c | 2020-04-03 20:36:40 +0100 | [diff] [blame] | 68 | marl::mutex mutex; |
| 69 | VkResult status GUARDED_BY(mutex) = VK_EVENT_RESET; |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 70 | std::condition_variable condition; |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 73 | static inline Event *Cast(VkEvent object) |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 74 | { |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 75 | return Event::Cast(object); |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 78 | } // namespace vk |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 79 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 80 | #endif // VK_EVENT_HPP_ |