-
Notifications
You must be signed in to change notification settings - Fork 4
/
pipe_waiter.c
79 lines (74 loc) · 1.85 KB
/
pipe_waiter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifdef _WIN32
//Windows version
#include <windows.h>
#include "sdl_callbacks.h"
static DWORD WINAPI win_thread(void* param){
void* buf[1]={NULL};
HANDLE handle=(HANDLE)((void**)param)[0];
void* ptr_fn_data=((void**)param)[1];
void* ptr_this_data=((void**)param)[2];
void* ptr_fn_close=((void**)param)[3];
void* ptr_this_close=((void**)param)[4];
free(param);
param=NULL;
for(;;){
int n_read=0;
int ret=ReadFile(handle,buf,1,&n_read,NULL);
if(!ret||!n_read){
break;
}
sdlcbQueueSync(ptr_fn_data,ptr_this_data,buf[0]);
}
sdlcbQueue(ptr_fn_close,ptr_this_close,NULL);
return 0;
}
void CreatePipeWaiterThread(HANDLE handle,
void* ptr_fn_data,void* ptr_this_data,
void* ptr_fn_close,void* ptr_this_close){
int tid=0;
void** param=(void**)calloc(5,sizeof(void*));
param[0]=handle;
param[1]=ptr_fn_data;
param[2]=ptr_this_data;
param[3]=ptr_fn_close;
param[4]=ptr_this_close;
CreateThread(NULL,0,win_thread,param,0,&tid);
}
#else
//Unix version
#include <unistd.h>
#include <stdint.h>
#include "SDL.h"
#include "sdl_callbacks.h"
static int unix_thread(void* param){
void* buf[1]={NULL};
int handle=(int)(intptr_t)((void**)param)[0];
void* ptr_fn_data=((void**)param)[1];
void* ptr_this_data=((void**)param)[2];
void* ptr_fn_close=((void**)param)[3];
void* ptr_this_close=((void**)param)[4];
free(param);
param=NULL;
for(;;){
int ret=read(handle,buf,1);
if(!(ret>0)){
break;
}
sdlcbQueueSync(ptr_fn_data,ptr_this_data,buf[0]);
}
sdlcbQueue(ptr_fn_close,ptr_this_close,NULL);
return 0;
}
void CreatePipeWaiterThread(void* handle,
void* ptr_fn_data,void* ptr_this_data,
void* ptr_fn_close,void* ptr_this_close){
int tid=0;
void** param=(void**)calloc(5,sizeof(void*));
param[0]=handle;
param[1]=ptr_fn_data;
param[2]=ptr_this_data;
param[3]=ptr_fn_close;
param[4]=ptr_this_close;
SDL_CreateThread(unix_thread,"pipe_waiter",param);
}
#endif