From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 164E5A0524; Tue, 1 Jun 2021 22:56:18 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4F4D5410E2; Tue, 1 Jun 2021 22:56:07 +0200 (CEST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id 487B840E64 for ; Tue, 1 Jun 2021 22:56:04 +0200 (CEST) Received: by linux.microsoft.com (Postfix, from userid 1059) id AA4E320B8008; Tue, 1 Jun 2021 13:56:03 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com AA4E320B8008 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1622580963; bh=hsiMU0MVocxBXIF+7iw9aUB8IydDuodKSS+/3eRyt7Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rLRNCA/RbHzkzzF1mQsUpzGTl3MD2ivnBABLofG/mKErdb8tZ72+5h6lXeUhRO+Hq v6Ek8Qliz9ToqvRVfBSzE9LurjxhdXQgr++uIKnNHU2hs/jnH5dFaKOljPT0rfSFYQ RnbNv8kBfxnYPsywku/2KdjShXIqD19GbJcb4p/g= From: Narcisa Ana Maria Vasile To: dev@dpdk.org, thomas@monjalon.net, dmitry.kozliuk@gmail.com, khot@microsoft.com, navasile@microsoft.com, dmitrym@microsoft.com, roretzla@microsoft.com, talshn@nvidia.com, ocardona@microsoft.com Cc: bruce.richardson@intel.com, david.marchand@redhat.com, pallavi.kadam@intel.com Date: Tue, 1 Jun 2021 13:55:45 -0700 Message-Id: <1622580952-25169-4-git-send-email-navasile@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1622580952-25169-1-git-send-email-navasile@linux.microsoft.com> References: <1617413948-10504-1-git-send-email-navasile@linux.microsoft.com> <1622580952-25169-1-git-send-email-navasile@linux.microsoft.com> Subject: [dpdk-dev] [PATCH v7 03/10] eal/windows: translate Windows errors to errno-style errors X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 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" From: Narcisa Vasile Add function to translate Windows error codes to errno-style error codes. The possible return values are chosen so that we have as much semantical compatibility between platforms as possible. --- lib/eal/include/rte_thread.h | 5 +- lib/eal/windows/rte_thread.c | 90 +++++++++++++++++++++++++++--------- 2 files changed, 71 insertions(+), 24 deletions(-) diff --git a/lib/eal/include/rte_thread.h b/lib/eal/include/rte_thread.h index eff00023d7..f3eeb28753 100644 --- a/lib/eal/include/rte_thread.h +++ b/lib/eal/include/rte_thread.h @@ -236,9 +236,8 @@ int rte_thread_value_set(rte_thread_key key, const void *value); * * @return * On success, value data pointer (can also be NULL). - * On failure, NULL and an error number is set in rte_errno. - * rte_errno can be: EINVAL - Invalid parameter passed. - * ENOEXEC - Specific OS error. + * On failure, NULL and a positive error number is set in rte_errno. + * */ __rte_experimental void *rte_thread_value_get(rte_thread_key key); diff --git a/lib/eal/windows/rte_thread.c b/lib/eal/windows/rte_thread.c index cc319d3628..6ea1dc2a05 100644 --- a/lib/eal/windows/rte_thread.c +++ b/lib/eal/windows/rte_thread.c @@ -13,6 +13,54 @@ struct eal_tls_key { DWORD thread_index; }; +/* Translates the most common error codes related to threads */ +static int +thread_translate_win32_error(DWORD error) +{ + switch (error) { + case ERROR_SUCCESS: + return 0; + + case ERROR_INVALID_PARAMETER: + return EINVAL; + + case ERROR_INVALID_HANDLE: + return EFAULT; + + case ERROR_NOT_ENOUGH_MEMORY: + /* FALLTHROUGH */ + case ERROR_NO_SYSTEM_RESOURCES: + return ENOMEM; + + case ERROR_PRIVILEGE_NOT_HELD: + /* FALLTHROUGH */ + case ERROR_ACCESS_DENIED: + return EACCES; + + case ERROR_ALREADY_EXISTS: + return EEXIST; + + case ERROR_POSSIBLE_DEADLOCK: + return EDEADLK; + + case ERROR_INVALID_FUNCTION: + /* FALLTHROUGH */ + case ERROR_CALL_NOT_IMPLEMENTED: + return ENOSYS; + } + + return EINVAL; +} + +static int +thread_log_last_error(const char* message) +{ + DWORD error = GetLastError(); + RTE_LOG(DEBUG, EAL, "GetLastError()=%lu: %s\n", error, message); + + return thread_translate_win32_error(error); +} + rte_thread_t rte_thread_self(void) { @@ -85,18 +133,18 @@ int rte_thread_key_create(rte_thread_key *key, __rte_unused void (*destructor)(void *)) { + int ret; + *key = malloc(sizeof(**key)); if ((*key) == NULL) { RTE_LOG(DEBUG, EAL, "Cannot allocate TLS key.\n"); - rte_errno = ENOMEM; - return -1; + return ENOMEM; } (*key)->thread_index = TlsAlloc(); if ((*key)->thread_index == TLS_OUT_OF_INDEXES) { - RTE_LOG_WIN32_ERR("TlsAlloc()"); + ret = thread_log_last_error("TlsAlloc()"); free(*key); - rte_errno = ENOEXEC; - return -1; + return ret; } return 0; } @@ -104,16 +152,16 @@ rte_thread_key_create(rte_thread_key *key, int rte_thread_key_delete(rte_thread_key key) { - if (!key) { + int ret; + + if (key == NULL) { RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); - rte_errno = EINVAL; - return -1; + return EINVAL; } if (!TlsFree(key->thread_index)) { - RTE_LOG_WIN32_ERR("TlsFree()"); + ret = thread_log_last_error("TlsFree()"); free(key); - rte_errno = ENOEXEC; - return -1; + return ret; } free(key); return 0; @@ -122,19 +170,17 @@ rte_thread_key_delete(rte_thread_key key) int rte_thread_value_set(rte_thread_key key, const void *value) { + int ret; char *p; - if (!key) { + if (key == NULL) { RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); - rte_errno = EINVAL; - return -1; + return EINVAL; } /* discard const qualifier */ p = (char *) (uintptr_t) value; if (!TlsSetValue(key->thread_index, p)) { - RTE_LOG_WIN32_ERR("TlsSetValue()"); - rte_errno = ENOEXEC; - return -1; + return thread_log_last_error("TlsSetValue()"); } return 0; } @@ -143,16 +189,18 @@ void * rte_thread_value_get(rte_thread_key key) { void *output; + DWORD ret = 0; - if (!key) { + if (key == NULL) { RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); rte_errno = EINVAL; return NULL; } output = TlsGetValue(key->thread_index); - if (GetLastError() != ERROR_SUCCESS) { - RTE_LOG_WIN32_ERR("TlsGetValue()"); - rte_errno = ENOEXEC; + ret = GetLastError(); + if (ret != 0) { + RTE_LOG(DEBUG, EAL, "GetLastError()=%lu: TlsGetValue()\n", ret); + rte_errno = thread_translate_win32_error(ret); return NULL; } return output; -- 2.31.0.vfs.0.1