eal: provide current thread identifier
[dpdk.git] / lib / eal / include / rte_thread.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2021 Mellanox Technologies, Ltd
3  * Copyright (C) 2022 Microsoft Corporation
4  */
5
6 #include <stdint.h>
7
8 #include <rte_os.h>
9 #include <rte_compat.h>
10
11 #ifndef _RTE_THREAD_H_
12 #define _RTE_THREAD_H_
13
14 /**
15  * @file
16  *
17  * Threading functions
18  *
19  * Simple threads functionality supplied by EAL.
20  */
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 /**
27  * Thread id descriptor.
28  */
29 typedef struct {
30         uintptr_t opaque_id; /**< thread identifier */
31 } rte_thread_t;
32
33 /**
34  * TLS key type, an opaque pointer.
35  */
36 typedef struct eal_tls_key *rte_thread_key;
37
38 /**
39  * @warning
40  * @b EXPERIMENTAL: this API may change without prior notice.
41  *
42  * Get the id of the calling thread.
43  *
44  * @return
45  *   Return the thread id of the calling thread.
46  */
47 __rte_experimental
48 rte_thread_t rte_thread_self(void);
49
50 #ifdef RTE_HAS_CPUSET
51
52 /**
53  * Set core affinity of the current thread.
54  * Support both EAL and non-EAL thread and update TLS.
55  *
56  * @param cpusetp
57  *   Pointer to CPU affinity to set.
58  * @return
59  *   On success, return 0; otherwise return -1;
60  */
61 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
62
63 /**
64  * Get core affinity of the current thread.
65  *
66  * @param cpusetp
67  *   Pointer to CPU affinity of current thread.
68  *   It presumes input is not NULL, otherwise it causes panic.
69  *
70  */
71 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
72
73 #endif /* RTE_HAS_CPUSET */
74
75 /**
76  * Create a TLS data key visible to all threads in the process.
77  * the created key is later used to get/set a value.
78  * and optional destructor can be set to be called when a thread exits.
79  *
80  * @param key
81  *   Pointer to store the allocated key.
82  * @param destructor
83  *   The function to be called when the thread exits.
84  *   Ignored on Windows OS.
85  *
86  * @return
87  *   On success, zero.
88  *   On failure, a negative number and an error number is set in rte_errno.
89  *   rte_errno can be: ENOMEM  - Memory allocation error.
90  *                     ENOEXEC - Specific OS error.
91  */
92
93 __rte_experimental
94 int rte_thread_key_create(rte_thread_key *key,
95                         void (*destructor)(void *));
96
97 /**
98  * Delete a TLS data key visible to all threads in the process.
99  *
100  * @param key
101  *   The key allocated by rte_thread_key_create().
102  *
103  * @return
104  *   On success, zero.
105  *   On failure, a negative number and an error number is set in rte_errno.
106  *   rte_errno can be: EINVAL  - Invalid parameter passed.
107  *                     ENOEXEC - Specific OS error.
108  */
109 __rte_experimental
110 int rte_thread_key_delete(rte_thread_key key);
111
112 /**
113  * Set value bound to the TLS key on behalf of the calling thread.
114  *
115  * @param key
116  *   The key allocated by rte_thread_key_create().
117  * @param value
118  *   The value bound to the rte_thread_key key for the calling thread.
119  *
120  * @return
121  *   On success, zero.
122  *   On failure, a negative number and an error number is set in rte_errno.
123  *   rte_errno can be: EINVAL  - Invalid parameter passed.
124  *                     ENOEXEC - Specific OS error.
125  */
126 __rte_experimental
127 int rte_thread_value_set(rte_thread_key key, const void *value);
128
129 /**
130  * Get value bound to the TLS key on behalf of the calling thread.
131  *
132  * @param key
133  *   The key allocated by rte_thread_key_create().
134  *
135  * @return
136  *   On success, value data pointer (can also be NULL).
137  *   On failure, NULL and an error number is set in rte_errno.
138  *   rte_errno can be: EINVAL  - Invalid parameter passed.
139  *                     ENOEXEC - Specific OS error.
140  */
141 __rte_experimental
142 void *rte_thread_value_get(rte_thread_key key);
143
144 #ifdef __cplusplus
145 }
146 #endif
147
148 #endif /* _RTE_THREAD_H_ */