From 6aabffa2cf5303f4936c8e0ce21b5a7ec00792cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Moreau?= Date: Mon, 17 Sep 2012 19:17:19 -0400 Subject: [PATCH] libwinpr-thread: started stubbing threading API --- winpr/include/winpr/thread.h | 138 +++++++++++++++++ winpr/include/winpr/wtypes.h | 11 ++ winpr/libwinpr/CMakeLists.txt | 2 + winpr/libwinpr/thread/CMakeLists.txt | 50 ++++++ winpr/libwinpr/thread/thread.c | 219 +++++++++++++++++++++++++++ 5 files changed, 420 insertions(+) create mode 100644 winpr/include/winpr/thread.h create mode 100644 winpr/libwinpr/thread/CMakeLists.txt create mode 100644 winpr/libwinpr/thread/thread.c diff --git a/winpr/include/winpr/thread.h b/winpr/include/winpr/thread.h new file mode 100644 index 000000000..d9e7d67c6 --- /dev/null +++ b/winpr/include/winpr/thread.h @@ -0,0 +1,138 @@ +/** + * WinPR: Windows Portable Runtime + * Process Thread Functions + * + * Copyright 2012 Marc-Andre Moreau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WINPR_THREAD_H +#define WINPR_THREAD_H + +#include +#include + +#include + +#ifndef _WIN32 + +typedef struct _STARTUPINFOA +{ + DWORD cb; + LPSTR lpReserved; + LPSTR lpDesktop; + LPSTR lpTitle; + DWORD dwX; + DWORD dwY; + DWORD dwXSize; + DWORD dwYSize; + DWORD dwXCountChars; + DWORD dwYCountChars; + DWORD dwFillAttribute; + DWORD dwFlags; + WORD wShowWindow; + WORD cbReserved2; + LPBYTE lpReserved2; + HANDLE hStdInput; + HANDLE hStdOutput; + HANDLE hStdError; +} STARTUPINFOA, *LPSTARTUPINFOA; + +typedef struct _STARTUPINFOW +{ + DWORD cb; + LPWSTR lpReserved; + LPWSTR lpDesktop; + LPWSTR lpTitle; + DWORD dwX; + DWORD dwY; + DWORD dwXSize; + DWORD dwYSize; + DWORD dwXCountChars; + DWORD dwYCountChars; + DWORD dwFillAttribute; + DWORD dwFlags; + WORD wShowWindow; + WORD cbReserved2; + LPBYTE lpReserved2; + HANDLE hStdInput; + HANDLE hStdOutput; + HANDLE hStdError; +} STARTUPINFOW, *LPSTARTUPINFOW; + +#ifdef UNICODE +typedef STARTUPINFOW STARTUPINFO; +typedef LPSTARTUPINFOW LPSTARTUPINFO; +#else +typedef STARTUPINFOA STARTUPINFO; +typedef LPSTARTUPINFOA LPSTARTUPINFO; +#endif + +WINPR_API BOOL CreateProcessA(LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, + LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, + LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation); + +WINPR_API BOOL CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, + LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, + LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation); + +#ifdef UNICODE +#define CreateProcess CreateProcessW +#else +#define CreateProcess CreateProcessA +#endif + +WINPR_API BOOL CreateProcessAsUserA(HANDLE hToken, LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, + LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, + LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation); + +WINPR_API BOOL CreateProcessAsUserW(HANDLE hToken, LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, + LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, + LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation); + +#ifdef UNICODE +#define CreateProcessAsUser CreateProcessAsUserW +#else +#define CreateProcessAsUser CreateProcessAsUserA +#endif + +WINPR_API HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, + LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId); + +WINPR_API VOID ExitProcess(UINT uExitCode); +WINPR_API VOID ExitThread(DWORD dwExitCode); + +WINPR_API HANDLE GetCurrentProcess(VOID); +WINPR_API DWORD GetCurrentProcessId(VOID); +WINPR_API DWORD GetCurrentProcessorNumber(VOID); + +WINPR_API HANDLE GetCurrentThread(VOID); +WINPR_API DWORD GetCurrentThreadId(VOID); + +WINPR_API DWORD ResumeThread(HANDLE hThread); +WINPR_API DWORD SuspendThread(HANDLE hThread); +WINPR_API BOOL SwitchToThread(VOID); + +WINPR_API BOOL TerminateProcess(HANDLE hProcess, UINT uExitCode); +WINPR_API BOOL TerminateThread(HANDLE hThread, DWORD dwExitCode); + +WINPR_API DWORD TlsAlloc(VOID); +WINPR_API LPVOID TlsGetValue(DWORD dwTlsIndex); +WINPR_API BOOL TlsSetValue(DWORD dwTlsIndex, LPVOID lpTlsValue); +WINPR_API BOOL TlsFree(DWORD dwTlsIndex); + +#endif + +#endif /* WINPR_THREAD_H */ + diff --git a/winpr/include/winpr/wtypes.h b/winpr/include/winpr/wtypes.h index d7cdba3ac..6600cb9f7 100644 --- a/winpr/include/winpr/wtypes.h +++ b/winpr/include/winpr/wtypes.h @@ -208,6 +208,17 @@ typedef struct _SECURITY_ATTRIBUTES BOOL bInheritHandle; } SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES, *LPSECURITY_ATTRIBUTES; +typedef struct _PROCESS_INFORMATION +{ + HANDLE hProcess; + HANDLE hThread; + DWORD dwProcessId; + DWORD dwThreadId; +} PROCESS_INFORMATION, *PPROCESS_INFORMATION, *LPPROCESS_INFORMATION; + +typedef DWORD (*PTHREAD_START_ROUTINE)(LPVOID lpThreadParameter); +typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE; + typedef void* HMODULE; typedef void* FARPROC; diff --git a/winpr/libwinpr/CMakeLists.txt b/winpr/libwinpr/CMakeLists.txt index c3b03afa2..5b1472347 100644 --- a/winpr/libwinpr/CMakeLists.txt +++ b/winpr/libwinpr/CMakeLists.txt @@ -30,6 +30,7 @@ add_subdirectory(utils) add_subdirectory(heap) add_subdirectory(handle) add_subdirectory(synch) +add_subdirectory(thread) add_subdirectory(sysinfo) add_subdirectory(bcrypt) add_subdirectory(dsparse) @@ -47,6 +48,7 @@ if(WITH_MONOLITHIC_BUILD) $ $ $ + $ $ $ $ diff --git a/winpr/libwinpr/thread/CMakeLists.txt b/winpr/libwinpr/thread/CMakeLists.txt new file mode 100644 index 000000000..b72e39feb --- /dev/null +++ b/winpr/libwinpr/thread/CMakeLists.txt @@ -0,0 +1,50 @@ +# WinPR: Windows Portable Runtime +# libwinpr-thread cmake build script +# +# Copyright 2012 Marc-Andre Moreau +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set(CMAKE_THREAD_PREFER_PTHREAD) +find_required_package(Threads) + +set(WINPR_THREAD_SRCS + thread.c) + +if(WITH_MONOLITHIC_BUILD) + add_library(winpr-thread OBJECT ${WINPR_THREAD_SRCS}) +else() + add_library(winpr-thread ${WINPR_THREAD_SRCS}) +endif() + +set_target_properties(winpr-thread PROPERTIES VERSION ${FREERDP_VERSION_FULL} SOVERSION ${FREERDP_VERSION} PREFIX "lib") + +set(WINPR_SYNCH_LIBS + ${CMAKE_THREAD_LIBS_INIT} + ${CMAKE_DL_LIBS}) + +if(${CMAKE_SYSTEM_NAME} MATCHES SunOS) + set(WINPR_SYNCH_LIBS ${WINPR_SYNCH_LIBS} rt) +endif() + +if(WITH_MONOLITHIC_BUILD) + set(WINPR_LIBS ${WINPR_LIBS} ${WINPR_SYNCH_LIBS} PARENT_SCOPE) +else() + if(NOT WIN32) + set(WINPR_SYNCH_LIBS ${WINPR_SYNCH_LIBS} winpr-handle) + endif() + + target_link_libraries(winpr-thread ${WINPR_SYNCH_LIBS}) + install(TARGETS winpr-thread DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endif() + diff --git a/winpr/libwinpr/thread/thread.c b/winpr/libwinpr/thread/thread.c new file mode 100644 index 000000000..c0d631928 --- /dev/null +++ b/winpr/libwinpr/thread/thread.c @@ -0,0 +1,219 @@ +/** + * WinPR: Windows Portable Runtime + * Process Thread Functions + * + * Copyright 2012 Marc-Andre Moreau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +/** + * api-ms-win-core-processthreads-l1-1-1.dll + * + * CreateProcessA + * CreateProcessAsUserW + * CreateProcessW + * CreateRemoteThread + * CreateRemoteThreadEx + * CreateThread + * DeleteProcThreadAttributeList + * ExitProcess + * ExitThread + * FlushInstructionCache + * FlushProcessWriteBuffers + * GetCurrentProcess + * GetCurrentProcessId + * GetCurrentProcessorNumber + * GetCurrentProcessorNumberEx + * GetCurrentThread + * GetCurrentThreadId + * GetCurrentThreadStackLimits + * GetExitCodeProcess + * GetExitCodeThread + * GetPriorityClass + * GetProcessHandleCount + * GetProcessId + * GetProcessIdOfThread + * GetProcessMitigationPolicy + * GetProcessTimes + * GetProcessVersion + * GetStartupInfoW + * GetThreadContext + * GetThreadId + * GetThreadIdealProcessorEx + * GetThreadPriority + * GetThreadPriorityBoost + * GetThreadTimes + * InitializeProcThreadAttributeList + * IsProcessorFeaturePresent + * OpenProcess + * OpenProcessToken + * OpenThread + * OpenThreadToken + * ProcessIdToSessionId + * QueryProcessAffinityUpdateMode + * QueueUserAPC + * ResumeThread + * SetPriorityClass + * SetProcessAffinityUpdateMode + * SetProcessMitigationPolicy + * SetProcessShutdownParameters + * SetThreadContext + * SetThreadIdealProcessorEx + * SetThreadPriority + * SetThreadPriorityBoost + * SetThreadStackGuarantee + * SetThreadToken + * SuspendThread + * SwitchToThread + * TerminateProcess + * TerminateThread + * TlsAlloc + * TlsFree + * TlsGetValue + * TlsSetValue + * UpdateProcThreadAttribute + */ + +#ifndef _WIN32 + +#include + +typedef void *(*pthread_start_routine)(void*); + +BOOL CreateProcessA(LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, + LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, + LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation) +{ + return TRUE; +} + +BOOL CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, + LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, + LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation) +{ + return TRUE; +} + +BOOL CreateProcessAsUserA(HANDLE hToken, LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, + LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, + LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation) +{ + return TRUE; +} + +BOOL CreateProcessAsUserW(HANDLE hToken, LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, + LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, + LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation) +{ + return TRUE; +} + +HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, + LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId) +{ + pthread_t thread; + pthread_create(&thread, 0, (pthread_start_routine) lpStartAddress, lpParameter); + pthread_detach(thread); + + return NULL; +} + +VOID ExitProcess(UINT uExitCode) +{ + +} + +VOID ExitThread(DWORD dwExitCode) +{ + +} + +HANDLE GetCurrentProcess(VOID) +{ + return NULL; +} + +DWORD GetCurrentProcessId(VOID) +{ + return 0; +} + +DWORD GetCurrentProcessorNumber(VOID) +{ + return 0; +} + +HANDLE GetCurrentThread(VOID) +{ + return NULL; +} + +DWORD GetCurrentThreadId(VOID) +{ + return 0; +} + +DWORD ResumeThread(HANDLE hThread) +{ + return 0; +} + +DWORD SuspendThread(HANDLE hThread) +{ + return 0; +} + +BOOL SwitchToThread(VOID) +{ + return TRUE; +} + +BOOL TerminateProcess(HANDLE hProcess, UINT uExitCode) +{ + return TRUE; +} + +BOOL TerminateThread(HANDLE hThread, DWORD dwExitCode) +{ + return TRUE; +} + +DWORD TlsAlloc(VOID) +{ + return 0; +} + +LPVOID TlsGetValue(DWORD dwTlsIndex) +{ + return NULL; +} + +BOOL TlsSetValue(DWORD dwTlsIndex, LPVOID lpTlsValue) +{ + return TRUE; +} + +BOOL TlsFree(DWORD dwTlsIndex) +{ + return TRUE; +} + +#endif +