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