media/libvpx/vpx_ports/vpx_once.h

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /*
michael@0 2 * Copyright (c) 2011 The WebM project authors. All Rights Reserved.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license
michael@0 5 * that can be found in the LICENSE file in the root of the source
michael@0 6 * tree. An additional intellectual property rights grant can be found
michael@0 7 * in the file PATENTS. All contributing project authors may
michael@0 8 * be found in the AUTHORS file in the root of the source tree.
michael@0 9 */
michael@0 10 #ifndef VPX_ONCE_H
michael@0 11 #define VPX_ONCE_H
michael@0 12
michael@0 13 #include "vpx_config.h"
michael@0 14
michael@0 15 #if CONFIG_MULTITHREAD && defined(_WIN32)
michael@0 16 #include <windows.h>
michael@0 17 #include <stdlib.h>
michael@0 18 static void once(void (*func)(void))
michael@0 19 {
michael@0 20 static CRITICAL_SECTION *lock;
michael@0 21 static LONG waiters;
michael@0 22 static int done;
michael@0 23 void *lock_ptr = &lock;
michael@0 24
michael@0 25 /* If the initialization is complete, return early. This isn't just an
michael@0 26 * optimization, it prevents races on the destruction of the global
michael@0 27 * lock.
michael@0 28 */
michael@0 29 if(done)
michael@0 30 return;
michael@0 31
michael@0 32 InterlockedIncrement(&waiters);
michael@0 33
michael@0 34 /* Get a lock. We create one and try to make it the one-true-lock,
michael@0 35 * throwing it away if we lost the race.
michael@0 36 */
michael@0 37
michael@0 38 {
michael@0 39 /* Scope to protect access to new_lock */
michael@0 40 CRITICAL_SECTION *new_lock = malloc(sizeof(CRITICAL_SECTION));
michael@0 41 InitializeCriticalSection(new_lock);
michael@0 42 if (InterlockedCompareExchangePointer(lock_ptr, new_lock, NULL) != NULL)
michael@0 43 {
michael@0 44 DeleteCriticalSection(new_lock);
michael@0 45 free(new_lock);
michael@0 46 }
michael@0 47 }
michael@0 48
michael@0 49 /* At this point, we have a lock that can be synchronized on. We don't
michael@0 50 * care which thread actually performed the allocation.
michael@0 51 */
michael@0 52
michael@0 53 EnterCriticalSection(lock);
michael@0 54
michael@0 55 if (!done)
michael@0 56 {
michael@0 57 func();
michael@0 58 done = 1;
michael@0 59 }
michael@0 60
michael@0 61 LeaveCriticalSection(lock);
michael@0 62
michael@0 63 /* Last one out should free resources. The destructed objects are
michael@0 64 * protected by checking if(done) above.
michael@0 65 */
michael@0 66 if(!InterlockedDecrement(&waiters))
michael@0 67 {
michael@0 68 DeleteCriticalSection(lock);
michael@0 69 free(lock);
michael@0 70 lock = NULL;
michael@0 71 }
michael@0 72 }
michael@0 73
michael@0 74
michael@0 75 #elif CONFIG_MULTITHREAD && HAVE_PTHREAD_H
michael@0 76 #include <pthread.h>
michael@0 77 static void once(void (*func)(void))
michael@0 78 {
michael@0 79 static pthread_once_t lock = PTHREAD_ONCE_INIT;
michael@0 80 pthread_once(&lock, func);
michael@0 81 }
michael@0 82
michael@0 83
michael@0 84 #else
michael@0 85 /* No-op version that performs no synchronization. vp8_rtcd() is idempotent,
michael@0 86 * so as long as your platform provides atomic loads/stores of pointers
michael@0 87 * no synchronization is strictly necessary.
michael@0 88 */
michael@0 89
michael@0 90 static void once(void (*func)(void))
michael@0 91 {
michael@0 92 static int done;
michael@0 93
michael@0 94 if(!done)
michael@0 95 {
michael@0 96 func();
michael@0 97 done = 1;
michael@0 98 }
michael@0 99 }
michael@0 100 #endif
michael@0 101
michael@0 102 #endif

mercurial