blob: f168d8ca980efae65026dd8391f759a3164588b9 [file] [log] [blame]
[email protected]5d904b9e2011-08-30 19:38:311/* 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/* These precompiler names were copied from chromium's build_config.h. */
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>
21#elif defined(PPAPI_HAS_WINDOWS_THREADS)
22#include <process.h>
23#include <windows.h>
24#endif
25
26/**
27 * @file
28 * 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
30 * support both trusted platforms (Windows, Mac, Linux) and untrusted (Native
31 * Client). Apps that use PPAPI only with Native Client should generally use the
32 * Native Client POSIX implementation instead.
33 *
34 * 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
36 * 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
38 * used in ppapi/tests, so is not part of the published API.
39 */
40
41#if defined(PPAPI_HAS_POSIX_THREADS)
42typedef pthread_t PP_ThreadType;
43#elif defined(PPAPI_HAS_WINDOWS_THREADS)
44typedef uintptr_t PP_ThreadType;
45#endif
46
47typedef void (PP_ThreadFunction)(void* data);
48
49PP_INLINE bool PP_CreateThread(PP_ThreadType* thread,
50 PP_ThreadFunction function,
51 void* thread_arg);
52PP_INLINE void PP_JoinThread(PP_ThreadType thread);
53
54#if defined(PPAPI_HAS_POSIX_THREADS)
55/* Because POSIX thread functions return void* and Windows thread functions do
56 * 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
58 * function type on POSIX platforms.
59 */
60struct PP_ThreadFunctionArgWrapper {
61 void* user_data;
62 PP_ThreadFunction* user_function;
63};
64
65PP_INLINE void* PP_POSIXThreadFunctionThunk(void* posix_thread_arg) {
66 PP_ThreadFunctionArgWrapper* arg_wrapper =
67 (PP_ThreadFunctionArgWrapper*)posix_thread_arg;
68 arg_wrapper->user_function(arg_wrapper->user_data);
69 free(posix_thread_arg);
70 return NULL;
71}
72
73PP_INLINE bool PP_CreateThread(PP_ThreadType* thread,
74 PP_ThreadFunction function,
75 void* thread_arg) {
76 PP_ThreadFunctionArgWrapper* arg_wrapper =
77 (PP_ThreadFunctionArgWrapper*)malloc(sizeof(PP_ThreadFunctionArgWrapper));
78 arg_wrapper->user_function = function;
79 arg_wrapper->user_data = thread_arg;
80 return (pthread_create(thread,
81 NULL,
82 PP_POSIXThreadFunctionThunk,
83 arg_wrapper) == 0);
84}
85
86PP_INLINE void PP_JoinThread(PP_ThreadType thread) {
87 void* exit_status;
88 pthread_join(thread, &exit_status);
89}
90
91#elif defined(PPAPI_HAS_WINDOWS_THREADS)
92typedef DWORD (PP_WindowsThreadFunction)(void* data);
93
94PP_INLINE bool PP_CreateThread(PP_ThreadType* thread,
95 PP_ThreadFunction function,
96 void* thread_arg) {
97 if (!thread)
98 return false;
99 *thread = ::_beginthread(function,
100 0, /* Use default stack size. */
101 thread_arg);
102 return (*thread != NULL);
103}
104
105PP_INLINE void PP_JoinThread(PP_ThreadType thread) {
106 ::WaitForSingleObject((HANDLE)thread, INFINITE);
107}
108
109#endif
110
111
112/**
113 * @}
114 */
115
116#endif /* PPAPI_TESTS_PP_THREAD_H_ */
117