Chromium Code Reviews| Index: ppapi/tests/pp_thread.h |
| diff --git a/ppapi/tests/pp_thread.h b/ppapi/tests/pp_thread.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b188d27ea48fa2fc976a214737f48fcf13cb6b1b |
| --- /dev/null |
| +++ b/ppapi/tests/pp_thread.h |
| @@ -0,0 +1,97 @@ |
| +/* Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#ifndef PPAPI_TESTS_PP_THREAD_H_ |
| +#define PPAPI_TESTS_PP_THREAD_H_ |
| + |
| +#include "ppapi/c/pp_macros.h" |
| + |
| +/** |
| + * @file |
| + * This file provides platform-independent wrappers around threads. |
| + */ |
| +#if defined(PPAPI_HAS_POSIX_THREADS) |
| +#include <pthread.h> |
| +#elif defined(PPAPI_HAS_WINDOWS_THREADS) |
| +#include <process.h> |
| +#include <windows.h> |
| +#endif |
| + |
| +#if defined(PPAPI_HAS_POSIX_THREADS) |
| +typedef pthread_t PP_ThreadType; |
| +#elif defined(PPAPI_HAS_WINDOWS_THREADS) |
| +typedef uintptr_t PP_ThreadType; |
| +#endif |
| + |
| +typedef void (PP_ThreadFunction)(void* data); |
| + |
| +PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, |
| + PP_ThreadFunction function, |
| + void* thread_arg); |
| +PP_INLINE void PP_JoinThread(PP_ThreadType thread); |
| + |
| +#if defined(PPAPI_HAS_POSIX_THREADS) |
| +/* Because POSIX thread functions return void* and Windows thread functions do |
| + * not, we make PPAPI thread functions have the least capability (no returns). |
| + * This struct wraps the user data & function so that we can use the correct |
| + * function type on POSIX platforms. |
| + */ |
| +struct PP_ThreadFunctionArgWrapper { |
| + void* user_data; |
| + PP_ThreadFunction* user_function; |
| +}; |
| + |
| +PP_INLINE void* PP_POSIXThreadFunctionThunk(void* param) { |
|
polina
2011/08/17 17:58:15
s/param/posix_thread_arg/?
dmichael (off chromium)
2011/08/17 21:15:15
Done.
|
| + PP_ThreadFunctionArgWrapper* arg_wrapper = |
| + (PP_ThreadFunctionArgWrapper*)param; |
| + arg_wrapper->user_function(arg_wrapper->user_data); |
| + free(param); |
| + return NULL; |
| +} |
| + |
| +PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, |
| + PP_ThreadFunction function, |
| + void* thread_arg) { |
| + PP_ThreadFunctionArgWrapper* arg_wrapper = |
| + (PP_ThreadFunctionArgWrapper*)malloc(sizeof(PP_ThreadFunctionArgWrapper)); |
| + arg_wrapper->user_function = function; |
| + arg_wrapper->user_data = thread_arg; |
| + return (pthread_create((pthread_t*)thread, |
|
brettw
2011/08/16 22:55:53
It doesn't seem like this (pthread_t*) cast should
dmichael (off chromium)
2011/08/17 21:15:15
Done.
|
| + NULL, |
| + PP_POSIXThreadFunctionThunk, |
| + arg_wrapper) == 0); |
| +} |
| + |
| +PP_INLINE void PP_JoinThread(PP_ThreadType thread) { |
| + void* exit_status; |
| + pthread_join(thread, &exit_status); |
| +} |
| + |
| +#elif defined(PPAPI_HAS_WINDOWS_THREADS) |
| +typedef DWORD (PP_WindowsThreadFunction)(void* data); |
|
polina
2011/08/17 17:58:15
missing newlines for visual separation?
dmichael (off chromium)
2011/08/17 21:15:15
I'm not sure where you wanted them. I added one be
|
| +PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, |
| + PP_ThreadFunction function, |
| + void* thread_arg) { |
| + if (!thread) |
| + return false; |
| + *thread = ::_beginthread(function, |
| + 0, /* Use default stack size. */ |
| + thread_arg); |
| + return (*thread != NULL); |
| +} |
| + |
| +PP_INLINE void PP_JoinThread(PP_ThreadType thread) { |
| + ::WaitForSingleObject((HANDLE)thread, INFINITE); |
| +} |
| + |
| +#endif |
| + |
| + |
| +/** |
| + * @} |
| + */ |
| + |
| +#endif /* PPAPI_TESTS_PP_THREAD_H_ */ |
| + |