common/octeontx2: fix memory mapping API usage
[dpdk.git] / lib / librte_eal / windows / include / pthread.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #ifndef _PTHREAD_H_
6 #define _PTHREAD_H_
7
8 #include <stdint.h>
9 #include <sched.h>
10
11 /**
12  * This file is required to support the common code in eal_common_proc.c,
13  * eal_common_thread.c and common\include\rte_per_lcore.h as Microsoft libc
14  * does not contain pthread.h. This may be removed in future releases.
15  */
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include <rte_common.h>
21 #include <rte_windows.h>
22
23 #define PTHREAD_BARRIER_SERIAL_THREAD TRUE
24
25 /* defining pthread_t type on Windows since there is no in Microsoft libc*/
26 typedef uintptr_t pthread_t;
27
28 /* defining pthread_attr_t type on Windows since there is no in Microsoft libc*/
29 typedef void *pthread_attr_t;
30
31 typedef SYNCHRONIZATION_BARRIER pthread_barrier_t;
32
33 #define pthread_barrier_init(barrier, attr, count) \
34         InitializeSynchronizationBarrier(barrier, count, -1)
35 #define pthread_barrier_wait(barrier) EnterSynchronizationBarrier(barrier, \
36         SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY)
37 #define pthread_barrier_destroy(barrier) \
38         DeleteSynchronizationBarrier(barrier)
39 #define pthread_cancel(thread) TerminateThread((HANDLE) thread, 0)
40
41 /* pthread function overrides */
42 #define pthread_self() \
43         ((pthread_t)GetCurrentThreadId())
44
45 static inline int
46 pthread_setaffinity_np(pthread_t threadid, size_t cpuset_size,
47                         rte_cpuset_t *cpuset)
48 {
49         DWORD_PTR ret = 0;
50         HANDLE thread_handle;
51
52         if (cpuset == NULL || cpuset_size == 0)
53                 return -1;
54
55         thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadid);
56         if (thread_handle == NULL) {
57                 RTE_LOG_WIN32_ERR("OpenThread()");
58                 return -1;
59         }
60
61         ret = SetThreadAffinityMask(thread_handle, *cpuset->_bits);
62         if (ret == 0) {
63                 RTE_LOG_WIN32_ERR("SetThreadAffinityMask()");
64                 goto close_handle;
65         }
66
67 close_handle:
68         if (CloseHandle(thread_handle) == 0) {
69                 RTE_LOG_WIN32_ERR("CloseHandle()");
70                 return -1;
71         }
72         return (ret == 0) ? -1 : 0;
73 }
74
75 static inline int
76 pthread_getaffinity_np(pthread_t threadid, size_t cpuset_size,
77                         rte_cpuset_t *cpuset)
78 {
79         /* Workaround for the lack of a GetThreadAffinityMask()
80          *API in Windows
81          */
82         DWORD_PTR prev_affinity_mask;
83         HANDLE thread_handle;
84         DWORD_PTR ret = 0;
85
86         if (cpuset == NULL || cpuset_size == 0)
87                 return -1;
88
89         thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadid);
90         if (thread_handle == NULL) {
91                 RTE_LOG_WIN32_ERR("OpenThread()");
92                 return -1;
93         }
94
95         /* obtain previous mask by setting dummy mask */
96         prev_affinity_mask = SetThreadAffinityMask(thread_handle, 0x1);
97         if (prev_affinity_mask == 0) {
98                 RTE_LOG_WIN32_ERR("SetThreadAffinityMask()");
99                 goto close_handle;
100         }
101
102         /* set it back! */
103         ret = SetThreadAffinityMask(thread_handle, prev_affinity_mask);
104         if (ret == 0) {
105                 RTE_LOG_WIN32_ERR("SetThreadAffinityMask()");
106                 goto close_handle;
107         }
108
109         memset(cpuset, 0, cpuset_size);
110         *cpuset->_bits = prev_affinity_mask;
111
112 close_handle:
113         if (CloseHandle(thread_handle) == 0) {
114                 RTE_LOG_WIN32_ERR("SetThreadAffinityMask()");
115                 return -1;
116         }
117         return (ret == 0) ? -1 : 0;
118 }
119
120 static inline int
121 pthread_create(void *threadid, const void *threadattr, void *threadfunc,
122                 void *args)
123 {
124         RTE_SET_USED(threadattr);
125         HANDLE hThread;
126         hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadfunc,
127                 args, 0, (LPDWORD)threadid);
128         if (hThread) {
129                 SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
130                 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
131         }
132         return ((hThread != NULL) ? 0 : E_FAIL);
133 }
134
135 static inline int
136 pthread_join(__rte_unused pthread_t thread,
137         __rte_unused void **value_ptr)
138 {
139         return 0;
140 }
141
142 #ifdef __cplusplus
143 }
144 #endif
145
146 #endif /* _PTHREAD_H_ */