doc: add Meson coding style to contributors guide
[dpdk.git] / lib / librte_eal / windows / rte_thread.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2021 Mellanox Technologies, Ltd
3  */
4
5 #include <rte_common.h>
6 #include <rte_errno.h>
7 #include <rte_thread.h>
8 #include <rte_windows.h>
9
10 struct eal_tls_key {
11         DWORD thread_index;
12 };
13
14 int
15 rte_thread_key_create(rte_thread_key *key,
16                 __rte_unused void (*destructor)(void *))
17 {
18         *key = malloc(sizeof(**key));
19         if ((*key) == NULL) {
20                 RTE_LOG(DEBUG, EAL, "Cannot allocate TLS key.\n");
21                 rte_errno = ENOMEM;
22                 return -1;
23         }
24         (*key)->thread_index = TlsAlloc();
25         if ((*key)->thread_index == TLS_OUT_OF_INDEXES) {
26                 RTE_LOG_WIN32_ERR("TlsAlloc()");
27                 free(*key);
28                 rte_errno = ENOEXEC;
29                 return -1;
30         }
31         return 0;
32 }
33
34 int
35 rte_thread_key_delete(rte_thread_key key)
36 {
37         if (!key) {
38                 RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n");
39                 rte_errno = EINVAL;
40                 return -1;
41         }
42         if (!TlsFree(key->thread_index)) {
43                 RTE_LOG_WIN32_ERR("TlsFree()");
44                 free(key);
45                 rte_errno = ENOEXEC;
46                 return -1;
47         }
48         free(key);
49         return 0;
50 }
51
52 int
53 rte_thread_value_set(rte_thread_key key, const void *value)
54 {
55         char *p;
56
57         if (!key) {
58                 RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n");
59                 rte_errno = EINVAL;
60                 return -1;
61         }
62         /* discard const qualifier */
63         p = (char *) (uintptr_t) value;
64         if (!TlsSetValue(key->thread_index, p)) {
65                 RTE_LOG_WIN32_ERR("TlsSetValue()");
66                 rte_errno = ENOEXEC;
67                 return -1;
68         }
69         return 0;
70 }
71
72 void *
73 rte_thread_value_get(rte_thread_key key)
74 {
75         void *output;
76
77         if (!key) {
78                 RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n");
79                 rte_errno = EINVAL;
80                 return NULL;
81         }
82         output = TlsGetValue(key->thread_index);
83         if (GetLastError() != ERROR_SUCCESS) {
84                 RTE_LOG_WIN32_ERR("TlsGetValue()");
85                 rte_errno = ENOEXEC;
86                 return NULL;
87         }
88         return output;
89 }