|
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 <Cocoa/Cocoa.h> |
|
6 #include "CustomCocoaEvents.h" |
|
7 #include <Foundation/NSAutoreleasePool.h> |
|
8 #include <mozilla/CondVar.h> |
|
9 #include <mozilla/Mutex.h> |
|
10 #include "mozilla/WidgetTraceEvent.h" |
|
11 |
|
12 using mozilla::CondVar; |
|
13 using mozilla::Mutex; |
|
14 using mozilla::MutexAutoLock; |
|
15 |
|
16 namespace { |
|
17 |
|
18 Mutex* sMutex = NULL; |
|
19 CondVar* sCondVar = NULL; |
|
20 bool sTracerProcessed = false; |
|
21 |
|
22 } |
|
23 |
|
24 namespace mozilla { |
|
25 |
|
26 bool InitWidgetTracing() |
|
27 { |
|
28 sMutex = new Mutex("Event tracer thread mutex"); |
|
29 sCondVar = new CondVar(*sMutex, "Event tracer thread condvar"); |
|
30 return sMutex && sCondVar; |
|
31 } |
|
32 |
|
33 void CleanUpWidgetTracing() |
|
34 { |
|
35 delete sMutex; |
|
36 delete sCondVar; |
|
37 sMutex = NULL; |
|
38 sCondVar = NULL; |
|
39 } |
|
40 |
|
41 // This function is called from the main (UI) thread. |
|
42 void SignalTracerThread() |
|
43 { |
|
44 if (!sMutex || !sCondVar) |
|
45 return; |
|
46 MutexAutoLock lock(*sMutex); |
|
47 if (!sTracerProcessed) { |
|
48 sTracerProcessed = true; |
|
49 sCondVar->Notify(); |
|
50 } |
|
51 } |
|
52 |
|
53 // This function is called from the background tracer thread. |
|
54 bool FireAndWaitForTracerEvent() |
|
55 { |
|
56 NS_ABORT_IF_FALSE(sMutex && sCondVar, "Tracing not initialized!"); |
|
57 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
|
58 MutexAutoLock lock(*sMutex); |
|
59 if (sTracerProcessed) { |
|
60 // Things are out of sync. This is likely because we're in |
|
61 // the middle of shutting down. Just return false and hope the |
|
62 // tracer thread is quitting anyway. |
|
63 return false; |
|
64 } |
|
65 |
|
66 // Post an application-defined event to the main thread's event queue |
|
67 // and wait for it to get processed. |
|
68 [NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined |
|
69 location:NSMakePoint(0,0) |
|
70 modifierFlags:0 |
|
71 timestamp:0 |
|
72 windowNumber:0 |
|
73 context:NULL |
|
74 subtype:kEventSubtypeTrace |
|
75 data1:0 |
|
76 data2:0] |
|
77 atStart:NO]; |
|
78 while (!sTracerProcessed) |
|
79 sCondVar->Wait(); |
|
80 sTracerProcessed = false; |
|
81 [pool release]; |
|
82 return true; |
|
83 } |
|
84 |
|
85 } // namespace mozilla |