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/. */
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"
12 using mozilla::CondVar;
14 using mozilla::MutexAutoLock;
19 CondVar* sCondVar = NULL;
20 bool sTracerProcessed = false;
26 bool InitWidgetTracing()
28 sMutex = new Mutex("Event tracer thread mutex");
29 sCondVar = new CondVar(*sMutex, "Event tracer thread condvar");
30 return sMutex && sCondVar;
33 void CleanUpWidgetTracing()
41 // This function is called from the main (UI) thread.
42 void SignalTracerThread()
44 if (!sMutex || !sCondVar)
46 MutexAutoLock lock(*sMutex);
47 if (!sTracerProcessed) {
48 sTracerProcessed = true;
53 // This function is called from the background tracer thread.
54 bool FireAndWaitForTracerEvent()
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.
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)
74 subtype:kEventSubtypeTrace
78 while (!sTracerProcessed)
80 sTracerProcessed = false;
85 } // namespace mozilla