michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include "CustomCocoaEvents.h" michael@0: #include michael@0: #include michael@0: #include michael@0: #include "mozilla/WidgetTraceEvent.h" michael@0: michael@0: using mozilla::CondVar; michael@0: using mozilla::Mutex; michael@0: using mozilla::MutexAutoLock; michael@0: michael@0: namespace { michael@0: michael@0: Mutex* sMutex = NULL; michael@0: CondVar* sCondVar = NULL; michael@0: bool sTracerProcessed = false; michael@0: michael@0: } michael@0: michael@0: namespace mozilla { michael@0: michael@0: bool InitWidgetTracing() michael@0: { michael@0: sMutex = new Mutex("Event tracer thread mutex"); michael@0: sCondVar = new CondVar(*sMutex, "Event tracer thread condvar"); michael@0: return sMutex && sCondVar; michael@0: } michael@0: michael@0: void CleanUpWidgetTracing() michael@0: { michael@0: delete sMutex; michael@0: delete sCondVar; michael@0: sMutex = NULL; michael@0: sCondVar = NULL; michael@0: } michael@0: michael@0: // This function is called from the main (UI) thread. michael@0: void SignalTracerThread() michael@0: { michael@0: if (!sMutex || !sCondVar) michael@0: return; michael@0: MutexAutoLock lock(*sMutex); michael@0: if (!sTracerProcessed) { michael@0: sTracerProcessed = true; michael@0: sCondVar->Notify(); michael@0: } michael@0: } michael@0: michael@0: // This function is called from the background tracer thread. michael@0: bool FireAndWaitForTracerEvent() michael@0: { michael@0: NS_ABORT_IF_FALSE(sMutex && sCondVar, "Tracing not initialized!"); michael@0: NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; michael@0: MutexAutoLock lock(*sMutex); michael@0: if (sTracerProcessed) { michael@0: // Things are out of sync. This is likely because we're in michael@0: // the middle of shutting down. Just return false and hope the michael@0: // tracer thread is quitting anyway. michael@0: return false; michael@0: } michael@0: michael@0: // Post an application-defined event to the main thread's event queue michael@0: // and wait for it to get processed. michael@0: [NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined michael@0: location:NSMakePoint(0,0) michael@0: modifierFlags:0 michael@0: timestamp:0 michael@0: windowNumber:0 michael@0: context:NULL michael@0: subtype:kEventSubtypeTrace michael@0: data1:0 michael@0: data2:0] michael@0: atStart:NO]; michael@0: while (!sTracerProcessed) michael@0: sCondVar->Wait(); michael@0: sTracerProcessed = false; michael@0: [pool release]; michael@0: return true; michael@0: } michael@0: michael@0: } // namespace mozilla