blob: 62eb47aa0e11b269b06689863e5383fdc8e71e04 [file] [log] [blame]
Alexis Hetu1f23d8c2018-10-16 14:40:19 -04001// 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 Hetue1f51b92019-04-23 15:34:34 -040019#include <condition_variable>
20#include <mutex>
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040021
22namespace vk
23{
24
25class Event : public Object<Event, VkEvent>
26{
27public:
28 Event(const VkEventCreateInfo* pCreateInfo, void* mem)
29 {
30 }
31
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040032 static size_t ComputeRequiredAllocationSize(const VkEventCreateInfo* pCreateInfo)
33 {
34 return 0;
35 }
36
Alexis Hetue1f51b92019-04-23 15:34:34 -040037 void signal()
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040038 {
Alexis Hetue1f51b92019-04-23 15:34:34 -040039 std::unique_lock<std::mutex> lock(mutex);
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040040 status = VK_EVENT_SET;
Alexis Hetue1f51b92019-04-23 15:34:34 -040041 lock.unlock();
42 condition.notify_all();
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040043 }
44
45 void reset()
46 {
Alexis Hetue1f51b92019-04-23 15:34:34 -040047 std::unique_lock<std::mutex> lock(mutex);
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040048 status = VK_EVENT_RESET;
49 }
50
Alexis Hetue1f51b92019-04-23 15:34:34 -040051 VkResult getStatus()
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040052 {
Alexis Hetue1f51b92019-04-23 15:34:34 -040053 std::unique_lock<std::mutex> lock(mutex);
54 auto result = status;
55 lock.unlock();
56 return result;
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040057 }
58
Alexis Hetue1f51b92019-04-23 15:34:34 -040059 void wait()
Alexis Hetu9041bb72019-03-15 14:45:45 -040060 {
Alexis Hetue1f51b92019-04-23 15:34:34 -040061 std::unique_lock<std::mutex> lock(mutex);
62 condition.wait(lock, [this] { return status == VK_EVENT_SET; });
Alexis Hetu9041bb72019-03-15 14:45:45 -040063 }
64
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040065private:
Alexis Hetue1f51b92019-04-23 15:34:34 -040066 VkResult status = VK_EVENT_RESET; // guarded by mutex
67 std::mutex mutex;
68 std::condition_variable condition;
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040069};
70
71static inline Event* Cast(VkEvent object)
72{
Alexis Hetu67cf8a92019-05-09 17:46:07 -040073 return reinterpret_cast<Event*>(object.get());
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040074}
75
76} // namespace vk
77
78#endif // VK_EVENT_HPP_