Backed out changeset 3fe07c50c854 (bug 946316) for bustage. a=backout
[gecko.git] / widget / cocoa / WidgetTraceEvent.mm
blob7ea8313be6df9fb5f5eab9cf407e50e02d56c9ea
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;
24 namespace mozilla {
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()
35   delete sMutex;
36   delete sCondVar;
37   sMutex = NULL;
38   sCondVar = NULL;
41 // This function is called from the main (UI) thread.
42 void SignalTracerThread()
44   if (!sMutex || !sCondVar)
45     return;
46   MutexAutoLock lock(*sMutex);
47   if (!sTracerProcessed) {
48     sTracerProcessed = true;
49     sCondVar->Notify();
50   }
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.
63     return false;
64   }
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)
70                                  modifierFlags:0
71                                      timestamp:0
72                                   windowNumber:0
73                                        context:NULL
74                                        subtype:kEventSubtypeTrace
75                                          data1:0
76                                          data2:0]
77              atStart:NO];
78   while (!sTracerProcessed)
79     sCondVar->Wait();
80   sTracerProcessed = false;
81   [pool release];
82   return true;
85 }  // namespace mozilla