-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcpool.h
475 lines (430 loc) · 15.6 KB
/
tcpool.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#ifndef _TCPOOL_H_
#define _TCPOOL_H_
#ifndef _GNU_SOURCE
/* See feature_test_macros(7) */
#define _GNU_SOURCE 1
#endif
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#ifndef TCPOOL_THREAD_TIMER_T_SZ
#define TCPOOL_THREAD_TIMER_T_SZ 1024
#endif
#define TCPOOL_THREAD_TIMER_T_HEAD_SZ ( \
sizeof(uint32_t) \
+sizeof(int) \
+sizeof(time_t) \
+sizeof(pthread_rwlock_t) \
+2*sizeof(pthread_t) \
+2*sizeof(pthread_cond_t) \
+2*sizeof(pthread_mutex_t) \
+sizeof(pthread_rwlock_t) \
+2*sizeof(uint8_t) \
)
#ifndef TCPOOL_THREADCNT
#define TCPOOL_THREADCNT 32
#endif
#ifndef TCPOOL_MAXWAITSEC
#define TCPOOL_MAXWAITSEC 8
#endif
#ifndef TCPOOL_THREAD_CONTEXT
#define TCPOOL_THREAD_CONTEXT uint8_t __padding[ \
TCPOOL_THREAD_TIMER_T_SZ \
-TCPOOL_THREAD_TIMER_T_HEAD_SZ \
]
#endif
#ifndef TCPOOL_TOUCH_TIMER_CONDITION
#define TCPOOL_TOUCH_TIMER_CONDITION (0)
#endif
#ifndef TCPOOL_INIT_ACTION
#define TCPOOL_INIT_ACTION ;
#endif
#ifndef TCPOOL_PREHANDLE_ACCEPT_ACTION
#define TCPOOL_PREHANDLE_ACCEPT_ACTION(timer) ;
#endif
#ifndef TCPOOL_CLEANUP_THREAD_ACTION
#define TCPOOL_CLEANUP_THREAD_ACTION(timer) ;
#endif
struct tcpool_thread_timer_t {
uint32_t index;
int accept_fd;
time_t touch; // lock by mt
pthread_rwlock_t mt; // lock touch
pthread_t thread;
pthread_t timerthread;
pthread_cond_t c; // lock by mc
pthread_mutex_t mc; // lock c
pthread_cond_t tc; // lock by tmc
pthread_mutex_t tmc; // lock tc&hastimerslept
pthread_rwlock_t mb; // lock isbusy
TCPOOL_THREAD_CONTEXT;
uint8_t isbusy; // lock by mb
uint8_t hastimerslept; // lock by tmc
};
typedef struct tcpool_thread_timer_t tcpool_thread_timer_t;
static tcpool_thread_timer_t tcpool_timers[TCPOOL_THREADCNT];
#define tcpool_timer_pointer_of(x) ((tcpool_thread_timer_t*)(x))
#define tcpool_touch_timer(x) { \
pthread_rwlock_wrlock(&tcpool_timer_pointer_of(x)->mt); \
tcpool_timer_pointer_of(x)->touch = time(NULL); \
printf("Touch timer@%d\n", tcpool_timer_pointer_of(x)->index);\
pthread_rwlock_unlock(&tcpool_timer_pointer_of(x)->mt); \
}
#ifdef LISTEN_ON_IPV6
static socklen_t tcpool_struct_len = sizeof(struct sockaddr_in6);
static struct sockaddr_in6 tcpool_server_addr;
#else
static socklen_t tcpool_struct_len = sizeof(struct sockaddr_in);
static struct sockaddr_in tcpool_server_addr;
#endif
static struct sockaddr_un tcpool_server_uname;
static pthread_attr_t __tcpool_thread_attr;
static pthread_key_t __tcpool_pthread_key_index;
static sigjmp_buf __tcpool_jmp2convend[TCPOOL_THREADCNT];
static void accept_action(tcpool_thread_timer_t *timer);
static void accept_client(int fd);
static void accept_timer(void *p);
static int bind_server(uint16_t* port);
static int bind_server_unix(char *path);
static void cleanup_thread(tcpool_thread_timer_t* timer);
static void handle_accept(void *accept_fd_p);
static void handle_int(int signo);
static void handle_kill(int signo);
static void handle_pipe(int signo);
static void handle_quit(int signo);
static void handle_segv(int signo);
static int listen_socket(int fd, int listen_queue_len);
static int bind_server(uint16_t* port) {
#ifdef LISTEN_ON_IPV6
tcpool_server_addr.sin6_family = AF_INET6;
tcpool_server_addr.sin6_port = htons(*port);
bzero(&(tcpool_server_addr.sin6_addr), sizeof(tcpool_server_addr.sin6_addr));
int fd = socket(PF_INET6, SOCK_STREAM, 0);
#else
tcpool_server_addr.sin_family = AF_INET;
tcpool_server_addr.sin_port = htons(*port);
tcpool_server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(tcpool_server_addr.sin_zero), 8);
int fd = socket(AF_INET, SOCK_STREAM, 0);
#endif
int on = 1;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
perror("setsockopt");
return -1;
}
if(bind(fd, (struct sockaddr *)&tcpool_server_addr, tcpool_struct_len) < 0) {
perror("bind");
return -2;
}
/* if dynamically allocating a port */
if(*port == 0) {
if(getsockname(fd, (struct sockaddr *)&tcpool_server_addr, &tcpool_struct_len) < 0) {
perror("getsockname");
return -3;
}
#ifdef LISTEN_ON_IPV6
*port = ntohs(tcpool_server_addr.sin6_port);
#else
*port = ntohs(tcpool_server_addr.sin_port);
#endif
}
#ifdef LISTEN_ON_IPV6
struct in6_addr in = tcpool_server_addr.sin6_addr;
char str[INET6_ADDRSTRLEN]; // 46
inet_ntop(AF_INET6, &in, str, sizeof(str));
printf("Bind server successfully on [%s]:%u\n", str, *port);
#else
struct in_addr in = tcpool_server_addr.sin_addr;
char str[INET_ADDRSTRLEN]; // 16
inet_ntop(AF_INET, &in, str, sizeof(str));
printf("Bind server successfully on %s:%u\n", str, *port);
#endif
return fd;
}
static int bind_server_unix(char *path) {
int httpd = socket(AF_UNIX, SOCK_STREAM, 0);
if(httpd < 0) {
perror("socket(unix)");
return -1;
}
tcpool_server_uname.sun_family = AF_UNIX;
strncpy(tcpool_server_uname.sun_path, path, sizeof(tcpool_server_uname.sun_path));
tcpool_server_uname.sun_path[sizeof(tcpool_server_uname.sun_path)-1] = 0; // avoid overlap
#if __APPLE__
tcpool_server_uname.sun_len = strlen(tcpool_server_uname.sun_path);
#endif
unlink(path); // in case it already exists
int on = 1;
if(setsockopt(httpd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
perror("setsockopt(unix)");
return -2;
}
if(bind(httpd, (struct sockaddr *)&tcpool_server_uname, SUN_LEN(&tcpool_server_uname)) < 0) {
perror("bind(unix)");
return -3;
}
printf("Bind server successfully on %s\n", path);
return httpd;
}
static int listen_socket(int fd, int listen_queue_len) {
if(listen_queue_len <= 0) listen_queue_len = TCPOOL_THREADCNT;
if(listen(fd, listen_queue_len) < 0) {
perror("listen");
return -1;
}
puts("Listening...");
return fd;
}
static void handle_quit(int signo) {
uint32_t index = (uint32_t)((uintptr_t)pthread_getspecific(__tcpool_pthread_key_index));
printf("Handle sigquit@%d\n", index-1);
fflush(stdout);
if(index) {
sigaction(SIGQUIT, &(const struct sigaction){handle_quit}, NULL);
siglongjmp(__tcpool_jmp2convend[index-1], signo);
}
else pthread_exit(NULL);
}
static void handle_segv(int signo) {
uint32_t index = (uint32_t)((uintptr_t)pthread_getspecific(__tcpool_pthread_key_index));
printf("Handle sigsegv@%d\n", index-1);
fflush(stdout);
if(index) {
sigaction(SIGSEGV, &(const struct sigaction){handle_segv}, NULL);
siglongjmp(__tcpool_jmp2convend[index-1], signo);
}
else pthread_exit(NULL);
}
static void handle_kill(int signo) {
puts("Handle sigkill/sigterm");
fflush(stdout);
exit(signo);
}
static void handle_int(int signo) {
puts("Keyboard interrupted");
fflush(stdout);
exit(signo);
}
static void handle_pipe(int signo) {
uint32_t index = (uint32_t)((uintptr_t)pthread_getspecific(__tcpool_pthread_key_index));
printf("Pipe error@%d, break loop...\n", index-1);
fflush(stdout);
if(index) {
sigaction(SIGPIPE, &(const struct sigaction){handle_pipe}, NULL);
siglongjmp(__tcpool_jmp2convend[index-1], signo);
}
else pthread_exit(NULL);
}
static void accept_timer(void *p) {
tcpool_thread_timer_t *timer = tcpool_timer_pointer_of(p);
uint32_t index = timer->index;
pthread_t thread = timer->thread;
uint8_t isbusy;
sleep(TCPOOL_MAXWAITSEC / 4);
while(thread && !pthread_kill(thread, 0)) {
pthread_rwlock_rdlock(&timer->mb);
isbusy = timer->isbusy;
pthread_rwlock_unlock(&timer->mb);
if(!isbusy) {
TIMER_SLEEP:
pthread_mutex_lock(&timer->tmc);
timer->hastimerslept = 1;
printf("Timer@%d sleep\n", timer->index);
pthread_cond_wait(&timer->tc, &timer->tmc);
timer->hastimerslept = 0;
pthread_mutex_unlock(&timer->tmc);
printf("Timer@%d wake up\n", timer->index);
sleep(TCPOOL_MAXWAITSEC / 4);
thread = timer->thread;
}
if(TCPOOL_TOUCH_TIMER_CONDITION) tcpool_touch_timer(p);
pthread_rwlock_rdlock(&timer->mt);
time_t waitsec = time(NULL) - timer->touch;
pthread_rwlock_unlock(&timer->mt);
printf("Wait@%d sec: %u, max: %u\n", timer->index, (unsigned int)waitsec, TCPOOL_MAXWAITSEC);
if(waitsec > TCPOOL_MAXWAITSEC) {
if(thread) {
pthread_kill(thread, SIGQUIT);
printf("Kill thread@%d\n", timer->index);
}
break;
}
sleep(TCPOOL_MAXWAITSEC / 4);
thread = timer->thread;
}
goto TIMER_SLEEP;
}
static void cleanup_thread(tcpool_thread_timer_t* timer) {
printf("Start cleaning@%d, ", timer->index);
if(timer->accept_fd) {
close(timer->accept_fd);
timer->accept_fd = 0;
printf("Close accept, ");
}
TCPOOL_CLEANUP_THREAD_ACTION(timer);
timer->thread = 0;
printf("Clear thread, ");
pthread_cond_destroy(&timer->c);
printf("Destroy accept cond, ");
pthread_mutex_destroy(&timer->mc);
printf("Destroy accept mutex, ");
pthread_rwlock_wrlock(&timer->mb);
timer->isbusy = 0;
printf("Clear busy, ");
pthread_rwlock_unlock(&timer->mb);
puts("Finish cleaning");
}
static void handle_accept(void *p) {
#ifdef DEBUG
printf("accept ptr: %p\n", p);
#endif
pthread_cleanup_push((void (*)(void*))&cleanup_thread, p);
puts("Handling accept...");
pthread_setspecific(__tcpool_pthread_key_index, (void*)((uintptr_t)tcpool_timer_pointer_of(p)->index+1));
if(sigsetjmp(__tcpool_jmp2convend[tcpool_timer_pointer_of(p)->index], 1)) {
printf("Long Jump@%d\n", tcpool_timer_pointer_of(p)->index);
goto CONV_END;
}
while(1) {
accept_action(tcpool_timer_pointer_of(p));
CONV_END: puts("Conversation end");
if(tcpool_timer_pointer_of(p)->accept_fd) {
close(tcpool_timer_pointer_of(p)->accept_fd);
tcpool_timer_pointer_of(p)->accept_fd = 0;
puts("Close accept");
}
TCPOOL_CLEANUP_THREAD_ACTION(tcpool_timer_pointer_of(p));
pthread_mutex_lock(&tcpool_timer_pointer_of(p)->mc);
pthread_rwlock_wrlock(&tcpool_timer_pointer_of(p)->mb);
tcpool_timer_pointer_of(p)->isbusy = 0;
pthread_rwlock_unlock(&tcpool_timer_pointer_of(p)->mb);
puts("Set thread status to idle");
pthread_cond_wait(&tcpool_timer_pointer_of(p)->c, &tcpool_timer_pointer_of(p)->mc);
pthread_mutex_unlock(&tcpool_timer_pointer_of(p)->mc);
puts("Thread wakeup");
}
pthread_cleanup_pop(1);
}
static void accept_client(int fd) {
sigaction(SIGINT , &(const struct sigaction){handle_int}, NULL);
sigaction(SIGQUIT, &(const struct sigaction){handle_quit}, NULL);
sigaction(SIGKILL, &(const struct sigaction){handle_kill}, NULL);
sigaction(SIGSEGV, &(const struct sigaction){handle_segv}, NULL);
sigaction(SIGPIPE, &(const struct sigaction){handle_pipe}, NULL);
sigaction(SIGTERM, &(const struct sigaction){handle_kill}, NULL);
pthread_attr_init(&__tcpool_thread_attr);
pthread_attr_setdetachstate(&__tcpool_thread_attr, PTHREAD_CREATE_DETACHED);
TCPOOL_INIT_ACTION;
int i = 0;
for(; i < TCPOOL_THREADCNT; i++) {
pthread_rwlock_init(&tcpool_timers[i].mt, NULL);
pthread_rwlock_init(&tcpool_timers[i].mb, NULL);
}
pthread_key_create(&__tcpool_pthread_key_index, NULL);
while(1) {
int p = 0;
while(p < TCPOOL_THREADCNT) {
pthread_rwlock_rdlock(&tcpool_timers[p].mb);
if(!tcpool_timers[p].isbusy) break;
pthread_rwlock_unlock(&tcpool_timers[p].mb);
p++;
}
if(p >= TCPOOL_THREADCNT) {
puts("Max thread cnt exceeded");
sleep(1);
continue;
}
printf("Ready for accept on slot No.%d\n", p);
tcpool_thread_timer_t* timer = &tcpool_timers[p];
pthread_rwlock_unlock(&timer->mb);
#ifdef LISTEN_ON_IPV6
struct sockaddr_in6 client_addr;
#else
struct sockaddr_in client_addr;
#endif
int accept_fd;
if((accept_fd=accept(fd, (struct sockaddr *)&client_addr, &tcpool_struct_len))<=0) {
perror("accept");
continue;
}
pthread_rwlock_wrlock(&timer->mb);
timer->isbusy = 1;
pthread_rwlock_unlock(&timer->mb);
#ifdef LISTEN_ON_IPV6
uint16_t port = ntohs(client_addr.sin6_port);
struct in6_addr in = client_addr.sin6_addr;
char str[INET6_ADDRSTRLEN]; // 46
inet_ntop(AF_INET6, &in, str, sizeof(str));
#else
uint16_t port = ntohs(client_addr.sin_port);
struct in_addr in = client_addr.sin_addr;
char str[INET_ADDRSTRLEN]; // 16
inet_ntop(AF_INET, &in, str, sizeof(str));
#endif
time_t t = time(NULL);
printf("\n> %sAccept client %s:%u at slot No.%d, ", ctime(&t), str, port, p);
timer->accept_fd = accept_fd;
timer->index = p;
pthread_rwlock_wrlock(&timer->mt);
timer->touch = time(NULL);
pthread_rwlock_unlock(&timer->mt);
TCPOOL_PREHANDLE_ACCEPT_ACTION(timer);
// start or wakeup accept thread
pthread_t thread = timer->thread;
if(thread && !pthread_kill(thread, 0)) {
pthread_mutex_lock(&timer->mc);
pthread_cond_signal(&timer->c); // wakeup thread
pthread_mutex_unlock(&timer->mc);
puts("Pick thread from pool");
} else {
pthread_cond_init(&timer->c, NULL);
pthread_mutex_init(&timer->mc, NULL);
if (pthread_create(&timer->thread, &__tcpool_thread_attr, (void* (*)(void*))&handle_accept, timer)) {
perror("pthread_create(accept)");
cleanup_thread(timer);
putchar('\n');
continue;
}
puts("Thread created");
}
// start or wakeup timer thread
thread = timer->timerthread;
if(!thread || pthread_kill(thread, 0)) {
printf("Creating timer thread...");
pthread_cond_init(&timer->tc, NULL);
pthread_mutex_init(&timer->tmc, NULL);
timer->hastimerslept = 0;
if (pthread_create(&timer->timerthread, &__tcpool_thread_attr, (void* (*)(void*))&accept_timer, timer)) {
perror("pthread_create(timer)");
cleanup_thread(timer);
putchar('\n');
continue;
}
puts("succeeded");
} else {
pthread_mutex_lock(&timer->tmc);
uint8_t hastimerslept = timer->hastimerslept;
pthread_mutex_unlock(&timer->tmc);
if(hastimerslept) {
printf("Waking up timer thread...");
pthread_mutex_lock(&timer->tmc);
pthread_cond_signal(&timer->tc); // wakeup thread
pthread_mutex_unlock(&timer->tmc);
puts("succeeded");
} else puts("Timer already running");
}
}
}
#endif /* _TCPOOL_H_ */