This source file includes following definitions.
- PP_POSIXThreadFunctionThunk
- PP_CreateThread
- PP_JoinThread
- PP_CreateThread
- PP_JoinThread
#ifndef PPAPI_TESTS_PP_THREAD_H_
#define PPAPI_TESTS_PP_THREAD_H_
#include "ppapi/c/pp_macros.h"
#include "ppapi/tests/test_utils.h"
#if defined(PPAPI_POSIX)
#include <pthread.h>
#elif defined(PPAPI_OS_WIN)
#include <process.h>
#include <windows.h>
#else
#error No thread library detected.
#endif
#if defined(PPAPI_POSIX)
typedef pthread_t PP_ThreadType;
#elif defined(PPAPI_OS_WIN)
typedef uintptr_t PP_ThreadType;
#endif
typedef void (PP_ThreadFunction)(void* data);
PP_INLINE bool PP_CreateThread(PP_ThreadType* thread,
PP_ThreadFunction function,
void* thread_arg);
PP_INLINE void PP_JoinThread(PP_ThreadType thread);
#if defined(PPAPI_POSIX)
struct PP_ThreadFunctionArgWrapper {
void* user_data;
PP_ThreadFunction* user_function;
};
PP_INLINE void* PP_POSIXThreadFunctionThunk(void* posix_thread_arg) {
PP_ThreadFunctionArgWrapper* arg_wrapper =
(PP_ThreadFunctionArgWrapper*)posix_thread_arg;
arg_wrapper->user_function(arg_wrapper->user_data);
free(posix_thread_arg);
return NULL;
}
PP_INLINE bool PP_CreateThread(PP_ThreadType* thread,
PP_ThreadFunction function,
void* thread_arg) {
PP_ThreadFunctionArgWrapper* arg_wrapper =
(PP_ThreadFunctionArgWrapper*)malloc(sizeof(PP_ThreadFunctionArgWrapper));
arg_wrapper->user_function = function;
arg_wrapper->user_data = thread_arg;
return (pthread_create(thread,
NULL,
PP_POSIXThreadFunctionThunk,
arg_wrapper) == 0);
}
PP_INLINE void PP_JoinThread(PP_ThreadType thread) {
void* exit_status;
pthread_join(thread, &exit_status);
}
#elif defined(PPAPI_OS_WIN)
typedef DWORD (PP_WindowsThreadFunction)(void* data);
PP_INLINE bool PP_CreateThread(PP_ThreadType* thread,
PP_ThreadFunction function,
void* thread_arg) {
if (!thread)
return false;
*thread = ::_beginthread(function,
0,
thread_arg);
return (*thread != NULL);
}
PP_INLINE void PP_JoinThread(PP_ThreadType thread) {
::WaitForSingleObject((HANDLE)thread, INFINITE);
}
#endif
#endif