no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / widget / cocoa / WidgetTraceEvent.mm
blob54fe497e3b38c2057327178aaa149a9f18e864ef
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;
13 using mozilla::Mutex;
14 using mozilla::MutexAutoLock;
16 namespace {
18 Mutex* sMutex = NULL;
19 CondVar* sCondVar = NULL;
20 bool sTracerProcessed = false;
22 }  // namespace
24 namespace mozilla {
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() {
33   delete sMutex;
34   delete sCondVar;
35   sMutex = NULL;
36   sCondVar = NULL;
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;
45     sCondVar->Notify();
46   }
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.
58     return false;
59   }
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)
65                                  modifierFlags:0
66                                      timestamp:0
67                                   windowNumber:0
68                                        context:NULL
69                                        subtype:kEventSubtypeTrace
70                                          data1:0
71                                          data2:0]
72            atStart:NO];
73   while (!sTracerProcessed) sCondVar->Wait();
74   sTracerProcessed = false;
75   [pool release];
76   return true;
79 }  // namespace mozilla