Skip to content

Commit a4b1035

Browse files
committed
windows: Add examples/csp_server_client_windows.c
csp_server_client requires arch specific thread creation functions. With xamples/csp_server_client_windows.c, csp_server_client can now be compiled for Windows. I use uintptr_t for the return variable because it what _beginthreadex() returns. We don't have anything to pass to the function except the thread entry function. The wrapper was using 0 for the last parameter but it's actually a pointer to an unsigned int. So I've changed it to NULL. That's what the doc says: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/beginthread-beginthreadex thrdaddr Points to a 32-bit variable that receives the thread identifier. If it's NULL, it's not used. Signed-off-by: Yasushi SHOJI <[email protected]>
1 parent cec8bcb commit a4b1035

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

examples/csp_server_client_windows.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <csp/csp.h>
2+
#include <windows.h>
3+
#include <process.h>
4+
5+
void server(void);
6+
void client(void);
7+
8+
static int csp_win_thread_create(unsigned int (* routine)(void *)) {
9+
10+
uintptr_t ret = _beginthreadex(NULL, 0, routine, NULL, 0, NULL);
11+
if (ret == 0) {
12+
return CSP_ERR_NOMEM;
13+
}
14+
15+
return CSP_ERR_NONE;
16+
}
17+
18+
static unsigned int task_router(void * param) {
19+
20+
/* Here there be routing */
21+
while (1) {
22+
csp_route_work();
23+
}
24+
25+
return 0;
26+
}
27+
28+
static unsigned int task_server(void * param) {
29+
server();
30+
return 0;
31+
}
32+
33+
static unsigned int task_client(void * param) {
34+
client();
35+
return 0;
36+
}
37+
38+
int router_start(void) {
39+
return csp_win_thread_create(task_router);
40+
}
41+
42+
int server_start(void) {
43+
return csp_win_thread_create(task_server);
44+
}
45+
46+
int client_start(void) {
47+
return csp_win_thread_create(task_client);
48+
}

0 commit comments

Comments
 (0)