/src/mozilla-central/widget/gtk/WidgetTraceEvent.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | #include "mozilla/WidgetTraceEvent.h" |
6 | | |
7 | | #include <glib.h> |
8 | | #include <mozilla/CondVar.h> |
9 | | #include <mozilla/Mutex.h> |
10 | | #include <stdio.h> |
11 | | |
12 | | using mozilla::CondVar; |
13 | | using mozilla::Mutex; |
14 | | using mozilla::MutexAutoLock; |
15 | | |
16 | | namespace { |
17 | | |
18 | | Mutex* sMutex = nullptr; |
19 | | CondVar* sCondVar = nullptr; |
20 | | bool sTracerProcessed = false; |
21 | | |
22 | | // This function is called from the main (UI) thread. |
23 | | gboolean TracerCallback(gpointer data) |
24 | 0 | { |
25 | 0 | mozilla::SignalTracerThread(); |
26 | 0 | return FALSE; |
27 | 0 | } |
28 | | |
29 | | } // namespace |
30 | | |
31 | | namespace mozilla { |
32 | | |
33 | | bool InitWidgetTracing() |
34 | 0 | { |
35 | 0 | sMutex = new Mutex("Event tracer thread mutex"); |
36 | 0 | sCondVar = new CondVar(*sMutex, "Event tracer thread condvar"); |
37 | 0 | return true; |
38 | 0 | } |
39 | | |
40 | | void CleanUpWidgetTracing() |
41 | 0 | { |
42 | 0 | delete sMutex; |
43 | 0 | delete sCondVar; |
44 | 0 | sMutex = nullptr; |
45 | 0 | sCondVar = nullptr; |
46 | 0 | } |
47 | | |
48 | | // This function is called from the background tracer thread. |
49 | | bool FireAndWaitForTracerEvent() |
50 | 0 | { |
51 | 0 | MOZ_ASSERT(sMutex && sCondVar, "Tracing not initialized!"); |
52 | 0 |
|
53 | 0 | // Send a default-priority idle event through the |
54 | 0 | // event loop, and wait for it to finish. |
55 | 0 | MutexAutoLock lock(*sMutex); |
56 | 0 | MOZ_ASSERT(!sTracerProcessed, "Tracer synchronization state is wrong"); |
57 | 0 | g_idle_add_full(G_PRIORITY_DEFAULT, |
58 | 0 | TracerCallback, |
59 | 0 | nullptr, |
60 | 0 | nullptr); |
61 | 0 | while (!sTracerProcessed) |
62 | 0 | sCondVar->Wait(); |
63 | 0 | sTracerProcessed = false; |
64 | 0 | return true; |
65 | 0 | } |
66 | | |
67 | | void SignalTracerThread() |
68 | 0 | { |
69 | 0 | if (!sMutex || !sCondVar) |
70 | 0 | return; |
71 | 0 | MutexAutoLock lock(*sMutex); |
72 | 0 | if (!sTracerProcessed) { |
73 | 0 | sTracerProcessed = true; |
74 | 0 | sCondVar->Notify(); |
75 | 0 | } |
76 | 0 | } |
77 | | |
78 | | } // namespace mozilla |