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() {
27 sMutex = new Mutex("Event tracer thread mutex");
28 sCondVar = new CondVar(*sMutex, "Event tracer thread condvar");
29 return sMutex && sCondVar;
32 void CleanUpWidgetTracing() {
39 // This function is called from the main (UI) thread.
40 void SignalTracerThread() {
41 if (!sMutex || !sCondVar) return;
42 MutexAutoLock lock(*sMutex);
43 if (!sTracerProcessed) {
44 sTracerProcessed = true;
49 // This function is called from the background tracer thread.
50 bool FireAndWaitForTracerEvent() {
51 MOZ_ASSERT(sMutex && sCondVar, "Tracing not initialized!");
52 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
53 MutexAutoLock lock(*sMutex);
54 if (sTracerProcessed) {
55 // Things are out of sync. This is likely because we're in
56 // the middle of shutting down. Just return false and hope the
57 // tracer thread is quitting anyway.
61 // Post an application-defined event to the main thread's event queue
62 // and wait for it to get processed.
63 [NSApp postEvent:[NSEvent otherEventWithType:NSEventTypeApplicationDefined
64 location:NSMakePoint(0, 0)
69 subtype:kEventSubtypeTrace
73 while (!sTracerProcessed) sCondVar->Wait();
74 sTracerProcessed = false;
79 } // namespace mozilla