… | |
… | |
3 | |
3 | |
4 | #include "EXTERN.h" |
4 | #include "EXTERN.h" |
5 | #include "perl.h" |
5 | #include "perl.h" |
6 | #include "XSUB.h" |
6 | #include "XSUB.h" |
7 | |
7 | |
8 | /* perl-related */ |
8 | #ifndef pTHX_ |
9 | #define TRANSFER_SAVE_DEFAV 0x00000001 |
9 | # define pTHX_ |
10 | #define TRANSFER_SAVE_DEFSV 0x00000002 |
10 | # define aTHX_ |
11 | #define TRANSFER_SAVE_ERRSV 0x00000004 |
11 | # define pTHX |
12 | /* c-related */ |
12 | # define aTHX |
13 | #define TRANSFER_SAVE_CCTXT 0x00000008 |
|
|
14 | #ifdef CORO_LAZY_STACK |
|
|
15 | # define TRANSFER_LAZY_STACK 0x00000010 |
|
|
16 | #else |
|
|
17 | # define TRANSFER_LAZY_STACK 0x00000000 |
|
|
18 | #endif |
13 | #endif |
19 | |
14 | |
20 | #define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \ |
15 | /* perl-related */ |
21 | |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT) |
16 | #define TRANSFER_SAVE_DEFAV 0x00000001 /* @_ */ |
|
|
17 | #define TRANSFER_SAVE_DEFSV 0x00000002 /* $_ */ |
|
|
18 | #define TRANSFER_SAVE_ERRSV 0x00000004 /* $@ */ |
22 | |
19 | |
23 | struct coro; /* opaque */ |
20 | #define TRANSFER_SAVE_ALL ( TRANSFER_SAVE_DEFAV \ |
|
|
21 | | TRANSFER_SAVE_DEFSV \ |
|
|
22 | | TRANSFER_SAVE_ERRSV ) |
24 | |
23 | |
|
|
24 | /*struct coro;*/ /* opaque */ |
|
|
25 | |
|
|
26 | /* private structure, always use the provided macros below */ |
25 | struct CoroAPI { |
27 | struct CoroAPI { |
26 | I32 ver; |
28 | I32 ver; |
27 | #define CORO_API_VERSION 1 |
29 | #define CORO_API_VERSION 2 |
|
|
30 | #define CORO_API_REVISION 0 |
28 | |
31 | |
29 | /* internal */ |
32 | /* internal */ |
30 | /*struct coro *(*sv_to_coro)(SV *arg, const char *funcname, const char *varname);*/ |
33 | /*struct coro *(*sv_to_coro)(SV *arg, const char *funcname, const char *varname);*/ |
31 | |
34 | |
32 | /* public, state */ |
35 | /* public API, Coro::State */ |
33 | void (*transfer)(pTHX_ SV *prev, SV *next, int flags); |
36 | void (*transfer) (SV *prev, SV *next, int flags); |
34 | |
37 | |
35 | /* public, coro */ |
38 | /* public API, Coro */ |
36 | void (*schedule)(int cede); |
39 | void (*schedule) (void); |
|
|
40 | void (*cede) (void); |
37 | void (*ready)(SV *sv); |
41 | int (*ready) (SV *coro_sv); |
|
|
42 | int (*is_ready) (SV *coro_sv); |
38 | int *nready; |
43 | int *nready; |
39 | GV *current; |
44 | SV *current; |
40 | }; |
45 | }; |
41 | |
46 | |
42 | static struct CoroAPI *GCoroAPI; |
47 | static struct CoroAPI *GCoroAPI; |
43 | |
48 | |
44 | #define CORO_TRANSFER(prev,next) GCoroAPI->transfer(aTHX_ (prev),(next)) |
49 | #define CORO_TRANSFER(prev,next,flags) GCoroAPI->transfer (aTHX_ (prev), (next), (flags)) |
45 | #define CORO_SCHEDULE GCoroAPI->schedule(0) |
50 | #define CORO_SCHEDULE GCoroAPI->schedule () |
46 | #define CORO_CEDE GCoroAPI->schedule(1) |
51 | #define CORO_CEDE GCoroAPI->cede () |
47 | #define CORO_READY(coro) GCoroAPI->ready(coro) |
52 | #define CORO_READY(coro) GCoroAPI->ready (coro) |
|
|
53 | #define CORO_IS_READY(coro) GCoroAPI->is_ready (coro) |
48 | #define CORO_NREADY (*GCoroAPI->nready) |
54 | #define CORO_NREADY (*GCoroAPI->nready) |
49 | #define CORO_CURRENT GvSV(GCoroAPI->current) |
55 | #define CORO_CURRENT SvRV (GCoroAPI->current) |
50 | |
56 | |
51 | #define I_CORO_API(YourName) \ |
57 | #define I_CORO_API(YourName) \ |
52 | STMT_START { \ |
58 | STMT_START { \ |
53 | SV *sv = perl_get_sv("Coro::API",0); \ |
59 | SV *sv = perl_get_sv("Coro::API",0); \ |
54 | if (!sv) croak("Coro::API not found"); \ |
60 | if (!sv) croak("Coro::API not found"); \ |