| 1 |
|
| #ifndef DBIL_TXN_H |
| 2 |
|
| #define DBIL_TXN_H |
| 3 |
|
| |
| 4 |
|
| /* Transactions, pinned to one pool slot, orchestrated in C. |
| 5 |
|
| * |
| 6 |
|
| * $db->txn(sub { my $tx = shift; ...; return $value_or_future }) |
| 7 |
|
| * |
| 8 |
|
| * acquire a slot (waits when the pool is saturated) -> BEGIN -> run the block |
| 9 |
|
| * with a DBIx::Loop::Txn handle whose query/do are pinned to that slot -> |
| 10 |
|
| * COMMIT on success / ROLLBACK on a die or a failed block-future -> release |
| 11 |
|
| * the slot. Returns a future of the block's result. The whole chain is C |
| 12 |
|
| * closures registered as on_ready callbacks; nothing blocks the loop. |
| 13 |
|
| * |
| 14 |
|
| * Two txns run concurrently on different slots. A txn() inside a block is an |
| 15 |
|
| * INDEPENDENT transaction on another slot (document: do not await one while |
| 16 |
|
| * the pool is exhausted by its parents). A worker death mid-txn fails the |
| 17 |
|
| * transaction (generation check) - it is never silently resumed on the |
| 18 |
|
| * respawned connection. |
| 19 |
|
| * |
| 20 |
|
| * Depends on dbil_pool.h (slot checkout) and dbil_future.h. */ |
| 21 |
|
| |
| 22 |
|
| /* capture AV layout shared by the stage closures */ |
| 23 |
|
| enum { |
| 24 |
|
| DBIL_TC_SELF = 0, /* the DBIx::Loop object (+1) */ |
| 25 |
|
| DBIL_TC_BLOCK, /* the user block CV (+1) */ |
| 26 |
|
| DBIL_TC_OUTER, /* the future txn() returned (+1) */ |
| 27 |
|
| DBIL_TC_TX, /* the DBIx::Loop::Txn handle (+1) */ |
| 28 |
|
| DBIL_TC_POOL, /* pool pointer IV */ |
| 29 |
|
| DBIL_TC_RESULT, /* block result AV-ref once known */ |
| 30 |
|
| DBIL_TC_ERROR /* error SV once known */ |
| 31 |
|
| }; |
| 32 |
|
| |
| 33 |
|
| /* register a C-closure coderef as on_ready of a DBIx::Loop::Future from C */ |
| 34 |
53 |
| static void dbil_txn_on_ready(pTHX_ SV *future, XSUBADDR_t body, AV *cap) { |
| 35 |
53 |
| dbil_future *f = dbil_future_of(aTHX_ future); |
| 36 |
53 |
| SV *cb = dbil_closure(aTHX_ body, cap); |
| 37 |
53 |
▶ | if (f->state) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->state) { |
12 |
41 |
53 |
|
| 38 |
12 |
| dSP; |
| 39 |
12 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
12 |
12 |
|
| 40 |
12 |
▶ | EXTEND(SP, 1); PUSHs(future); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); PUSHs(future); PUTBACK; |
0 |
12 |
12 |
|
| 41 |
12 |
| call_sv(cb, G_DISCARD | G_EVAL); |
| 42 |
12 |
▶ | if (SvTRUE(ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) |
12 |
0 |
12 |
| if (SvTRUE(ERRSV)) |
0 |
12 |
12 |
|
| 43 |
0 |
▶ | warn("DBIx::Loop::txn: stage callback died: %s", SvPV_nolen(ERRSV)); |
Branch: 1 branch
| Branch | True | False | Total |
| warn("DBIx::Loop::txn: stage callback died: %s", SvPV_nolen(ERRSV)); |
0 |
0 |
0 |
|
| 44 |
12 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
12 |
12 |
|
| 45 |
12 |
| SvREFCNT_dec(cb); |
| 46 |
|
| } else { |
| 47 |
41 |
| av_push(f->cbs, cb); |
| 48 |
|
| } |
| 49 |
53 |
| } |
| 50 |
|
| |
| 51 |
|
| /* freeze one request tuple for the wire (mortal bytes, or NULL) */ |
| 52 |
28 |
| static SV *dbil_txn_freeze_req(pTHX_ int is_query, const char *sql, AV *bind) { |
| 53 |
28 |
| AV *req = newAV(); |
| 54 |
28 |
| av_push(req, newSViv(is_query)); |
| 55 |
28 |
| av_push(req, newSVpv(sql, 0)); |
| 56 |
28 |
▶ | av_push(req, bind ? newRV_inc((SV *)bind) : newRV_noinc((SV *)newAV())); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(req, bind ? newRV_inc((SV *)bind) : newRV_noinc((SV *)newAV())); |
0 |
28 |
28 |
|
| 57 |
28 |
| return dbil_freeze(aTHX_ sv_2mortal(newRV_noinc((SV *)req))); |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
|
| /* tx handle fields */ |
| 61 |
133 |
| static IV dbil_tx_iv(pTHX_ SV *tx_rv, const char *k) { |
| 62 |
133 |
| HV *h = (HV *)SvRV(tx_rv); |
| 63 |
133 |
| SV **e = hv_fetch(h, k, (I32)strlen(k), 0); |
| 64 |
133 |
▶ | return (e && *e && SvIOK(*e)) ? SvIV(*e) : -1; |
Branch: 3 branches
| Branch | True | False | Total |
| return (e && *e && SvIOK(*e)) ? SvIV(*e) : -1; |
133 |
0 |
133 |
| return (e && *e && SvIOK(*e)) ? SvIV(*e) : -1; |
133 |
0 |
133 |
| return (e && *e && SvIOK(*e)) ? SvIV(*e) : -1; |
133 |
0 |
133 |
|
| 65 |
|
| } |
| 66 |
56 |
| static void dbil_tx_set_iv(pTHX_ SV *tx_rv, const char *k, IV v) { |
| 67 |
56 |
| (void)hv_store((HV *)SvRV(tx_rv), k, (I32)strlen(k), newSViv(v), 0); |
| 68 |
56 |
| } |
| 69 |
|
| |
| 70 |
|
| /* release the slot and mark the handle finished */ |
| 71 |
14 |
| static void dbil_txn_finish(pTHX_ AV *cap) { |
| 72 |
14 |
| dbil_pool *p = INT2PTR(dbil_pool *, SvIV(*av_fetch(cap, DBIL_TC_POOL, 0))); |
| 73 |
14 |
| SV *tx = *av_fetch(cap, DBIL_TC_TX, 0); |
| 74 |
14 |
| IV wi = dbil_tx_iv(aTHX_ tx, "wi"); |
| 75 |
14 |
| dbil_tx_set_iv(aTHX_ tx, "active", 0); |
| 76 |
14 |
▶ | if (wi >= 0) dbil_pool_release(aTHX_ p, (int)wi); |
Branch: 1 branch
| Branch | True | False | Total |
| if (wi >= 0) dbil_pool_release(aTHX_ p, (int)wi); |
14 |
0 |
14 |
|
| 77 |
14 |
| } |
| 78 |
|
| |
| 79 |
|
| /* stage 4: COMMIT (or ROLLBACK) completed - settle the outer future */ |
| 80 |
|
| XS_INTERNAL(dbil_txn_commit_cb); |
| 81 |
14 |
| XS_INTERNAL(dbil_txn_commit_cb) { |
| 82 |
14 |
| dXSARGS; |
| 83 |
14 |
| AV *cap = dbil_clos_cap(aTHX_ cv); |
| 84 |
|
| SV *fut, *outer, *errv; |
| 85 |
14 |
▶ | if (!cap || items < 1) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cap || items < 1) XSRETURN_EMPTY; |
14 |
0 |
14 |
| if (!cap || items < 1) XSRETURN_EMPTY; |
0 |
14 |
14 |
|
| 86 |
14 |
| fut = ST(0); |
| 87 |
14 |
| outer = *av_fetch(cap, DBIL_TC_OUTER, 0); |
| 88 |
14 |
| errv = *av_fetch(cap, DBIL_TC_ERROR, 0); |
| 89 |
14 |
| dbil_txn_finish(aTHX_ cap); |
| 90 |
14 |
▶ | if (errv && SvOK(errv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (errv && SvOK(errv)) { |
14 |
0 |
14 |
| if (errv && SvOK(errv)) { |
3 |
11 |
14 |
|
| 91 |
|
| /* this was the ROLLBACK completing: fail with the stored error */ |
| 92 |
3 |
| dbil_future_settle_fail(aTHX_ outer, errv); |
| 93 |
|
| } |
| 94 |
11 |
▶ | else if (dbil_future_of(aTHX_ fut)->state == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (dbil_future_of(aTHX_ fut)->state == 2) { |
0 |
11 |
11 |
|
| 95 |
0 |
| dbil_future_settle_fail(aTHX_ outer, |
| 96 |
0 |
▶ | dbil_future_of(aTHX_ fut)->error |
Branch: 1 branch
| Branch | True | False | Total |
| dbil_future_of(aTHX_ fut)->error |
0 |
0 |
0 |
|
| 97 |
0 |
| ? dbil_future_of(aTHX_ fut)->error |
| 98 |
0 |
| : sv_2mortal(newSVpvs("commit failed"))); |
| 99 |
|
| } |
| 100 |
|
| else { |
| 101 |
11 |
| SV *res = *av_fetch(cap, DBIL_TC_RESULT, 0); |
| 102 |
22 |
▶ | if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVAV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVAV) { |
11 |
0 |
11 |
| if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVAV) { |
11 |
0 |
11 |
| if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVAV) { |
11 |
0 |
11 |
|
| 103 |
|
| /* settle done with the block's (list) result */ |
| 104 |
11 |
| AV *ra = (AV *)SvRV(res); |
| 105 |
11 |
| dbil_future *of = dbil_future_of(aTHX_ outer); |
| 106 |
11 |
▶ | if (!of->state) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!of->state) { |
11 |
0 |
11 |
|
| 107 |
11 |
| of->result = newRV_inc((SV *)ra); |
| 108 |
11 |
| of->state = 1; |
| 109 |
11 |
| dbil_future_fire(aTHX_ outer, of); |
| 110 |
|
| } |
| 111 |
|
| } else { |
| 112 |
0 |
| dbil_future_settle_done1(aTHX_ outer, &PL_sv_undef); |
| 113 |
|
| } |
| 114 |
|
| } |
| 115 |
14 |
| XSRETURN_EMPTY; |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
|
| /* send COMMIT or ROLLBACK on the pinned slot, then finish via commit_cb */ |
| 119 |
14 |
| static void dbil_txn_end(pTHX_ AV *cap, const char *sql, SV *errv) { |
| 120 |
14 |
| dbil_pool *p = INT2PTR(dbil_pool *, SvIV(*av_fetch(cap, DBIL_TC_POOL, 0))); |
| 121 |
14 |
| SV *tx = *av_fetch(cap, DBIL_TC_TX, 0); |
| 122 |
14 |
| IV wi = dbil_tx_iv(aTHX_ tx, "wi"); |
| 123 |
14 |
| IV gen = dbil_tx_iv(aTHX_ tx, "gen"); |
| 124 |
14 |
| SV *bytes = dbil_txn_freeze_req(aTHX_ 0, sql, NULL); |
| 125 |
14 |
| SV *ef = dbil_future_new(aTHX_ "DBIx::Loop::Future"); |
| 126 |
14 |
▶ | if (errv && SvOK(errv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (errv && SvOK(errv)) |
3 |
11 |
14 |
| if (errv && SvOK(errv)) |
3 |
0 |
3 |
|
| 127 |
3 |
| av_store(cap, DBIL_TC_ERROR, newSVsv(errv)); |
| 128 |
14 |
▶ | if (!bytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bytes) { |
0 |
14 |
14 |
|
| 129 |
0 |
| dbil_txn_finish(aTHX_ cap); |
| 130 |
0 |
| dbil_future_settle_fail(aTHX_ *av_fetch(cap, DBIL_TC_OUTER, 0), |
| 131 |
|
| sv_2mortal(newSVpvs("could not serialise txn end"))); |
| 132 |
0 |
| SvREFCNT_dec(ef); |
| 133 |
0 |
| return; |
| 134 |
|
| } |
| 135 |
14 |
| dbil_pool_send_tx(aTHX_ p, (int)wi, (int)gen, bytes, ef); |
| 136 |
14 |
| SvREFCNT_inc((SV *)cap); |
| 137 |
14 |
| dbil_txn_on_ready(aTHX_ ef, dbil_txn_commit_cb, cap); |
| 138 |
14 |
| SvREFCNT_dec(ef); |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
|
| /* stage 3: the block's future settled */ |
| 142 |
|
| XS_INTERNAL(dbil_txn_block_cb); |
| 143 |
11 |
| XS_INTERNAL(dbil_txn_block_cb) { |
| 144 |
11 |
| dXSARGS; |
| 145 |
11 |
| AV *cap = dbil_clos_cap(aTHX_ cv); |
| 146 |
|
| SV *fut; |
| 147 |
|
| dbil_future *f; |
| 148 |
11 |
▶ | if (!cap || items < 1) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cap || items < 1) XSRETURN_EMPTY; |
11 |
0 |
11 |
| if (!cap || items < 1) XSRETURN_EMPTY; |
0 |
11 |
11 |
|
| 149 |
11 |
| fut = ST(0); |
| 150 |
11 |
| f = dbil_future_of(aTHX_ fut); |
| 151 |
11 |
▶ | if (f->state == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->state == 1) { |
9 |
2 |
11 |
|
| 152 |
9 |
▶ | av_store(cap, DBIL_TC_RESULT, |
Branch: 1 branch
| Branch | True | False | Total |
| av_store(cap, DBIL_TC_RESULT, |
9 |
0 |
9 |
|
| 153 |
|
| f->result ? SvREFCNT_inc(f->result) : &PL_sv_undef); |
| 154 |
9 |
| dbil_txn_end(aTHX_ cap, "COMMIT", NULL); |
| 155 |
|
| } else { |
| 156 |
2 |
| dbil_txn_end(aTHX_ cap, "ROLLBACK", |
| 157 |
2 |
▶ | f->error ? f->error : sv_2mortal(newSVpvs("transaction block failed"))); |
Branch: 1 branch
| Branch | True | False | Total |
| f->error ? f->error : sv_2mortal(newSVpvs("transaction block failed"))); |
2 |
0 |
2 |
|
| 158 |
|
| } |
| 159 |
11 |
| XSRETURN_EMPTY; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
|
| /* stage 2: BEGIN completed - run the user block */ |
| 163 |
|
| XS_INTERNAL(dbil_txn_begin_cb); |
| 164 |
14 |
| XS_INTERNAL(dbil_txn_begin_cb) { |
| 165 |
14 |
| dXSARGS; |
| 166 |
14 |
| AV *cap = dbil_clos_cap(aTHX_ cv); |
| 167 |
|
| SV *fut, *block, *tx; |
| 168 |
14 |
▶ | if (!cap || items < 1) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cap || items < 1) XSRETURN_EMPTY; |
14 |
0 |
14 |
| if (!cap || items < 1) XSRETURN_EMPTY; |
0 |
14 |
14 |
|
| 169 |
14 |
| fut = ST(0); |
| 170 |
14 |
▶ | if (dbil_future_of(aTHX_ fut)->state == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dbil_future_of(aTHX_ fut)->state == 2) { |
0 |
14 |
14 |
|
| 171 |
0 |
| SV *outer = *av_fetch(cap, DBIL_TC_OUTER, 0); |
| 172 |
0 |
| dbil_txn_finish(aTHX_ cap); |
| 173 |
0 |
| dbil_future_settle_fail(aTHX_ outer, |
| 174 |
0 |
▶ | dbil_future_of(aTHX_ fut)->error |
Branch: 1 branch
| Branch | True | False | Total |
| dbil_future_of(aTHX_ fut)->error |
0 |
0 |
0 |
|
| 175 |
0 |
| ? dbil_future_of(aTHX_ fut)->error |
| 176 |
0 |
| : sv_2mortal(newSVpvs("BEGIN failed"))); |
| 177 |
0 |
| XSRETURN_EMPTY; |
| 178 |
|
| } |
| 179 |
14 |
| block = *av_fetch(cap, DBIL_TC_BLOCK, 0); |
| 180 |
14 |
| tx = *av_fetch(cap, DBIL_TC_TX, 0); |
| 181 |
|
| { |
| 182 |
14 |
| dSP; int n; SV *res = NULL; |
| 183 |
14 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
14 |
14 |
|
| 184 |
14 |
▶ | EXTEND(SP, 1); PUSHs(tx); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); PUSHs(tx); PUTBACK; |
0 |
14 |
14 |
|
| 185 |
14 |
| n = call_sv(block, G_SCALAR | G_EVAL); |
| 186 |
14 |
| SPAGAIN; |
| 187 |
14 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
14 |
0 |
14 |
| if (SvTRUE(ERRSV)) { |
1 |
13 |
14 |
|
| 188 |
1 |
▶ | SV *e = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = newSVsv(ERRSV); |
1 |
0 |
1 |
|
| 189 |
1 |
▶ | if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) (void)POPs; |
1 |
0 |
1 |
|
| 190 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 191 |
1 |
| dbil_txn_end(aTHX_ cap, "ROLLBACK", sv_2mortal(e)); |
| 192 |
1 |
| XSRETURN_EMPTY; |
| 193 |
|
| } |
| 194 |
13 |
▶ | res = (n > 0) ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| res = (n > 0) ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
13 |
0 |
13 |
|
| 195 |
13 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
13 |
0 |
13 |
|
| 196 |
|
| |
| 197 |
13 |
▶ | if (res && SvROK(res) && sv_derived_from(res, "DBIx::Loop::Future")) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (res && SvROK(res) && sv_derived_from(res, "DBIx::Loop::Future")) { |
13 |
0 |
13 |
| if (res && SvROK(res) && sv_derived_from(res, "DBIx::Loop::Future")) { |
11 |
2 |
13 |
| if (res && SvROK(res) && sv_derived_from(res, "DBIx::Loop::Future")) { |
11 |
0 |
11 |
|
| 198 |
11 |
| SvREFCNT_inc((SV *)cap); |
| 199 |
11 |
| dbil_txn_on_ready(aTHX_ res, dbil_txn_block_cb, cap); |
| 200 |
11 |
| SvREFCNT_dec(res); |
| 201 |
|
| } else { |
| 202 |
2 |
| AV *ra = newAV(); |
| 203 |
2 |
▶ | av_push(ra, res ? res : &PL_sv_undef); /* takes the +1 */ |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(ra, res ? res : &PL_sv_undef); /* takes the +1 */ |
2 |
0 |
2 |
|
| 204 |
2 |
| av_store(cap, DBIL_TC_RESULT, newRV_noinc((SV *)ra)); |
| 205 |
2 |
| dbil_txn_end(aTHX_ cap, "COMMIT", NULL); |
| 206 |
|
| } |
| 207 |
|
| } |
| 208 |
13 |
| XSRETURN_EMPTY; |
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
|
| /* stage 1: slot acquired - pin the handle, send BEGIN */ |
| 212 |
|
| XS_INTERNAL(dbil_txn_acq_cb); |
| 213 |
14 |
| XS_INTERNAL(dbil_txn_acq_cb) { |
| 214 |
14 |
| dXSARGS; |
| 215 |
14 |
| AV *cap = dbil_clos_cap(aTHX_ cv); |
| 216 |
|
| dbil_pool *p; |
| 217 |
|
| SV *fut, *tx; |
| 218 |
|
| IV wi; |
| 219 |
14 |
▶ | if (!cap || items < 1) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cap || items < 1) XSRETURN_EMPTY; |
14 |
0 |
14 |
| if (!cap || items < 1) XSRETURN_EMPTY; |
0 |
14 |
14 |
|
| 220 |
14 |
| fut = ST(0); |
| 221 |
14 |
| p = INT2PTR(dbil_pool *, SvIV(*av_fetch(cap, DBIL_TC_POOL, 0))); |
| 222 |
14 |
| tx = *av_fetch(cap, DBIL_TC_TX, 0); |
| 223 |
|
| { |
| 224 |
14 |
| dbil_future *f = dbil_future_of(aTHX_ fut); |
| 225 |
|
| SV **r0; |
| 226 |
14 |
▶ | if (f->state != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->state != 1) { |
0 |
14 |
14 |
|
| 227 |
0 |
| dbil_future_settle_fail(aTHX_ *av_fetch(cap, DBIL_TC_OUTER, 0), |
| 228 |
0 |
▶ | f->error ? f->error : sv_2mortal(newSVpvs("could not acquire a slot"))); |
Branch: 1 branch
| Branch | True | False | Total |
| f->error ? f->error : sv_2mortal(newSVpvs("could not acquire a slot"))); |
0 |
0 |
0 |
|
| 229 |
0 |
| XSRETURN_EMPTY; |
| 230 |
|
| } |
| 231 |
14 |
| r0 = av_fetch((AV *)SvRV(f->result), 0, 0); |
| 232 |
14 |
▶ | wi = (r0 && *r0) ? SvIV(*r0) : -1; |
Branch: 2 branches
| Branch | True | False | Total |
| wi = (r0 && *r0) ? SvIV(*r0) : -1; |
14 |
0 |
14 |
| wi = (r0 && *r0) ? SvIV(*r0) : -1; |
14 |
0 |
14 |
|
| 233 |
|
| } |
| 234 |
14 |
| dbil_tx_set_iv(aTHX_ tx, "wi", wi); |
| 235 |
14 |
▶ | dbil_tx_set_iv(aTHX_ tx, "gen", (wi >= 0) ? p->w[wi].gen : -1); |
Branch: 1 branch
| Branch | True | False | Total |
| dbil_tx_set_iv(aTHX_ tx, "gen", (wi >= 0) ? p->w[wi].gen : -1); |
14 |
0 |
14 |
|
| 236 |
14 |
| dbil_tx_set_iv(aTHX_ tx, "active", 1); |
| 237 |
|
| { |
| 238 |
14 |
| SV *bytes = dbil_txn_freeze_req(aTHX_ 0, "BEGIN", NULL); |
| 239 |
14 |
| SV *bf = dbil_future_new(aTHX_ "DBIx::Loop::Future"); |
| 240 |
14 |
▶ | if (!bytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bytes) { |
0 |
14 |
14 |
|
| 241 |
0 |
| dbil_txn_finish(aTHX_ cap); |
| 242 |
0 |
| dbil_future_settle_fail(aTHX_ *av_fetch(cap, DBIL_TC_OUTER, 0), |
| 243 |
|
| sv_2mortal(newSVpvs("could not serialise BEGIN"))); |
| 244 |
0 |
| SvREFCNT_dec(bf); |
| 245 |
0 |
| XSRETURN_EMPTY; |
| 246 |
|
| } |
| 247 |
14 |
| dbil_pool_send_tx(aTHX_ p, (int)wi, (int)dbil_tx_iv(aTHX_ tx, "gen"), |
| 248 |
|
| bytes, bf); |
| 249 |
14 |
| SvREFCNT_inc((SV *)cap); |
| 250 |
14 |
| dbil_txn_on_ready(aTHX_ bf, dbil_txn_begin_cb, cap); |
| 251 |
14 |
| SvREFCNT_dec(bf); |
| 252 |
|
| } |
| 253 |
14 |
| XSRETURN_EMPTY; |
| 254 |
|
| } |
| 255 |
|
| |
| 256 |
|
| /* entry: $db->txn($block) -> outer future (+1) */ |
| 257 |
14 |
| static SV *dbil_txn_start(pTHX_ SV *self, SV *block, dbil_pool *p) { |
| 258 |
14 |
| SV *outer = dbil_future_new(aTHX_ "DBIx::Loop::Future"); |
| 259 |
|
| SV *tx; |
| 260 |
|
| AV *cap; |
| 261 |
|
| { /* the Txn handle: { db, wi, gen, active, pool } */ |
| 262 |
14 |
| HV *h = newHV(); |
| 263 |
14 |
| (void)hv_stores(h, "db", newSVsv(self)); |
| 264 |
14 |
| (void)hv_stores(h, "wi", newSViv(-1)); |
| 265 |
14 |
| (void)hv_stores(h, "gen", newSViv(-1)); |
| 266 |
14 |
| (void)hv_stores(h, "active", newSViv(0)); |
| 267 |
14 |
| (void)hv_stores(h, "_pool", newSViv(PTR2IV(p))); |
| 268 |
14 |
| tx = sv_bless(newRV_noinc((SV *)h), |
| 269 |
|
| gv_stashpv("DBIx::Loop::Txn", GV_ADD)); |
| 270 |
|
| } |
| 271 |
14 |
| cap = newAV(); |
| 272 |
14 |
| av_extend(cap, DBIL_TC_ERROR); |
| 273 |
14 |
| av_store(cap, DBIL_TC_SELF, newSVsv(self)); |
| 274 |
14 |
| av_store(cap, DBIL_TC_BLOCK, newSVsv(block)); |
| 275 |
14 |
| av_store(cap, DBIL_TC_OUTER, SvREFCNT_inc(outer)); |
| 276 |
14 |
| av_store(cap, DBIL_TC_TX, tx); /* takes the +1 */ |
| 277 |
14 |
| av_store(cap, DBIL_TC_POOL, newSViv(PTR2IV(p))); |
| 278 |
14 |
| av_store(cap, DBIL_TC_RESULT, newSV(0)); |
| 279 |
14 |
| av_store(cap, DBIL_TC_ERROR, newSV(0)); |
| 280 |
|
| { |
| 281 |
14 |
| SV *acq = dbil_pool_acquire(aTHX_ p); |
| 282 |
14 |
| dbil_txn_on_ready(aTHX_ acq, dbil_txn_acq_cb, cap); /* cap owned */ |
| 283 |
14 |
| SvREFCNT_dec(acq); |
| 284 |
|
| } |
| 285 |
14 |
| return outer; |
| 286 |
|
| } |
| 287 |
|
| |
| 288 |
|
| /* a statement on the Txn handle, pinned to its slot (+1 future) */ |
| 289 |
19 |
| static SV *dbil_txn_stmt(pTHX_ SV *tx_rv, int is_query, SV *sql, AV *bind) { |
| 290 |
19 |
| SV *future = dbil_future_new(aTHX_ "DBIx::Loop::Future"); |
| 291 |
19 |
| IV active = dbil_tx_iv(aTHX_ tx_rv, "active"); |
| 292 |
19 |
| IV wi = dbil_tx_iv(aTHX_ tx_rv, "wi"); |
| 293 |
19 |
| IV gen = dbil_tx_iv(aTHX_ tx_rv, "gen"); |
| 294 |
19 |
| dbil_pool *p = INT2PTR(dbil_pool *, |
| 295 |
|
| dbil_tx_iv(aTHX_ tx_rv, "_pool")); |
| 296 |
19 |
▶ | if (!active || wi < 0 || !p) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!active || wi < 0 || !p) { |
18 |
1 |
19 |
| if (!active || wi < 0 || !p) { |
18 |
0 |
18 |
| if (!active || wi < 0 || !p) { |
0 |
18 |
18 |
|
| 297 |
1 |
| dbil_future_settle_fail(aTHX_ future, |
| 298 |
|
| sv_2mortal(newSVpvs("transaction is not active"))); |
| 299 |
1 |
| return future; |
| 300 |
|
| } |
| 301 |
|
| { |
| 302 |
18 |
| AV *req = newAV(); |
| 303 |
|
| SV *bytes; |
| 304 |
18 |
| av_push(req, newSViv(is_query)); |
| 305 |
18 |
| av_push(req, newSVsv(sql)); |
| 306 |
18 |
▶ | av_push(req, bind ? newRV_inc((SV *)bind) : newRV_noinc((SV *)newAV())); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(req, bind ? newRV_inc((SV *)bind) : newRV_noinc((SV *)newAV())); |
18 |
0 |
18 |
|
| 307 |
18 |
| bytes = dbil_freeze(aTHX_ sv_2mortal(newRV_noinc((SV *)req))); |
| 308 |
18 |
▶ | if (!bytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bytes) { |
0 |
18 |
18 |
|
| 309 |
0 |
| dbil_future_settle_fail(aTHX_ future, |
| 310 |
|
| sv_2mortal(newSVpvs("could not serialise request"))); |
| 311 |
0 |
| return future; |
| 312 |
|
| } |
| 313 |
18 |
| dbil_pool_send_tx(aTHX_ p, (int)wi, (int)gen, bytes, future); |
| 314 |
|
| } |
| 315 |
18 |
| return future; |
| 316 |
|
| } |
| 317 |
|
| |
| 318 |
|
| #endif /* DBIL_TXN_H */ |