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