aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/Core/Listener.h
blob: a12a65d705db2d61ccca44160763b21089dc797d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//===-- Listener.h ----------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_Select_h_
#define liblldb_Select_h_

// C Includes
// C++ Includes
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>


// Other libraries and framework includes
// Project includes
#include "lldb/lldb-private.h"
#include "lldb/Host/Predicate.h"
#include "lldb/Core/Event.h"

namespace lldb_private {

class Listener
{
public:
    typedef bool (*HandleBroadcastCallback) (lldb::EventSP &event_sp, void *baton);

    friend class Broadcaster;
    friend class BroadcasterManager;

    //------------------------------------------------------------------
    // Constructors and Destructors
    //------------------------------------------------------------------
    Listener (const char *name);

    ~Listener ();

    void
    AddEvent (lldb::EventSP &event);

    void
    Clear ();

    const char *
    GetName ()
    {
        return m_name.c_str();
    }

    uint32_t
    StartListeningForEventSpec (BroadcasterManager &manager, 
                                 const BroadcastEventSpec &event_spec);
    
    bool
    StopListeningForEventSpec (BroadcasterManager &manager, 
                                 const BroadcastEventSpec &event_spec);
    
    uint32_t
    StartListeningForEvents (Broadcaster* broadcaster, 
                             uint32_t event_mask);
    
    uint32_t
    StartListeningForEvents (Broadcaster* broadcaster,
                             uint32_t event_mask,
                             HandleBroadcastCallback callback,
                             void *callback_user_data);

    bool
    StopListeningForEvents (Broadcaster* broadcaster,
                            uint32_t event_mask);

    // Returns true if an event was recieved, false if we timed out.
    bool
    WaitForEvent (const TimeValue *timeout,
                  lldb::EventSP &event_sp);

    bool
    WaitForEventForBroadcaster (const TimeValue *timeout,
                                Broadcaster *broadcaster,
                                lldb::EventSP &event_sp);

    bool
    WaitForEventForBroadcasterWithType (const TimeValue *timeout,
                                        Broadcaster *broadcaster,
                                        uint32_t event_type_mask,
                                        lldb::EventSP &event_sp);

    Event *
    PeekAtNextEvent ();

    Event *
    PeekAtNextEventForBroadcaster (Broadcaster *broadcaster);

    Event *
    PeekAtNextEventForBroadcasterWithType (Broadcaster *broadcaster,
                                           uint32_t event_type_mask);

    bool
    GetNextEvent (lldb::EventSP &event_sp);

    bool
    GetNextEventForBroadcaster (Broadcaster *broadcaster,
                                lldb::EventSP &event_sp);

    bool
    GetNextEventForBroadcasterWithType (Broadcaster *broadcaster,
                                        uint32_t event_type_mask,
                                        lldb::EventSP &event_sp);

    size_t
    HandleBroadcastEvent (lldb::EventSP &event_sp);

private:

    //------------------------------------------------------------------
    // Classes that inherit from Listener can see and modify these
    //------------------------------------------------------------------
    struct BroadcasterInfo
    {
        BroadcasterInfo(uint32_t mask, HandleBroadcastCallback cb = NULL, void *ud = NULL) :
            event_mask (mask),
            callback (cb),
            callback_user_data (ud)
        {
        }

        uint32_t event_mask;
        HandleBroadcastCallback callback;
        void *callback_user_data;
    };

    typedef std::multimap<Broadcaster*, BroadcasterInfo> broadcaster_collection;
    typedef std::list<lldb::EventSP> event_collection;
    typedef std::vector<BroadcasterManager *> broadcaster_manager_collection;

    bool
    FindNextEventInternal (Broadcaster *broadcaster,   // NULL for any broadcaster
                           const ConstString *sources, // NULL for any event
                           uint32_t num_sources,
                           uint32_t event_type_mask,
                           lldb::EventSP &event_sp,
                           bool remove);

    bool
    GetNextEventInternal (Broadcaster *broadcaster,   // NULL for any broadcaster
                          const ConstString *sources, // NULL for any event
                          uint32_t num_sources,
                          uint32_t event_type_mask,
                          lldb::EventSP &event_sp);

    bool
    WaitForEventsInternal (const TimeValue *timeout,
                           Broadcaster *broadcaster,   // NULL for any broadcaster
                           const ConstString *sources, // NULL for any event
                           uint32_t num_sources,
                           uint32_t event_type_mask,
                           lldb::EventSP &event_sp);

    std::string m_name;
    broadcaster_collection m_broadcasters;
    Mutex m_broadcasters_mutex; // Protects m_broadcasters
    event_collection m_events;
    Mutex m_events_mutex; // Protects m_broadcasters and m_events
    Predicate<bool> m_cond_wait;
    broadcaster_manager_collection m_broadcaster_managers;

    void
    BroadcasterWillDestruct (Broadcaster *);
    
    void
    BroadcasterManagerWillDestruct (BroadcasterManager *manager);
    

//    broadcaster_collection::iterator
//    FindBroadcasterWithMask (Broadcaster *broadcaster,
//                             uint32_t event_mask,
//                             bool exact);

    //------------------------------------------------------------------
    // For Listener only
    //------------------------------------------------------------------
    DISALLOW_COPY_AND_ASSIGN (Listener);
};

} // namespace lldb_private

#endif  // liblldb_Select_h_