Pārlūkot izejas kodu

Z991239-342 #comment 处理获取当前线程的问题

gifur 5 gadi atpakaļ
vecāks
revīzija
606397f097

+ 1 - 0
winpr/include/winpr/thread.h

@@ -99,6 +99,7 @@ extern "C"
 #define STARTF_TITLEISAPPID 0x00001000
 #define STARTF_PREVENTPINNING 0x00002000
 
+#define THREAD_CUR_ONE_HANDLE ((HANDLE)(LONG_PTR)-2)
 
 #define THREAD_BASE_PRIORITY_LOWRT  15  // value that gets a thread to LowRealtime-1
 #define THREAD_BASE_PRIORITY_MAX    2   // maximum thread base priority boost

+ 0 - 1
winpr/libwinpr/handle/handle.h

@@ -131,7 +131,6 @@ static INLINE BOOL winpr_Handle_GetInfo(HANDLE handle, ULONG* pType, WINPR_HANDL
 
 	if (handle == NULL)
 		return FALSE;
-
 		/* INVALID_HANDLE_VALUE is an invalid value for every handle, but it
 		 * confuses the clang scanbuild analyzer. */
 #ifndef __clang_analyzer__

+ 22 - 15
winpr/libwinpr/thread/thread.c

@@ -482,7 +482,7 @@ HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize
 
 	WINPR_HANDLE_SET_TYPE_AND_MODE(thread, HANDLE_TYPE_THREAD, WINPR_FD_READ);
 	handle = (HANDLE)thread;
-
+	assert(handle != THREAD_CUR_ONE_HANDLE);
 	if (!thread_list)
 	{
 		thread_list = ListDictionary_New(TRUE);
@@ -624,10 +624,9 @@ VOID ExitThread(DWORD dwExitCode)
 {
 	DWORD rc;
 	pthread_t tid = pthread_self();
-
 	if (!thread_list)
 	{
-		WLog_ERR(TAG, "function called without existing thread list!");
+		WLog_ERR(TAG, "%s called without existing thread list!", __FUNCTION__);
 #if defined(WITH_DEBUG_THREADS)
 		DumpThreadHandles();
 #endif
@@ -679,12 +678,11 @@ BOOL GetExitCodeThread(HANDLE hThread, LPDWORD lpExitCode)
 
 HANDLE _GetCurrentThread(VOID)
 {
-	HANDLE hdl = NULL;
+	HANDLE hdl = THREAD_CUR_ONE_HANDLE;
 	pthread_t tid = pthread_self();
-
 	if (!thread_list)
 	{
-		WLog_ERR(TAG, "function called without existing thread list!");
+		WLog_ERR(TAG, "%s called without existing thread list!", __FUNCTION__);
 #if defined(WITH_DEBUG_THREADS)
 		DumpThreadHandles();
 #endif
@@ -700,7 +698,6 @@ HANDLE _GetCurrentThread(VOID)
 	{
 		hdl = ListDictionary_GetItemValue(thread_list, &tid);
 	}
-
 	return hdl;
 }
 
@@ -804,7 +801,7 @@ HANDLE OpenThread(DWORD dwDesiredAccess, BOOL  bInheritHandle, DWORD dwThreadId)
 	}
 	if (!thread_list)
 	{
-		WLog_ERR(TAG, "function called without existing thread list!");
+		WLog_ERR(TAG, "%s called without existing thread list!", __FUNCTION__);
 #if defined(WITH_DEBUG_THREADS)
 		DumpThreadHandles();
 #endif
@@ -830,19 +827,29 @@ BOOL SetThreadPriority(HANDLE hThread, int nPriority)
 	BOOL result = TRUE;
 	ULONG Type;
 	WINPR_HANDLE* Object;
-	WINPR_THREAD* thread;
+	WINPR_THREAD* thread = NULL;
 	struct sched_param param;
 	pthread_attr_t attr;
+	pthread_t cur_thread;
 	int policy = 0;
 	int max_prio_for_policy = 0;
 	int min_prio_for_policy = 0;
 	int ret = 0;
 	if (!winpr_Handle_GetInfo(hThread, &Type, &Object))
 	{
-		WLog_ERR(TAG, "get thread handle information failed!");
-		return FALSE;
+		if (hThread == THREAD_CUR_ONE_HANDLE) {
+			WLog_DBG(TAG, "current thread flag.");
+			cur_thread = pthread_self();
+		}
+		else {
+			WLog_ERR(TAG, "get thread handle information failed!");
+			return FALSE;
+		}
+	}
+	else {
+		thread = (WINPR_THREAD*)Object;
+		cur_thread = thread->thread;
 	}
-	thread = (WINPR_THREAD*)Object;
 
 #if 0
 	/*https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_setschedprio.html*/
@@ -853,14 +860,14 @@ BOOL SetThreadPriority(HANDLE hThread, int nPriority)
 		/*
 		 * set the scheduling priority for the thread to prio
 		 */
-		if(0 != pthread_setschedprio(thread->thread, max_prio_for_policy)) {
+		if(0 != pthread_setschedprio(cur_thread, max_prio_for_policy)) {
 			result = FALSE;
 		}
 	}
 	pthread_attr_destroy(&attr);
 #else
 	/*it might be preferable conceptually to solve this problem.*/
-	ret = pthread_getschedparam(thread->thread, &policy, &param);
+	ret = pthread_getschedparam(cur_thread, &policy, &param);
 	if(0 != ret)
 	{
 		WLog_ERR(TAG, "get thread schedule param failed, err: %d", ret);
@@ -894,7 +901,7 @@ BOOL SetThreadPriority(HANDLE hThread, int nPriority)
 	 * the caller does not have appropriate permission to set either the scheduling
 	 * parameters or the scheduling policy of the specified thread.
 	 */
-	ret = pthread_setschedparam(thread->thread, policy, &param);
+	ret = pthread_setschedparam(cur_thread, policy, &param);
 	if (0 != ret) {
 		WLog_ERR(TAG, "set scheduled param failed! policy: %d, priority: %d, err: %d",
 			policy, param.sched_priority, ret);