blob: 9e33ba4623d7087d9405eb2a52f251cc3b16b31d [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"
Ben Clayton377573c2020-04-03 20:36:40 +010019
20#include "marl/mutex.h"
21#include "marl/tsa.h"
22
Alexis Hetue1f51b92019-04-23 15:34:34 -040023#include <condition_variable>
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040024
Nicolas Capens157ba262019-12-10 17:49:14 -050025namespace vk {
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040026
27class Event : public Object<Event, VkEvent>
28{
29public:
Ben Clayton2ed93ab2019-12-17 20:38:03 +000030 Event(const VkEventCreateInfo *pCreateInfo, void *mem)
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040031 {
32 }
33
Ben Clayton2ed93ab2019-12-17 20:38:03 +000034 static size_t ComputeRequiredAllocationSize(const VkEventCreateInfo *pCreateInfo)
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040035 {
36 return 0;
37 }
38
Alexis Hetue1f51b92019-04-23 15:34:34 -040039 void signal()
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040040 {
Ben Clayton377573c2020-04-03 20:36:40 +010041 {
42 marl::lock lock(mutex);
43 status = VK_EVENT_SET;
44 }
Alexis Hetue1f51b92019-04-23 15:34:34 -040045 condition.notify_all();
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040046 }
47
48 void reset()
49 {
Ben Clayton377573c2020-04-03 20:36:40 +010050 marl::lock lock(mutex);
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040051 status = VK_EVENT_RESET;
52 }
53
Alexis Hetue1f51b92019-04-23 15:34:34 -040054 VkResult getStatus()
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040055 {
Ben Clayton377573c2020-04-03 20:36:40 +010056 marl::lock lock(mutex);
Alexis Hetue1f51b92019-04-23 15:34:34 -040057 auto result = status;
Alexis Hetue1f51b92019-04-23 15:34:34 -040058 return result;
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040059 }
60
Alexis Hetue1f51b92019-04-23 15:34:34 -040061 void wait()
Alexis Hetu9041bb72019-03-15 14:45:45 -040062 {
Ben Clayton377573c2020-04-03 20:36:40 +010063 marl::lock lock(mutex);
64 lock.wait(condition, [this]() REQUIRES(mutex) { return status == VK_EVENT_SET; });
Alexis Hetu9041bb72019-03-15 14:45:45 -040065 }
66
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040067private:
Ben Clayton377573c2020-04-03 20:36:40 +010068 marl::mutex mutex;
69 VkResult status GUARDED_BY(mutex) = VK_EVENT_RESET;
Alexis Hetue1f51b92019-04-23 15:34:34 -040070 std::condition_variable condition;
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040071};
72
Ben Clayton2ed93ab2019-12-17 20:38:03 +000073static inline Event *Cast(VkEvent object)
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040074{
Alexis Hetubd4cf812019-06-14 15:14:07 -040075 return Event::Cast(object);
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040076}
77
Nicolas Capens157ba262019-12-10 17:49:14 -050078} // namespace vk
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040079
Ben Clayton2ed93ab2019-12-17 20:38:03 +000080#endif // VK_EVENT_HPP_