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" |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 19 | #include <condition_variable> |
| 20 | #include <mutex> |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 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 | |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 32 | static size_t ComputeRequiredAllocationSize(const VkEventCreateInfo* pCreateInfo) |
| 33 | { |
| 34 | return 0; |
| 35 | } |
| 36 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 37 | void signal() |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 38 | { |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 39 | std::unique_lock<std::mutex> lock(mutex); |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 40 | status = VK_EVENT_SET; |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 41 | lock.unlock(); |
| 42 | condition.notify_all(); |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void reset() |
| 46 | { |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 47 | std::unique_lock<std::mutex> lock(mutex); |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 48 | status = VK_EVENT_RESET; |
| 49 | } |
| 50 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 51 | VkResult getStatus() |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 52 | { |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 53 | std::unique_lock<std::mutex> lock(mutex); |
| 54 | auto result = status; |
| 55 | lock.unlock(); |
| 56 | return result; |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 59 | void wait() |
Alexis Hetu | 9041bb7 | 2019-03-15 14:45:45 -0400 | [diff] [blame] | 60 | { |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 61 | std::unique_lock<std::mutex> lock(mutex); |
| 62 | condition.wait(lock, [this] { return status == VK_EVENT_SET; }); |
Alexis Hetu | 9041bb7 | 2019-03-15 14:45:45 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 65 | private: |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 66 | VkResult status = VK_EVENT_RESET; // guarded by mutex |
| 67 | std::mutex mutex; |
| 68 | std::condition_variable condition; |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static inline Event* Cast(VkEvent object) |
| 72 | { |
Alexis Hetu | 67cf8a9 | 2019-05-09 17:46:07 -0400 | [diff] [blame] | 73 | return reinterpret_cast<Event*>(object.get()); |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | } // namespace vk |
| 77 | |
| 78 | #endif // VK_EVENT_HPP_ |