]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/Core/Listener.h
Vendor import of lldb trunk r338536:
[FreeBSD/FreeBSD.git] / include / lldb / Core / Listener.h
1 //===-- Listener.h ----------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_Select_h_
11 #define liblldb_Select_h_
12
13 #include "lldb/Core/Broadcaster.h" // for Broadcaster::BroadcasterImplWP
14 #include "lldb/Utility/Timeout.h"
15 #include "lldb/lldb-defines.h" // for DISALLOW_COPY_AND_ASSIGN
16 #include "lldb/lldb-forward.h" // for BroadcasterManagerWP, EventSP
17
18 #include <condition_variable>
19 #include <list>
20 #include <map>
21 #include <memory> // for owner_less, enable_shared_from_this
22 #include <mutex>
23 #include <ratio> // for micro
24 #include <string>
25 #include <vector>
26
27 #include <stddef.h> // for size_t
28 #include <stdint.h> // for uint32_t
29
30 namespace lldb_private {
31 class ConstString;
32 }
33 namespace lldb_private {
34 class Event;
35 }
36
37 namespace lldb_private {
38
39 class Listener : public std::enable_shared_from_this<Listener> {
40 public:
41   typedef bool (*HandleBroadcastCallback)(lldb::EventSP &event_sp, void *baton);
42
43   friend class Broadcaster;
44   friend class BroadcasterManager;
45
46   //------------------------------------------------------------------
47   // Constructors and Destructors
48   //------------------------------------------------------------------
49   //
50   // Listeners have to be constructed into shared pointers - at least if you
51   // want them to listen to Broadcasters,
52 protected:
53   Listener(const char *name);
54
55 public:
56   static lldb::ListenerSP MakeListener(const char *name);
57
58   ~Listener();
59
60   void AddEvent(lldb::EventSP &event);
61
62   void Clear();
63
64   const char *GetName() { return m_name.c_str(); }
65
66   uint32_t StartListeningForEventSpec(lldb::BroadcasterManagerSP manager_sp,
67                                       const BroadcastEventSpec &event_spec);
68
69   bool StopListeningForEventSpec(lldb::BroadcasterManagerSP manager_sp,
70                                  const BroadcastEventSpec &event_spec);
71
72   uint32_t StartListeningForEvents(Broadcaster *broadcaster,
73                                    uint32_t event_mask);
74
75   uint32_t StartListeningForEvents(Broadcaster *broadcaster,
76                                    uint32_t event_mask,
77                                    HandleBroadcastCallback callback,
78                                    void *callback_user_data);
79
80   bool StopListeningForEvents(Broadcaster *broadcaster, uint32_t event_mask);
81
82   Event *PeekAtNextEvent();
83
84   Event *PeekAtNextEventForBroadcaster(Broadcaster *broadcaster);
85
86   Event *PeekAtNextEventForBroadcasterWithType(Broadcaster *broadcaster,
87                                                uint32_t event_type_mask);
88
89   // Returns true if an event was received, false if we timed out.
90   bool GetEvent(lldb::EventSP &event_sp, const Timeout<std::micro> &timeout);
91
92   bool GetEventForBroadcaster(Broadcaster *broadcaster, lldb::EventSP &event_sp,
93                               const Timeout<std::micro> &timeout);
94
95   bool GetEventForBroadcasterWithType(Broadcaster *broadcaster,
96                                       uint32_t event_type_mask,
97                                       lldb::EventSP &event_sp,
98                                       const Timeout<std::micro> &timeout);
99
100   size_t HandleBroadcastEvent(lldb::EventSP &event_sp);
101
102 private:
103   //------------------------------------------------------------------
104   // Classes that inherit from Listener can see and modify these
105   //------------------------------------------------------------------
106   struct BroadcasterInfo {
107     BroadcasterInfo(uint32_t mask, HandleBroadcastCallback cb = nullptr,
108                     void *ud = nullptr)
109         : event_mask(mask), callback(cb), callback_user_data(ud) {}
110
111     uint32_t event_mask;
112     HandleBroadcastCallback callback;
113     void *callback_user_data;
114   };
115
116   typedef std::multimap<Broadcaster::BroadcasterImplWP, BroadcasterInfo,
117                         std::owner_less<Broadcaster::BroadcasterImplWP>>
118       broadcaster_collection;
119   typedef std::list<lldb::EventSP> event_collection;
120   typedef std::vector<lldb::BroadcasterManagerWP>
121       broadcaster_manager_collection;
122
123   bool
124   FindNextEventInternal(std::unique_lock<std::mutex> &lock,
125                         Broadcaster *broadcaster, // nullptr for any broadcaster
126                         const ConstString *sources, // nullptr for any event
127                         uint32_t num_sources, uint32_t event_type_mask,
128                         lldb::EventSP &event_sp, bool remove);
129
130   bool GetEventInternal(const Timeout<std::micro> &timeout,
131                         Broadcaster *broadcaster, // nullptr for any broadcaster
132                         const ConstString *sources, // nullptr for any event
133                         uint32_t num_sources, uint32_t event_type_mask,
134                         lldb::EventSP &event_sp);
135
136   std::string m_name;
137   broadcaster_collection m_broadcasters;
138   std::recursive_mutex m_broadcasters_mutex; // Protects m_broadcasters
139   event_collection m_events;
140   std::mutex m_events_mutex; // Protects m_broadcasters and m_events
141   std::condition_variable m_events_condition;
142   broadcaster_manager_collection m_broadcaster_managers;
143
144   void BroadcasterWillDestruct(Broadcaster *);
145
146   void BroadcasterManagerWillDestruct(lldb::BroadcasterManagerSP manager_sp);
147
148   //    broadcaster_collection::iterator
149   //    FindBroadcasterWithMask (Broadcaster *broadcaster,
150   //                             uint32_t event_mask,
151   //                             bool exact);
152
153   //------------------------------------------------------------------
154   // For Listener only
155   //------------------------------------------------------------------
156   DISALLOW_COPY_AND_ASSIGN(Listener);
157 };
158
159 } // namespace lldb_private
160
161 #endif // liblldb_Select_h_