2012-09-18 03:17:19 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Process Thread Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
#include <winpr/handle.h>
|
|
|
|
|
2012-09-18 03:17:19 +04:00
|
|
|
#include <winpr/thread.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* api-ms-win-core-processthreads-l1-1-1.dll
|
|
|
|
*
|
|
|
|
* CreateRemoteThread
|
|
|
|
* CreateRemoteThreadEx
|
|
|
|
* CreateThread
|
|
|
|
* DeleteProcThreadAttributeList
|
|
|
|
* ExitThread
|
|
|
|
* FlushInstructionCache
|
|
|
|
* FlushProcessWriteBuffers
|
|
|
|
* GetCurrentThread
|
|
|
|
* GetCurrentThreadId
|
|
|
|
* GetCurrentThreadStackLimits
|
|
|
|
* GetExitCodeThread
|
|
|
|
* GetPriorityClass
|
|
|
|
* GetStartupInfoW
|
|
|
|
* GetThreadContext
|
|
|
|
* GetThreadId
|
|
|
|
* GetThreadIdealProcessorEx
|
|
|
|
* GetThreadPriority
|
|
|
|
* GetThreadPriorityBoost
|
|
|
|
* GetThreadTimes
|
|
|
|
* InitializeProcThreadAttributeList
|
|
|
|
* OpenThread
|
|
|
|
* OpenThreadToken
|
|
|
|
* QueryProcessAffinityUpdateMode
|
|
|
|
* QueueUserAPC
|
|
|
|
* ResumeThread
|
|
|
|
* SetPriorityClass
|
|
|
|
* SetThreadContext
|
|
|
|
* SetThreadPriority
|
|
|
|
* SetThreadPriorityBoost
|
|
|
|
* SetThreadStackGuarantee
|
|
|
|
* SetThreadToken
|
|
|
|
* SuspendThread
|
|
|
|
* SwitchToThread
|
|
|
|
* TerminateThread
|
|
|
|
* UpdateProcThreadAttribute
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
#include "thread.h"
|
2012-09-18 03:17:19 +04:00
|
|
|
|
2012-09-20 05:42:46 +04:00
|
|
|
/**
|
|
|
|
* TODO: implement thread suspend/resume using pthreads
|
|
|
|
* http://stackoverflow.com/questions/3140867/suspend-pthreads-without-using-condition
|
|
|
|
*/
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
void winpr_StartThread(WINPR_THREAD* thread)
|
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
|
|
|
|
if (thread->dwStackSize > 0)
|
|
|
|
pthread_attr_setstacksize(&attr, (size_t) thread->dwStackSize);
|
|
|
|
|
|
|
|
thread->started = TRUE;
|
|
|
|
pthread_create(&thread->thread, &attr, (pthread_start_routine) thread->lpStartAddress, thread->lpParameter);
|
|
|
|
|
|
|
|
pthread_attr_destroy(&attr);
|
2012-09-18 08:10:14 +04:00
|
|
|
}
|
|
|
|
|
2012-09-18 03:17:19 +04:00
|
|
|
HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
|
|
|
|
LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
|
|
|
|
{
|
2012-09-19 03:24:03 +04:00
|
|
|
HANDLE handle;
|
2012-09-23 21:54:14 +04:00
|
|
|
WINPR_THREAD* thread;
|
2012-09-18 20:57:19 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
thread = (WINPR_THREAD*) malloc(sizeof(WINPR_THREAD));
|
|
|
|
ZeroMemory(thread, sizeof(WINPR_THREAD));
|
2012-09-20 05:42:46 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
thread->started = FALSE;
|
|
|
|
thread->dwStackSize = dwStackSize;
|
|
|
|
thread->lpParameter = lpParameter;
|
|
|
|
thread->lpStartAddress = lpStartAddress;
|
|
|
|
thread->lpThreadAttributes = lpThreadAttributes;
|
2012-09-20 05:42:46 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
pthread_mutex_init(&thread->mutex, 0);
|
2012-09-18 03:17:19 +04:00
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
handle = winpr_Handle_Insert(HANDLE_TYPE_THREAD, (void*) thread);
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
if (!(dwCreationFlags & CREATE_SUSPENDED))
|
|
|
|
winpr_StartThread(thread);
|
2012-09-20 05:42:46 +04:00
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
return handle;
|
2012-09-18 03:17:19 +04:00
|
|
|
}
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
HANDLE CreateRemoteThread(HANDLE hProcess, LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
|
|
|
|
LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
|
2012-09-18 03:17:19 +04:00
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-09-18 03:17:19 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
VOID ExitThread(DWORD dwExitCode)
|
|
|
|
{
|
|
|
|
pthread_exit((void*) dwExitCode);
|
2012-09-18 03:17:19 +04:00
|
|
|
}
|
|
|
|
|
2013-01-20 05:32:14 +04:00
|
|
|
HANDLE _GetCurrentThread(VOID)
|
2012-09-18 03:17:19 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD GetCurrentThreadId(VOID)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD ResumeThread(HANDLE hThread)
|
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
|
|
|
WINPR_THREAD* thread;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hThread, &Type, &Object))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
thread = (WINPR_THREAD*) Object;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&thread->mutex);
|
|
|
|
|
|
|
|
if (!thread->started)
|
|
|
|
winpr_StartThread(thread);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&thread->mutex);
|
|
|
|
|
2012-09-18 03:17:19 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD SuspendThread(HANDLE hThread)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SwitchToThread(VOID)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL TerminateThread(HANDLE hThread, DWORD dwExitCode)
|
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
|
|
|
WINPR_THREAD* thread;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hThread, &Type, &Object))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
thread = (WINPR_THREAD*) Object;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&thread->mutex);
|
|
|
|
|
2012-10-14 04:31:01 +04:00
|
|
|
#ifndef ANDROID
|
2012-09-23 21:54:14 +04:00
|
|
|
pthread_cancel(thread->thread);
|
2012-10-14 04:31:01 +04:00
|
|
|
#endif
|
2012-09-23 21:54:14 +04:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&thread->mutex);
|
|
|
|
|
2012-09-18 03:17:19 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|