1 |
root |
1.1 |
#ifndef CORO_API_H |
2 |
|
|
#define CORO_API_H |
3 |
|
|
|
4 |
|
|
#include "EXTERN.h" |
5 |
|
|
#include "perl.h" |
6 |
|
|
#include "XSUB.h" |
7 |
|
|
|
8 |
pcg |
1.5 |
#ifndef pTHX_ |
9 |
|
|
# define pTHX_ |
10 |
|
|
# define aTHX_ |
11 |
|
|
# define pTHX |
12 |
|
|
# define aTHX |
13 |
|
|
#endif |
14 |
|
|
|
15 |
root |
1.21 |
/* C-level coroutine struct, opaque, not used much */ |
16 |
|
|
struct coro; |
17 |
root |
1.1 |
|
18 |
root |
1.21 |
/* used for schedule-like-function prepares */ |
19 |
|
|
struct coro_transfer_args |
20 |
|
|
{ |
21 |
|
|
struct coro *prev, *next; |
22 |
root |
1.19 |
}; |
23 |
|
|
|
24 |
root |
1.22 |
/* this is the per-perl-coro slf frame info */ |
25 |
|
|
/* it is treated like other "global" interpreter data */ |
26 |
|
|
/* and unfortunately is copied around, so keep it small */ |
27 |
root |
1.19 |
struct CoroSLF |
28 |
|
|
{ |
29 |
root |
1.23 |
void (*prepare) (pTHX_ struct coro_transfer_args *ta); /* 0 means not yet initialised */ |
30 |
root |
1.22 |
int (*check) (pTHX_ struct CoroSLF *frame); |
31 |
root |
1.29 |
void *data; /* for use by prepare/check/destroy */ |
32 |
|
|
void (*destroy) (pTHX_ struct coro *coro); |
33 |
root |
1.19 |
}; |
34 |
|
|
|
35 |
root |
1.22 |
/* needs to fill in the *frame */ |
36 |
root |
1.24 |
typedef void (*coro_slf_cb) (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items); |
37 |
root |
1.22 |
|
38 |
root |
1.10 |
/* private structure, always use the provided macros below */ |
39 |
root |
1.19 |
struct CoroAPI |
40 |
|
|
{ |
41 |
root |
1.28 |
/* private */ |
42 |
root |
1.1 |
I32 ver; |
43 |
root |
1.15 |
I32 rev; |
44 |
root |
1.29 |
#define CORO_API_VERSION 7 /* reorder CoroSLF on change */ |
45 |
|
|
#define CORO_API_REVISION 1 |
46 |
root |
1.19 |
|
47 |
root |
1.20 |
/* Coro */ |
48 |
|
|
int nready; |
49 |
|
|
SV *current; |
50 |
root |
1.26 |
SV *except; |
51 |
root |
1.20 |
void (*readyhook) (void); |
52 |
|
|
|
53 |
|
|
void (*schedule) (pTHX); |
54 |
root |
1.27 |
void (*schedule_to) (pTHX_ SV *coro_sv); |
55 |
root |
1.19 |
int (*cede) (pTHX); |
56 |
|
|
int (*cede_notself) (pTHX); |
57 |
|
|
int (*ready) (pTHX_ SV *coro_sv); |
58 |
|
|
int (*is_ready) (pTHX_ SV *coro_sv); |
59 |
root |
1.18 |
|
60 |
root |
1.20 |
/* Coro::State */ |
61 |
|
|
void (*transfer) (pTHX_ SV *prev_sv, SV *next_sv); /* Coro::State */ |
62 |
root |
1.22 |
|
63 |
|
|
/* SLF */ |
64 |
root |
1.21 |
struct coro *(*sv_state) (pTHX_ SV *coro); |
65 |
root |
1.25 |
void (*execute_slf) (pTHX_ CV *cv, coro_slf_cb init_cb, I32 ax); |
66 |
root |
1.28 |
|
67 |
|
|
/* public */ |
68 |
root |
1.22 |
/* for use as CoroSLF.prepare */ |
69 |
root |
1.23 |
void (*prepare_nop) (pTHX_ struct coro_transfer_args *ta); |
70 |
|
|
void (*prepare_schedule) (pTHX_ struct coro_transfer_args *ta); |
71 |
|
|
void (*prepare_cede) (pTHX_ struct coro_transfer_args *ta); |
72 |
|
|
void (*prepare_cede_notself) (pTHX_ struct coro_transfer_args *ta); |
73 |
root |
1.1 |
}; |
74 |
|
|
|
75 |
|
|
static struct CoroAPI *GCoroAPI; |
76 |
|
|
|
77 |
root |
1.11 |
/* public API macros */ |
78 |
|
|
#define CORO_TRANSFER(prev,next) GCoroAPI->transfer (aTHX_ (prev), (next)) |
79 |
root |
1.22 |
|
80 |
|
|
#define CORO_SV_STATE(coro) GCoroAPI->sv_state (aTHX_ (coro)) |
81 |
root |
1.25 |
#define CORO_EXECUTE_SLF(cv,init,ax) GCoroAPI->execute_slf (aTHX_ (cv), (init), (ax)) |
82 |
|
|
#define CORO_EXECUTE_SLF_XS(init) CORO_EXECUTE_SLF (cv, (init), ax) |
83 |
root |
1.22 |
|
84 |
root |
1.19 |
#define CORO_SCHEDULE GCoroAPI->schedule (aTHX) |
85 |
|
|
#define CORO_CEDE GCoroAPI->cede (aTHX) |
86 |
|
|
#define CORO_CEDE_NOTSELF GCoroAPI->cede_notself (aTHX) |
87 |
|
|
#define CORO_READY(coro) GCoroAPI->ready (aTHX_ coro) |
88 |
root |
1.11 |
#define CORO_IS_READY(coro) GCoroAPI->is_ready (coro) |
89 |
root |
1.26 |
#define CORO_NREADY (GCoroAPI->nready) |
90 |
|
|
#define CORO_THROW (GCoroAPI->except) |
91 |
root |
1.22 |
#define CORO_CURRENT SvRV (GCoroAPI->current) |
92 |
root |
1.26 |
#define CORO_READYHOOK (GCoroAPI->readyhook) |
93 |
root |
1.21 |
|
94 |
root |
1.17 |
#define I_CORO_API(YourName) \ |
95 |
|
|
STMT_START { \ |
96 |
|
|
SV *sv = perl_get_sv ("Coro::API", 0); \ |
97 |
|
|
if (!sv) croak ("Coro::API not found"); \ |
98 |
|
|
GCoroAPI = (struct CoroAPI*) SvIV (sv); \ |
99 |
|
|
if (GCoroAPI->ver != CORO_API_VERSION \ |
100 |
|
|
|| GCoroAPI->rev < CORO_API_REVISION) \ |
101 |
root |
1.16 |
croak ("Coro::API version mismatch (%d.%d vs. %d.%d) -- please recompile %s", \ |
102 |
|
|
GCoroAPI->ver, GCoroAPI->rev, CORO_API_VERSION, CORO_API_REVISION, YourName); \ |
103 |
root |
1.1 |
} STMT_END |
104 |
|
|
|
105 |
|
|
#endif |
106 |
|
|
|