xref: /dpdk/lib/eal/windows/include/pthread.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2019 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef _PTHREAD_H_
699a2dd95SBruce Richardson #define _PTHREAD_H_
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson #include <stdint.h>
999a2dd95SBruce Richardson #include <sched.h>
1099a2dd95SBruce Richardson 
1199a2dd95SBruce Richardson /**
1299a2dd95SBruce Richardson  * This file is required to support the common code in eal_common_proc.c,
1399a2dd95SBruce Richardson  * eal_common_thread.c and common\include\rte_per_lcore.h as Microsoft libc
1499a2dd95SBruce Richardson  * does not contain pthread.h. This may be removed in future releases.
1599a2dd95SBruce Richardson  */
16*719834a6SMattias Rönnblom #include <rte_common.h>
17*719834a6SMattias Rönnblom #include <rte_windows.h>
18*719834a6SMattias Rönnblom 
1999a2dd95SBruce Richardson #ifdef __cplusplus
2099a2dd95SBruce Richardson extern "C" {
2199a2dd95SBruce Richardson #endif
2299a2dd95SBruce Richardson 
2399a2dd95SBruce Richardson #define PTHREAD_BARRIER_SERIAL_THREAD TRUE
2499a2dd95SBruce Richardson 
2599a2dd95SBruce Richardson /* defining pthread_t type on Windows since there is no in Microsoft libc*/
2699a2dd95SBruce Richardson typedef uintptr_t pthread_t;
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson /* defining pthread_attr_t type on Windows since there is no in Microsoft libc*/
2999a2dd95SBruce Richardson typedef void *pthread_attr_t;
3099a2dd95SBruce Richardson 
3199a2dd95SBruce Richardson typedef void *pthread_mutexattr_t;
3299a2dd95SBruce Richardson 
3399a2dd95SBruce Richardson typedef CRITICAL_SECTION pthread_mutex_t;
3499a2dd95SBruce Richardson 
3599a2dd95SBruce Richardson typedef SYNCHRONIZATION_BARRIER pthread_barrier_t;
3699a2dd95SBruce Richardson 
3799a2dd95SBruce Richardson #define pthread_barrier_init(barrier, attr, count) \
3899a2dd95SBruce Richardson 	!InitializeSynchronizationBarrier(barrier, count, -1)
3999a2dd95SBruce Richardson #define pthread_barrier_wait(barrier) EnterSynchronizationBarrier(barrier, \
4099a2dd95SBruce Richardson 	SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY)
4199a2dd95SBruce Richardson #define pthread_barrier_destroy(barrier) \
4299a2dd95SBruce Richardson 	!DeleteSynchronizationBarrier(barrier)
4399a2dd95SBruce Richardson #define pthread_cancel(thread) !TerminateThread((HANDLE) thread, 0)
4499a2dd95SBruce Richardson 
4599a2dd95SBruce Richardson static inline int
4699a2dd95SBruce Richardson pthread_create(void *threadid, const void *threadattr, void *threadfunc,
4799a2dd95SBruce Richardson 		void *args)
4899a2dd95SBruce Richardson {
4999a2dd95SBruce Richardson 	RTE_SET_USED(threadattr);
5099a2dd95SBruce Richardson 	HANDLE hThread;
514ef69b28SThomas Monjalon 	hThread = CreateThread(NULL, 0,
524ef69b28SThomas Monjalon 		(LPTHREAD_START_ROUTINE)(uintptr_t)threadfunc,
5399a2dd95SBruce Richardson 		args, 0, (LPDWORD)threadid);
5499a2dd95SBruce Richardson 	if (hThread) {
5599a2dd95SBruce Richardson 		SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
5699a2dd95SBruce Richardson 		SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL);
5799a2dd95SBruce Richardson 	}
5899a2dd95SBruce Richardson 	return ((hThread != NULL) ? 0 : E_FAIL);
5999a2dd95SBruce Richardson }
6099a2dd95SBruce Richardson 
6199a2dd95SBruce Richardson static inline int
6299a2dd95SBruce Richardson pthread_mutex_init(pthread_mutex_t *mutex,
6399a2dd95SBruce Richardson 		   __rte_unused pthread_mutexattr_t *attr)
6499a2dd95SBruce Richardson {
6599a2dd95SBruce Richardson 	InitializeCriticalSection(mutex);
6699a2dd95SBruce Richardson 	return 0;
6799a2dd95SBruce Richardson }
6899a2dd95SBruce Richardson 
6999a2dd95SBruce Richardson static inline int
7099a2dd95SBruce Richardson pthread_mutex_lock(pthread_mutex_t *mutex)
7199a2dd95SBruce Richardson {
7299a2dd95SBruce Richardson 	EnterCriticalSection(mutex);
7399a2dd95SBruce Richardson 	return 0;
7499a2dd95SBruce Richardson }
7599a2dd95SBruce Richardson 
7699a2dd95SBruce Richardson static inline int
7799a2dd95SBruce Richardson pthread_mutex_unlock(pthread_mutex_t *mutex)
7899a2dd95SBruce Richardson {
7999a2dd95SBruce Richardson 	LeaveCriticalSection(mutex);
8099a2dd95SBruce Richardson 	return 0;
8199a2dd95SBruce Richardson }
8299a2dd95SBruce Richardson 
8399a2dd95SBruce Richardson static inline int
8499a2dd95SBruce Richardson pthread_mutex_destroy(pthread_mutex_t *mutex)
8599a2dd95SBruce Richardson {
8699a2dd95SBruce Richardson 	DeleteCriticalSection(mutex);
8799a2dd95SBruce Richardson 	return 0;
8899a2dd95SBruce Richardson }
8999a2dd95SBruce Richardson 
9099a2dd95SBruce Richardson #ifdef __cplusplus
9199a2dd95SBruce Richardson }
9299a2dd95SBruce Richardson #endif
9399a2dd95SBruce Richardson 
9499a2dd95SBruce Richardson #endif /* _PTHREAD_H_ */
95