Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:57

0001 //===-- Listener.h ----------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_UTILITY_LISTENER_H
0010 #define LLDB_UTILITY_LISTENER_H
0011 
0012 #include "lldb/Utility/Broadcaster.h"
0013 #include "lldb/Utility/Timeout.h"
0014 #include "lldb/lldb-defines.h"
0015 #include "lldb/lldb-forward.h"
0016 
0017 #include <condition_variable>
0018 #include <list>
0019 #include <map>
0020 #include <memory>
0021 #include <mutex>
0022 #include <ratio>
0023 #include <string>
0024 #include <vector>
0025 
0026 #include <cstddef>
0027 #include <cstdint>
0028 
0029 namespace lldb_private {
0030 class Event;
0031 }
0032 
0033 namespace lldb_private {
0034 
0035 class Listener : public std::enable_shared_from_this<Listener> {
0036 public:
0037   typedef bool (*HandleBroadcastCallback)(lldb::EventSP &event_sp, void *baton);
0038 
0039   friend class Broadcaster;
0040   friend class BroadcasterManager;
0041 
0042   // Constructors and Destructors
0043   //
0044   // Listeners have to be constructed into shared pointers - at least if you
0045   // want them to listen to Broadcasters,
0046 protected:
0047   Listener(const char *name);
0048 
0049 public:
0050   static lldb::ListenerSP MakeListener(const char *name);
0051 
0052   ~Listener();
0053 
0054   void AddEvent(lldb::EventSP &event);
0055 
0056   void Clear();
0057 
0058   const char *GetName() { return m_name.c_str(); }
0059 
0060   uint32_t
0061   StartListeningForEventSpec(const lldb::BroadcasterManagerSP &manager_sp,
0062                              const BroadcastEventSpec &event_spec);
0063 
0064   bool StopListeningForEventSpec(const lldb::BroadcasterManagerSP &manager_sp,
0065                                  const BroadcastEventSpec &event_spec);
0066 
0067   uint32_t StartListeningForEvents(Broadcaster *broadcaster,
0068                                    uint32_t event_mask);
0069 
0070   uint32_t StartListeningForEvents(Broadcaster *broadcaster,
0071                                    uint32_t event_mask,
0072                                    HandleBroadcastCallback callback,
0073                                    void *callback_user_data);
0074 
0075   bool StopListeningForEvents(Broadcaster *broadcaster, uint32_t event_mask);
0076 
0077   Event *PeekAtNextEvent();
0078 
0079   Event *PeekAtNextEventForBroadcaster(Broadcaster *broadcaster);
0080 
0081   Event *PeekAtNextEventForBroadcasterWithType(Broadcaster *broadcaster,
0082                                                uint32_t event_type_mask);
0083 
0084   // Returns true if an event was received, false if we timed out.
0085   bool GetEvent(lldb::EventSP &event_sp, const Timeout<std::micro> &timeout);
0086 
0087   bool GetEventForBroadcaster(Broadcaster *broadcaster, lldb::EventSP &event_sp,
0088                               const Timeout<std::micro> &timeout);
0089 
0090   bool GetEventForBroadcasterWithType(Broadcaster *broadcaster,
0091                                       uint32_t event_type_mask,
0092                                       lldb::EventSP &event_sp,
0093                                       const Timeout<std::micro> &timeout);
0094 
0095   size_t HandleBroadcastEvent(lldb::EventSP &event_sp);
0096 
0097 private:
0098   // Classes that inherit from Listener can see and modify these
0099   struct BroadcasterInfo {
0100     BroadcasterInfo(uint32_t mask, HandleBroadcastCallback cb = nullptr,
0101                     void *ud = nullptr)
0102         : event_mask(mask), callback(cb), callback_user_data(ud) {}
0103 
0104     uint32_t event_mask;
0105     HandleBroadcastCallback callback;
0106     void *callback_user_data;
0107   };
0108 
0109   typedef std::multimap<Broadcaster::BroadcasterImplWP, BroadcasterInfo,
0110                         std::owner_less<Broadcaster::BroadcasterImplWP>>
0111       broadcaster_collection;
0112   typedef std::list<lldb::EventSP> event_collection;
0113   typedef std::vector<lldb::BroadcasterManagerWP>
0114       broadcaster_manager_collection;
0115 
0116   bool
0117   FindNextEventInternal(std::unique_lock<std::mutex> &lock,
0118                         Broadcaster *broadcaster, // nullptr for any broadcaster
0119                         uint32_t event_type_mask, lldb::EventSP &event_sp,
0120                         bool remove);
0121 
0122   bool GetEventInternal(const Timeout<std::micro> &timeout,
0123                         Broadcaster *broadcaster, // nullptr for any broadcaster
0124                         uint32_t event_type_mask, lldb::EventSP &event_sp);
0125 
0126   std::string m_name;
0127   broadcaster_collection m_broadcasters;
0128   std::mutex m_broadcasters_mutex; // Protects m_broadcasters
0129   event_collection m_events;
0130   std::mutex m_events_mutex; // Protects m_broadcasters and m_events
0131   std::condition_variable m_events_condition;
0132   broadcaster_manager_collection m_broadcaster_managers;
0133 
0134   void BroadcasterWillDestruct(Broadcaster *);
0135 
0136   void BroadcasterManagerWillDestruct(lldb::BroadcasterManagerSP manager_sp);
0137 
0138   //    broadcaster_collection::iterator
0139   //    FindBroadcasterWithMask (Broadcaster *broadcaster,
0140   //                             uint32_t event_mask,
0141   //                             bool exact);
0142 
0143   // For Listener only
0144   Listener(const Listener &) = delete;
0145   const Listener &operator=(const Listener &) = delete;
0146 };
0147 
0148 } // namespace lldb_private
0149 
0150 #endif // LLDB_UTILITY_LISTENER_H