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 #include "ppapi/tests/test_utils.h" | 10 #include "ppapi/tests/test_utils.h" |
(...skipping 15 matching lines...) Expand all Loading... | |
26 * Client). Apps that use PPAPI only with Native Client should generally use the | 26 * Client). Apps that use PPAPI only with Native Client should generally use the |
27 * Native Client POSIX implementation instead. | 27 * Native Client POSIX implementation instead. |
28 * | 28 * |
29 * TODO(dmichael): Move this file to ppapi/c and delete this comment, if we end | 29 * TODO(dmichael): Move this file to ppapi/c and delete this comment, if we end |
30 * up needing platform independent threads in PPAPI C or C++. This file was | 30 * up needing platform independent threads in PPAPI C or C++. This file was |
31 * written using inline functions and PPAPI naming conventions with the intent | 31 * written using inline functions and PPAPI naming conventions with the intent |
32 * of making it possible to put it in to ppapi/c. Currently, however, it's only | 32 * of making it possible to put it in to ppapi/c. Currently, however, it's only |
33 * used in ppapi/tests, so is not part of the published API. | 33 * used in ppapi/tests, so is not part of the published API. |
34 */ | 34 */ |
35 | 35 |
36 typedef void (PP_ThreadFunction)(void* data); | |
37 | |
36 #if defined(PPAPI_POSIX) | 38 #if defined(PPAPI_POSIX) |
37 typedef pthread_t PP_ThreadType; | 39 typedef pthread_t PP_ThreadType; |
38 #elif defined(PPAPI_OS_WIN) | 40 #elif defined(PPAPI_OS_WIN) |
39 typedef uintptr_t PP_ThreadType; | 41 struct PP_ThreadType { |
bbudge
2014/09/05 17:22:59
Given that this is now a struct, PP_ThreadType doe
| |
42 HANDLE handle; | |
43 PP_ThreadFunction* thread_func; | |
44 void* thread_arg; | |
45 }; | |
40 #endif | 46 #endif |
41 | 47 |
42 typedef void (PP_ThreadFunction)(void* data); | |
43 | |
44 PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, | 48 PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, |
45 PP_ThreadFunction function, | 49 PP_ThreadFunction function, |
46 void* thread_arg); | 50 void* thread_arg); |
47 PP_INLINE void PP_JoinThread(PP_ThreadType thread); | 51 PP_INLINE void PP_JoinThread(PP_ThreadType thread); |
48 | 52 |
49 #if defined(PPAPI_POSIX) | 53 #if defined(PPAPI_POSIX) |
50 /* Because POSIX thread functions return void* and Windows thread functions do | 54 /* Because POSIX thread functions return void* and Windows thread functions do |
51 * not, we make PPAPI thread functions have the least capability (no returns). | 55 * not, we make PPAPI thread functions have the least capability (no returns). |
52 * This struct wraps the user data & function so that we can use the correct | 56 * This struct wraps the user data & function so that we can use the correct |
53 * function type on POSIX platforms. | 57 * function type on POSIX platforms. |
(...skipping 23 matching lines...) Expand all Loading... | |
77 PP_POSIXThreadFunctionThunk, | 81 PP_POSIXThreadFunctionThunk, |
78 arg_wrapper) == 0); | 82 arg_wrapper) == 0); |
79 } | 83 } |
80 | 84 |
81 PP_INLINE void PP_JoinThread(PP_ThreadType thread) { | 85 PP_INLINE void PP_JoinThread(PP_ThreadType thread) { |
82 void* exit_status; | 86 void* exit_status; |
83 pthread_join(thread, &exit_status); | 87 pthread_join(thread, &exit_status); |
84 } | 88 } |
85 | 89 |
86 #elif defined(PPAPI_OS_WIN) | 90 #elif defined(PPAPI_OS_WIN) |
87 typedef DWORD (PP_WindowsThreadFunction)(void* data); | 91 typedef DWORD (PP_WindowsThreadFunction)(void* data); |
bbudge
2014/09/05 17:22:59
This typedef doesn't appear to be used anywhere.
| |
88 | 92 |
93 PP_INLINE unsigned int __stdcall ProxyThreadFunc(void* param) { | |
bbudge
2014/09/05 17:22:59
This is really an adapter or 'thunk' for the threa
| |
94 PP_ThreadType* params = reinterpret_cast<PP_ThreadType*>(param); | |
95 params->thread_func(params->thread_arg); | |
96 return 0; | |
97 } | |
98 | |
89 PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, | 99 PP_INLINE bool PP_CreateThread(PP_ThreadType* thread, |
90 PP_ThreadFunction function, | 100 PP_ThreadFunction function, |
91 void* thread_arg) { | 101 void* thread_arg) { |
92 if (!thread) | 102 if (!thread) |
93 return false; | 103 return false; |
94 *thread = ::_beginthread(function, | 104 thread->thread_func = function; |
95 0, /* Use default stack size. */ | 105 thread->thread_arg = thread_arg; |
96 thread_arg); | 106 uintptr_t raw_handle = ::_beginthreadex(NULL, |
97 return (*thread != NULL); | 107 0, /* Use default stack size. */ |
bbudge
2014/09/05 17:22:59
nit: indent is strange
| |
108 &ProxyThreadFunc, | |
109 thread, | |
110 0, | |
111 NULL); | |
112 thread->handle = reinterpret_cast<HANDLE>(raw_handle); | |
113 return (thread->handle != NULL); | |
98 } | 114 } |
99 | 115 |
100 PP_INLINE void PP_JoinThread(PP_ThreadType thread) { | 116 PP_INLINE void PP_JoinThread(PP_ThreadType thread_info) { |
101 ::WaitForSingleObject((HANDLE)thread, INFINITE); | 117 ::WaitForSingleObject(thread_info.handle, INFINITE); |
118 ::CloseHandle(thread_info.handle); | |
102 } | 119 } |
103 | 120 |
104 #endif | 121 #endif |
105 | 122 |
106 | 123 |
107 /** | 124 /** |
108 * @} | 125 * @} |
109 */ | 126 */ |
110 | 127 |
111 #endif /* PPAPI_TESTS_PP_THREAD_H_ */ | 128 #endif /* PPAPI_TESTS_PP_THREAD_H_ */ |
112 | 129 |
OLD | NEW |