1 : // Copyright 2015 Google Inc. 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 : #include "syzygy/bard/events/linked_event.h"
16 :
17 : namespace bard {
18 : namespace events {
19 :
20 E : LinkedEvent::LinkedEvent(scoped_ptr<EventInterface> event) {
21 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), event.get());
22 E : event_ = event.Pass();
23 E : }
24 :
25 E : bool LinkedEvent::Play(void* backdrop) {
26 E : DCHECK_NE(static_cast<void*>(nullptr), backdrop);
27 :
28 E : for (auto& dep : deps_) {
29 E : DCHECK_NE(static_cast<base::WaitableEvent*>(nullptr),
30 : dep->waitable_event_.get());
31 E : dep->waitable_event_->Wait();
32 E : }
33 :
34 : // Play the wrapped event.
35 E : if (!event_->Play(backdrop))
36 i : return false;
37 :
38 : // If this LinkedEvent is itself an input dependency of another
39 : // LinkedEvent then fire the signal.
40 E : if (waitable_event_.get())
41 E : waitable_event_->Signal();
42 :
43 E : return true;
44 E : }
45 :
46 E : bool LinkedEvent::Equals(const EventInterface* rhs) const {
47 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), rhs);
48 E : if (rhs->type() != kLinkedEvent)
49 i : return false;
50 E : const auto* e = reinterpret_cast<const LinkedEvent*>(rhs);
51 E : if (!this->event_->Equals(e->event_.get()))
52 i : return false;
53 :
54 : // Check that the dependencies are the same in number and content.
55 E : if (deps_.size() != e->deps_.size())
56 i : return false;
57 E : for (size_t i = 0; i < deps_.size(); ++i) {
58 E : const LinkedEvent* dep1 = deps_[i];
59 E : const LinkedEvent* dep2 = e->deps_[i];
60 E : if (!dep1->event_->Equals(dep2->event()))
61 i : return false;
62 E : }
63 :
64 E : return true;
65 E : }
66 :
67 : // static
68 : bool LinkedEvent::Save(const EventInterface* const event,
69 E : core::OutArchive* out_archive) {
70 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), event);
71 E : DCHECK_NE(static_cast<core::OutArchive*>(nullptr), out_archive);
72 E : if (event->type() != kLinkedEvent)
73 i : return false;
74 E : const auto* e = reinterpret_cast<const LinkedEvent*>(event);
75 E : return EventInterface::Save(e->event_.get(), out_archive);
76 E : }
77 :
78 : // static
79 E : scoped_ptr<LinkedEvent> LinkedEvent::Load(core::InArchive* in_archive) {
80 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
81 E : scoped_ptr<EventInterface> e = EventInterface::Load(in_archive);
82 E : return scoped_ptr<LinkedEvent>(new LinkedEvent(e.Pass()));
83 E : }
84 :
85 E : bool LinkedEvent::AddDep(EventInterface* dep) {
86 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), dep);
87 :
88 E : if (dep->type() != kLinkedEvent)
89 i : return false;
90 :
91 : // Get the underlying LinkedEvent. If this event hasn't yet been used as an
92 : // input dependency then allocate a waitable_event_ so that it can work as
93 : // one.
94 E : LinkedEvent* e = reinterpret_cast<LinkedEvent*>(dep);
95 E : if (!e->waitable_event_.get())
96 E : e->waitable_event_.reset(new base::WaitableEvent(true, false));
97 :
98 E : deps_.push_back(e);
99 E : return true;
100 E : }
101 :
102 : } // namespace events
103 : } // namespace bard
|