OLD | NEW |
(Empty) | |
| 1 /* Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. |
| 4 */ |
| 5 |
| 6 /* From pp_thread.idl modified Mon Jul 18 17:53:53 2011. */ |
| 7 |
| 8 #ifndef PPAPI_TESTS_PP_THREAD_H_ |
| 9 #define PPAPI_TESTS_PP_THREAD_H_ |
| 10 |
| 11 #include "ppapi/c/pp_macros.h" |
| 12 |
| 13 /** |
| 14 * @file |
| 15 * This file provides platform-independent wrappers around threads. |
| 16 */ |
| 17 #if defined(PPAPI_HAS_POSIX_THREADS) |
| 18 #include <pthread.h> |
| 19 #elif defined(PPAPI_HAS_WINDOWS_THREADS) |
| 20 #include <process.h> |
| 21 #include <windows.h> |
| 22 #endif |
| 23 |
| 24 #if defined(PPAPI_HAS_POSIX_THREADS) |
| 25 typedef pthread_t PP_ThreadType; |
| 26 #elif defined(PPAPI_HAS_WINDOWS_THREADS) |
| 27 typedef uintptr_t PP_ThreadType; |
| 28 #endif |
| 29 |
| 30 typedef void (PP_ThreadFunction)(void* data); |
| 31 |
| 32 PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, |
| 33 PP_ThreadFunction function, |
| 34 void* thread_arg); |
| 35 PP_INLINE void PP_JoinThread(PP_ThreadType thread); |
| 36 |
| 37 #if defined(PPAPI_HAS_POSIX_THREADS) |
| 38 /* Because POSIX thread functions return void* and Windows thread functions do |
| 39 * not, we make PPAPI thread functions have the least capability (no returns). |
| 40 * This struct wraps the user data & function so that we can use the correct |
| 41 * function type on POSIX platforms. |
| 42 */ |
| 43 struct PP_ThreadFunctionArgWrapper { |
| 44 void* user_data; |
| 45 PP_ThreadFunction* user_function; |
| 46 }; |
| 47 |
| 48 PP_INLINE void* PP_POSIXThreadFunctionThunk(void* param) { |
| 49 PP_ThreadFunctionArgWrapper* arg_wrapper = |
| 50 (PP_ThreadFunctionArgWrapper*)param; |
| 51 arg_wrapper->user_function(arg_wrapper->user_data); |
| 52 free(param); |
| 53 return NULL; |
| 54 } |
| 55 |
| 56 PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, |
| 57 PP_ThreadFunction function, |
| 58 void* thread_arg) { |
| 59 PP_ThreadFunctionArgWrapper* arg_wrapper = |
| 60 (PP_ThreadFunctionArgWrapper*)malloc(sizeof(PP_ThreadFunctionArgWrapper)); |
| 61 arg_wrapper->user_function = function; |
| 62 arg_wrapper->user_data = thread_arg; |
| 63 return (pthread_create((pthread_t*)thread, |
| 64 NULL, |
| 65 PP_POSIXThreadFunctionThunk, |
| 66 arg_wrapper) == 0); |
| 67 } |
| 68 |
| 69 PP_INLINE void PP_JoinThread(PP_ThreadType thread) { |
| 70 void* exit_status; |
| 71 pthread_join(thread, &exit_status); |
| 72 } |
| 73 |
| 74 #elif defined(PPAPI_HAS_WINDOWS_THREADS) |
| 75 typedef DWORD (PP_WindowsThreadFunction)(void* data); |
| 76 PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, |
| 77 PP_ThreadFunction function, |
| 78 void* thread_arg) { |
| 79 if (!thread) |
| 80 return false; |
| 81 *thread = ::_beginthread(function, |
| 82 0, /* Use default stack size. */ |
| 83 thread_arg); |
| 84 return (*thread != NULL); |
| 85 } |
| 86 |
| 87 PP_INLINE void PP_JoinThread(PP_ThreadType thread) { |
| 88 ::WaitForSingleObject((HANDLE)thread, INFINITE); |
| 89 } |
| 90 |
| 91 #endif |
| 92 |
| 93 |
| 94 /** |
| 95 * @} |
| 96 */ |
| 97 |
| 98 #endif /* PPAPI_TESTS_PP_THREAD_H_ */ |
| 99 |
OLD | NEW |