From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from dpdk.org (dpdk.org [92.243.14.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 9FFADA0093;
	Fri, 22 May 2020 02:13:00 +0200 (CEST)
Received: from [92.243.14.124] (localhost [127.0.0.1])
	by dpdk.org (Postfix) with ESMTP id B8D8C1D735;
	Fri, 22 May 2020 02:12:59 +0200 (CEST)
Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129])
 by dpdk.org (Postfix) with ESMTP id 894D31D728
 for <dev@dpdk.org>; Fri, 22 May 2020 02:12:58 +0200 (CEST)
Received: from Internal Mail-Server by MTLPINE2 (envelope-from
 tbashar@mellanox.com)
 with ESMTPS (AES256-SHA encrypted); 22 May 2020 03:12:57 +0300
Received: from mtsdev.labs.mlnx (mtsdev.mts.labs.mlnx [10.9.76.240])
 by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id 04M0Csx0025611;
 Fri, 22 May 2020 03:12:54 +0300
From: Tasnim Bashar <tbashar@mellanox.com>
To: dev@dpdk.org
Cc: harini.ramakrishnan@microsoft.com, pallavi.kadam@intel.com,
 ranjit.menon@intel.com, ocardona@microsoft.com,
 navasile@linux.microsoft.com, dmitry.kozliuk@gmail.com,
 talshn@mellanox.com, fady@mellanox.com, ophirmu@mellanox.com,
 thomas@monjalon.net
Date: Thu, 21 May 2020 17:11:12 -0700
Message-Id: <20200522001112.48932-1-tbashar@mellanox.com>
X-Mailer: git-send-email 2.19.1.windows.1
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Subject: [dpdk-dev] [PATCH] eal/windows: fix invalid thread handle
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>

Casting thread ID to handle is not accurate way to get thread handle.
Need to use OpenThread function to get thread handle from thread ID.

pthread_setaffinity_np and pthread_getaffinity_np functions
for Windows are affected because of it.

Signed-off-by: Tasnim Bashar <tbashar@mellanox.com>
---
 lib/librte_eal/windows/include/pthread.h | 40 +++++++++++++++++++++---
 1 file changed, 35 insertions(+), 5 deletions(-)

diff --git a/lib/librte_eal/windows/include/pthread.h b/lib/librte_eal/windows/include/pthread.h
index 0bbed5c3b8..d2a986f8fd 100644
--- a/lib/librte_eal/windows/include/pthread.h
+++ b/lib/librte_eal/windows/include/pthread.h
@@ -18,6 +18,7 @@ extern "C" {
 
 #include <windows.h>
 #include <rte_common.h>
+#include <rte_windows.h>
 
 #define PTHREAD_BARRIER_SERIAL_THREAD TRUE
 
@@ -50,7 +51,19 @@ typedef SYNCHRONIZATION_BARRIER pthread_barrier_t;
 static inline int
 eal_set_thread_affinity_mask(pthread_t threadid, unsigned long *cpuset)
 {
-	SetThreadAffinityMask((HANDLE) threadid, *cpuset);
+	HANDLE thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadid);
+	if (thread_handle == NULL) {
+		RTE_LOG_WIN32_ERR("OpenThread()");
+		return -1;
+	}
+
+	DWORD ret = SetThreadAffinityMask(thread_handle, *cpuset);
+	if (ret == 0) {
+		RTE_LOG_WIN32_ERR("SetThreadAffinityMask()");
+		CloseHandle(thread_handle);
+		return -1;
+	}
+	CloseHandle(thread_handle);
 	return 0;
 }
 
@@ -60,12 +73,29 @@ eal_get_thread_affinity_mask(pthread_t threadid, unsigned long *cpuset)
 	/* Workaround for the lack of a GetThreadAffinityMask()
 	 *API in Windows
 	 */
-		/* obtain previous mask by setting dummy mask */
-	DWORD dwprevaffinitymask =
-		SetThreadAffinityMask((HANDLE) threadid, 0x1);
+	HANDLE thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadid);
+	if (thread_handle == NULL) {
+		RTE_LOG_WIN32_ERR("OpenThread()");
+		return -1;
+	}
+
+	/* obtain previous mask by setting dummy mask */
+	DWORD dwprevaffinitymask = SetThreadAffinityMask(thread_handle, 0x1);
+	if (dwprevaffinitymask == 0) {
+		RTE_LOG_WIN32_ERR("SetThreadAffinityMask()");
+		CloseHandle(thread_handle);
+		return -1;
+	}
+
 	/* set it back! */
-	SetThreadAffinityMask((HANDLE) threadid, dwprevaffinitymask);
+	DWORD ret = SetThreadAffinityMask(thread_handle, dwprevaffinitymask);
+	if (ret == 0) {
+		RTE_LOG_WIN32_ERR("SetThreadAffinityMask()");
+		CloseHandle(thread_handle);
+		return -1;
+	}
 	*cpuset = dwprevaffinitymask;
+	CloseHandle(thread_handle);
 	return 0;
 }
 
-- 
2.19.1.windows.1