1 //===-- Broadcaster.h -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Broadcaster_h_
11 #define liblldb_Broadcaster_h_
19 // Other libraries and framework includes
21 #include "lldb/lldb-private.h"
22 //#include "lldb/Core/Flags.h"
23 #include "lldb/Core/ConstString.h"
24 #include "lldb/Core/Listener.h"
26 namespace lldb_private {
28 //----------------------------------------------------------------------
29 // lldb::BroadcastEventSpec
31 // This class is used to specify a kind of event to register for. The Debugger
32 // maintains a list of BroadcastEventSpec's and when it is made
33 //----------------------------------------------------------------------
34 class BroadcastEventSpec
37 BroadcastEventSpec (const ConstString &broadcaster_class, uint32_t event_bits) :
38 m_broadcaster_class (broadcaster_class),
39 m_event_bits (event_bits)
43 BroadcastEventSpec (const BroadcastEventSpec &rhs);
45 ~BroadcastEventSpec() = default;
47 const ConstString &GetBroadcasterClass() const
49 return m_broadcaster_class;
52 uint32_t GetEventBits () const
57 // Tell whether this BroadcastEventSpec is contained in in_spec.
59 // (a) the two spec's share the same broadcaster class
60 // (b) the event bits of this spec are wholly contained in those of in_spec.
61 bool IsContainedIn (BroadcastEventSpec in_spec) const
63 if (m_broadcaster_class != in_spec.GetBroadcasterClass())
65 uint32_t in_bits = in_spec.GetEventBits();
66 if (in_bits == m_event_bits)
70 if ((m_event_bits & in_bits) != 0
71 && (m_event_bits & ~in_bits) == 0)
77 bool operator< (const BroadcastEventSpec &rhs) const;
78 const BroadcastEventSpec &operator= (const BroadcastEventSpec &rhs);
81 ConstString m_broadcaster_class;
82 uint32_t m_event_bits;
85 class BroadcasterManager
88 friend class Listener;
90 BroadcasterManager ();
92 ~BroadcasterManager() = default;
95 RegisterListenerForEvents (Listener &listener, BroadcastEventSpec event_spec);
98 UnregisterListenerForEvents (Listener &listener, BroadcastEventSpec event_spec);
101 GetListenerForEventSpec (BroadcastEventSpec event_spec) const;
104 SignUpListenersForBroadcaster (Broadcaster &broadcaster);
107 RemoveListener (Listener &Listener);
113 typedef std::pair<BroadcastEventSpec, Listener *> event_listener_key;
114 typedef std::map<BroadcastEventSpec, Listener *> collection;
115 typedef std::set<Listener *> listener_collection;
116 collection m_event_map;
117 listener_collection m_listeners;
119 Mutex m_manager_mutex;
121 // A couple of comparator classes for find_if:
123 class BroadcasterClassMatches
126 BroadcasterClassMatches (const ConstString &broadcaster_class) :
127 m_broadcaster_class (broadcaster_class)
131 ~BroadcasterClassMatches() = default;
133 bool operator() (const event_listener_key input) const
135 return (input.first.GetBroadcasterClass() == m_broadcaster_class);
139 ConstString m_broadcaster_class;
142 class BroadcastEventSpecMatches
145 BroadcastEventSpecMatches (BroadcastEventSpec broadcaster_spec) :
146 m_broadcaster_spec (broadcaster_spec)
150 ~BroadcastEventSpecMatches() = default;
152 bool operator() (const event_listener_key input) const
154 return (input.first.IsContainedIn (m_broadcaster_spec));
158 BroadcastEventSpec m_broadcaster_spec;
161 class ListenerMatchesAndSharedBits
164 ListenerMatchesAndSharedBits (BroadcastEventSpec broadcaster_spec,
165 const Listener &listener) :
166 m_broadcaster_spec (broadcaster_spec),
167 m_listener (&listener)
171 ~ListenerMatchesAndSharedBits() = default;
173 bool operator() (const event_listener_key input) const
175 return (input.first.GetBroadcasterClass() == m_broadcaster_spec.GetBroadcasterClass()
176 && (input.first.GetEventBits() & m_broadcaster_spec.GetEventBits()) != 0
177 && input.second == m_listener);
181 BroadcastEventSpec m_broadcaster_spec;
182 const Listener *m_listener;
185 class ListenerMatches
188 ListenerMatches (const Listener &in_listener) :
189 m_listener (&in_listener)
193 ~ListenerMatches() = default;
195 bool operator () (const event_listener_key input) const
197 if (input.second == m_listener)
204 const Listener *m_listener;
209 //----------------------------------------------------------------------
210 /// @class Broadcaster Broadcaster.h "lldb/Core/Broadcaster.h"
211 /// @brief An event broadcasting class.
213 /// The Broadcaster class is designed to be subclassed by objects that
214 /// wish to vend events in a multi-threaded environment. Broadcaster
215 /// objects can each vend 32 events. Each event is represented by a bit
216 /// in a 32 bit value and these bits can be set:
217 /// @see Broadcaster::SetEventBits(uint32_t)
219 /// @see Broadcaster::ResetEventBits(uint32_t)
220 /// When an event gets set the Broadcaster object will notify the
221 /// Listener object that is listening for the event (if there is one).
223 /// Subclasses should provide broadcast bit definitions for any events
224 /// they vend, typically using an enumeration:
226 /// class Foo : public Broadcaster
229 /// //----------------------------------------------------------
230 /// // Broadcaster event bits definitions.
231 /// //----------------------------------------------------------
234 /// eBroadcastBitStateChanged = (1 << 0),
235 /// eBroadcastBitInterrupt = (1 << 1),
236 /// eBroadcastBitSTDOUT = (1 << 2),
237 /// eBroadcastBitSTDERR = (1 << 3),
238 /// eBroadcastBitProfileData = (1 << 4)
241 //----------------------------------------------------------------------
245 //------------------------------------------------------------------
246 /// Construct with a broadcaster with a name.
249 /// A NULL terminated C string that contains the name of the
250 /// broadcaster object.
251 //------------------------------------------------------------------
252 Broadcaster (BroadcasterManager *manager, const char *name);
254 //------------------------------------------------------------------
257 /// The destructor is virtual since this class gets subclassed.
258 //------------------------------------------------------------------
263 CheckInWithManager ();
265 //------------------------------------------------------------------
266 /// Broadcast an event which has no associated data.
268 /// @param[in] event_type
269 /// The element from the enum defining this broadcaster's events
270 /// that is being broadcast.
272 /// @param[in] event_data
273 /// User event data that will be owned by the lldb::Event that
274 /// is created internally.
276 /// @param[in] unique
277 /// If true, then only add an event of this type if there isn't
278 /// one already in the queue.
280 //------------------------------------------------------------------
282 BroadcastEvent (lldb::EventSP &event_sp);
285 BroadcastEventIfUnique (lldb::EventSP &event_sp);
288 BroadcastEvent(uint32_t event_type, EventData *event_data = nullptr);
291 BroadcastEventIfUnique(uint32_t event_type, EventData *event_data = nullptr);
297 AddInitialEventsToListener (Listener *listener, uint32_t requested_events);
299 //------------------------------------------------------------------
300 /// Listen for any events specified by \a event_mask.
302 /// Only one listener can listen to each event bit in a given
303 /// Broadcaster. Once a listener has acquired an event bit, no
304 /// other broadcaster will have access to it until it is
305 /// relinquished by the first listener that gets it. The actual
306 /// event bits that get acquired by \a listener may be different
307 /// from what is requested in \a event_mask, and to track this the
308 /// actual event bits that are acquired get returned.
310 /// @param[in] listener
311 /// The Listener object that wants to monitor the events that
312 /// get broadcast by this object.
314 /// @param[in] event_mask
315 /// A bit mask that indicates which events the listener is
316 /// asking to monitor.
319 /// The actual event bits that were acquired by \a listener.
320 //------------------------------------------------------------------
322 AddListener (Listener* listener, uint32_t event_mask);
324 //------------------------------------------------------------------
325 /// Get the NULL terminated C string name of this Broadcaster
329 /// The NULL terminated C string name of this Broadcaster.
330 //------------------------------------------------------------------
332 GetBroadcasterName ();
334 //------------------------------------------------------------------
335 /// Get the event name(s) for one or more event bits.
337 /// @param[in] event_mask
338 /// A bit mask that indicates which events to get names for.
341 /// The NULL terminated C string name of this Broadcaster.
342 //------------------------------------------------------------------
344 GetEventNames (Stream &s, const uint32_t event_mask, bool prefix_with_broadcaster_name) const;
346 //------------------------------------------------------------------
347 /// Set the name for an event bit.
349 /// @param[in] event_mask
350 /// A bit mask that indicates which events the listener is
351 /// asking to monitor.
354 /// The NULL terminated C string name of this Broadcaster.
355 //------------------------------------------------------------------
357 SetEventName (uint32_t event_mask, const char *name)
359 m_event_names[event_mask] = name;
363 GetEventName (uint32_t event_mask) const
365 const auto pos = m_event_names.find (event_mask);
366 if (pos != m_event_names.end())
367 return pos->second.c_str();
372 EventTypeHasListeners (uint32_t event_type);
374 //------------------------------------------------------------------
375 /// Removes a Listener from this broadcasters list and frees the
376 /// event bits specified by \a event_mask that were previously
377 /// acquired by \a listener (assuming \a listener was listening to
378 /// this object) for other listener objects to use.
380 /// @param[in] listener
381 /// A Listener object that previously called AddListener.
383 /// @param[in] event_mask
384 /// The event bits \a listener wishes to relinquish.
387 /// \b True if the listener was listening to this broadcaster
388 /// and was removed, \b false otherwise.
390 /// @see uint32_t Broadcaster::AddListener (Listener*, uint32_t)
391 //------------------------------------------------------------------
393 RemoveListener (Listener* listener, uint32_t event_mask = UINT32_MAX);
395 //------------------------------------------------------------------
396 /// Provides a simple mechanism to temporarily redirect events from
397 /// broadcaster. When you call this function passing in a listener and
398 /// event type mask, all events from the broadcaster matching the mask
399 /// will now go to the hijacking listener.
400 /// Only one hijack can occur at a time. If we need more than this we
401 /// will have to implement a Listener stack.
403 /// @param[in] listener
404 /// A Listener object. You do not need to call StartListeningForEvents
405 /// for this broadcaster (that would fail anyway since the event bits
406 /// would most likely be taken by the listener(s) you are usurping.
408 /// @param[in] event_mask
409 /// The event bits \a listener wishes to hijack.
412 /// \b True if the event mask could be hijacked, \b false otherwise.
414 /// @see uint32_t Broadcaster::AddListener (Listener*, uint32_t)
415 //------------------------------------------------------------------
417 HijackBroadcaster (Listener *listener, uint32_t event_mask = UINT32_MAX);
420 IsHijackedForEvent (uint32_t event_mask);
422 //------------------------------------------------------------------
423 /// Restore the state of the Broadcaster from a previous hijack attempt.
425 //------------------------------------------------------------------
427 RestoreBroadcaster ();
429 // This needs to be filled in if you are going to register the broadcaster with the broadcaster
430 // manager and do broadcaster class matching.
431 // FIXME: Probably should make a ManagedBroadcaster subclass with all the bits needed to work
432 // with the BroadcasterManager, so that it is clearer how to add one.
433 virtual ConstString &GetBroadcasterClass() const;
435 BroadcasterManager *GetManager();
439 PrivateBroadcastEvent (lldb::EventSP &event_sp, bool unique);
441 //------------------------------------------------------------------
442 // Classes that inherit from Broadcaster can see and modify these
443 //------------------------------------------------------------------
444 typedef std::vector< std::pair<Listener*,uint32_t> > collection;
445 typedef std::map<uint32_t, std::string> event_names_map;
446 // Prefix the name of our member variables with "m_broadcaster_"
447 // since this is a class that gets subclassed.
448 const ConstString m_broadcaster_name; ///< The name of this broadcaster object.
449 event_names_map m_event_names; ///< Optionally define event names for readability and logging for each event bit
450 collection m_listeners; ///< A list of Listener / event_mask pairs that are listening to this broadcaster.
451 Mutex m_listeners_mutex; ///< A mutex that protects \a m_listeners.
452 std::vector<Listener *> m_hijacking_listeners; // A simple mechanism to intercept events from a broadcaster
453 std::vector<uint32_t> m_hijacking_masks; // At some point we may want to have a stack or Listener
454 // collections, but for now this is just for private hijacking.
455 BroadcasterManager *m_manager;
458 //------------------------------------------------------------------
459 // For Broadcaster only
460 //------------------------------------------------------------------
461 DISALLOW_COPY_AND_ASSIGN (Broadcaster);
464 } // namespace lldb_private
466 #endif // liblldb_Broadcaster_h_