Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include <Cocoa/Cocoa.h> |
michael@0 | 6 | #include "CustomCocoaEvents.h" |
michael@0 | 7 | #include <Foundation/NSAutoreleasePool.h> |
michael@0 | 8 | #include <mozilla/CondVar.h> |
michael@0 | 9 | #include <mozilla/Mutex.h> |
michael@0 | 10 | #include "mozilla/WidgetTraceEvent.h" |
michael@0 | 11 | |
michael@0 | 12 | using mozilla::CondVar; |
michael@0 | 13 | using mozilla::Mutex; |
michael@0 | 14 | using mozilla::MutexAutoLock; |
michael@0 | 15 | |
michael@0 | 16 | namespace { |
michael@0 | 17 | |
michael@0 | 18 | Mutex* sMutex = NULL; |
michael@0 | 19 | CondVar* sCondVar = NULL; |
michael@0 | 20 | bool sTracerProcessed = false; |
michael@0 | 21 | |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | namespace mozilla { |
michael@0 | 25 | |
michael@0 | 26 | bool InitWidgetTracing() |
michael@0 | 27 | { |
michael@0 | 28 | sMutex = new Mutex("Event tracer thread mutex"); |
michael@0 | 29 | sCondVar = new CondVar(*sMutex, "Event tracer thread condvar"); |
michael@0 | 30 | return sMutex && sCondVar; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | void CleanUpWidgetTracing() |
michael@0 | 34 | { |
michael@0 | 35 | delete sMutex; |
michael@0 | 36 | delete sCondVar; |
michael@0 | 37 | sMutex = NULL; |
michael@0 | 38 | sCondVar = NULL; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | // This function is called from the main (UI) thread. |
michael@0 | 42 | void SignalTracerThread() |
michael@0 | 43 | { |
michael@0 | 44 | if (!sMutex || !sCondVar) |
michael@0 | 45 | return; |
michael@0 | 46 | MutexAutoLock lock(*sMutex); |
michael@0 | 47 | if (!sTracerProcessed) { |
michael@0 | 48 | sTracerProcessed = true; |
michael@0 | 49 | sCondVar->Notify(); |
michael@0 | 50 | } |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | // This function is called from the background tracer thread. |
michael@0 | 54 | bool FireAndWaitForTracerEvent() |
michael@0 | 55 | { |
michael@0 | 56 | NS_ABORT_IF_FALSE(sMutex && sCondVar, "Tracing not initialized!"); |
michael@0 | 57 | NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
michael@0 | 58 | MutexAutoLock lock(*sMutex); |
michael@0 | 59 | if (sTracerProcessed) { |
michael@0 | 60 | // Things are out of sync. This is likely because we're in |
michael@0 | 61 | // the middle of shutting down. Just return false and hope the |
michael@0 | 62 | // tracer thread is quitting anyway. |
michael@0 | 63 | return false; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | // Post an application-defined event to the main thread's event queue |
michael@0 | 67 | // and wait for it to get processed. |
michael@0 | 68 | [NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined |
michael@0 | 69 | location:NSMakePoint(0,0) |
michael@0 | 70 | modifierFlags:0 |
michael@0 | 71 | timestamp:0 |
michael@0 | 72 | windowNumber:0 |
michael@0 | 73 | context:NULL |
michael@0 | 74 | subtype:kEventSubtypeTrace |
michael@0 | 75 | data1:0 |
michael@0 | 76 | data2:0] |
michael@0 | 77 | atStart:NO]; |
michael@0 | 78 | while (!sTracerProcessed) |
michael@0 | 79 | sCondVar->Wait(); |
michael@0 | 80 | sTracerProcessed = false; |
michael@0 | 81 | [pool release]; |
michael@0 | 82 | return true; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | } // namespace mozilla |