• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // C Includes
14 // C++ Includes
15 #include <list>
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <vector>
20 
21 
22 // Other libraries and framework includes
23 // Project includes
24 #include "lldb/lldb-private.h"
25 #include "lldb/Host/Predicate.h"
26 #include "lldb/Core/Event.h"
27 
28 namespace lldb_private {
29 
30 class Listener
31 {
32 public:
33     typedef bool (*HandleBroadcastCallback) (lldb::EventSP &event_sp, void *baton);
34 
35     friend class Broadcaster;
36     friend class BroadcasterManager;
37 
38     //------------------------------------------------------------------
39     // Constructors and Destructors
40     //------------------------------------------------------------------
41     Listener (const char *name);
42 
43     ~Listener ();
44 
45     void
46     AddEvent (lldb::EventSP &event);
47 
48     void
49     Clear ();
50 
51     const char *
GetName()52     GetName ()
53     {
54         return m_name.c_str();
55     }
56 
57     uint32_t
58     StartListeningForEventSpec (BroadcasterManager &manager,
59                                  const BroadcastEventSpec &event_spec);
60 
61     bool
62     StopListeningForEventSpec (BroadcasterManager &manager,
63                                  const BroadcastEventSpec &event_spec);
64 
65     uint32_t
66     StartListeningForEvents (Broadcaster* broadcaster,
67                              uint32_t event_mask);
68 
69     uint32_t
70     StartListeningForEvents (Broadcaster* broadcaster,
71                              uint32_t event_mask,
72                              HandleBroadcastCallback callback,
73                              void *callback_user_data);
74 
75     bool
76     StopListeningForEvents (Broadcaster* broadcaster,
77                             uint32_t event_mask);
78 
79     // Returns true if an event was recieved, false if we timed out.
80     bool
81     WaitForEvent (const TimeValue *timeout,
82                   lldb::EventSP &event_sp);
83 
84     bool
85     WaitForEventForBroadcaster (const TimeValue *timeout,
86                                 Broadcaster *broadcaster,
87                                 lldb::EventSP &event_sp);
88 
89     bool
90     WaitForEventForBroadcasterWithType (const TimeValue *timeout,
91                                         Broadcaster *broadcaster,
92                                         uint32_t event_type_mask,
93                                         lldb::EventSP &event_sp);
94 
95     Event *
96     PeekAtNextEvent ();
97 
98     Event *
99     PeekAtNextEventForBroadcaster (Broadcaster *broadcaster);
100 
101     Event *
102     PeekAtNextEventForBroadcasterWithType (Broadcaster *broadcaster,
103                                            uint32_t event_type_mask);
104 
105     bool
106     GetNextEvent (lldb::EventSP &event_sp);
107 
108     bool
109     GetNextEventForBroadcaster (Broadcaster *broadcaster,
110                                 lldb::EventSP &event_sp);
111 
112     bool
113     GetNextEventForBroadcasterWithType (Broadcaster *broadcaster,
114                                         uint32_t event_type_mask,
115                                         lldb::EventSP &event_sp);
116 
117     size_t
118     HandleBroadcastEvent (lldb::EventSP &event_sp);
119 
120 private:
121 
122     //------------------------------------------------------------------
123     // Classes that inherit from Listener can see and modify these
124     //------------------------------------------------------------------
125     struct BroadcasterInfo
126     {
127         BroadcasterInfo(uint32_t mask, HandleBroadcastCallback cb = NULL, void *ud = NULL) :
event_maskBroadcasterInfo128             event_mask (mask),
129             callback (cb),
130             callback_user_data (ud)
131         {
132         }
133 
134         uint32_t event_mask;
135         HandleBroadcastCallback callback;
136         void *callback_user_data;
137     };
138 
139     typedef std::multimap<Broadcaster*, BroadcasterInfo> broadcaster_collection;
140     typedef std::list<lldb::EventSP> event_collection;
141     typedef std::vector<BroadcasterManager *> broadcaster_manager_collection;
142 
143     bool
144     FindNextEventInternal (Broadcaster *broadcaster,   // NULL for any broadcaster
145                            const ConstString *sources, // NULL for any event
146                            uint32_t num_sources,
147                            uint32_t event_type_mask,
148                            lldb::EventSP &event_sp,
149                            bool remove);
150 
151     bool
152     GetNextEventInternal (Broadcaster *broadcaster,   // NULL for any broadcaster
153                           const ConstString *sources, // NULL for any event
154                           uint32_t num_sources,
155                           uint32_t event_type_mask,
156                           lldb::EventSP &event_sp);
157 
158     bool
159     WaitForEventsInternal (const TimeValue *timeout,
160                            Broadcaster *broadcaster,   // NULL for any broadcaster
161                            const ConstString *sources, // NULL for any event
162                            uint32_t num_sources,
163                            uint32_t event_type_mask,
164                            lldb::EventSP &event_sp);
165 
166     std::string m_name;
167     broadcaster_collection m_broadcasters;
168     Mutex m_broadcasters_mutex; // Protects m_broadcasters
169     event_collection m_events;
170     Mutex m_events_mutex; // Protects m_broadcasters and m_events
171     Predicate<bool> m_cond_wait;
172     broadcaster_manager_collection m_broadcaster_managers;
173 
174     void
175     BroadcasterWillDestruct (Broadcaster *);
176 
177     void
178     BroadcasterManagerWillDestruct (BroadcasterManager *manager);
179 
180 
181 //    broadcaster_collection::iterator
182 //    FindBroadcasterWithMask (Broadcaster *broadcaster,
183 //                             uint32_t event_mask,
184 //                             bool exact);
185 
186     //------------------------------------------------------------------
187     // For Listener only
188     //------------------------------------------------------------------
189     DISALLOW_COPY_AND_ASSIGN (Listener);
190 };
191 
192 } // namespace lldb_private
193 
194 #endif  // liblldb_Select_h_
195