Thu, 15 Jan 2015 15:59:08 +0100
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 | // Copyright 2013 Google Inc. All Rights Reserved. |
michael@0 | 2 | // |
michael@0 | 3 | // Use of this source code is governed by a BSD-style license |
michael@0 | 4 | // that can be found in the COPYING file in the root of the source |
michael@0 | 5 | // tree. An additional intellectual property rights grant can be found |
michael@0 | 6 | // in the file PATENTS. All contributing project authors may |
michael@0 | 7 | // be found in the AUTHORS file in the root of the source tree. |
michael@0 | 8 | // ----------------------------------------------------------------------------- |
michael@0 | 9 | // |
michael@0 | 10 | // Multi-threaded worker |
michael@0 | 11 | // |
michael@0 | 12 | // Original source: |
michael@0 | 13 | // http://git.chromium.org/webm/libwebp.git |
michael@0 | 14 | // 100644 blob eff8f2a8c20095aade3c292b0e9292dac6cb3587 src/utils/thread.c |
michael@0 | 15 | |
michael@0 | 16 | |
michael@0 | 17 | #include <assert.h> |
michael@0 | 18 | #include <string.h> // for memset() |
michael@0 | 19 | #include "./vp9_thread.h" |
michael@0 | 20 | |
michael@0 | 21 | #if defined(__cplusplus) || defined(c_plusplus) |
michael@0 | 22 | extern "C" { |
michael@0 | 23 | #endif |
michael@0 | 24 | |
michael@0 | 25 | #if CONFIG_MULTITHREAD |
michael@0 | 26 | |
michael@0 | 27 | #if defined(_WIN32) |
michael@0 | 28 | |
michael@0 | 29 | //------------------------------------------------------------------------------ |
michael@0 | 30 | // simplistic pthread emulation layer |
michael@0 | 31 | |
michael@0 | 32 | #include <process.h> // NOLINT |
michael@0 | 33 | |
michael@0 | 34 | // _beginthreadex requires __stdcall |
michael@0 | 35 | #define THREADFN unsigned int __stdcall |
michael@0 | 36 | #define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val) |
michael@0 | 37 | |
michael@0 | 38 | static int pthread_create(pthread_t* const thread, const void* attr, |
michael@0 | 39 | unsigned int (__stdcall *start)(void*), void* arg) { |
michael@0 | 40 | (void)attr; |
michael@0 | 41 | *thread = (pthread_t)_beginthreadex(NULL, /* void *security */ |
michael@0 | 42 | 0, /* unsigned stack_size */ |
michael@0 | 43 | start, |
michael@0 | 44 | arg, |
michael@0 | 45 | 0, /* unsigned initflag */ |
michael@0 | 46 | NULL); /* unsigned *thrdaddr */ |
michael@0 | 47 | if (*thread == NULL) return 1; |
michael@0 | 48 | SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL); |
michael@0 | 49 | return 0; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | static int pthread_join(pthread_t thread, void** value_ptr) { |
michael@0 | 53 | (void)value_ptr; |
michael@0 | 54 | return (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 || |
michael@0 | 55 | CloseHandle(thread) == 0); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | // Mutex |
michael@0 | 59 | static int pthread_mutex_init(pthread_mutex_t* const mutex, void* mutexattr) { |
michael@0 | 60 | (void)mutexattr; |
michael@0 | 61 | InitializeCriticalSection(mutex); |
michael@0 | 62 | return 0; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | static int pthread_mutex_lock(pthread_mutex_t* const mutex) { |
michael@0 | 66 | EnterCriticalSection(mutex); |
michael@0 | 67 | return 0; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | static int pthread_mutex_unlock(pthread_mutex_t* const mutex) { |
michael@0 | 71 | LeaveCriticalSection(mutex); |
michael@0 | 72 | return 0; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | static int pthread_mutex_destroy(pthread_mutex_t* const mutex) { |
michael@0 | 76 | DeleteCriticalSection(mutex); |
michael@0 | 77 | return 0; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | // Condition |
michael@0 | 81 | static int pthread_cond_destroy(pthread_cond_t* const condition) { |
michael@0 | 82 | int ok = 1; |
michael@0 | 83 | ok &= (CloseHandle(condition->waiting_sem_) != 0); |
michael@0 | 84 | ok &= (CloseHandle(condition->received_sem_) != 0); |
michael@0 | 85 | ok &= (CloseHandle(condition->signal_event_) != 0); |
michael@0 | 86 | return !ok; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | static int pthread_cond_init(pthread_cond_t* const condition, void* cond_attr) { |
michael@0 | 90 | (void)cond_attr; |
michael@0 | 91 | condition->waiting_sem_ = CreateSemaphore(NULL, 0, 1, NULL); |
michael@0 | 92 | condition->received_sem_ = CreateSemaphore(NULL, 0, 1, NULL); |
michael@0 | 93 | condition->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL); |
michael@0 | 94 | if (condition->waiting_sem_ == NULL || |
michael@0 | 95 | condition->received_sem_ == NULL || |
michael@0 | 96 | condition->signal_event_ == NULL) { |
michael@0 | 97 | pthread_cond_destroy(condition); |
michael@0 | 98 | return 1; |
michael@0 | 99 | } |
michael@0 | 100 | return 0; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | static int pthread_cond_signal(pthread_cond_t* const condition) { |
michael@0 | 104 | int ok = 1; |
michael@0 | 105 | if (WaitForSingleObject(condition->waiting_sem_, 0) == WAIT_OBJECT_0) { |
michael@0 | 106 | // a thread is waiting in pthread_cond_wait: allow it to be notified |
michael@0 | 107 | ok = SetEvent(condition->signal_event_); |
michael@0 | 108 | // wait until the event is consumed so the signaler cannot consume |
michael@0 | 109 | // the event via its own pthread_cond_wait. |
michael@0 | 110 | ok &= (WaitForSingleObject(condition->received_sem_, INFINITE) != |
michael@0 | 111 | WAIT_OBJECT_0); |
michael@0 | 112 | } |
michael@0 | 113 | return !ok; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | static int pthread_cond_wait(pthread_cond_t* const condition, |
michael@0 | 117 | pthread_mutex_t* const mutex) { |
michael@0 | 118 | int ok; |
michael@0 | 119 | // note that there is a consumer available so the signal isn't dropped in |
michael@0 | 120 | // pthread_cond_signal |
michael@0 | 121 | if (!ReleaseSemaphore(condition->waiting_sem_, 1, NULL)) |
michael@0 | 122 | return 1; |
michael@0 | 123 | // now unlock the mutex so pthread_cond_signal may be issued |
michael@0 | 124 | pthread_mutex_unlock(mutex); |
michael@0 | 125 | ok = (WaitForSingleObject(condition->signal_event_, INFINITE) == |
michael@0 | 126 | WAIT_OBJECT_0); |
michael@0 | 127 | ok &= ReleaseSemaphore(condition->received_sem_, 1, NULL); |
michael@0 | 128 | pthread_mutex_lock(mutex); |
michael@0 | 129 | return !ok; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | #else // _WIN32 |
michael@0 | 133 | # define THREADFN void* |
michael@0 | 134 | # define THREAD_RETURN(val) val |
michael@0 | 135 | #endif |
michael@0 | 136 | |
michael@0 | 137 | //------------------------------------------------------------------------------ |
michael@0 | 138 | |
michael@0 | 139 | static THREADFN thread_loop(void *ptr) { // thread loop |
michael@0 | 140 | VP9Worker* const worker = (VP9Worker*)ptr; |
michael@0 | 141 | int done = 0; |
michael@0 | 142 | while (!done) { |
michael@0 | 143 | pthread_mutex_lock(&worker->mutex_); |
michael@0 | 144 | while (worker->status_ == OK) { // wait in idling mode |
michael@0 | 145 | pthread_cond_wait(&worker->condition_, &worker->mutex_); |
michael@0 | 146 | } |
michael@0 | 147 | if (worker->status_ == WORK) { |
michael@0 | 148 | vp9_worker_execute(worker); |
michael@0 | 149 | worker->status_ = OK; |
michael@0 | 150 | } else if (worker->status_ == NOT_OK) { // finish the worker |
michael@0 | 151 | done = 1; |
michael@0 | 152 | } |
michael@0 | 153 | // signal to the main thread that we're done (for Sync()) |
michael@0 | 154 | pthread_cond_signal(&worker->condition_); |
michael@0 | 155 | pthread_mutex_unlock(&worker->mutex_); |
michael@0 | 156 | } |
michael@0 | 157 | return THREAD_RETURN(NULL); // Thread is finished |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | // main thread state control |
michael@0 | 161 | static void change_state(VP9Worker* const worker, |
michael@0 | 162 | VP9WorkerStatus new_status) { |
michael@0 | 163 | // no-op when attempting to change state on a thread that didn't come up |
michael@0 | 164 | if (worker->status_ < OK) return; |
michael@0 | 165 | |
michael@0 | 166 | pthread_mutex_lock(&worker->mutex_); |
michael@0 | 167 | // wait for the worker to finish |
michael@0 | 168 | while (worker->status_ != OK) { |
michael@0 | 169 | pthread_cond_wait(&worker->condition_, &worker->mutex_); |
michael@0 | 170 | } |
michael@0 | 171 | // assign new status and release the working thread if needed |
michael@0 | 172 | if (new_status != OK) { |
michael@0 | 173 | worker->status_ = new_status; |
michael@0 | 174 | pthread_cond_signal(&worker->condition_); |
michael@0 | 175 | } |
michael@0 | 176 | pthread_mutex_unlock(&worker->mutex_); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | #endif // CONFIG_MULTITHREAD |
michael@0 | 180 | |
michael@0 | 181 | //------------------------------------------------------------------------------ |
michael@0 | 182 | |
michael@0 | 183 | void vp9_worker_init(VP9Worker* const worker) { |
michael@0 | 184 | memset(worker, 0, sizeof(*worker)); |
michael@0 | 185 | worker->status_ = NOT_OK; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | int vp9_worker_sync(VP9Worker* const worker) { |
michael@0 | 189 | #if CONFIG_MULTITHREAD |
michael@0 | 190 | change_state(worker, OK); |
michael@0 | 191 | #endif |
michael@0 | 192 | assert(worker->status_ <= OK); |
michael@0 | 193 | return !worker->had_error; |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | int vp9_worker_reset(VP9Worker* const worker) { |
michael@0 | 197 | int ok = 1; |
michael@0 | 198 | worker->had_error = 0; |
michael@0 | 199 | if (worker->status_ < OK) { |
michael@0 | 200 | #if CONFIG_MULTITHREAD |
michael@0 | 201 | if (pthread_mutex_init(&worker->mutex_, NULL) || |
michael@0 | 202 | pthread_cond_init(&worker->condition_, NULL)) { |
michael@0 | 203 | return 0; |
michael@0 | 204 | } |
michael@0 | 205 | pthread_mutex_lock(&worker->mutex_); |
michael@0 | 206 | ok = !pthread_create(&worker->thread_, NULL, thread_loop, worker); |
michael@0 | 207 | if (ok) worker->status_ = OK; |
michael@0 | 208 | pthread_mutex_unlock(&worker->mutex_); |
michael@0 | 209 | #else |
michael@0 | 210 | worker->status_ = OK; |
michael@0 | 211 | #endif |
michael@0 | 212 | } else if (worker->status_ > OK) { |
michael@0 | 213 | ok = vp9_worker_sync(worker); |
michael@0 | 214 | } |
michael@0 | 215 | assert(!ok || (worker->status_ == OK)); |
michael@0 | 216 | return ok; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | void vp9_worker_execute(VP9Worker* const worker) { |
michael@0 | 220 | if (worker->hook != NULL) { |
michael@0 | 221 | worker->had_error |= !worker->hook(worker->data1, worker->data2); |
michael@0 | 222 | } |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | void vp9_worker_launch(VP9Worker* const worker) { |
michael@0 | 226 | #if CONFIG_MULTITHREAD |
michael@0 | 227 | change_state(worker, WORK); |
michael@0 | 228 | #else |
michael@0 | 229 | vp9_worker_execute(worker); |
michael@0 | 230 | #endif |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | void vp9_worker_end(VP9Worker* const worker) { |
michael@0 | 234 | if (worker->status_ >= OK) { |
michael@0 | 235 | #if CONFIG_MULTITHREAD |
michael@0 | 236 | change_state(worker, NOT_OK); |
michael@0 | 237 | pthread_join(worker->thread_, NULL); |
michael@0 | 238 | pthread_mutex_destroy(&worker->mutex_); |
michael@0 | 239 | pthread_cond_destroy(&worker->condition_); |
michael@0 | 240 | #else |
michael@0 | 241 | worker->status_ = NOT_OK; |
michael@0 | 242 | #endif |
michael@0 | 243 | } |
michael@0 | 244 | assert(worker->status_ == NOT_OK); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | //------------------------------------------------------------------------------ |
michael@0 | 248 | |
michael@0 | 249 | #if defined(__cplusplus) || defined(c_plusplus) |
michael@0 | 250 | } // extern "C" |
michael@0 | 251 | #endif |