1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/cocoa/WidgetTraceEvent.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include <Cocoa/Cocoa.h> 1.9 +#include "CustomCocoaEvents.h" 1.10 +#include <Foundation/NSAutoreleasePool.h> 1.11 +#include <mozilla/CondVar.h> 1.12 +#include <mozilla/Mutex.h> 1.13 +#include "mozilla/WidgetTraceEvent.h" 1.14 + 1.15 +using mozilla::CondVar; 1.16 +using mozilla::Mutex; 1.17 +using mozilla::MutexAutoLock; 1.18 + 1.19 +namespace { 1.20 + 1.21 +Mutex* sMutex = NULL; 1.22 +CondVar* sCondVar = NULL; 1.23 +bool sTracerProcessed = false; 1.24 + 1.25 +} 1.26 + 1.27 +namespace mozilla { 1.28 + 1.29 +bool InitWidgetTracing() 1.30 +{ 1.31 + sMutex = new Mutex("Event tracer thread mutex"); 1.32 + sCondVar = new CondVar(*sMutex, "Event tracer thread condvar"); 1.33 + return sMutex && sCondVar; 1.34 +} 1.35 + 1.36 +void CleanUpWidgetTracing() 1.37 +{ 1.38 + delete sMutex; 1.39 + delete sCondVar; 1.40 + sMutex = NULL; 1.41 + sCondVar = NULL; 1.42 +} 1.43 + 1.44 +// This function is called from the main (UI) thread. 1.45 +void SignalTracerThread() 1.46 +{ 1.47 + if (!sMutex || !sCondVar) 1.48 + return; 1.49 + MutexAutoLock lock(*sMutex); 1.50 + if (!sTracerProcessed) { 1.51 + sTracerProcessed = true; 1.52 + sCondVar->Notify(); 1.53 + } 1.54 +} 1.55 + 1.56 +// This function is called from the background tracer thread. 1.57 +bool FireAndWaitForTracerEvent() 1.58 +{ 1.59 + NS_ABORT_IF_FALSE(sMutex && sCondVar, "Tracing not initialized!"); 1.60 + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; 1.61 + MutexAutoLock lock(*sMutex); 1.62 + if (sTracerProcessed) { 1.63 + // Things are out of sync. This is likely because we're in 1.64 + // the middle of shutting down. Just return false and hope the 1.65 + // tracer thread is quitting anyway. 1.66 + return false; 1.67 + } 1.68 + 1.69 + // Post an application-defined event to the main thread's event queue 1.70 + // and wait for it to get processed. 1.71 + [NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined 1.72 + location:NSMakePoint(0,0) 1.73 + modifierFlags:0 1.74 + timestamp:0 1.75 + windowNumber:0 1.76 + context:NULL 1.77 + subtype:kEventSubtypeTrace 1.78 + data1:0 1.79 + data2:0] 1.80 + atStart:NO]; 1.81 + while (!sTracerProcessed) 1.82 + sCondVar->Wait(); 1.83 + sTracerProcessed = false; 1.84 + [pool release]; 1.85 + return true; 1.86 +} 1.87 + 1.88 +} // namespace mozilla