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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
//===-- ThreadStateCoordinator.cpp ------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#if !defined (__STDC_FORMAT_MACROS)
#define __STDC_FORMAT_MACROS 1
#endif
#include <inttypes.h>
#include "ThreadStateCoordinator.h"
#include <memory>
using namespace lldb_private;
//===----------------------------------------------------------------------===//
class ThreadStateCoordinator::EventBase : public std::enable_shared_from_this<ThreadStateCoordinator::EventBase>
{
public:
EventBase ()
{
}
virtual
~EventBase ()
{
}
// Return false if the coordinator should terminate running.
virtual bool
ProcessEvent (ThreadStateCoordinator &coordinator) = 0;
};
//===----------------------------------------------------------------------===//
class ThreadStateCoordinator::EventStopCoordinator : public ThreadStateCoordinator::EventBase
{
public:
EventStopCoordinator ():
EventBase ()
{
}
~EventStopCoordinator () override
{
}
bool
ProcessEvent(ThreadStateCoordinator &coordinator) override
{
return false;
}
};
//===----------------------------------------------------------------------===//
class ThreadStateCoordinator::EventCallAfterThreadsStop : public ThreadStateCoordinator::EventBase
{
public:
EventCallAfterThreadsStop (lldb::tid_t triggering_tid,
const ThreadIDSet &wait_for_stop_tids,
const ThreadIDFunc &request_thread_stop_func,
const ThreadIDFunc &call_after_func):
EventBase (),
m_triggering_tid (triggering_tid),
m_wait_for_stop_tids (wait_for_stop_tids),
m_request_thread_stop_func (request_thread_stop_func),
m_call_after_func (call_after_func)
{
}
~EventCallAfterThreadsStop () override
{
}
lldb::tid_t GetTriggeringTID () const
{
return m_triggering_tid;
}
ThreadIDSet &
GetRemainingWaitTIDs ()
{
return m_wait_for_stop_tids;
}
bool
ProcessEvent(ThreadStateCoordinator &coordinator) override
{
// Request a stop for all the thread stops that need to be stopped
// and are not already known to be stopped. Keep a list of all the
// threads from which we still need to hear a stop reply.
ThreadIDSet sent_tids;
for (auto tid : m_wait_for_stop_tids)
{
// If we don't know about the thread's stop state or we
// know it is not stopped, we need to send it a stop request.
auto find_it = coordinator.m_tid_stop_map.find (tid);
if ((find_it == coordinator.m_tid_stop_map.end ()) || !find_it->second)
{
m_request_thread_stop_func (tid);
sent_tids.insert (tid);
}
}
// We only need to wait for the sent_tids - so swap our wait set
// to the sent tids. The rest are already stopped and we won't
// be receiving stop notifications for them.
m_wait_for_stop_tids.swap (sent_tids);
if (m_wait_for_stop_tids.empty ())
{
// We're not waiting for any threads. Fire off the deferred signal delivery event.
NotifyNow ();
}
else
{
// The real meat of this class: wait until all
// the thread stops (or thread deaths) come in
// before firing off that the triggering signal
// arrived.
coordinator.SetPendingNotification (shared_from_this ());
}
return true;
}
// Return true if still pending thread stops waiting; false if no more stops.
// If no more pending stops, signal.
bool
RemoveThreadStopRequirementAndMaybeSignal (lldb::tid_t tid)
{
// Remove this tid if it was in it.
m_wait_for_stop_tids.erase (tid);
// Fire pending notification if no pending thread stops remain.
if (m_wait_for_stop_tids.empty ())
{
// Fire the pending notification now.
NotifyNow ();
return false;
}
// Still have pending thread stops.
return true;
}
void
AddThreadStopRequirement (lldb::tid_t tid)
{
// Add this tid.
auto insert_result = m_wait_for_stop_tids.insert (tid);
// If it was really added, send the stop request to it.
if (insert_result.second)
m_request_thread_stop_func (tid);
}
private:
void
NotifyNow ()
{
m_call_after_func (m_triggering_tid);
}
const lldb::tid_t m_triggering_tid;
ThreadIDSet m_wait_for_stop_tids;
ThreadIDFunc m_request_thread_stop_func;
ThreadIDFunc m_call_after_func;
};
//===----------------------------------------------------------------------===//
class ThreadStateCoordinator::EventThreadStopped : public ThreadStateCoordinator::EventBase
{
public:
EventThreadStopped (lldb::tid_t tid):
EventBase (),
m_tid (tid)
{
}
~EventThreadStopped () override
{
}
bool
ProcessEvent(ThreadStateCoordinator &coordinator) override
{
coordinator.ThreadDidStop (m_tid);
return true;
}
private:
const lldb::tid_t m_tid;
};
//===----------------------------------------------------------------------===//
class ThreadStateCoordinator::EventThreadCreate : public ThreadStateCoordinator::EventBase
{
public:
EventThreadCreate (lldb::tid_t tid):
EventBase (),
m_tid (tid)
{
}
~EventThreadCreate () override
{
}
bool
ProcessEvent(ThreadStateCoordinator &coordinator) override
{
coordinator.ThreadWasCreated (m_tid);
return true;
}
private:
const lldb::tid_t m_tid;
};
//===----------------------------------------------------------------------===//
class ThreadStateCoordinator::EventThreadDeath : public ThreadStateCoordinator::EventBase
{
public:
EventThreadDeath (lldb::tid_t tid):
EventBase (),
m_tid (tid)
{
}
~EventThreadDeath () override
{
}
bool
ProcessEvent(ThreadStateCoordinator &coordinator) override
{
coordinator.ThreadDidDie (m_tid);
return true;
}
private:
const lldb::tid_t m_tid;
};
//===----------------------------------------------------------------------===//
ThreadStateCoordinator::ThreadStateCoordinator (const LogFunc &log_func) :
m_log_func (log_func),
m_event_queue (),
m_queue_condition (),
m_queue_mutex (),
m_tid_stop_map ()
{
}
void
ThreadStateCoordinator::EnqueueEvent (EventBaseSP event_sp)
{
std::lock_guard<std::mutex> lock (m_queue_mutex);
m_event_queue.push (event_sp);
m_queue_condition.notify_one ();
}
ThreadStateCoordinator::EventBaseSP
ThreadStateCoordinator::DequeueEventWithWait ()
{
// Wait for an event to be present.
std::unique_lock<std::mutex> lock (m_queue_mutex);
m_queue_condition.wait (lock,
[this] { return !m_event_queue.empty (); });
// Grab the event and pop it off the queue.
EventBaseSP event_sp = m_event_queue.front ();
m_event_queue.pop ();
return event_sp;
}
void
ThreadStateCoordinator::SetPendingNotification (const EventBaseSP &event_sp)
{
assert (event_sp && "null event_sp");
if (!event_sp)
return;
const EventCallAfterThreadsStop *new_call_after_event = static_cast<EventCallAfterThreadsStop*> (event_sp.get ());
if (m_pending_notification_sp)
{
const EventCallAfterThreadsStop *prev_call_after_event = static_cast<EventCallAfterThreadsStop*> (m_pending_notification_sp.get ());
// Yikes - we've already got a pending signal notification in progress.
// Log this info. We lose the pending notification here.
Log ("ThreadStateCoordinator::%s dropping existing pending signal notification for tid %" PRIu64 ", to be replaced with signal for tid %" PRIu64,
__FUNCTION__,
prev_call_after_event->GetTriggeringTID (),
new_call_after_event->GetTriggeringTID ());
}
m_pending_notification_sp = event_sp;
}
void
ThreadStateCoordinator::CallAfterThreadsStop (const lldb::tid_t triggering_tid,
const ThreadIDSet &wait_for_stop_tids,
const ThreadIDFunc &request_thread_stop_func,
const ThreadIDFunc &call_after_func)
{
EnqueueEvent (EventBaseSP (new EventCallAfterThreadsStop (triggering_tid,
wait_for_stop_tids,
request_thread_stop_func,
call_after_func)));
}
void
ThreadStateCoordinator::ThreadDidStop (lldb::tid_t tid)
{
// Update the global list of known thread states. This one is definitely stopped.
m_tid_stop_map[tid] = true;
// If we have a pending notification, remove this from the set.
if (m_pending_notification_sp)
{
EventCallAfterThreadsStop *const call_after_event = static_cast<EventCallAfterThreadsStop*> (m_pending_notification_sp.get ());
const bool pending_stops_remain = call_after_event->RemoveThreadStopRequirementAndMaybeSignal (tid);
if (!pending_stops_remain)
{
// Clear the pending notification now.
m_pending_notification_sp.reset ();
}
}
}
void
ThreadStateCoordinator::ThreadWasCreated (lldb::tid_t tid)
{
// Add the new thread to the stop map.
// We assume a created thread is not stopped.
m_tid_stop_map[tid] = false;
if (m_pending_notification_sp)
{
// Tell the pending notification that we need to wait
// for this new thread to stop.
EventCallAfterThreadsStop *const call_after_event = static_cast<EventCallAfterThreadsStop*> (m_pending_notification_sp.get ());
call_after_event->AddThreadStopRequirement (tid);
}
}
void
ThreadStateCoordinator::ThreadDidDie (lldb::tid_t tid)
{
// Update the global list of known thread states. While this one is stopped, it is also dead.
// So stop tracking it. We assume the user of this coordinator will not keep trying to add
// dependencies on a thread after it is known to be dead.
m_tid_stop_map.erase (tid);
// If we have a pending notification, remove this from the set.
if (m_pending_notification_sp)
{
EventCallAfterThreadsStop *const call_after_event = static_cast<EventCallAfterThreadsStop*> (m_pending_notification_sp.get ());
const bool pending_stops_remain = call_after_event->RemoveThreadStopRequirementAndMaybeSignal (tid);
if (!pending_stops_remain)
{
// Clear the pending notification now.
m_pending_notification_sp.reset ();
}
}
}
void
ThreadStateCoordinator::Log (const char *format, ...)
{
va_list args;
va_start (args, format);
m_log_func (format, args);
va_end (args);
}
void
ThreadStateCoordinator::NotifyThreadStop (lldb::tid_t tid)
{
EnqueueEvent (EventBaseSP (new EventThreadStopped (tid)));
}
void
ThreadStateCoordinator::NotifyThreadCreate (lldb::tid_t tid)
{
EnqueueEvent (EventBaseSP (new EventThreadCreate (tid)));
}
void
ThreadStateCoordinator::NotifyThreadDeath (lldb::tid_t tid)
{
EnqueueEvent (EventBaseSP (new EventThreadDeath (tid)));
}
void
ThreadStateCoordinator::StopCoordinator ()
{
EnqueueEvent (EventBaseSP (new EventStopCoordinator ()));
}
bool
ThreadStateCoordinator::ProcessNextEvent ()
{
return DequeueEventWithWait()->ProcessEvent (*this);
}
|