xref: /dpdk/lib/eal/windows/eal_thread.c (revision 515cd4a488b6a0c6e40d20e6b10d8e89657dc23f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4 
5 #include <io.h>
6 
7 #include <rte_atomic.h>
8 #include <rte_debug.h>
9 #include <rte_launch.h>
10 #include <rte_lcore.h>
11 #include <rte_per_lcore.h>
12 #include <rte_common.h>
13 #include <rte_memory.h>
14 
15 #include "eal_private.h"
16 #include "eal_thread.h"
17 #include "eal_windows.h"
18 
19 int
20 eal_thread_wake_worker(unsigned int worker_id)
21 {
22 	int m2w = lcore_config[worker_id].pipe_main2worker[1];
23 	int w2m = lcore_config[worker_id].pipe_worker2main[0];
24 	char c = 0;
25 	int n;
26 
27 	do {
28 		n = _write(m2w, &c, 1);
29 	} while (n == 0 || (n < 0 && errno == EINTR));
30 	if (n < 0)
31 		return -EPIPE;
32 
33 	do {
34 		n = _read(w2m, &c, 1);
35 	} while (n < 0 && errno == EINTR);
36 	if (n <= 0)
37 		return -EPIPE;
38 	return 0;
39 }
40 
41 void
42 eal_thread_wait_command(void)
43 {
44 	unsigned int lcore_id = rte_lcore_id();
45 	int m2w;
46 	char c;
47 	int n;
48 
49 	m2w = lcore_config[lcore_id].pipe_main2worker[0];
50 	do {
51 		n = _read(m2w, &c, 1);
52 	} while (n < 0 && errno == EINTR);
53 	if (n <= 0)
54 		rte_panic("cannot read on configuration pipe\n");
55 }
56 
57 void
58 eal_thread_ack_command(void)
59 {
60 	unsigned int lcore_id = rte_lcore_id();
61 	char c = 0;
62 	int w2m;
63 	int n;
64 
65 	w2m = lcore_config[lcore_id].pipe_worker2main[1];
66 	do {
67 		n = _write(w2m, &c, 1);
68 	} while (n == 0 || (n < 0 && errno == EINTR));
69 	if (n < 0)
70 		rte_panic("cannot write on configuration pipe\n");
71 }
72 
73 /* function to create threads */
74 int
75 eal_thread_create(pthread_t *thread, unsigned int lcore_id)
76 {
77 	HANDLE th;
78 
79 	th = CreateThread(NULL, 0,
80 		(LPTHREAD_START_ROUTINE)(ULONG_PTR)eal_thread_loop,
81 						(LPVOID)(uintptr_t)lcore_id,
82 						CREATE_SUSPENDED,
83 						(LPDWORD)thread);
84 	if (!th)
85 		return -1;
86 
87 	SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
88 	SetThreadPriority(th, THREAD_PRIORITY_NORMAL);
89 
90 	if (ResumeThread(th) == (DWORD)-1) {
91 		(void)CloseHandle(th);
92 		return -1;
93 	}
94 
95 	return 0;
96 }
97 
98 /* get current thread ID */
99 int
100 rte_sys_gettid(void)
101 {
102 	return GetCurrentThreadId();
103 }
104 
105 int
106 rte_thread_setname(__rte_unused pthread_t id, __rte_unused const char *name)
107 {
108 	/* TODO */
109 	/* This is a stub, not the expected result */
110 	return 0;
111 }
112