From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 20839A04A3; Sat, 23 May 2020 09:27:12 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 6601C1D946; Sat, 23 May 2020 09:27:11 +0200 (CEST) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by dpdk.org (Postfix) with ESMTP id 395BE1D938 for ; Sat, 23 May 2020 09:27:10 +0200 (CEST) Received: from Internal Mail-Server by MTLPINE1 (envelope-from tbashar@mellanox.com) with ESMTPS (AES256-SHA encrypted); 23 May 2020 10:27:09 +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 04N7R5ek030675; Sat, 23 May 2020 10:27:06 +0300 From: Tasnim Bashar 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: Sat, 23 May 2020 00:25:56 -0700 Message-Id: <20200523072556.56564-1-tbashar@mellanox.com> X-Mailer: git-send-email 2.19.1.windows.1 In-Reply-To: <20200522001112> References: <20200522001112> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v2] 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" 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 --- lib/librte_eal/windows/include/pthread.h | 56 ++++++++++++++++---- lib/librte_eal/windows/include/rte_windows.h | 1 + 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/lib/librte_eal/windows/include/pthread.h b/lib/librte_eal/windows/include/pthread.h index 0bbed5c3b8..88dab36b79 100644 --- a/lib/librte_eal/windows/include/pthread.h +++ b/lib/librte_eal/windows/include/pthread.h @@ -16,8 +16,8 @@ extern "C" { #endif -#include #include +#include #define PTHREAD_BARRIER_SERIAL_THREAD TRUE @@ -41,31 +41,67 @@ typedef SYNCHRONIZATION_BARRIER pthread_barrier_t; #define pthread_self() \ ((pthread_t)GetCurrentThreadId()) #define pthread_setaffinity_np(thread, size, cpuset) \ - eal_set_thread_affinity_mask(thread, (unsigned long *) cpuset) + eal_set_thread_affinity_mask(thread, (long long *) cpuset) #define pthread_getaffinity_np(thread, size, cpuset) \ - eal_get_thread_affinity_mask(thread, (unsigned long *) cpuset) + eal_get_thread_affinity_mask(thread, (long long *) cpuset) #define pthread_create(threadid, threadattr, threadfunc, args) \ eal_create_thread(threadid, threadfunc, args) static inline int -eal_set_thread_affinity_mask(pthread_t threadid, unsigned long *cpuset) +eal_set_thread_affinity_mask(pthread_t threadid, long long *cpuset) { - SetThreadAffinityMask((HANDLE) threadid, *cpuset); + DWORD_PTR ret; + HANDLE thread_handle; + + thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadid); + if (thread_handle == NULL) { + RTE_LOG_WIN32_ERR("OpenThread()"); + return -1; + } + + ret = SetThreadAffinityMask(thread_handle, *cpuset); + if (ret == 0) { + RTE_LOG_WIN32_ERR("SetThreadAffinityMask()"); + CloseHandle(thread_handle); + return -1; + } + CloseHandle(thread_handle); return 0; } static inline int -eal_get_thread_affinity_mask(pthread_t threadid, unsigned long *cpuset) +eal_get_thread_affinity_mask(pthread_t threadid, long 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); + DWORD_PTR dwprevaffinitymask; + HANDLE thread_handle; + DWORD_PTR ret; + + 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 */ + 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); + 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; } diff --git a/lib/librte_eal/windows/include/rte_windows.h b/lib/librte_eal/windows/include/rte_windows.h index ed6e4c1485..677b63c42d 100644 --- a/lib/librte_eal/windows/include/rte_windows.h +++ b/lib/librte_eal/windows/include/rte_windows.h @@ -29,6 +29,7 @@ #define INITGUID #endif #include +#include /** * Log GetLastError() with context, usually a Win32 API function and arguments. -- 2.19.1.windows.1