b9dd18e56815afec486a983197f48aac372f9dba
[dpdk.git] / lib / librte_eal / windows / eal / 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 /**
9  * This file is required to support the common code in eal_common_proc.c,
10  * eal_common_thread.c and common\include\rte_per_lcore.h as Microsoft libc
11  * does not contain pthread.h. This may be removed in future releases.
12  */
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 #include <windows.h>
18
19 #define PTHREAD_BARRIER_SERIAL_THREAD TRUE
20
21 /* defining pthread_t type on Windows since there is no in Microsoft libc*/
22 typedef uintptr_t pthread_t;
23
24 /* defining pthread_attr_t type on Windows since there is no in Microsoft libc*/
25 typedef void *pthread_attr_t;
26
27 typedef SYNCHRONIZATION_BARRIER pthread_barrier_t;
28
29 #define pthread_barrier_init(barrier, attr, count) \
30         InitializeSynchronizationBarrier(barrier, count, -1)
31 #define pthread_barrier_wait(barrier) EnterSynchronizationBarrier(barrier, \
32         SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY)
33 #define pthread_barrier_destroy(barrier) \
34         DeleteSynchronizationBarrier(barrier)
35 #define pthread_cancel(thread) TerminateThread((HANDLE) thread, 0)
36
37 /* pthread function overrides */
38 #define pthread_self() \
39         ((pthread_t)GetCurrentThreadId())
40 #define pthread_setaffinity_np(thread, size, cpuset) \
41         eal_set_thread_affinity_mask(thread, (unsigned long *) cpuset)
42 #define pthread_getaffinity_np(thread, size, cpuset) \
43         eal_get_thread_affinity_mask(thread, (unsigned long *) cpuset)
44 #define pthread_create(threadid, threadattr, threadfunc, args) \
45         eal_create_thread(threadid, threadfunc, args)
46
47 static inline int
48 eal_set_thread_affinity_mask(pthread_t threadid, unsigned long *cpuset)
49 {
50         SetThreadAffinityMask((HANDLE) threadid, *cpuset);
51         return 0;
52 }
53
54 static inline int
55 eal_get_thread_affinity_mask(pthread_t threadid, unsigned long *cpuset)
56 {
57         /* Workaround for the lack of a GetThreadAffinityMask()
58          *API in Windows
59          */
60                 /* obtain previous mask by setting dummy mask */
61         DWORD dwprevaffinitymask =
62                 SetThreadAffinityMask((HANDLE) threadid, 0x1);
63         /* set it back! */
64         SetThreadAffinityMask((HANDLE) threadid, dwprevaffinitymask);
65         *cpuset = dwprevaffinitymask;
66         return 0;
67 }
68
69 static inline int
70 eal_create_thread(void *threadid, void *threadfunc, void *args)
71 {
72         HANDLE hThread;
73         hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadfunc,
74                 args, 0, (LPDWORD)threadid);
75         if (hThread) {
76                 SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
77                 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
78         }
79         return ((hThread != NULL) ? 0 : E_FAIL);
80 }
81
82 static inline int
83 pthread_join(pthread_t thread __attribute__((__unused__)),
84         void **value_ptr __attribute__((__unused__)))
85 {
86         return 0;
87 }
88
89 #ifdef __cplusplus
90 }
91 #endif
92
93 #endif /* _PTHREAD_H_ */