… | |
… | |
10 | # define aTHX_ |
10 | # define aTHX_ |
11 | # define pTHX |
11 | # define pTHX |
12 | # define aTHX |
12 | # define aTHX |
13 | #endif |
13 | #endif |
14 | |
14 | |
15 | /*struct coro;*/ /* opaque */ |
15 | /* C-level coroutine struct, opaque, not used much */ |
|
|
16 | struct coro; |
16 | |
17 | |
17 | enum { |
18 | /* used for schedule-like-function prepares */ |
18 | CORO_SLF_SCHEDULE = 3, |
19 | struct coro_transfer_args |
19 | CORO_SLF_CEDE = 4, |
20 | { |
20 | CORO_SLF_CEDE_NOTSELF = 5 |
21 | struct coro *prev, *next; |
21 | }; |
22 | }; |
22 | |
23 | |
23 | struct CoroSLF |
24 | struct CoroSLF |
24 | { |
25 | { |
25 | int (*prepare) (pTHX_ SV **arg, int items); /* returns CORO_SLF_* */ |
26 | void (*init) (pTHX_ SV **arg, int items); /* returns CORO_SLF_* */ |
|
|
27 | void (*prepare) (struct coro_transfer_args *ta); |
26 | int (*check) (pTHX); /* returns repeat-flag */ |
28 | int (*check) (pTHX); /* returns repeat-flag, may be zero */ |
27 | }; |
29 | }; |
28 | |
30 | |
29 | /* private structure, always use the provided macros below */ |
31 | /* private structure, always use the provided macros below */ |
30 | struct CoroAPI |
32 | struct CoroAPI |
31 | { |
33 | { |
… | |
… | |
46 | int (*is_ready) (pTHX_ SV *coro_sv); |
48 | int (*is_ready) (pTHX_ SV *coro_sv); |
47 | |
49 | |
48 | /* Coro::State */ |
50 | /* Coro::State */ |
49 | void (*transfer) (pTHX_ SV *prev_sv, SV *next_sv); /* Coro::State */ |
51 | void (*transfer) (pTHX_ SV *prev_sv, SV *next_sv); /* Coro::State */ |
50 | void (*execute_slf) (pTHX_ CV *cv, const struct CoroSLF *slf, SV **arg, int nitems); |
52 | void (*execute_slf) (pTHX_ CV *cv, const struct CoroSLF *slf, SV **arg, int nitems); |
51 | |
53 | struct coro *(*sv_state) (pTHX_ SV *coro); |
|
|
54 | void *slf_data; |
52 | }; |
55 | }; |
53 | |
56 | |
54 | static struct CoroAPI *GCoroAPI; |
57 | static struct CoroAPI *GCoroAPI; |
55 | |
58 | |
56 | /* public API macros */ |
59 | /* public API macros */ |
… | |
… | |
58 | #define CORO_SCHEDULE GCoroAPI->schedule (aTHX) |
61 | #define CORO_SCHEDULE GCoroAPI->schedule (aTHX) |
59 | #define CORO_CEDE GCoroAPI->cede (aTHX) |
62 | #define CORO_CEDE GCoroAPI->cede (aTHX) |
60 | #define CORO_CEDE_NOTSELF GCoroAPI->cede_notself (aTHX) |
63 | #define CORO_CEDE_NOTSELF GCoroAPI->cede_notself (aTHX) |
61 | #define CORO_READY(coro) GCoroAPI->ready (aTHX_ coro) |
64 | #define CORO_READY(coro) GCoroAPI->ready (aTHX_ coro) |
62 | #define CORO_IS_READY(coro) GCoroAPI->is_ready (coro) |
65 | #define CORO_IS_READY(coro) GCoroAPI->is_ready (coro) |
63 | #define CORO_NREADY GCoroAPI->nready |
66 | #define CORO_NREADY (GCoroAPI->nready) |
64 | #define CORO_CURRENT SvRV (GCoroAPI->current) |
67 | #define CORO_CURRENT (SvRV (GCoroAPI->current)) |
65 | #define CORO_READYHOOK GCoroAPI->readyhook |
68 | #define CORO_READYHOOK (GCoroAPI->readyhook) |
66 | |
69 | |
67 | #define CORO_EXECUTE_SLF(cv,slf,arg,nitems) GCoroAPI->execute_slf (aTHX_ (cv), &(slf), (arg), (nitems)) |
70 | #define CORO_EXECUTE_SLF(cv,slf,arg,nitems) GCoroAPI->execute_slf (aTHX_ (cv), &(slf), (arg), (nitems)) |
68 | #define CORO_EXECUTE_SLF_XS(slf) CORO_EXECUTE_SLF (cv, (slf), &ST (0), nitems) |
71 | #define CORO_EXECUTE_SLF_XS(slf) CORO_EXECUTE_SLF (cv, (slf), &ST (0), nitems) |
|
|
72 | |
|
|
73 | #define CORO_SV_STATE(coro) GCoroAPI->sv_state (aTHX_ (coro)) |
|
|
74 | |
|
|
75 | #define CORO_SLF_DATA (GCoroAPI->slf_data) |
69 | |
76 | |
70 | #define I_CORO_API(YourName) \ |
77 | #define I_CORO_API(YourName) \ |
71 | STMT_START { \ |
78 | STMT_START { \ |
72 | SV *sv = perl_get_sv ("Coro::API", 0); \ |
79 | SV *sv = perl_get_sv ("Coro::API", 0); \ |
73 | if (!sv) croak ("Coro::API not found"); \ |
80 | if (!sv) croak ("Coro::API not found"); \ |