-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdselect_t.c
executable file
·154 lines (121 loc) · 3.21 KB
/
fdselect_t.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "config.h"
#include "sock.h"
#include "fdselect.h"
#include "warn.h"
#include "rand.h"
#if OS & OS_WIN32
#include "bkwin32.h"
typedef DWORD WINAPI thread_func_t(LPVOID lpParameter);
#define THREAD_FUNC(name, arg) DWORD WINAPI name(LPVOID arg)
tid_t
thread_tid() {
return GetCurrentThreadId();
}
tid_t
thread_start(thread_func_t func, void *arg) {
DWORD dw;
HANDLE h;
h = CreateThread(0, 0, func, arg, 0, &dw);
CloseHandle(h);
return (tid_t)dw;
}
#endif // OS_WIN32
#define CHILD_FORK_MAX 10
#define CHILD_BUF_MAX 32768
#define CHILD_SLEEP_RAND 100
#define CHILD_CALL_MAX 20
typedef unsigned long tid_t;
typedef struct {
sock_t sock;
int id;
} pipe_child_arg_t;
/* just keep sending and receiving for a while */
THREAD_FUNC(pipe_child, varg) {
pipe_child_arg_t *arg = (pipe_child_arg_t*)varg;
int id = arg->id;
sock_t fd = arg->sock;
int i, n;
char buf[CHILD_BUF_MAX];
tid_t tid;
tid = thread_tid();
for(i=0; i<CHILD_CALL_MAX; i++) {
n = rand_u32(CHILD_BUF_MAX);
n = send(fd, buf, n, 0);
printf("pipe_child[tid=%d]: send n=%d\n", tid, n);
if( n <= 0 ) break;
n = recv(fd, buf, sizeof(buf), 0);
printf("pipe_child[tid=%d]: recv n=%d\n", tid, n);
if( n <= 0 ) break;
//usleep((double)rand() * CHILD_SLEEP_RAND / RAND_MAX);
}
sock_close(fd);
free(arg);
return 0;
}
void
pipe_select_read(fdselect_t *sel, int fd, int event, void *arg);
void
pipe_select_write(fdselect_t *sel, int fd, int event, void *arg);
void
pipe_select_read(fdselect_t *sel, int fd, int event, void *arg) {
int n;
char buf[CHILD_BUF_MAX];
tid_t tid = (tid_t)arg;
n = recv(fd, buf, sizeof(buf), 0);
printf("pipe_select_read[tid=%d]: recv n=%d\n", tid, n);
if( sock_wouldblock(fd, n) ) {
return;
}
fdselect_unset(sel, fd, FDSELECT_READ);
if( n>0 ) {
fdselect_set(sel, fd, FDSELECT_WRITE, pipe_select_write, arg);
}
}
void
pipe_select_write(fdselect_t *sel, int fd, int event, void *arg) {
int n;
char buf[CHILD_BUF_MAX];
tid_t tid = (tid_t)arg;
n = rand_u32(CHILD_BUF_MAX);
n = send(fd, buf, n, 0);
printf("pipe_select_write[tid=%d]: send n=%d\n", tid, n);
if( sock_wouldblock(fd, n) ) {
return;
}
fdselect_unset(sel, fd, FDSELECT_WRITE);
if( n>0 ) {
fdselect_set(sel, fd, FDSELECT_READ, pipe_select_read, arg);
}
}
int
main() {
fdselect_t sel;
sock_t pipe[2];
int i, n;
tid_t tid;
debug_init(DEBUG_INFO, 0, 0);
sock_init();
fdselect_init(&sel);
for(i=0; i<CHILD_FORK_MAX; i++) {
pipe_child_arg_t *a;
n = sock_pair(pipe);
assertb_sockerr(n>=0);
n = sock_nonblock(pipe[0], 1);
assertb_sockerr(n>=0);
a = calloc(1, sizeof(*a));
a->sock = pipe[1];
a->id = i;
tid = thread_start(pipe_child, a);
fdselect_set(&sel, pipe[0], FDSELECT_READ, pipe_select_read, (void*)tid);
}
while(1) {
i = fdselect_count(&sel);
printf("fdselect_select: count=%d\n", i);
if( i<=0 ) break;
fdselect_select(&sel, 1000);
}
return 0;
}