OLD | NEW |
1 /* Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 #ifndef PPAPI_TESTS_PP_THREAD_H_ | 6 #ifndef PPAPI_TESTS_PP_THREAD_H_ |
7 #define PPAPI_TESTS_PP_THREAD_H_ | 7 #define PPAPI_TESTS_PP_THREAD_H_ |
8 | 8 |
9 #include "ppapi/c/pp_macros.h" | 9 #include "ppapi/c/pp_macros.h" |
10 | 10 |
11 /* These precompiler names were copied from chromium's build_config.h. */ | 11 #if defined(PPAPI_POSIX) |
12 #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \ | |
13 defined(__OpenBSD__) || defined(__sun) || defined(__native_client__) | |
14 #define PPAPI_HAS_POSIX_THREADS 1 | |
15 #elif defined (_MSC_VER) | |
16 #define PPAPI_HAS_WINDOWS_THREADS 1 | |
17 #endif | |
18 | |
19 #if defined(PPAPI_HAS_POSIX_THREADS) | |
20 #include <pthread.h> | 12 #include <pthread.h> |
21 #elif defined(PPAPI_HAS_WINDOWS_THREADS) | 13 #elif defined(PPAPI_OS_WIN) |
22 #include <process.h> | 14 #include <process.h> |
23 #include <windows.h> | 15 #include <windows.h> |
| 16 #else |
| 17 #error No thread library detected. |
24 #endif | 18 #endif |
25 | 19 |
26 /** | 20 /** |
27 * @file | 21 * @file |
28 * This file provides platform-independent wrappers around threads. This is for | 22 * This file provides platform-independent wrappers around threads. This is for |
29 * use by PPAPI wrappers and tests which need to run on multiple platforms to | 23 * use by PPAPI wrappers and tests which need to run on multiple platforms to |
30 * support both trusted platforms (Windows, Mac, Linux) and untrusted (Native | 24 * support both trusted platforms (Windows, Mac, Linux) and untrusted (Native |
31 * Client). Apps that use PPAPI only with Native Client should generally use the | 25 * Client). Apps that use PPAPI only with Native Client should generally use the |
32 * Native Client POSIX implementation instead. | 26 * Native Client POSIX implementation instead. |
33 * | 27 * |
34 * TODO(dmichael): Move this file to ppapi/c and delete this comment, if we end | 28 * TODO(dmichael): Move this file to ppapi/c and delete this comment, if we end |
35 * up needing platform independent threads in PPAPI C or C++. This file was | 29 * up needing platform independent threads in PPAPI C or C++. This file was |
36 * written using inline functions and PPAPI naming conventions with the intent | 30 * written using inline functions and PPAPI naming conventions with the intent |
37 * of making it possible to put it in to ppapi/c. Currently, however, it's only | 31 * of making it possible to put it in to ppapi/c. Currently, however, it's only |
38 * used in ppapi/tests, so is not part of the published API. | 32 * used in ppapi/tests, so is not part of the published API. |
39 */ | 33 */ |
40 | 34 |
41 #if defined(PPAPI_HAS_POSIX_THREADS) | 35 #if defined(PPAPI_POSIX) |
42 typedef pthread_t PP_ThreadType; | 36 typedef pthread_t PP_ThreadType; |
43 #elif defined(PPAPI_HAS_WINDOWS_THREADS) | 37 #elif defined(PPAPI_OS_WIN) |
44 typedef uintptr_t PP_ThreadType; | 38 typedef uintptr_t PP_ThreadType; |
45 #endif | 39 #endif |
46 | 40 |
47 typedef void (PP_ThreadFunction)(void* data); | 41 typedef void (PP_ThreadFunction)(void* data); |
48 | 42 |
49 PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, | 43 PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, |
50 PP_ThreadFunction function, | 44 PP_ThreadFunction function, |
51 void* thread_arg); | 45 void* thread_arg); |
52 PP_INLINE void PP_JoinThread(PP_ThreadType thread); | 46 PP_INLINE void PP_JoinThread(PP_ThreadType thread); |
53 | 47 |
54 #if defined(PPAPI_HAS_POSIX_THREADS) | 48 #if defined(PPAPI_POSIX) |
55 /* Because POSIX thread functions return void* and Windows thread functions do | 49 /* Because POSIX thread functions return void* and Windows thread functions do |
56 * not, we make PPAPI thread functions have the least capability (no returns). | 50 * not, we make PPAPI thread functions have the least capability (no returns). |
57 * This struct wraps the user data & function so that we can use the correct | 51 * This struct wraps the user data & function so that we can use the correct |
58 * function type on POSIX platforms. | 52 * function type on POSIX platforms. |
59 */ | 53 */ |
60 struct PP_ThreadFunctionArgWrapper { | 54 struct PP_ThreadFunctionArgWrapper { |
61 void* user_data; | 55 void* user_data; |
62 PP_ThreadFunction* user_function; | 56 PP_ThreadFunction* user_function; |
63 }; | 57 }; |
64 | 58 |
(...skipping 16 matching lines...) Expand all Loading... |
81 NULL, | 75 NULL, |
82 PP_POSIXThreadFunctionThunk, | 76 PP_POSIXThreadFunctionThunk, |
83 arg_wrapper) == 0); | 77 arg_wrapper) == 0); |
84 } | 78 } |
85 | 79 |
86 PP_INLINE void PP_JoinThread(PP_ThreadType thread) { | 80 PP_INLINE void PP_JoinThread(PP_ThreadType thread) { |
87 void* exit_status; | 81 void* exit_status; |
88 pthread_join(thread, &exit_status); | 82 pthread_join(thread, &exit_status); |
89 } | 83 } |
90 | 84 |
91 #elif defined(PPAPI_HAS_WINDOWS_THREADS) | 85 #elif defined(PPAPI_OS_WIN) |
92 typedef DWORD (PP_WindowsThreadFunction)(void* data); | 86 typedef DWORD (PP_WindowsThreadFunction)(void* data); |
93 | 87 |
94 PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, | 88 PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, |
95 PP_ThreadFunction function, | 89 PP_ThreadFunction function, |
96 void* thread_arg) { | 90 void* thread_arg) { |
97 if (!thread) | 91 if (!thread) |
98 return false; | 92 return false; |
99 *thread = ::_beginthread(function, | 93 *thread = ::_beginthread(function, |
100 0, /* Use default stack size. */ | 94 0, /* Use default stack size. */ |
101 thread_arg); | 95 thread_arg); |
102 return (*thread != NULL); | 96 return (*thread != NULL); |
103 } | 97 } |
104 | 98 |
105 PP_INLINE void PP_JoinThread(PP_ThreadType thread) { | 99 PP_INLINE void PP_JoinThread(PP_ThreadType thread) { |
106 ::WaitForSingleObject((HANDLE)thread, INFINITE); | 100 ::WaitForSingleObject((HANDLE)thread, INFINITE); |
107 } | 101 } |
108 | 102 |
109 #endif | 103 #endif |
110 | 104 |
111 | 105 |
112 /** | 106 /** |
113 * @} | 107 * @} |
114 */ | 108 */ |
115 | 109 |
116 #endif /* PPAPI_TESTS_PP_THREAD_H_ */ | 110 #endif /* PPAPI_TESTS_PP_THREAD_H_ */ |
117 | 111 |
OLD | NEW |