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