| 1 |
|
| /* hm_workerhook_perl.h - the Perl door onto the v4 on_worker_start registry. |
| 2 |
|
| * |
| 3 |
|
| * hm_workerhook.h holds C function pointers, which is what an XS consumer |
| 4 |
|
| * wants. This registers a SHIM into that same table whose user data is a Perl |
| 5 |
|
| * coderef, so an ordinary application can run code in each worker after the |
| 6 |
|
| * fork: |
| 7 |
|
| * |
| 8 |
|
| * Hyperman->on_worker_start(sub { $dbh = DBI->connect(...) }); |
| 9 |
|
| * Hyperman->run(app => $app, workers => 4); |
| 10 |
|
| * |
| 11 |
|
| * A prefork server needs that and PSGI has no standard for it: anything |
| 12 |
|
| * holding a file descriptor - a database handle, a cache connection, a |
| 13 |
|
| * seeded RNG - is wrong in a child that inherited it. |
| 14 |
|
| * |
| 15 |
|
| * Register BEFORE run(). The registry is walked in the child once its loop |
| 16 |
|
| * exists and before the loop starts turning, so a callback registered after |
| 17 |
|
| * run() has forked never fires in the workers already running. |
| 18 |
|
| * |
| 19 |
|
| * The coderef survives the fork for free: it lives in the parent interpreter, |
| 20 |
|
| * which is what a child gets a copy of. That is why this can hold an SV where |
| 21 |
|
| * hm_workerhook.h deliberately does not. |
| 22 |
|
| * |
| 23 |
|
| * The shim runs under G_EVAL and turns a death into a warning. A die |
| 24 |
|
| * propagating out of here would come up through hm_worker and take the whole |
| 25 |
|
| * worker with it before it had served anything; a worker that could not run |
| 26 |
|
| * your setup code is still a worker. |
| 27 |
|
| * |
| 28 |
|
| * Needs hm_workerhook.h. Included after it. |
| 29 |
|
| */ |
| 30 |
|
| |
| 31 |
|
| #ifndef HM_WORKERHOOK_PERL_H |
| 32 |
|
| #define HM_WORKERHOOK_PERL_H |
| 33 |
|
| |
| 34 |
|
| #include "hm_workerhook.h" |
| 35 |
|
| |
| 36 |
|
| typedef struct { SV *cb; } hm_worker_perl; |
| 37 |
|
| |
| 38 |
|
| /* The shim. Called with no arguments: the callback that wants this worker's |
| 39 |
|
| * loop asks Hyperman->loop for it, and the one that wants the pid has $$. |
| 40 |
|
| * Passing the raw loop pointer would mean blessing something whose only |
| 41 |
|
| * documented use is a C entry point. */ |
| 42 |
6 |
| static void hm_worker_perl_cb(pTHX_ void *loop, void *ud) { |
| 43 |
6 |
| hm_worker_perl *p = (hm_worker_perl *)ud; |
| 44 |
6 |
| dSP; |
| 45 |
|
| PERL_UNUSED_ARG(loop); |
| 46 |
|
| /* |
| 47 |
|
| # uncoverable branch true class:ignore_covered_err |
| 48 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 49 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 50 |
6 |
▶ | if (!p || !p->cb) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!p || !p->cb) return; |
6 |
0 |
6 |
| if (!p || !p->cb) return; |
0 |
6 |
6 |
|
| 51 |
6 |
| ENTER; SAVETMPS; |
| 52 |
6 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
6 |
6 |
|
| 53 |
6 |
| PUTBACK; |
| 54 |
6 |
| call_sv(p->cb, G_DISCARD | G_EVAL); |
| 55 |
6 |
▶ | SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
0 |
6 |
6 |
|
| 56 |
6 |
▶ | if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */ |
6 |
0 |
6 |
| if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */ |
1 |
5 |
6 |
|
| 57 |
2 |
▶ | warn("Hyperman: on_worker_start callback died: %s " |
Branch: 1 branch
| Branch | True | False | Total |
| warn("Hyperman: on_worker_start callback died: %s " |
1 |
0 |
1 |
|
| 58 |
|
| "(this worker is still serving)", SvPV_nolen(ERRSV)); |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
|
| /* Hyperman->on_worker_start(\&cb). Returns 1, or 0 when the table is full |
| 62 |
|
| * (HM_ABI_MAX_WORKER_CB). A non-coderef croaks here, in the caller's own |
| 63 |
|
| * frame, which is long before any worker exists to be confused by it. */ |
| 64 |
6 |
| static int hm_worker_hook_add_perl(pTHX_ SV *cb) { |
| 65 |
|
| hm_worker_perl *p; |
| 66 |
|
| /* |
| 67 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 68 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 69 |
6 |
▶ | if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) |
6 |
0 |
6 |
| if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) |
4 |
2 |
6 |
| if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) |
0 |
4 |
4 |
|
| 70 |
2 |
| croak("Hyperman->on_worker_start: expects a code reference"); |
| 71 |
4 |
| Newxz(p, 1, hm_worker_perl); |
| 72 |
4 |
| p->cb = newSVsv(cb); |
| 73 |
|
| /* |
| 74 |
|
| # uncoverable branch true |
| 75 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 76 |
4 |
▶ | if (!hm_worker_hook_add(aTHX_ hm_worker_perl_cb, p)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hm_worker_hook_add(aTHX_ hm_worker_perl_cb, p)) { |
0 |
4 |
4 |
|
| 77 |
0 |
| SvREFCNT_dec(p->cb); /* # uncoverable statement */ |
| 78 |
0 |
| Safefree(p); /* # uncoverable statement */ |
| 79 |
0 |
| return 0; /* # uncoverable statement */ |
| 80 |
|
| } |
| 81 |
4 |
| return 1; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
|
| #endif /* HM_WORKERHOOK_PERL_H */ |