1 // Copyright 2016 The Fuchsia Authors
2 //
3 // Use of this source code is governed by a MIT-style
4 // license that can be found in the LICENSE file or at
5 // https://opensource.org/licenses/MIT
6 
7 #pragma once
8 
9 #include <stdint.h>
10 
11 #include <kernel/event.h>
12 #include <object/dispatcher.h>
13 #include <object/state_observer.h>
14 
15 #include <zircon/types.h>
16 #include <fbl/canary.h>
17 #include <fbl/ref_ptr.h>
18 
19 class Event;
20 
21 class WaitStateObserver final : public StateObserver {
22 public:
WaitStateObserver()23     WaitStateObserver() : StateObserver() { }
24     ~WaitStateObserver();
25 
26     // This should be called under the handle table lock. If this succeeds, End() must be called
27     // (before the Event is destroyed).
28     zx_status_t Begin(Event* event,
29                       Handle* handle,
30                       zx_signals_t watched_signals);
31 
32     // This should *not* be called under the handle table lock.
33     zx_signals_t End();
34 
35 private:
36     WaitStateObserver(const WaitStateObserver&) = delete;
37     WaitStateObserver& operator=(const WaitStateObserver&) = delete;
38 
39     // StateObserver implementation:
40     Flags OnInitialize(zx_signals_t initial_state, const StateObserver::CountInfo* cinfo) final;
41     Flags OnStateChange(zx_signals_t new_state) final;
42     Flags OnCancel(const Handle* handle) final;
43 
44     fbl::Canary<fbl::magic("WTSO")> canary_;
45 
46     Event* event_ = nullptr;
47     Handle* handle_ = nullptr;
48     zx_signals_t watched_signals_ = 0u;
49     zx_signals_t wakeup_reasons_;
50     fbl::RefPtr<Dispatcher> dispatcher_;  // Non-null only between Begin() and End().
51 };
52