media/libvpx/vp8/common/threading.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) 2010 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
michael@0 11
michael@0 12 #ifndef _PTHREAD_EMULATION
michael@0 13 #define _PTHREAD_EMULATION
michael@0 14
michael@0 15 #if CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD
michael@0 16
michael@0 17 /* Thread management macros */
michael@0 18 #ifdef _WIN32
michael@0 19 /* Win32 */
michael@0 20 #include <process.h>
michael@0 21 #include <windows.h>
michael@0 22 #define THREAD_FUNCTION DWORD WINAPI
michael@0 23 #define THREAD_FUNCTION_RETURN DWORD
michael@0 24 #define THREAD_SPECIFIC_INDEX DWORD
michael@0 25 #define pthread_t HANDLE
michael@0 26 #define pthread_attr_t DWORD
michael@0 27 #define pthread_create(thhandle,attr,thfunc,tharg) (int)((*thhandle=(HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))thfunc,tharg,0,NULL))==NULL)
michael@0 28 #define pthread_join(thread, result) ((WaitForSingleObject((thread),INFINITE)!=WAIT_OBJECT_0) || !CloseHandle(thread))
michael@0 29 #define pthread_detach(thread) if(thread!=NULL)CloseHandle(thread)
michael@0 30 #define thread_sleep(nms) Sleep(nms)
michael@0 31 #define pthread_cancel(thread) terminate_thread(thread,0)
michael@0 32 #define ts_key_create(ts_key, destructor) {ts_key = TlsAlloc();};
michael@0 33 #define pthread_getspecific(ts_key) TlsGetValue(ts_key)
michael@0 34 #define pthread_setspecific(ts_key, value) TlsSetValue(ts_key, (void *)value)
michael@0 35 #define pthread_self() GetCurrentThreadId()
michael@0 36
michael@0 37 #elif defined(__OS2__)
michael@0 38 /* OS/2 */
michael@0 39 #define INCL_DOS
michael@0 40 #include <os2.h>
michael@0 41
michael@0 42 #include <stdlib.h>
michael@0 43 #define THREAD_FUNCTION void
michael@0 44 #define THREAD_FUNCTION_RETURN void
michael@0 45 #define THREAD_SPECIFIC_INDEX PULONG
michael@0 46 #define pthread_t TID
michael@0 47 #define pthread_attr_t ULONG
michael@0 48 #define pthread_create(thhandle,attr,thfunc,tharg) \
michael@0 49 ((int)((*(thhandle)=_beginthread(thfunc,NULL,1024*1024,tharg))==-1))
michael@0 50 #define pthread_join(thread, result) ((int)DosWaitThread(&(thread),0))
michael@0 51 #define pthread_detach(thread) 0
michael@0 52 #define thread_sleep(nms) DosSleep(nms)
michael@0 53 #define pthread_cancel(thread) DosKillThread(thread)
michael@0 54 #define ts_key_create(ts_key, destructor) \
michael@0 55 DosAllocThreadLocalMemory(1, &(ts_key));
michael@0 56 #define pthread_getspecific(ts_key) ((void *)(*(ts_key)))
michael@0 57 #define pthread_setspecific(ts_key, value) (*(ts_key)=(ULONG)(value))
michael@0 58 #define pthread_self() _gettid()
michael@0 59 #else
michael@0 60 #ifdef __APPLE__
michael@0 61 #include <mach/mach_init.h>
michael@0 62 #include <mach/semaphore.h>
michael@0 63 #include <mach/task.h>
michael@0 64 #include <time.h>
michael@0 65 #include <unistd.h>
michael@0 66
michael@0 67 #else
michael@0 68 #include <semaphore.h>
michael@0 69 #endif
michael@0 70
michael@0 71 #include <pthread.h>
michael@0 72 /* pthreads */
michael@0 73 /* Nearly everything is already defined */
michael@0 74 #define THREAD_FUNCTION void *
michael@0 75 #define THREAD_FUNCTION_RETURN void *
michael@0 76 #define THREAD_SPECIFIC_INDEX pthread_key_t
michael@0 77 #define ts_key_create(ts_key, destructor) pthread_key_create (&(ts_key), destructor);
michael@0 78 #endif
michael@0 79
michael@0 80 /* Syncrhronization macros: Win32 and Pthreads */
michael@0 81 #ifdef _WIN32
michael@0 82 #define sem_t HANDLE
michael@0 83 #define pause(voidpara) __asm PAUSE
michael@0 84 #define sem_init(sem, sem_attr1, sem_init_value) (int)((*sem = CreateSemaphore(NULL,0,32768,NULL))==NULL)
michael@0 85 #define sem_wait(sem) (int)(WAIT_OBJECT_0 != WaitForSingleObject(*sem,INFINITE))
michael@0 86 #define sem_post(sem) ReleaseSemaphore(*sem,1,NULL)
michael@0 87 #define sem_destroy(sem) if(*sem)((int)(CloseHandle(*sem))==TRUE)
michael@0 88 #define thread_sleep(nms) Sleep(nms)
michael@0 89
michael@0 90 #elif defined(__OS2__)
michael@0 91 typedef struct
michael@0 92 {
michael@0 93 HEV event;
michael@0 94 HMTX wait_mutex;
michael@0 95 HMTX count_mutex;
michael@0 96 int count;
michael@0 97 } sem_t;
michael@0 98
michael@0 99 static inline int sem_init(sem_t *sem, int pshared, unsigned int value)
michael@0 100 {
michael@0 101 DosCreateEventSem(NULL, &sem->event, pshared ? DC_SEM_SHARED : 0,
michael@0 102 value > 0 ? TRUE : FALSE);
michael@0 103 DosCreateMutexSem(NULL, &sem->wait_mutex, 0, FALSE);
michael@0 104 DosCreateMutexSem(NULL, &sem->count_mutex, 0, FALSE);
michael@0 105
michael@0 106 sem->count = value;
michael@0 107
michael@0 108 return 0;
michael@0 109 }
michael@0 110
michael@0 111 static inline int sem_wait(sem_t * sem)
michael@0 112 {
michael@0 113 DosRequestMutexSem(sem->wait_mutex, -1);
michael@0 114
michael@0 115 DosWaitEventSem(sem->event, -1);
michael@0 116
michael@0 117 DosRequestMutexSem(sem->count_mutex, -1);
michael@0 118
michael@0 119 sem->count--;
michael@0 120 if (sem->count == 0)
michael@0 121 {
michael@0 122 ULONG post_count;
michael@0 123
michael@0 124 DosResetEventSem(sem->event, &post_count);
michael@0 125 }
michael@0 126
michael@0 127 DosReleaseMutexSem(sem->count_mutex);
michael@0 128
michael@0 129 DosReleaseMutexSem(sem->wait_mutex);
michael@0 130
michael@0 131 return 0;
michael@0 132 }
michael@0 133
michael@0 134 static inline int sem_post(sem_t * sem)
michael@0 135 {
michael@0 136 DosRequestMutexSem(sem->count_mutex, -1);
michael@0 137
michael@0 138 if (sem->count < 32768)
michael@0 139 {
michael@0 140 sem->count++;
michael@0 141 DosPostEventSem(sem->event);
michael@0 142 }
michael@0 143
michael@0 144 DosReleaseMutexSem(sem->count_mutex);
michael@0 145
michael@0 146 return 0;
michael@0 147 }
michael@0 148
michael@0 149 static inline int sem_destroy(sem_t * sem)
michael@0 150 {
michael@0 151 DosCloseEventSem(sem->event);
michael@0 152 DosCloseMutexSem(sem->wait_mutex);
michael@0 153 DosCloseMutexSem(sem->count_mutex);
michael@0 154
michael@0 155 return 0;
michael@0 156 }
michael@0 157
michael@0 158 #define thread_sleep(nms) DosSleep(nms)
michael@0 159
michael@0 160 #else
michael@0 161
michael@0 162 #ifdef __APPLE__
michael@0 163 #define sem_t semaphore_t
michael@0 164 #define sem_init(X,Y,Z) semaphore_create(mach_task_self(), X, SYNC_POLICY_FIFO, Z)
michael@0 165 #define sem_wait(sem) (semaphore_wait(*sem) )
michael@0 166 #define sem_post(sem) semaphore_signal(*sem)
michael@0 167 #define sem_destroy(sem) semaphore_destroy(mach_task_self(),*sem)
michael@0 168 #define thread_sleep(nms) /* { struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
michael@0 169 #else
michael@0 170 #include <unistd.h>
michael@0 171 #include <sched.h>
michael@0 172 #define thread_sleep(nms) sched_yield();/* {struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
michael@0 173 #endif
michael@0 174 /* Not Windows. Assume pthreads */
michael@0 175
michael@0 176 #endif
michael@0 177
michael@0 178 #if ARCH_X86 || ARCH_X86_64
michael@0 179 #include "vpx_ports/x86.h"
michael@0 180 #else
michael@0 181 #define x86_pause_hint()
michael@0 182 #endif
michael@0 183
michael@0 184 #endif /* CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD */
michael@0 185
michael@0 186 #endif

mercurial