-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoroutine.c
More file actions
4522 lines (4108 loc) · 136 KB
/
coroutine.c
File metadata and controls
4522 lines (4108 loc) · 136 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ================================
* eli960@qq.com
* http://linuxmail.cn/
* 2017-06-26
* ================================
*/
#pragma GCC diagnostic ignored "-Wredundant-decls"
#pragma GCC diagnostic ignored "-Wpacked-not-aligned"
# include "./coroutine.h"
#ifndef __x86_64__
#include <ucontext.h>
#endif
/* {{{ include */
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <poll.h>
#include <pthread.h>
#include <resolv.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
/* }}} */
/* {{{ macro */
#define zvar_coroutine_max_timeout_millisecond (3600L * 24 * 365 * 10 * 1000)
#define ZEMPTY(str) (!(str)||!(*((const char *)str)))
#define ZCONTAINER_OF(ptr,app_type,member) ((app_type *) (((char *) (ptr)) - offsetof(app_type,member)))
#define zinline inline __attribute__((always_inline))
#define zcoroutine_fatal(fmt, args...) { \
fprintf(stderr, "FATAL "fmt, ##args); \
fprintf(stderr, " [%s:%u]\n", __FILE__, __LINE__);\
fflush(stderr); \
_exit(1); \
}
#define zpthread_lock(l) {if(pthread_mutex_lock((pthread_mutex_t *)(l))){zcoroutine_fatal("mutex:%m");}}
#define zpthread_unlock(l) {if(pthread_mutex_unlock((pthread_mutex_t *)(l))){zcoroutine_fatal("mutex:%m");}}
/* 一组宏, 可实现, 栈, 链表等, 例子见 src/stdlib/list.c */
/* head: 头; tail: 尾; node:节点变量; prev:head/tail 指向的struct成员的属性"前一个" */
/* 追加node到尾部 */
#define ZMLINK_APPEND(head, tail, node, prev, next) {\
typeof(head) _head_1106=head, _tail_1106=tail, _node_1106 = node;\
if(_head_1106 == 0){_node_1106->prev=_node_1106->next=0;_head_1106=_tail_1106=_node_1106;}\
else {_tail_1106->next=_node_1106;_node_1106->prev=_tail_1106;_node_1106->next=0;_tail_1106=_node_1106;}\
head = _head_1106; tail = _tail_1106; \
}
/* 追加node到首部 */
#define ZMLINK_PREPEND(head, tail, node, prev, next) {\
typeof(head) _head_1106=head, _tail_1106=tail, _node_1106 = node;\
if(_head_1106 == 0){_node_1106->prev=_node_1106->next=0;_head_1106=_tail_1106=_node_1106;}\
else {_head_1106->prev=_node_1106;_node_1106->next=_head_1106;_node_1106->prev=0;_head_1106=_node_1106;}\
head = _head_1106; tail = _tail_1106; \
}
/* 插入node到before前 */
#define ZMLINK_ATTACH_BEFORE(head, tail, node, prev, next, before) {\
typeof(head) _head_1106=head, _tail_1106=tail, _node_1106 = node, _before_1106 = before;\
if(_head_1106 == 0){_node_1106->prev=_node_1106->next=0;_head_1106=_tail_1106=_node_1106;}\
else if(_before_1106==0){_tail_1106->next=_node_1106;_node_1106->prev=_tail_1106;_node_1106->next=0;_tail_1106=_node_1106;}\
else if(_before_1106==_head_1106){_head_1106->prev=_node_1106;_node_1106->next=_head_1106;_node_1106->prev=0;_head_1106=_node_1106;}\
else {_node_1106->prev=_before_1106->prev; _node_1106->next=_before_1106; _before_1106->prev->next=_node_1106; _before_1106->prev=_node_1106;}\
head = _head_1106; tail = _tail_1106; \
}
/* 去掉节点node */
#define ZMLINK_DETACH(head, tail, node, prev, next) {\
typeof(head) _head_1106=head, _tail_1106=tail, _node_1106 = node;\
if(_node_1106->prev){ _node_1106->prev->next=_node_1106->next; }else{ _head_1106=_node_1106->next; }\
if(_node_1106->next){ _node_1106->next->prev=_node_1106->prev; }else{ _tail_1106=_node_1106->prev; }\
head = _head_1106; tail = _tail_1106; \
}
/* }}} */
#pragma pack(push, 4)
#ifdef __cplusplus
extern "C" {
#endif
/* {{{ type convert */
typedef union _co_type_convert_t _co_type_convert_t;
union _co_type_convert_t {
const void *CONST_VOID_PTR;
void *VOID_PTR;
const char *CONST_CHAR_PTR;
char *CHAR_PTR;
const unsigned char *CONST_UCHAR_PTR;
unsigned char *UCHAR_PTR;
long LONG;
unsigned long ULONG;
int INT;
unsigned int UINT;
short int SHORT_INT;
unsigned short int USHORT_INT;
char CHAR;
unsigned char UCHAR;
size_t SIZE_T;
ssize_t SSIZE_T;
mode_t MODE_T;
off_t OFF_T;
uid_t UID_T;
gid_t GID_T;
struct {
int fd:30;
int is_ssl:2;
int fd_type:8;
} fdinfo;
};
/* }}} */
/* {{{ malloc */
static void *_co_mem_valloc(int len)
{
void *r;
if ((r = valloc(len)) == 0) {
zcoroutine_fatal("_co_mem_valloc: insufficient memory for %d bytes: %m", len);
}
return r;
}
static void *_co_mem_malloc(int len)
{
void *r;
if (len < 0) {
len = 0;
}
if ((r = malloc(len)) == 0) {
zcoroutine_fatal("_co_mem_malloc: insufficient memory for %d bytes: %m", len);
}
return r;
}
static void *_co_mem_calloc(int nmemb, int size)
{
void *r;
if (nmemb < 0) {
nmemb = 0;
}
if (size < 0) {
size = 0;
}
if ((r = calloc(nmemb, size)) == 0) {
zcoroutine_fatal("_co_mem_calloc: insufficient memory for %dx%d bytes: %m", nmemb, size);
}
return r;
}
static void _co_mem_free(const void *ptr)
{
if (ptr) {
free((void *)ptr);
}
}
/* }}} */
/* {{{ syscall */
zinline static int _syscall_pipe(int pipefd[2])
{
#ifdef __NR_pipe
return syscall(__NR_pipe, pipefd);
#else
return syscall(__NR_pipe2, pipefd, 0);
#endif
}
zinline static int _syscall_pipe2(int pipefd[2], int flags)
{
return syscall(__NR_pipe2, pipefd, flags);
}
zinline static int _syscall_dup(int oldfd)
{
return syscall(__NR_dup, oldfd);
}
zinline static int _syscall_dup2(int oldfd, int newfd)
{
#ifdef __NR_dup2
return syscall(__NR_dup2, oldfd, newfd);
#else
return syscall(__NR_dup3, oldfd, newfd, 0);
#endif
}
zinline static int _syscall_dup3(int oldfd, int newfd, int flags)
{
return syscall(__NR_dup3, oldfd, newfd, flags);
}
zinline static int _syscall_socketpair(int domain, int type, int protocol, int sv[2])
{
return syscall(__NR_socketpair, domain, type, protocol, sv);
}
zinline static int _syscall_socket(int domain, int type, int protocol)
{
return syscall(__NR_socket, domain, type, protocol);
}
zinline static int _syscall_accept(int fd, struct sockaddr *addr, socklen_t *len)
{
return syscall(__NR_accept, fd, addr, len);
}
zinline static int _syscall_connect(int socket, const struct sockaddr *address, socklen_t address_len)
{
return syscall(__NR_connect, socket, address, address_len);
}
zinline static int _syscall_close(int fd)
{
return syscall(__NR_close, fd);
}
zinline static ssize_t _syscall_read(int fildes, void *buf, size_t nbyte)
{
return syscall(__NR_read, fildes, buf, nbyte);
}
zinline static ssize_t _syscall_readv(int fd, const struct iovec *iov, int iovcnt)
{
return syscall(__NR_readv, fd, iov, iovcnt);
}
zinline static ssize_t _syscall_write(int fildes, const void *buf, size_t nbyte)
{
return syscall(__NR_write, fildes, buf, nbyte);
}
zinline static ssize_t _syscall_writev(int fd, const struct iovec *iov, int iovcnt)
{
return syscall(__NR_writev, fd, iov, iovcnt);
}
zinline static ssize_t _syscall_sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
{
return syscall(__NR_sendto, socket, message, length, flags, dest_addr, dest_len);
}
zinline static ssize_t _syscall_recvfrom(int socket, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len)
{
return syscall(__NR_recvfrom, socket, buffer, length, flags, address, address_len);
}
zinline static size_t _syscall_send(int socket, const void *buffer, size_t length, int flags)
{
return syscall(__NR_sendto, socket, buffer, length, flags, 0, 0);
}
zinline static ssize_t _syscall_recv(int socket, void *buffer, size_t length, int flags)
{
return syscall(__NR_recvfrom, socket, buffer, length, flags, 0, 0);
}
zinline static int _syscall_poll(struct pollfd fds[], nfds_t nfds, int timeout)
{
#ifdef __NR_poll
return syscall(__NR_poll, fds, nfds, timeout);
#else
struct timespec tmo;
if (timeout > -1) {
tmo.tv_sec = timeout/1000;
tmo.tv_nsec = 1000000L * (timeout%1000);
}
return syscall(__NR_ppoll, fds, nfds, ((timeout>-1)?(&tmo):0), 0);
#endif
}
zinline static int _syscall_setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len)
{
return syscall(__NR_setsockopt, socket, level, option_name, option_value, option_len);
}
static int _syscall_fcntl(int fildes, int cmd, ...)
{
int ret = -1;
va_list args;
va_start(args,cmd);
switch(cmd)
{
case F_DUPFD:
case F_SETFD:
case F_SETFL:
case F_SETOWN:
{
int param = va_arg(args,int);
ret = syscall(__NR_fcntl, fildes, cmd, param);
break;
}
case F_GETFD:
case F_GETFL:
case F_GETOWN:
{
ret = syscall(__NR_fcntl, fildes, cmd);
break;
}
case F_GETLK:
case F_SETLK:
case F_SETLKW:
{
/* struct flock *param = va_arg(args,struct flock *); */
void *param = va_arg(args, void *);
ret = syscall(__NR_fcntl, fildes, cmd, param);
break;
}
}
va_end(args);
return ret;
}
zinline static pid_t _syscall_gettid(void)
{
return syscall(__NR_gettid);
}
static int _syscall_open(const char *pathname, int flags, ...)
{
mode_t mode = 0;
if (flags & O_CREAT) {
va_list args;
va_start(args, flags);
mode = va_arg(args, mode_t);
va_end(args);
}
#ifdef __NR_open
return syscall(__NR_open, pathname, flags, mode);
#else
return syscall(__NR_openat, AT_FDCWD, pathname, flags, mode);
#endif
}
static int _syscall_openat(int dirfd, const char *pathname, int flags, ...)
{
mode_t mode = 0;
if (flags & O_CREAT) {
va_list args;
va_start(args, flags);
mode = va_arg(args, mode_t);
va_end(args);
}
return syscall(__NR_openat, dirfd, pathname, flags, mode);
}
zinline static int _syscall_creat(const char *pathname, mode_t mode)
{
#ifdef __NR_creat
return syscall(__NR_creat, pathname, mode);
#else
return syscall(__NR_openat, AT_FDCWD, pathname, O_WRONLY|O_CREAT|O_TRUNC, mode);
#endif
}
zinline static off_t _syscall_lseek(int fd, off_t offset, int whence)
{
return syscall(__NR_lseek, fd, offset, whence);
}
zinline static int _syscall_fdatasync(int fd)
{
return syscall(__NR_fdatasync, fd);
}
zinline static int _syscall_fsync(int fd)
{
return syscall(__NR_fsync, fd);
}
zinline static int _syscall_rename(const char *oldpath, const char *newpath)
{
#ifdef __NR_rename
return syscall(__NR_rename, oldpath, newpath);
#else
return syscall(__NR_renameat, AT_FDCWD, oldpath, AT_FDCWD, newpath);
#endif
}
zinline static int _syscall_renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath)
{
return syscall(__NR_renameat, olddirfd, oldpath, newdirfd, newpath);
}
zinline static int _syscall_truncate(const char *path, off_t length)
{
return syscall(__NR_truncate, path, length);
}
zinline static int _syscall_ftruncate(int fd, off_t length)
{
return syscall(__NR_truncate, fd, length);
}
zinline static int _syscall_rmdir(const char *pathname)
{
#ifdef __NR_rmdir
return syscall(__NR_rmdir, pathname);
#else
return syscall(__NR_unlinkat, AT_FDCWD, pathname, AT_REMOVEDIR);
#endif
}
zinline static int _syscall_mkdir(const char *pathname, mode_t mode)
{
#ifdef mkdir
return syscall(__NR_mkdir, pathname, mode);
#else
return syscall(__NR_mkdirat, AT_FDCWD, pathname, mode);
#endif
}
zinline static int _syscall_mkdirat(int dirfd, const char *pathname, mode_t mode)
{
return syscall(__NR_mkdirat, dirfd, pathname, mode);
}
#ifdef __NR_getdents
zinline static int _syscall_getdents(unsigned int fd, void *dirp, unsigned int count)
{
return syscall(__NR_getdents, fd, dirp, count);
}
#endif
#ifdef __NR_stat
zinline static int _syscall_stat(const char *pathname, struct stat *buf)
{
return syscall(__NR_stat, pathname, buf);
}
#endif
zinline static int _syscall_fstat(int fd, struct stat *buf)
{
return syscall(__NR_fstat, fd, buf);
}
#ifdef __NR_lstat
zinline static int _syscall_lstat(const char *pathname, struct stat *buf)
{
return syscall(__NR_lstat, pathname, buf);
}
#endif
zinline static int _syscall_link(const char *oldpath, const char *newpath)
{
#ifdef __NR_link
return syscall(__NR_link, oldpath, newpath);
#else
return syscall(__NR_linkat, AT_FDCWD, oldpath, AT_FDCWD, newpath, 0);
#endif
}
zinline static int _syscall_linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags)
{
return syscall(__NR_linkat, olddirfd, oldpath, newdirfd, newpath, flags);
}
zinline static int _syscall_symlink(const char *target, const char *linkpath)
{
#ifdef __NR_symlink
return syscall(__NR_symlink, target, linkpath);
#else
return syscall(__NR_symlinkat, target, AT_FDCWD, linkpath);
#endif
}
zinline static int _syscall_symlinkat(const char *target, int newdirfd, const char *linkpath)
{
return syscall(__NR_symlinkat, target, newdirfd, linkpath);
}
zinline static ssize_t _syscall_readlink(const char *pathname, char *buf, size_t bufsiz)
{
#ifdef __NR_readlink
return syscall(__NR_readlink, pathname, buf, bufsiz);
#else
return syscall(__NR_readlinkat, AT_FDCWD, pathname, buf, bufsiz);
#endif
}
zinline static ssize_t _syscall_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
{
return syscall(__NR_readlinkat, dirfd, pathname, buf, bufsiz);
}
zinline static int _syscall_unlink(const char *pathname)
{
#ifdef __NR_unlink
return syscall(__NR_unlink, pathname);
#else
return syscall(__NR_unlinkat, AT_FDCWD, pathname);
#endif
}
zinline static int _syscall_unlinkat(int dirfd, const char *pathname, int flags)
{
return syscall(__NR_unlinkat, dirfd, pathname);
}
#ifdef __NR_chmod
zinline static int _syscall_chmod(const char *pathname, mode_t mode)
{
return syscall(__NR_chmod, pathname, mode);
}
#endif
zinline static int _syscall_fchmod(int fd, mode_t mode)
{
return syscall(__NR_fchmod, fd, mode);
}
#ifdef __NR_chown
zinline static int _syscall_chown(const char *pathname, uid_t owner, gid_t group)
{
return syscall(__NR_chown, pathname, owner, group);
}
#endif
zinline static int _syscall_fchown(int fd, uid_t owner, gid_t group)
{
return syscall(__NR_fchown, fd, owner, group);
}
#ifdef __NR_lchown
zinline static int _syscall_lchown(const char *pathname, uid_t owner, gid_t group)
{
return syscall(__NR_lchown, pathname, owner, group);
}
#endif
#ifdef __NR_utime
zinline static int _syscall_utime(const char *filename, const struct utimbuf *times)
{
return syscall(__NR_utime, filename, times);
}
#endif
#ifdef __NR_utimes
zinline static int _syscall_utimes(const char *filename, const struct timeval times[2])
{
return syscall(__NR_utimes, filename, times);
}
#endif
#if 0
int _syscall_futimes(int fd, const struct timeval tv[2])
{
return syscall(__NR_futimes, fd, tv);
}
int _syscall_lutimes(const char *filename, const struct timeval tv[2])
{
return syscall(__NR_lutimes, filename, tv);
}
#endif
static int zrobust_syscall_close(int fd) {
int ret;
do {
ret = _syscall_close(fd);
} while((ret<0) && (errno==EINTR));
return ret;
}
/* }}} */
/* {{{ list */
typedef struct _co_list_t _co_list_t;
typedef struct _co_list_node_t _co_list_node_t;
struct _co_list_t {
_co_list_node_t *head;
_co_list_node_t *tail;
int len;
};
struct _co_list_node_t {
_co_list_node_t *prev;
_co_list_node_t *next;
void *value;
};
static _co_list_t *_co_list_create(void);
static void _co_list_free(_co_list_t *list);
zinline static int _co_list_len(const _co_list_t *list) { return list->len; }
zinline static _co_list_node_t *_co_list_head(const _co_list_t *list) { return list->head; }
zinline static _co_list_node_t *_co_list_tail(const _co_list_t *list) { return list->tail; }
zinline static _co_list_node_t *_co_list_node_next(const _co_list_node_t *node) { return node->next; }
zinline static _co_list_node_t *_co_list_node_prev(const _co_list_node_t *node) { return node->prev; }
zinline static void *_co_list_node_value(const _co_list_node_t *node) { return node->value; }
static void _co_list_attach_before(_co_list_t *list, _co_list_node_t *n, _co_list_node_t *before);
static void _co_list_detach(_co_list_t *list, _co_list_node_t *n);
static _co_list_node_t *_co_list_add_before(_co_list_t *list, const void *value, _co_list_node_t *before);
static zbool_t _co_list_delete(_co_list_t *list, _co_list_node_t *n, void **value);
zinline static _co_list_node_t *_co_list_push(_co_list_t *l,const void *v){return _co_list_add_before(l,v,0);}
zinline static _co_list_node_t *_co_list_unshift(_co_list_t *l,const void *v){return _co_list_add_before(l,v,l->head);}
zinline static zbool_t _co_list_pop(_co_list_t *l, void **v){return _co_list_delete(l,l->tail,v);}
zinline static zbool_t _co_list_shift(_co_list_t *l, void **v){return _co_list_delete(l,l->head,v);}
/* 宏, 遍历 */
#define ZCOROUTINE_LIST_WALK_BEGIN(list, var_your_type, var_your_ptr) { \
_co_list_node_t *list_current_node=(list)->head; var_your_type var_your_ptr; \
for(;list_current_node;list_current_node=list_current_node->next){ \
var_your_ptr = (var_your_type)(void *)(list_current_node->value);
#define ZCOROUTINE_LIST_WALK_END }}
static void _co_list_init(_co_list_t *list)
{
memset(list, 0, sizeof(_co_list_t));
}
static void _co_list_fini(_co_list_t *list)
{
_co_list_node_t *n, *next;
if (!list) {
return;
}
n = list->head;
for (; n; n = next) {
next = n->next;
_co_mem_free(n);
}
}
static _co_list_t *_co_list_create(void)
{
_co_list_t *list = _co_mem_malloc(sizeof(_co_list_t));
_co_list_init(list);
return (list);
}
static void _co_list_free(_co_list_t * list)
{
_co_list_fini(list);
_co_mem_free(list);
}
static void _co_list_attach_before(_co_list_t * list, _co_list_node_t * n, _co_list_node_t * before)
{
ZMLINK_ATTACH_BEFORE(list->head, list->tail, n, prev, next, before);
list->len++;
}
static void _co_list_detach(_co_list_t * list, _co_list_node_t * n)
{
ZMLINK_DETACH(list->head, list->tail, n, prev, next);
list->len--;
}
static _co_list_node_t *_co_list_add_before(_co_list_t * list, const void *value, _co_list_node_t * before)
{
_co_list_node_t *n;
n = (_co_list_node_t *) _co_mem_calloc(1, sizeof(_co_list_node_t));
n->value = (char *)value;
_co_list_attach_before(list, n, before);
return n;
}
static int _co_list_delete(_co_list_t * list, _co_list_node_t * n, void **value)
{
if (n == 0) {
return 0;
}
if (value) {
*value = n->value;
}
_co_list_detach(list, n);
_co_mem_free(n);
return 1;
}
/* }}} */
/* {{{ rbtree */
typedef struct _co_rbtree_node_t _co_rbtree_node_t;
typedef struct _co_rbtree_t _co_rbtree_t;
typedef int (*_co_rbtree_cmp_t) (_co_rbtree_node_t *node1, _co_rbtree_node_t *node2);
struct _co_rbtree_t {
_co_rbtree_node_t *_co_rbtree_node;
_co_rbtree_cmp_t cmp_fn;
};
struct _co_rbtree_node_t {
unsigned long ___co_rbtree_parent_color;
_co_rbtree_node_t *_co_rbtree_right;
_co_rbtree_node_t *_co_rbtree_left;
/* The alignment might seem pointless, but allegedly CRIS needs it */
} __attribute__ ((aligned(sizeof(long))));
zinline static int _co_rbtree_have_data(_co_rbtree_t *tree) { return ((tree)->_co_rbtree_node?1:0); }
static void _co_rbtree_init(_co_rbtree_t *tree, _co_rbtree_cmp_t cmp_fn);
static void _co_rbtree_insert_color(_co_rbtree_t *, _co_rbtree_node_t *);
static void _co_rbtree_erase(_co_rbtree_t *tree, _co_rbtree_node_t *node);
static _co_rbtree_node_t *_co_rbtree_next(const _co_rbtree_node_t *tree);
static _co_rbtree_node_t *_co_rbtree_first(const _co_rbtree_t *node);
static _co_rbtree_node_t *_co_rbtree_parent(const _co_rbtree_node_t *node);
static _co_rbtree_node_t *_co_rbtree_attach(_co_rbtree_t *tree, _co_rbtree_node_t *node);
static _co_rbtree_node_t *_co_rbtree_detach(_co_rbtree_t *tree, _co_rbtree_node_t *node);
static void _co_rbtree_link_node(_co_rbtree_node_t *node, _co_rbtree_node_t *parent, _co_rbtree_node_t **_co_rbtree_link);
#define ZCOROUTINE_RBTREE_PARENT(node) ((_co_rbtree_node_t *)((node)->___co_rbtree_parent_color & ~3))
static void ___co_rbtree_insert_augmented(_co_rbtree_t * root, _co_rbtree_node_t * node, void (*augment_rotate) (_co_rbtree_node_t * old, _co_rbtree_node_t * new_node));
static void ___co_rbtree_erase_color(_co_rbtree_t * root, _co_rbtree_node_t * parent, void (*augment_rotate) (_co_rbtree_node_t * old, _co_rbtree_node_t * new_node));
#define RB_RED 0
#define RB_BLACK 1
#define true 1
#define false 0
#define RB_ROOT (_co_rbtree_t) { NULL, }
#define _co_rbtree_entry(ptr, type, member) container_of(ptr, type, member)
#define RB_EMPTY_ROOT(root) ((root)->_co_rbtree_node == NULL)
/* 'empty' nodes are nodes that are known not to be inserted in an rbree */
#define RB_EMPTY_NODE(node) \
((node)->___co_rbtree_parent_color == (unsigned long)(node))
#define RB_CLEAR_NODE(node) \
((node)->___co_rbtree_parent_color = (unsigned long)(node))
struct _co_rbtree_augment_callbacks {
void (*propagate) (_co_rbtree_node_t * node, _co_rbtree_node_t * stop);
void (*copy) (_co_rbtree_node_t * old, _co_rbtree_node_t * new_node);
void (*rotate) (_co_rbtree_node_t * old, _co_rbtree_node_t * new_node);
};
zinline static void _co_rbtree_insert_augmented(_co_rbtree_node_t * node, _co_rbtree_t * root, const struct _co_rbtree_augment_callbacks *augment)
{
___co_rbtree_insert_augmented(root, node, augment->rotate);
}
#define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
rbtype, rbaugmented, rbcompute) \
zinline static void \
rbname ## _propagate(_co_rbtree_node_t *rb, _co_rbtree_node_t *stop) \
{ \
while (rb != stop) { \
rbstruct *node = _co_rbtree_entry(rb, rbstruct, rbfield); \
rbtype augmented = rbcompute(node); \
if (node->rbaugmented == augmented) \
break; \
node->rbaugmented = augmented; \
rb = ZCOROUTINE_RBTREE_PARENT(&node->rbfield); \
} \
} \
zinline static void \
rbname ## _copy(_co_rbtree_node_t *_co_rbtree_old, _co_rbtree_node_t *_co_rbtree_new) \
{ \
rbstruct *old = _co_rbtree_entry(_co_rbtree_old, rbstruct, rbfield); \
rbstruct *new_node = _co_rbtree_entry(_co_rbtree_new, rbstruct, rbfield); \
new_node->rbaugmented = old->rbaugmented; \
} \
static void \
rbname ## _rotate(_co_rbtree_node_t *_co_rbtree_old, _co_rbtree_node_t *_co_rbtree_new) \
{ \
rbstruct *old = _co_rbtree_entry(_co_rbtree_old, rbstruct, rbfield); \
rbstruct *new_node = _co_rbtree_entry(_co_rbtree_new, rbstruct, rbfield); \
new_node->rbaugmented = old->rbaugmented; \
old->rbaugmented = rbcompute(old); \
} \
static rbstatic const struct _co_rbtree_augment_callbacks rbname = { \
rbname ## _propagate, rbname ## _copy, rbname ## _rotate \
};
#define RB_RED 0
#define RB_BLACK 1
#define __ZCOROUTINE_RBTREE_PARENT(pc) ((_co_rbtree_node_t *)(pc & ~3))
#define ___co_rbtree_color(pc) ((pc) & 1)
#define ___co_rbtree_is_black(pc) ___co_rbtree_color(pc)
#define ___co_rbtree_is_red(pc) (!___co_rbtree_color(pc))
#define _co_rbtree_color(rb) ___co_rbtree_color((rb)->___co_rbtree_parent_color)
#define _co_rbtree_is_red(rb) ___co_rbtree_is_red((rb)->___co_rbtree_parent_color)
#define _co_rbtree_is_black(rb) ___co_rbtree_is_black((rb)->___co_rbtree_parent_color)
zinline static void _co_rbtree_set_parent(_co_rbtree_node_t * rb, _co_rbtree_node_t * p)
{
rb->___co_rbtree_parent_color = _co_rbtree_color(rb) | (unsigned long)p;
}
zinline static void _co_rbtree_set_parent_color(_co_rbtree_node_t * rb, _co_rbtree_node_t * p, int color)
{
rb->___co_rbtree_parent_color = (unsigned long)p | color;
}
zinline static void ___co_rbtree_change_child(_co_rbtree_node_t * old, _co_rbtree_node_t * new_node, _co_rbtree_node_t * parent, _co_rbtree_t * root)
{
if (parent) {
if (parent->_co_rbtree_left == old)
parent->_co_rbtree_left = new_node;
else
parent->_co_rbtree_right = new_node;
} else
root->_co_rbtree_node = new_node;
}
static void ___co_rbtree_erase_color(_co_rbtree_t * root, _co_rbtree_node_t * parent, void (*augment_rotate) (_co_rbtree_node_t * old, _co_rbtree_node_t * new_node));
zinline static _co_rbtree_node_t *___co_rbtree_erase_augmented(_co_rbtree_node_t * node, _co_rbtree_t * root, const struct _co_rbtree_augment_callbacks *augment)
{
_co_rbtree_node_t *child = node->_co_rbtree_right, *tmp = node->_co_rbtree_left;
_co_rbtree_node_t *parent, *rebalance;
unsigned long pc;
if (!tmp) {
/*
* Case 1: node to erase has no more than 1 child (easy!)
*
* Note that if there is one child it must be red due to 5)
* and node must be black due to 4). We adjust colors locally
* so as to bypass ___co_rbtree_erase_color() later on.
*/
pc = node->___co_rbtree_parent_color;
parent = __ZCOROUTINE_RBTREE_PARENT(pc);
___co_rbtree_change_child(node, child, parent, root);
if (child) {
child->___co_rbtree_parent_color = pc;
rebalance = NULL;
} else
rebalance = ___co_rbtree_is_black(pc) ? parent : NULL;
tmp = parent;
} else if (!child) {
/* Still case 1, but this time the child is node->_co_rbtree_left */
tmp->___co_rbtree_parent_color = pc = node->___co_rbtree_parent_color;
parent = __ZCOROUTINE_RBTREE_PARENT(pc);
___co_rbtree_change_child(node, tmp, parent, root);
rebalance = NULL;
tmp = parent;
} else {
_co_rbtree_node_t *successor = child, *child2;
tmp = child->_co_rbtree_left;
if (!tmp) {
/*
* Case 2: node's successor is its right child
*
* (n) (s)
* / \ / \
* (x) (s) -> (x) (c)
* \
* (c)
*/
parent = successor;
child2 = successor->_co_rbtree_right;
augment->copy(node, successor);
} else {
/*
* Case 3: node's successor is leftmost under
* node's right child subtree
*
* (n) (s)
* / \ / \
* (x) (y) -> (x) (y)
* / /
* (p) (p)
* / /
* (s) (c)
* \
* (c)
*/
do {
parent = successor;
successor = tmp;
tmp = tmp->_co_rbtree_left;
}
while (tmp);
parent->_co_rbtree_left = child2 = successor->_co_rbtree_right;
successor->_co_rbtree_right = child;
_co_rbtree_set_parent(child, successor);
augment->copy(node, successor);
augment->propagate(parent, successor);
}
successor->_co_rbtree_left = tmp = node->_co_rbtree_left;
_co_rbtree_set_parent(tmp, successor);
pc = node->___co_rbtree_parent_color;
tmp = __ZCOROUTINE_RBTREE_PARENT(pc);
___co_rbtree_change_child(node, successor, tmp, root);
if (child2) {
successor->___co_rbtree_parent_color = pc;
_co_rbtree_set_parent_color(child2, parent, RB_BLACK);
rebalance = NULL;
} else {
unsigned long pc2 = successor->___co_rbtree_parent_color;
successor->___co_rbtree_parent_color = pc;
rebalance = ___co_rbtree_is_black(pc2) ? parent : NULL;
}
tmp = successor;
}
augment->propagate(tmp, NULL);
return rebalance;
}
zinline static void _co_rbtree_erase_augmented(_co_rbtree_node_t * node, _co_rbtree_t * root, const struct _co_rbtree_augment_callbacks *augment)
{
_co_rbtree_node_t *rebalance = ___co_rbtree_erase_augmented(node, root, augment);
if (rebalance)
___co_rbtree_erase_color(root, rebalance, augment->rotate);
}
zinline static void _co_rbtree_set_black(_co_rbtree_node_t * rb)
{
rb->___co_rbtree_parent_color |= RB_BLACK;
}
zinline static _co_rbtree_node_t *_co_rbtree_red_parent(_co_rbtree_node_t * red)
{
return (_co_rbtree_node_t *) red->___co_rbtree_parent_color;
}
/*
* Helper function for rotations:
* - old's parent and color get assigned to new_node
* - old gets assigned new_node as a parent and 'color' as a color.
*/
zinline static void ___co_rbtree_rotate_set_parents(_co_rbtree_node_t * old, _co_rbtree_node_t * new_node, _co_rbtree_t * root, int color)
{
_co_rbtree_node_t *parent = ZCOROUTINE_RBTREE_PARENT(old);
new_node->___co_rbtree_parent_color = old->___co_rbtree_parent_color;
_co_rbtree_set_parent_color(old, new_node, color);
___co_rbtree_change_child(old, new_node, parent, root);
}
zinline static void ___co_rbtree_insert(_co_rbtree_node_t * node, _co_rbtree_t * root, void (*augment_rotate) (_co_rbtree_node_t * old, _co_rbtree_node_t * new_node))
{
_co_rbtree_node_t *parent = _co_rbtree_red_parent(node), *gparent, *tmp;
while (true) {
/*
* Loop invariant: node is red
*
* If there is a black parent, we are done.
* Otherwise, take some corrective action as we don't
* want a red root or two consecutive red nodes.
*/
if (!parent) {
_co_rbtree_set_parent_color(node, NULL, RB_BLACK);
break;
} else if (_co_rbtree_is_black(parent))
break;
gparent = _co_rbtree_red_parent(parent);
tmp = gparent->_co_rbtree_right;
if (parent != tmp) { /* parent == gparent->_co_rbtree_left */
if (tmp && _co_rbtree_is_red(tmp)) {
/*
* Case 1 - color flips
*
* G g
* / \ / \
* p u --> P U
* / /
* n N
*
* However, since g's parent might be red, and
* 4) does not allow this, we need to recurse