1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
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.
20 #include <rte_common.h>
21 #include <rte_windows.h>
23 #define PTHREAD_BARRIER_SERIAL_THREAD TRUE
25 /* defining pthread_t type on Windows since there is no in Microsoft libc*/
26 typedef uintptr_t pthread_t;
28 /* defining pthread_attr_t type on Windows since there is no in Microsoft libc*/
29 typedef void *pthread_attr_t;
31 typedef void *pthread_mutexattr_t;
33 typedef CRITICAL_SECTION pthread_mutex_t;
35 typedef SYNCHRONIZATION_BARRIER pthread_barrier_t;
37 #define pthread_barrier_init(barrier, attr, count) \
38 !InitializeSynchronizationBarrier(barrier, count, -1)
39 #define pthread_barrier_wait(barrier) EnterSynchronizationBarrier(barrier, \
40 SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY)
41 #define pthread_barrier_destroy(barrier) \
42 !DeleteSynchronizationBarrier(barrier)
43 #define pthread_cancel(thread) !TerminateThread((HANDLE) thread, 0)
45 /* pthread function overrides */
46 #define pthread_self() \
47 ((pthread_t)GetCurrentThreadId())
51 pthread_equal(pthread_t t1, pthread_t t2)
57 pthread_setaffinity_np(pthread_t threadid, size_t cpuset_size,
63 if (cpuset == NULL || cpuset_size == 0)
66 thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadid);
67 if (thread_handle == NULL) {
68 RTE_LOG_WIN32_ERR("OpenThread()");
72 ret = SetThreadAffinityMask(thread_handle, *cpuset->_bits);
74 RTE_LOG_WIN32_ERR("SetThreadAffinityMask()");
79 if (CloseHandle(thread_handle) == 0) {
80 RTE_LOG_WIN32_ERR("CloseHandle()");
83 return (ret == 0) ? -1 : 0;
87 pthread_getaffinity_np(pthread_t threadid, size_t cpuset_size,
90 /* Workaround for the lack of a GetThreadAffinityMask()
93 DWORD_PTR prev_affinity_mask;
97 if (cpuset == NULL || cpuset_size == 0)
100 thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadid);
101 if (thread_handle == NULL) {
102 RTE_LOG_WIN32_ERR("OpenThread()");
106 /* obtain previous mask by setting dummy mask */
107 prev_affinity_mask = SetThreadAffinityMask(thread_handle, 0x1);
108 if (prev_affinity_mask == 0) {
109 RTE_LOG_WIN32_ERR("SetThreadAffinityMask()");
114 ret = SetThreadAffinityMask(thread_handle, prev_affinity_mask);
116 RTE_LOG_WIN32_ERR("SetThreadAffinityMask()");
120 memset(cpuset, 0, cpuset_size);
121 *cpuset->_bits = prev_affinity_mask;
124 if (CloseHandle(thread_handle) == 0) {
125 RTE_LOG_WIN32_ERR("SetThreadAffinityMask()");
128 return (ret == 0) ? -1 : 0;
132 pthread_create(void *threadid, const void *threadattr, void *threadfunc,
135 RTE_SET_USED(threadattr);
137 hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadfunc,
138 args, 0, (LPDWORD)threadid);
140 SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
141 SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL);
143 return ((hThread != NULL) ? 0 : E_FAIL);
147 pthread_join(__rte_unused pthread_t thread,
148 __rte_unused void **value_ptr)
154 pthread_mutex_init(pthread_mutex_t *mutex,
155 __rte_unused pthread_mutexattr_t *attr)
157 InitializeCriticalSection(mutex);
162 pthread_mutex_lock(pthread_mutex_t *mutex)
164 EnterCriticalSection(mutex);
169 pthread_mutex_unlock(pthread_mutex_t *mutex)
171 LeaveCriticalSection(mutex);
176 pthread_mutex_destroy(pthread_mutex_t *mutex)
178 DeleteCriticalSection(mutex);
186 #endif /* _PTHREAD_H_ */