2012-09-18 03:17:19 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Process Thread Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2015-05-27 19:24:56 +03:00
|
|
|
* Copyright 2015 Hewlett-Packard Development Company, L.P.
|
2021-03-24 20:32:43 +03:00
|
|
|
* Copyright 2021 David Fort <contact@hardening-consulting.com>
|
|
|
|
*
|
2012-09-18 03:17:19 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2012-09-18 03:17:19 +04:00
|
|
|
|
2021-06-09 15:03:34 +03:00
|
|
|
#include <winpr/assert.h>
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
#include <winpr/handle.h>
|
|
|
|
|
2012-09-18 03:17:19 +04:00
|
|
|
#include <winpr/thread.h>
|
|
|
|
|
2024-05-24 12:04:07 +03:00
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
|
|
|
|
#endif
|
|
|
|
|
2012-09-18 03:17:19 +04:00
|
|
|
/**
|
|
|
|
* api-ms-win-core-processthreads-l1-1-1.dll
|
2014-07-15 13:11:59 +04:00
|
|
|
*
|
2012-09-18 03:17:19 +04:00
|
|
|
* 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>
|
2014-05-11 13:23:30 +04:00
|
|
|
#include <winpr/platform.h>
|
|
|
|
|
2024-05-24 12:04:07 +03:00
|
|
|
#include <string.h>
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_UNISTD_H
|
2014-05-11 13:23:30 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_SYS_EVENTFD_H
|
2014-07-14 21:36:31 +04:00
|
|
|
#include <sys/eventfd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-10-27 13:39:02 +03:00
|
|
|
#include <winpr/debug.h>
|
2014-07-14 21:36:31 +04:00
|
|
|
|
|
|
|
#include <errno.h>
|
2014-11-20 19:48:02 +03:00
|
|
|
#include <fcntl.h>
|
2014-07-14 21:36:31 +04:00
|
|
|
|
|
|
|
#include <winpr/collections.h>
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
#include "thread.h"
|
2021-03-24 20:32:43 +03:00
|
|
|
#include "apc.h"
|
2012-09-18 03:17:19 +04:00
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "../handle/handle.h"
|
2014-10-27 13:45:39 +03:00
|
|
|
#include "../log.h"
|
|
|
|
#define TAG WINPR_TAG("thread")
|
2013-05-17 01:32:58 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
static WINPR_THREAD mainThread;
|
2021-09-17 09:55:54 +03:00
|
|
|
|
|
|
|
#if defined(WITH_THREAD_LIST)
|
2014-11-20 19:48:02 +03:00
|
|
|
static wListDictionary* thread_list = NULL;
|
2021-09-17 09:55:54 +03:00
|
|
|
#endif
|
2014-07-14 21:36:31 +04:00
|
|
|
|
|
|
|
static BOOL ThreadCloseHandle(HANDLE handle);
|
2014-11-20 19:48:02 +03:00
|
|
|
static void cleanup_handle(void* obj);
|
2014-07-14 21:36:31 +04:00
|
|
|
|
|
|
|
static BOOL ThreadIsHandled(HANDLE handle)
|
|
|
|
{
|
2022-04-27 16:56:39 +03:00
|
|
|
return WINPR_HANDLE_IS_HANDLED(handle, HANDLE_TYPE_THREAD, FALSE);
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
static int ThreadGetFd(HANDLE handle)
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_THREAD* pThread = (WINPR_THREAD*)handle;
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
if (!ThreadIsHandled(handle))
|
|
|
|
return -1;
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
return pThread->event.fds[0];
|
2015-03-11 19:57:01 +03:00
|
|
|
}
|
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
#define run_mutex_init(fkt, mux, arg) run_mutex_init_(fkt, #fkt, mux, arg)
|
|
|
|
static BOOL run_mutex_init_(int (*fkt)(pthread_mutex_t*, const pthread_mutexattr_t*),
|
|
|
|
const char* name, pthread_mutex_t* mutex,
|
|
|
|
const pthread_mutexattr_t* mutexattr)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int rc = 0;
|
2022-04-27 14:33:08 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(fkt);
|
|
|
|
WINPR_ASSERT(mutex);
|
|
|
|
|
|
|
|
rc = fkt(mutex, mutexattr);
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
2024-02-05 15:01:08 +03:00
|
|
|
char ebuffer[256] = { 0 };
|
|
|
|
WLog_WARN(TAG, "[%s] failed with [%s]", name, winpr_strerror(rc, ebuffer, sizeof(ebuffer)));
|
2022-04-27 14:33:08 +03:00
|
|
|
}
|
|
|
|
return rc == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define run_mutex_fkt(fkt, mux) run_mutex_fkt_(fkt, #fkt, mux)
|
|
|
|
static BOOL run_mutex_fkt_(int (*fkt)(pthread_mutex_t* mux), const char* name,
|
|
|
|
pthread_mutex_t* mutex)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int rc = 0;
|
2022-04-27 14:33:08 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(fkt);
|
|
|
|
WINPR_ASSERT(mutex);
|
|
|
|
|
|
|
|
rc = fkt(mutex);
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
2024-02-05 15:01:08 +03:00
|
|
|
char ebuffer[256] = { 0 };
|
|
|
|
WLog_WARN(TAG, "[%s] failed with [%s]", name, winpr_strerror(rc, ebuffer, sizeof(ebuffer)));
|
2022-04-27 14:33:08 +03:00
|
|
|
}
|
|
|
|
return rc == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define run_cond_init(fkt, cond, arg) run_cond_init_(fkt, #fkt, cond, arg)
|
|
|
|
static BOOL run_cond_init_(int (*fkt)(pthread_cond_t*, const pthread_condattr_t*), const char* name,
|
|
|
|
pthread_cond_t* condition, const pthread_condattr_t* conditionattr)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int rc = 0;
|
2022-04-27 14:33:08 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(fkt);
|
|
|
|
WINPR_ASSERT(condition);
|
|
|
|
|
|
|
|
rc = fkt(condition, conditionattr);
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
2024-02-05 15:01:08 +03:00
|
|
|
char ebuffer[256] = { 0 };
|
|
|
|
WLog_WARN(TAG, "[%s] failed with [%s]", name, winpr_strerror(rc, ebuffer, sizeof(ebuffer)));
|
2022-04-27 14:33:08 +03:00
|
|
|
}
|
|
|
|
return rc == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define run_cond_fkt(fkt, cond) run_cond_fkt_(fkt, #fkt, cond)
|
|
|
|
static BOOL run_cond_fkt_(int (*fkt)(pthread_cond_t* mux), const char* name,
|
|
|
|
pthread_cond_t* condition)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int rc = 0;
|
2022-04-27 14:33:08 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(fkt);
|
|
|
|
WINPR_ASSERT(condition);
|
|
|
|
|
|
|
|
rc = fkt(condition);
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
2024-02-05 15:01:08 +03:00
|
|
|
char ebuffer[256] = { 0 };
|
|
|
|
WLog_WARN(TAG, "[%s] failed with [%s]", name, winpr_strerror(rc, ebuffer, sizeof(ebuffer)));
|
2022-04-27 14:33:08 +03:00
|
|
|
}
|
|
|
|
return rc == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pthread_mutex_checked_unlock(pthread_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(mutex);
|
|
|
|
WINPR_ASSERT(pthread_mutex_trylock(mutex) == EBUSY);
|
|
|
|
return pthread_mutex_unlock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL mux_condition_bundle_init(mux_condition_bundle* bundle)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(bundle);
|
|
|
|
|
|
|
|
bundle->val = FALSE;
|
|
|
|
if (!run_mutex_init(pthread_mutex_init, &bundle->mux, NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!run_cond_init(pthread_cond_init, &bundle->cond, NULL))
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mux_condition_bundle_uninit(mux_condition_bundle* bundle)
|
|
|
|
{
|
|
|
|
mux_condition_bundle empty = { 0 };
|
|
|
|
|
|
|
|
WINPR_ASSERT(bundle);
|
|
|
|
|
|
|
|
run_cond_fkt(pthread_cond_destroy, &bundle->cond);
|
|
|
|
run_mutex_fkt(pthread_mutex_destroy, &bundle->mux);
|
|
|
|
*bundle = empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL mux_condition_bundle_signal(mux_condition_bundle* bundle)
|
|
|
|
{
|
|
|
|
BOOL rc = TRUE;
|
|
|
|
WINPR_ASSERT(bundle);
|
|
|
|
|
|
|
|
if (!run_mutex_fkt(pthread_mutex_lock, &bundle->mux))
|
|
|
|
return FALSE;
|
|
|
|
bundle->val = TRUE;
|
|
|
|
if (!run_cond_fkt(pthread_cond_signal, &bundle->cond))
|
|
|
|
rc = FALSE;
|
|
|
|
if (!run_mutex_fkt(pthread_mutex_checked_unlock, &bundle->mux))
|
|
|
|
rc = FALSE;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL mux_condition_bundle_lock(mux_condition_bundle* bundle)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(bundle);
|
|
|
|
return run_mutex_fkt(pthread_mutex_lock, &bundle->mux);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL mux_condition_bundle_unlock(mux_condition_bundle* bundle)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(bundle);
|
|
|
|
return run_mutex_fkt(pthread_mutex_checked_unlock, &bundle->mux);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL mux_condition_bundle_wait(mux_condition_bundle* bundle, const char* name)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
|
|
|
|
WINPR_ASSERT(bundle);
|
|
|
|
WINPR_ASSERT(name);
|
|
|
|
WINPR_ASSERT(pthread_mutex_trylock(&bundle->mux) == EBUSY);
|
|
|
|
|
|
|
|
while (!bundle->val)
|
|
|
|
{
|
|
|
|
int r = pthread_cond_wait(&bundle->cond, &bundle->mux);
|
|
|
|
if (r != 0)
|
|
|
|
{
|
2024-02-05 15:01:08 +03:00
|
|
|
char ebuffer[256] = { 0 };
|
|
|
|
WLog_ERR(TAG, "failed to wait for %s [%s]", name,
|
|
|
|
winpr_strerror(r, ebuffer, sizeof(ebuffer)));
|
2022-04-27 14:33:08 +03:00
|
|
|
switch (r)
|
|
|
|
{
|
|
|
|
case ENOTRECOVERABLE:
|
|
|
|
case EPERM:
|
|
|
|
case ETIMEDOUT:
|
|
|
|
case EINVAL:
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = bundle->val;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL signal_thread_ready(WINPR_THREAD* thread)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(thread);
|
|
|
|
|
|
|
|
return mux_condition_bundle_signal(&thread->isCreated);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL signal_thread_is_running(WINPR_THREAD* thread)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(thread);
|
|
|
|
|
|
|
|
return mux_condition_bundle_signal(&thread->isRunning);
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
static DWORD ThreadCleanupHandle(HANDLE handle)
|
|
|
|
{
|
2022-04-27 14:33:08 +03:00
|
|
|
DWORD status = WAIT_FAILED;
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_THREAD* thread = (WINPR_THREAD*)handle;
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
if (!ThreadIsHandled(handle))
|
|
|
|
return WAIT_FAILED;
|
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
if (!run_mutex_fkt(pthread_mutex_lock, &thread->mutex))
|
2015-07-16 14:48:14 +03:00
|
|
|
return WAIT_FAILED;
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
if (!thread->joined)
|
|
|
|
{
|
2022-04-28 00:26:28 +03:00
|
|
|
int rc = pthread_join(thread->thread, NULL);
|
2015-03-11 19:57:01 +03:00
|
|
|
|
2022-04-28 00:26:28 +03:00
|
|
|
if (rc != 0)
|
2015-03-11 19:57:01 +03:00
|
|
|
{
|
2024-02-05 15:01:08 +03:00
|
|
|
char ebuffer[256] = { 0 };
|
|
|
|
WLog_ERR(TAG, "pthread_join failure: [%d] %s", rc,
|
|
|
|
winpr_strerror(rc, ebuffer, sizeof(ebuffer)));
|
2022-04-27 14:33:08 +03:00
|
|
|
goto fail;
|
2015-03-11 19:57:01 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
thread->joined = TRUE;
|
|
|
|
}
|
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
status = WAIT_OBJECT_0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (!run_mutex_fkt(pthread_mutex_checked_unlock, &thread->mutex))
|
2015-07-16 14:48:14 +03:00
|
|
|
return WAIT_FAILED;
|
2015-03-11 19:57:01 +03:00
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
return status;
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static HANDLE_OPS ops = { ThreadIsHandled,
|
|
|
|
ThreadCloseHandle,
|
|
|
|
ThreadGetFd,
|
|
|
|
ThreadCleanupHandle,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2022-04-13 10:34:05 +03:00
|
|
|
NULL,
|
2019-11-06 17:24:51 +03:00
|
|
|
NULL };
|
2015-03-16 02:29:37 +03:00
|
|
|
|
2014-10-27 13:39:02 +03:00
|
|
|
static void dump_thread(WINPR_THREAD* thread)
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
#if defined(WITH_DEBUG_THREADS)
|
|
|
|
void* stack = winpr_backtrace(20);
|
2024-01-23 18:49:54 +03:00
|
|
|
char** msg = NULL;
|
|
|
|
size_t used = 0;
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_DBG(TAG, "Called from:");
|
|
|
|
msg = winpr_backtrace_symbols(stack, &used);
|
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (size_t i = 0; i < used; i++)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "[%" PRIdz "]: %s", i, msg[i]);
|
2014-10-27 13:39:02 +03:00
|
|
|
|
|
|
|
free(msg);
|
|
|
|
winpr_backtrace_free(stack);
|
|
|
|
WLog_DBG(TAG, "Thread handle created still not closed!");
|
|
|
|
msg = winpr_backtrace_symbols(thread->create_stack, &used);
|
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (size_t i = 0; i < used; i++)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "[%" PRIdz "]: %s", i, msg[i]);
|
2014-10-27 13:39:02 +03:00
|
|
|
|
|
|
|
free(msg);
|
2014-07-14 21:36:31 +04:00
|
|
|
|
|
|
|
if (thread->started)
|
2014-11-20 19:48:02 +03:00
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_DBG(TAG, "Thread still running!");
|
2014-11-20 19:48:02 +03:00
|
|
|
}
|
2014-07-14 21:36:31 +04:00
|
|
|
else if (!thread->exit_stack)
|
2014-11-20 19:48:02 +03:00
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_DBG(TAG, "Thread suspended.");
|
2014-11-20 19:48:02 +03:00
|
|
|
}
|
2014-07-14 21:36:31 +04:00
|
|
|
else
|
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_DBG(TAG, "Thread exited at:");
|
|
|
|
msg = winpr_backtrace_symbols(thread->exit_stack, &used);
|
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (size_t i = 0; i < used; i++)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "[%" PRIdz "]: %s", i, msg[i]);
|
2014-10-27 13:39:02 +03:00
|
|
|
|
|
|
|
free(msg);
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
2021-09-17 09:55:54 +03:00
|
|
|
#else
|
|
|
|
WINPR_UNUSED(thread);
|
2014-07-14 21:36:31 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2016-10-07 15:07:51 +03:00
|
|
|
static BOOL set_event(WINPR_THREAD* thread)
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
return winpr_event_set(&thread->event);
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
|
|
|
|
2016-10-07 15:07:51 +03:00
|
|
|
static BOOL reset_event(WINPR_THREAD* thread)
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
return winpr_event_reset(&thread->event);
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
|
|
|
|
2021-09-17 09:55:54 +03:00
|
|
|
#if defined(WITH_THREAD_LIST)
|
2018-10-18 10:06:55 +03:00
|
|
|
static BOOL thread_compare(const void* a, const void* b)
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2018-10-18 10:06:55 +03:00
|
|
|
const pthread_t* p1 = a;
|
|
|
|
const pthread_t* p2 = b;
|
2014-11-20 19:48:02 +03:00
|
|
|
BOOL rc = pthread_equal(*p1, *p2);
|
2014-07-14 21:36:31 +04:00
|
|
|
return rc;
|
|
|
|
}
|
2021-09-17 09:55:54 +03:00
|
|
|
#endif
|
2012-09-20 05:42:46 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
static INIT_ONCE threads_InitOnce = INIT_ONCE_STATIC_INIT;
|
|
|
|
static pthread_t mainThreadId;
|
|
|
|
static DWORD currentThreadTlsIndex = TLS_OUT_OF_INDEXES;
|
|
|
|
|
2021-06-17 10:21:20 +03:00
|
|
|
static BOOL initializeThreads(PINIT_ONCE InitOnce, PVOID Parameter, PVOID* Context)
|
2021-03-24 20:32:43 +03:00
|
|
|
{
|
|
|
|
if (!apc_init(&mainThread.apc))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to initialize APC");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-04-27 16:56:39 +03:00
|
|
|
mainThread.common.Type = HANDLE_TYPE_THREAD;
|
2021-03-24 20:32:43 +03:00
|
|
|
mainThreadId = pthread_self();
|
|
|
|
|
|
|
|
currentThreadTlsIndex = TlsAlloc();
|
|
|
|
if (currentThreadTlsIndex == TLS_OUT_OF_INDEXES)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Major bug, unable to allocate a TLS value for currentThread");
|
|
|
|
}
|
|
|
|
|
2021-09-17 09:55:54 +03:00
|
|
|
#if defined(WITH_THREAD_LIST)
|
|
|
|
thread_list = ListDictionary_New(TRUE);
|
|
|
|
|
|
|
|
if (!thread_list)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Couldn't create global thread list");
|
|
|
|
goto error_thread_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_list->objectKey.fnObjectEquals = thread_compare;
|
|
|
|
#endif
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
out:
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
static BOOL signal_and_wait_for_ready(WINPR_THREAD* thread)
|
|
|
|
{
|
|
|
|
BOOL res = FALSE;
|
|
|
|
|
|
|
|
WINPR_ASSERT(thread);
|
|
|
|
|
|
|
|
if (!mux_condition_bundle_lock(&thread->isRunning))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!signal_thread_ready(thread))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!mux_condition_bundle_wait(&thread->isRunning, "threadIsRunning"))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
#if defined(WITH_THREAD_LIST)
|
|
|
|
if (!ListDictionary_Contains(thread_list, &thread->thread))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Thread not in thread_list, startup failed!");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
res = TRUE;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (!mux_condition_bundle_unlock(&thread->isRunning))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-07-15 13:11:59 +04:00
|
|
|
/* Thread launcher function responsible for registering
|
|
|
|
* cleanup handlers and calling pthread_exit, if not done
|
|
|
|
* in thread function. */
|
2014-11-20 19:48:02 +03:00
|
|
|
static void* thread_launcher(void* arg)
|
2012-09-23 21:54:14 +04:00
|
|
|
{
|
2018-10-24 14:17:04 +03:00
|
|
|
DWORD rc = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_THREAD* thread = (WINPR_THREAD*)arg;
|
2024-01-23 18:49:54 +03:00
|
|
|
LPTHREAD_START_ROUTINE fkt = NULL;
|
2014-07-15 13:11:59 +04:00
|
|
|
|
|
|
|
if (!thread)
|
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_ERR(TAG, "Called with invalid argument %p", arg);
|
2014-07-15 13:11:59 +04:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (!TlsSetValue(currentThreadTlsIndex, thread))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "thread %d, unable to set current thread value", pthread_self());
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:17:04 +03:00
|
|
|
if (!(fkt = thread->lpStartAddress))
|
2015-07-23 12:52:24 +03:00
|
|
|
{
|
2024-08-30 15:07:46 +03:00
|
|
|
union
|
|
|
|
{
|
|
|
|
LPTHREAD_START_ROUTINE fkt;
|
|
|
|
void* pv;
|
|
|
|
} cnv;
|
|
|
|
cnv.fkt = fkt;
|
|
|
|
WLog_ERR(TAG, "Thread function argument is %p", cnv.pv);
|
2015-07-23 12:52:24 +03:00
|
|
|
goto exit;
|
|
|
|
}
|
2014-07-15 13:11:59 +04:00
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
if (!signal_and_wait_for_ready(thread))
|
2015-07-23 12:52:24 +03:00
|
|
|
goto exit;
|
2015-05-26 18:40:34 +03:00
|
|
|
|
2015-07-23 12:52:24 +03:00
|
|
|
rc = fkt(thread->lpParameter);
|
2014-07-15 13:11:59 +04:00
|
|
|
exit:
|
2014-07-16 11:40:59 +04:00
|
|
|
|
2014-11-16 18:51:04 +03:00
|
|
|
if (thread)
|
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
apc_cleanupThread(thread);
|
|
|
|
|
2014-11-16 18:51:04 +03:00
|
|
|
if (!thread->exited)
|
2018-10-24 14:17:04 +03:00
|
|
|
thread->dwExitCode = rc;
|
2014-07-16 11:40:59 +04:00
|
|
|
|
2014-11-16 18:51:04 +03:00
|
|
|
set_event(thread);
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
signal_thread_ready(thread);
|
|
|
|
|
2014-11-16 18:51:04 +03:00
|
|
|
if (thread->detached || !thread->started)
|
|
|
|
cleanup_handle(thread);
|
|
|
|
}
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2018-10-24 14:17:04 +03:00
|
|
|
return NULL;
|
2014-07-15 13:11:59 +04:00
|
|
|
}
|
|
|
|
|
2016-10-07 15:07:51 +03:00
|
|
|
static BOOL winpr_StartThread(WINPR_THREAD* thread)
|
2014-07-15 13:11:59 +04:00
|
|
|
{
|
2022-04-27 14:33:08 +03:00
|
|
|
BOOL rc = FALSE;
|
|
|
|
BOOL locked = FALSE;
|
|
|
|
pthread_attr_t attr = { 0 };
|
|
|
|
|
|
|
|
if (!mux_condition_bundle_lock(&thread->isCreated))
|
|
|
|
return FALSE;
|
|
|
|
locked = TRUE;
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
pthread_attr_init(&attr);
|
2013-01-23 01:19:32 +04:00
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
2012-09-23 21:54:14 +04:00
|
|
|
|
|
|
|
if (thread->dwStackSize > 0)
|
2019-11-06 17:24:51 +03:00
|
|
|
pthread_attr_setstacksize(&attr, (size_t)thread->dwStackSize);
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2015-01-16 13:26:31 +03:00
|
|
|
thread->started = TRUE;
|
2014-07-14 21:36:31 +04:00
|
|
|
reset_event(thread);
|
2015-01-16 13:26:31 +03:00
|
|
|
|
2021-09-17 09:55:54 +03:00
|
|
|
#if defined(WITH_THREAD_LIST)
|
2015-05-27 19:15:17 +03:00
|
|
|
if (!ListDictionary_Add(thread_list, &thread->thread, thread))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to add the thread to the thread list");
|
|
|
|
goto error;
|
|
|
|
}
|
2021-09-17 09:55:54 +03:00
|
|
|
#endif
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2021-09-17 09:55:54 +03:00
|
|
|
if (pthread_create(&thread->thread, &attr, thread_launcher, thread))
|
2015-05-18 12:28:00 +03:00
|
|
|
goto error;
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
if (!mux_condition_bundle_wait(&thread->isCreated, "threadIsCreated"))
|
|
|
|
goto error;
|
2021-09-17 09:55:54 +03:00
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
locked = FALSE;
|
|
|
|
if (!mux_condition_bundle_unlock(&thread->isCreated))
|
2021-09-17 09:55:54 +03:00
|
|
|
goto error;
|
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
if (!signal_thread_is_running(thread))
|
2021-09-17 09:55:54 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to signal the thread was ready");
|
2015-07-16 14:48:14 +03:00
|
|
|
goto error;
|
2021-09-17 09:55:54 +03:00
|
|
|
}
|
2015-05-26 18:55:27 +03:00
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
rc = TRUE;
|
2015-05-18 12:28:00 +03:00
|
|
|
error:
|
2022-04-27 14:33:08 +03:00
|
|
|
if (locked)
|
|
|
|
{
|
|
|
|
if (!mux_condition_bundle_unlock(&thread->isCreated))
|
|
|
|
rc = FALSE;
|
|
|
|
}
|
|
|
|
|
2015-05-18 12:28:00 +03:00
|
|
|
pthread_attr_destroy(&attr);
|
2022-04-27 14:33:08 +03:00
|
|
|
|
|
|
|
if (rc)
|
|
|
|
dump_thread(thread);
|
|
|
|
|
|
|
|
return rc;
|
2012-09-18 08:10:14 +04:00
|
|
|
}
|
|
|
|
|
2024-05-24 12:04:07 +03:00
|
|
|
BOOL SetThreadPriority(HANDLE hThread, int nPriority)
|
|
|
|
{
|
|
|
|
ULONG Type = 0;
|
|
|
|
WINPR_HANDLE* Object = NULL;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hThread, &Type, &Object) || Object->Type != HANDLE_TYPE_THREAD)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
WINPR_THREAD* thread = (WINPR_THREAD*)Object;
|
|
|
|
|
|
|
|
const int min = 19;
|
|
|
|
const int max = 0;
|
|
|
|
const int diff = (max - min);
|
|
|
|
const int normal = min + diff / 2;
|
|
|
|
const int off = MIN(1, diff / 4);
|
|
|
|
int sched_priority = -1;
|
|
|
|
|
|
|
|
switch (nPriority & ~(THREAD_MODE_BACKGROUND_BEGIN | THREAD_MODE_BACKGROUND_END))
|
|
|
|
{
|
|
|
|
case THREAD_PRIORITY_ABOVE_NORMAL:
|
|
|
|
sched_priority = MIN(normal + off, max);
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_BELOW_NORMAL:
|
|
|
|
sched_priority = MAX(normal - off, min);
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_HIGHEST:
|
|
|
|
sched_priority = max;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_IDLE:
|
|
|
|
sched_priority = min;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_LOWEST:
|
|
|
|
sched_priority = min;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_TIME_CRITICAL:
|
|
|
|
sched_priority = max;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case THREAD_PRIORITY_NORMAL:
|
|
|
|
sched_priority = normal;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L) && defined(PTHREAD_SETSCHEDPRIO)
|
|
|
|
const int rc = pthread_setschedprio(thread->thread, sched_priority);
|
|
|
|
if (rc != 0)
|
|
|
|
WLog_ERR(TAG, "pthread_setschedprio(%d) %s [%d]", sched_priority, strerror(rc), rc);
|
|
|
|
return rc == 0;
|
|
|
|
#else
|
|
|
|
WLog_WARN(TAG, "pthread_setschedprio(%d) not implemented, requires POSIX 2008 or later",
|
|
|
|
sched_priority);
|
|
|
|
return TRUE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
|
2016-10-07 15:07:51 +03:00
|
|
|
LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter,
|
|
|
|
DWORD dwCreationFlags, LPDWORD lpThreadId)
|
2012-09-18 03:17:19 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
HANDLE handle = NULL;
|
2021-09-17 09:55:54 +03:00
|
|
|
WINPR_THREAD* thread = (WINPR_THREAD*)calloc(1, sizeof(WINPR_THREAD));
|
2014-05-11 13:23:30 +04:00
|
|
|
|
2014-04-09 18:07:06 +04:00
|
|
|
if (!thread)
|
|
|
|
return NULL;
|
2012-09-20 05:42:46 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
thread->dwStackSize = dwStackSize;
|
|
|
|
thread->lpParameter = lpParameter;
|
|
|
|
thread->lpStartAddress = lpStartAddress;
|
|
|
|
thread->lpThreadAttributes = lpThreadAttributes;
|
2022-04-27 16:56:39 +03:00
|
|
|
thread->common.ops = &ops;
|
2014-10-27 13:39:02 +03:00
|
|
|
#if defined(WITH_DEBUG_THREADS)
|
|
|
|
thread->create_stack = winpr_backtrace(20);
|
2014-07-14 21:36:31 +04:00
|
|
|
dump_thread(thread);
|
|
|
|
#endif
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (!winpr_event_init(&thread->event))
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WLog_ERR(TAG, "failed to create event");
|
2021-09-17 09:55:54 +03:00
|
|
|
goto fail;
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
if (!run_mutex_init(pthread_mutex_init, &thread->mutex, NULL))
|
2014-11-21 23:12:49 +03:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WLog_ERR(TAG, "failed to initialize thread mutex");
|
2021-09-17 09:55:54 +03:00
|
|
|
goto fail;
|
2014-11-21 23:12:49 +03:00
|
|
|
}
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (!apc_init(&thread->apc))
|
2015-03-23 19:25:23 +03:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WLog_ERR(TAG, "failed to initialize APC");
|
2021-09-17 09:55:54 +03:00
|
|
|
goto fail;
|
2015-03-23 19:25:23 +03:00
|
|
|
}
|
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
if (!mux_condition_bundle_init(&thread->isCreated))
|
2021-09-17 09:55:54 +03:00
|
|
|
goto fail;
|
2022-04-27 14:33:08 +03:00
|
|
|
if (!mux_condition_bundle_init(&thread->isRunning))
|
2021-09-17 09:55:54 +03:00
|
|
|
goto fail;
|
2015-05-27 19:15:17 +03:00
|
|
|
|
2015-07-14 12:39:41 +03:00
|
|
|
WINPR_HANDLE_SET_TYPE_AND_MODE(thread, HANDLE_TYPE_THREAD, WINPR_FD_READ);
|
2019-11-06 17:24:51 +03:00
|
|
|
handle = (HANDLE)thread;
|
2012-09-19 03:24:03 +04:00
|
|
|
|
2021-09-17 09:55:54 +03:00
|
|
|
InitOnceExecuteOnce(&threads_InitOnce, initializeThreads, NULL, NULL);
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
if (!(dwCreationFlags & CREATE_SUSPENDED))
|
2015-05-18 12:28:00 +03:00
|
|
|
{
|
|
|
|
if (!winpr_StartThread(thread))
|
2021-09-17 09:55:54 +03:00
|
|
|
goto fail;
|
2015-05-18 12:28:00 +03:00
|
|
|
}
|
2014-07-14 21:36:31 +04:00
|
|
|
else
|
2015-05-18 12:28:00 +03:00
|
|
|
{
|
|
|
|
if (!set_event(thread))
|
2021-09-17 09:55:54 +03:00
|
|
|
goto fail;
|
2015-05-18 12:28:00 +03:00
|
|
|
}
|
2012-09-20 05:42:46 +04:00
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
return handle;
|
2021-09-17 09:55:54 +03:00
|
|
|
fail:
|
|
|
|
cleanup_handle(thread);
|
2015-05-18 12:28:00 +03:00
|
|
|
return NULL;
|
2012-09-18 03:17:19 +04:00
|
|
|
}
|
|
|
|
|
2016-10-07 15:07:51 +03:00
|
|
|
void cleanup_handle(void* obj)
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_THREAD* thread = (WINPR_THREAD*)obj;
|
2021-09-17 09:55:54 +03:00
|
|
|
if (!thread)
|
|
|
|
return;
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (!apc_uninit(&thread->apc))
|
|
|
|
WLog_ERR(TAG, "failed to destroy APC");
|
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
mux_condition_bundle_uninit(&thread->isCreated);
|
|
|
|
mux_condition_bundle_uninit(&thread->isRunning);
|
|
|
|
run_mutex_fkt(pthread_mutex_destroy, &thread->mutex);
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
winpr_event_uninit(&thread->event);
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2021-09-17 09:55:54 +03:00
|
|
|
#if defined(WITH_THREAD_LIST)
|
|
|
|
ListDictionary_Remove(thread_list, &thread->thread);
|
|
|
|
#endif
|
2014-10-27 13:39:02 +03:00
|
|
|
#if defined(WITH_DEBUG_THREADS)
|
|
|
|
|
|
|
|
if (thread->create_stack)
|
|
|
|
winpr_backtrace_free(thread->create_stack);
|
|
|
|
|
|
|
|
if (thread->exit_stack)
|
|
|
|
winpr_backtrace_free(thread->exit_stack);
|
|
|
|
|
|
|
|
#endif
|
2014-07-14 21:36:31 +04:00
|
|
|
free(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ThreadCloseHandle(HANDLE handle)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_THREAD* thread = (WINPR_THREAD*)handle;
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2021-09-17 09:55:54 +03:00
|
|
|
#if defined(WITH_THREAD_LIST)
|
2014-07-14 21:36:31 +04:00
|
|
|
if (!thread_list)
|
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_ERR(TAG, "Thread list does not exist, check call!");
|
2014-07-14 21:36:31 +04:00
|
|
|
dump_thread(thread);
|
|
|
|
}
|
|
|
|
else if (!ListDictionary_Contains(thread_list, &thread->thread))
|
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_ERR(TAG, "Thread list does not contain this thread! check call!");
|
2014-07-14 21:36:31 +04:00
|
|
|
dump_thread(thread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ListDictionary_Lock(thread_list);
|
2021-09-17 09:55:54 +03:00
|
|
|
#endif
|
2014-07-14 21:36:31 +04:00
|
|
|
dump_thread(thread);
|
|
|
|
|
2014-07-15 13:11:59 +04:00
|
|
|
if ((thread->started) && (WaitForSingleObject(thread, 0) != WAIT_OBJECT_0))
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2019-12-26 11:22:41 +03:00
|
|
|
WLog_DBG(TAG, "Thread running, setting to detached state!");
|
2014-07-14 21:36:31 +04:00
|
|
|
thread->detached = TRUE;
|
|
|
|
pthread_detach(thread->thread);
|
|
|
|
}
|
2014-07-15 13:11:59 +04:00
|
|
|
else
|
2014-11-20 19:48:02 +03:00
|
|
|
{
|
2014-07-15 13:11:59 +04:00
|
|
|
cleanup_handle(thread);
|
2014-11-20 19:48:02 +03:00
|
|
|
}
|
2014-07-15 14:42:53 +04:00
|
|
|
|
2021-09-17 09:55:54 +03:00
|
|
|
#if defined(WITH_THREAD_LIST)
|
2014-07-14 21:36:31 +04:00
|
|
|
ListDictionary_Unlock(thread_list);
|
|
|
|
}
|
2021-09-17 09:55:54 +03:00
|
|
|
#endif
|
2014-07-14 21:36:31 +04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03: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
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "not implemented");
|
2016-06-01 17:26:26 +03:00
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
2012-09-23 21:54:14 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-09-18 03:17:19 +04:00
|
|
|
|
2014-07-16 14:01:58 +04:00
|
|
|
VOID ExitThread(DWORD dwExitCode)
|
2012-09-23 21:54:14 +04:00
|
|
|
{
|
2021-09-20 09:08:02 +03:00
|
|
|
#if defined(WITH_THREAD_LIST)
|
2014-11-16 17:21:13 +03:00
|
|
|
DWORD rc;
|
2014-07-14 21:36:31 +04:00
|
|
|
pthread_t tid = pthread_self();
|
|
|
|
|
2014-11-11 19:21:34 +03:00
|
|
|
if (!thread_list)
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_ERR(TAG, "function called without existing thread list!");
|
2014-07-14 21:36:31 +04:00
|
|
|
#if defined(WITH_DEBUG_THREADS)
|
|
|
|
DumpThreadHandles();
|
|
|
|
#endif
|
2014-11-11 19:21:34 +03:00
|
|
|
pthread_exit(0);
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
|
|
|
else if (!ListDictionary_Contains(thread_list, &tid))
|
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_ERR(TAG, "function called, but no matching entry in thread list!");
|
2014-07-14 21:36:31 +04:00
|
|
|
#if defined(WITH_DEBUG_THREADS)
|
|
|
|
DumpThreadHandles();
|
|
|
|
#endif
|
2014-11-11 19:21:34 +03:00
|
|
|
pthread_exit(0);
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-11 19:21:34 +03:00
|
|
|
WINPR_THREAD* thread;
|
2014-07-14 21:36:31 +04:00
|
|
|
ListDictionary_Lock(thread_list);
|
|
|
|
thread = ListDictionary_GetItemValue(thread_list, &tid);
|
2021-06-09 15:03:34 +03:00
|
|
|
WINPR_ASSERT(thread);
|
2014-07-15 14:42:53 +04:00
|
|
|
thread->exited = TRUE;
|
|
|
|
thread->dwExitCode = dwExitCode;
|
2014-10-27 13:39:02 +03:00
|
|
|
#if defined(WITH_DEBUG_THREADS)
|
|
|
|
thread->exit_stack = winpr_backtrace(20);
|
2014-07-15 14:42:53 +04:00
|
|
|
#endif
|
2014-07-14 21:36:31 +04:00
|
|
|
ListDictionary_Unlock(thread_list);
|
2014-07-16 14:08:34 +04:00
|
|
|
set_event(thread);
|
2014-11-16 17:21:13 +03:00
|
|
|
rc = thread->dwExitCode;
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2014-07-16 14:08:34 +04:00
|
|
|
if (thread->detached || !thread->started)
|
|
|
|
cleanup_handle(thread);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pthread_exit((void*)(size_t)rc);
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
2021-09-20 09:08:02 +03:00
|
|
|
#else
|
|
|
|
WINPR_UNUSED(dwExitCode);
|
2021-09-17 09:55:54 +03:00
|
|
|
#endif
|
2012-09-18 03:17:19 +04:00
|
|
|
}
|
|
|
|
|
2013-04-02 22:51:12 +04:00
|
|
|
BOOL GetExitCodeThread(HANDLE hThread, LPDWORD lpExitCode)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
ULONG Type = 0;
|
|
|
|
WINPR_HANDLE* Object = NULL;
|
|
|
|
WINPR_THREAD* thread = NULL;
|
2013-04-02 22:51:12 +04:00
|
|
|
|
2022-09-21 12:01:52 +03:00
|
|
|
if (!winpr_Handle_GetInfo(hThread, &Type, &Object) || Object->Type != HANDLE_TYPE_THREAD)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "hThread is not a thread");
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
2013-04-02 22:51:12 +04:00
|
|
|
return FALSE;
|
2022-09-21 12:01:52 +03:00
|
|
|
}
|
2013-04-02 22:51:12 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
thread = (WINPR_THREAD*)Object;
|
2013-04-02 22:51:12 +04:00
|
|
|
*lpExitCode = thread->dwExitCode;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
WINPR_THREAD* winpr_GetCurrentThread(VOID)
|
2012-09-18 03:17:19 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
WINPR_THREAD* ret = NULL;
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
InitOnceExecuteOnce(&threads_InitOnce, initializeThreads, NULL, NULL);
|
|
|
|
if (mainThreadId == pthread_self())
|
|
|
|
return (HANDLE)&mainThread;
|
|
|
|
|
|
|
|
ret = TlsGetValue(currentThreadTlsIndex);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
HANDLE _GetCurrentThread(VOID)
|
|
|
|
{
|
|
|
|
return (HANDLE)winpr_GetCurrentThread();
|
2012-09-18 03:17:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD GetCurrentThreadId(VOID)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
pthread_t tid = 0;
|
2013-01-22 03:33:00 +04:00
|
|
|
tid = pthread_self();
|
2015-05-20 17:52:24 +03:00
|
|
|
/* Since pthread_t can be 64-bits on some systems, take just the */
|
|
|
|
/* lower 32-bits of it for the thread ID returned by this function. */
|
2016-02-04 14:57:21 +03:00
|
|
|
return (DWORD)tid & 0xffffffffUL;
|
2012-09-18 03:17:19 +04:00
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
WINPR_APC_ITEM apc;
|
|
|
|
PAPCFUNC completion;
|
|
|
|
ULONG_PTR completionArg;
|
|
|
|
} UserApcItem;
|
|
|
|
|
2021-06-17 10:21:20 +03:00
|
|
|
static void userAPC(LPVOID arg)
|
2021-03-24 20:32:43 +03:00
|
|
|
{
|
|
|
|
UserApcItem* userApc = (UserApcItem*)arg;
|
|
|
|
|
|
|
|
userApc->completion(userApc->completionArg);
|
|
|
|
|
|
|
|
userApc->apc.markedForRemove = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD QueueUserAPC(PAPCFUNC pfnAPC, HANDLE hThread, ULONG_PTR dwData)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
ULONG Type = 0;
|
|
|
|
WINPR_HANDLE* Object = NULL;
|
|
|
|
WINPR_APC_ITEM* apc = NULL;
|
|
|
|
UserApcItem* apcItem = NULL;
|
2021-03-24 20:32:43 +03:00
|
|
|
|
|
|
|
if (!pfnAPC)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hThread, &Type, &Object) || Object->Type != HANDLE_TYPE_THREAD)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "hThread is not a thread");
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return (DWORD)0;
|
|
|
|
}
|
|
|
|
|
|
|
|
apcItem = calloc(1, sizeof(*apcItem));
|
|
|
|
if (!apcItem)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return (DWORD)0;
|
|
|
|
}
|
|
|
|
|
|
|
|
apc = &apcItem->apc;
|
|
|
|
apc->type = APC_TYPE_USER;
|
|
|
|
apc->markedForFree = TRUE;
|
|
|
|
apc->alwaysSignaled = TRUE;
|
|
|
|
apc->completion = userAPC;
|
|
|
|
apc->completionArgs = apc;
|
|
|
|
apcItem->completion = pfnAPC;
|
|
|
|
apcItem->completionArg = dwData;
|
|
|
|
apc_register(hThread, apc);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-09-18 03:17:19 +04:00
|
|
|
DWORD ResumeThread(HANDLE hThread)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
ULONG Type = 0;
|
|
|
|
WINPR_HANDLE* Object = NULL;
|
|
|
|
WINPR_THREAD* thread = NULL;
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2022-09-21 12:01:52 +03:00
|
|
|
if (!winpr_Handle_GetInfo(hThread, &Type, &Object) || Object->Type != HANDLE_TYPE_THREAD)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "hThread is not a thread");
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
2019-11-06 17:24:51 +03:00
|
|
|
return (DWORD)-1;
|
2022-09-21 12:01:52 +03:00
|
|
|
}
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
thread = (WINPR_THREAD*)Object;
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
if (!run_mutex_fkt(pthread_mutex_lock, &thread->mutex))
|
2019-11-06 17:24:51 +03:00
|
|
|
return (DWORD)-1;
|
2012-09-23 21:54:14 +04:00
|
|
|
|
|
|
|
if (!thread->started)
|
2015-07-23 12:52:24 +03:00
|
|
|
{
|
|
|
|
if (!winpr_StartThread(thread))
|
|
|
|
{
|
2022-04-27 14:33:08 +03:00
|
|
|
run_mutex_fkt(pthread_mutex_checked_unlock, &thread->mutex);
|
2019-11-06 17:24:51 +03:00
|
|
|
return (DWORD)-1;
|
2015-07-23 12:52:24 +03:00
|
|
|
}
|
|
|
|
}
|
2014-07-14 21:36:31 +04:00
|
|
|
else
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_WARN(TAG, "Thread already started!");
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
if (!run_mutex_fkt(pthread_mutex_checked_unlock, &thread->mutex))
|
2019-11-06 17:24:51 +03:00
|
|
|
return (DWORD)-1;
|
2015-07-16 14:48:14 +03:00
|
|
|
|
2012-09-18 03:17:19 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD SuspendThread(HANDLE hThread)
|
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "not implemented");
|
2016-06-01 17:26:26 +03:00
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
2019-11-06 17:24:51 +03:00
|
|
|
return (DWORD)-1;
|
2012-09-18 03:17:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SwitchToThread(VOID)
|
|
|
|
{
|
2016-06-02 16:38:57 +03:00
|
|
|
/**
|
|
|
|
* Note: on some operating systems sched_yield is a stub returning -1.
|
|
|
|
* usleep should at least trigger a context switch if any thread is waiting.
|
|
|
|
*/
|
2019-01-18 15:02:30 +03:00
|
|
|
if (sched_yield() != 0)
|
2016-06-02 16:38:57 +03:00
|
|
|
usleep(1);
|
|
|
|
|
|
|
|
return TRUE;
|
2012-09-18 03:17:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL TerminateThread(HANDLE hThread, DWORD dwExitCode)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
ULONG Type = 0;
|
|
|
|
WINPR_HANDLE* Object = NULL;
|
|
|
|
WINPR_THREAD* thread = NULL;
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2024-05-24 12:04:07 +03:00
|
|
|
if (!winpr_Handle_GetInfo(hThread, &Type, &Object) || Object->Type != HANDLE_TYPE_THREAD)
|
2015-07-16 14:48:14 +03:00
|
|
|
return FALSE;
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
thread = (WINPR_THREAD*)Object;
|
2014-07-16 14:01:58 +04:00
|
|
|
thread->exited = TRUE;
|
|
|
|
thread->dwExitCode = dwExitCode;
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
if (!run_mutex_fkt(pthread_mutex_lock, &thread->mutex))
|
2015-07-16 14:48:14 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2012-10-14 04:31:01 +04:00
|
|
|
#ifndef ANDROID
|
2012-09-23 21:54:14 +04:00
|
|
|
pthread_cancel(thread->thread);
|
2014-07-14 21:36:31 +04:00
|
|
|
#else
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_ERR(TAG, "Function not supported on this platform!");
|
2012-10-14 04:31:01 +04:00
|
|
|
#endif
|
2016-10-07 15:07:51 +03:00
|
|
|
|
2022-04-27 14:33:08 +03:00
|
|
|
if (!run_mutex_fkt(pthread_mutex_checked_unlock, &thread->mutex))
|
2015-07-16 14:48:14 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2015-01-28 14:43:20 +03:00
|
|
|
set_event(thread);
|
2012-09-18 03:17:19 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-07-14 21:36:31 +04:00
|
|
|
VOID DumpThreadHandles(void)
|
|
|
|
{
|
2022-03-08 10:37:46 +03:00
|
|
|
#if defined(WITH_DEBUG_THREADS)
|
2024-01-23 18:49:54 +03:00
|
|
|
char** msg = NULL;
|
|
|
|
size_t used = 0;
|
2014-10-27 13:39:02 +03:00
|
|
|
void* stack = winpr_backtrace(20);
|
|
|
|
WLog_DBG(TAG, "---------------- Called from ----------------------------");
|
|
|
|
msg = winpr_backtrace_symbols(stack, &used);
|
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (size_t i = 0; i < used; i++)
|
2014-11-20 19:48:02 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "[%" PRIdz "]: %s", i, msg[i]);
|
2014-11-20 19:48:02 +03:00
|
|
|
}
|
2014-10-27 13:39:02 +03:00
|
|
|
|
|
|
|
free(msg);
|
|
|
|
winpr_backtrace_free(stack);
|
|
|
|
WLog_DBG(TAG, "---------------- Start Dumping thread handles -----------");
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2022-01-13 18:21:51 +03:00
|
|
|
#if defined(WITH_THREAD_LIST)
|
2014-07-14 21:36:31 +04:00
|
|
|
if (!thread_list)
|
2014-11-20 19:48:02 +03:00
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_DBG(TAG, "All threads properly shut down and disposed of.");
|
2014-11-20 19:48:02 +03:00
|
|
|
}
|
2014-07-14 21:36:31 +04:00
|
|
|
else
|
|
|
|
{
|
2016-10-07 15:07:51 +03:00
|
|
|
ULONG_PTR* keys = NULL;
|
2014-07-14 21:36:31 +04:00
|
|
|
ListDictionary_Lock(thread_list);
|
|
|
|
int x, count = ListDictionary_GetKeys(thread_list, &keys);
|
2015-01-20 13:04:23 +03:00
|
|
|
WLog_DBG(TAG, "Dumping %d elements", count);
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (size_t x = 0; x < count; x++)
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_THREAD* thread = ListDictionary_GetItemValue(thread_list, (void*)keys[x]);
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_DBG(TAG, "Thread [%d] handle created still not closed!", x);
|
|
|
|
msg = winpr_backtrace_symbols(thread->create_stack, &used);
|
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (size_t i = 0; i < used; i++)
|
2014-11-20 19:48:02 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "[%" PRIdz "]: %s", i, msg[i]);
|
2014-11-20 19:48:02 +03:00
|
|
|
}
|
2014-10-27 13:39:02 +03:00
|
|
|
|
|
|
|
free(msg);
|
2014-07-14 21:36:31 +04:00
|
|
|
|
|
|
|
if (thread->started)
|
2014-11-20 19:48:02 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "Thread [%d] still running!", x);
|
2014-11-20 19:48:02 +03:00
|
|
|
}
|
2014-07-14 21:36:31 +04:00
|
|
|
else
|
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_DBG(TAG, "Thread [%d] exited at:", x);
|
|
|
|
msg = winpr_backtrace_symbols(thread->exit_stack, &used);
|
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (size_t i = 0; i < used; i++)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "[%" PRIdz "]: %s", i, msg[i]);
|
2014-10-27 13:39:02 +03:00
|
|
|
|
|
|
|
free(msg);
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(keys);
|
2014-07-14 21:36:31 +04:00
|
|
|
ListDictionary_Unlock(thread_list);
|
|
|
|
}
|
2022-01-13 18:21:51 +03:00
|
|
|
#endif
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_DBG(TAG, "---------------- End Dumping thread handles -------------");
|
2014-07-14 21:36:31 +04:00
|
|
|
#endif
|
2022-03-08 10:37:46 +03:00
|
|
|
}
|
2012-09-18 03:17:19 +04:00
|
|
|
#endif
|