| 1 |
|
| #ifndef STORE_TXN_H |
| 2 |
|
| #define STORE_TXN_H |
| 3 |
|
| |
| 4 |
|
| /* Transactions. Each one, nested or not, keeps what the store was when it |
| 5 |
|
| * began: the value and copies of both stacks. A copy of a stack is a small |
| 6 |
|
| * struct that shares every node, so keeping one costs that struct. |
| 7 |
|
| * |
| 8 |
|
| * A transaction that fails puts back what it kept, which undoes everything |
| 9 |
|
| * done inside it and nothing done outside. One that succeeds lets go of what |
| 10 |
|
| * it kept, unless it is the outermost: that one makes everything done |
| 11 |
|
| * inside into one step, from the value it began with to the value it ends |
| 12 |
|
| * with, and has the watchers told of it once. If the value it ends with is |
| 13 |
|
| * the value it began with, there is no step: the stacks go back as they |
| 14 |
|
| * were, and nobody is told. */ |
| 15 |
|
| |
| 16 |
|
| typedef struct store_saved { |
| 17 |
|
| SV *value; |
| 18 |
|
| hamt_vec *past; |
| 19 |
|
| hamt_vec *future; |
| 20 |
|
| hamt_map *checkpoints; /* NULL when the store had none */ |
| 21 |
|
| size_t past_skip; |
| 22 |
|
| } store_saved; |
| 23 |
|
| |
| 24 |
4152 |
| static void store_txn_begin(pTHX_ store *s, store_saved *sv, const char *what) { |
| 25 |
4152 |
| sv->past = (STORE_HAMT->vec_copy)(aTHX_ s->past); |
| 26 |
4152 |
▶ | sv->future = sv->past ? (STORE_HAMT->vec_copy)(aTHX_ s->future) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| sv->future = sv->past ? (STORE_HAMT->vec_copy)(aTHX_ s->future) : NULL; |
4152 |
0 |
4152 |
|
| 27 |
4152 |
▶ | if (!sv->future) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv->future) { |
0 |
4152 |
4152 |
|
| 28 |
0 |
▶ | if (sv->past) (STORE_HAMT->vec_release)(aTHX_ sv->past); |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv->past) (STORE_HAMT->vec_release)(aTHX_ sv->past); |
0 |
0 |
0 |
|
| 29 |
0 |
| croak("%s: out of memory", what); |
| 30 |
|
| } |
| 31 |
4152 |
| sv->checkpoints = NULL; |
| 32 |
4152 |
▶ | if (s->checkpoints) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->checkpoints) { |
7 |
4145 |
4152 |
|
| 33 |
7 |
| sv->checkpoints = (STORE_HAMT->map_copy)(aTHX_ s->checkpoints); |
| 34 |
7 |
▶ | if (!sv->checkpoints) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv->checkpoints) { |
0 |
7 |
7 |
|
| 35 |
0 |
| (STORE_HAMT->vec_release)(aTHX_ sv->past); |
| 36 |
0 |
| (STORE_HAMT->vec_release)(aTHX_ sv->future); |
| 37 |
0 |
| croak("%s: out of memory", what); |
| 38 |
|
| } |
| 39 |
|
| } |
| 40 |
4152 |
| sv->value = SvREFCNT_inc_simple_NN(s->value); |
| 41 |
4152 |
| sv->past_skip = s->past_skip; |
| 42 |
4152 |
| s->txn_depth++; |
| 43 |
4152 |
| } |
| 44 |
|
| |
| 45 |
|
| /* The store as it was when `sv` was kept. What it had become is let go |
| 46 |
|
| * last, once the store holds the old state again. */ |
| 47 |
2072 |
| static void store_txn_restore(pTHX_ store *s, store_saved *sv) { |
| 48 |
2072 |
| SV *value = s->value; |
| 49 |
2072 |
| hamt_vec *past = s->past, *future = s->future; |
| 50 |
2072 |
| hamt_map *checkpoints = s->checkpoints; |
| 51 |
|
| |
| 52 |
2072 |
| s->value = sv->value; |
| 53 |
2072 |
| s->past = sv->past; |
| 54 |
2072 |
| s->future = sv->future; |
| 55 |
2072 |
| s->checkpoints = sv->checkpoints; |
| 56 |
2072 |
| s->past_skip = sv->past_skip; |
| 57 |
2072 |
| s->txn_depth--; |
| 58 |
|
| |
| 59 |
2072 |
| store_sv_release(aTHX_ value); |
| 60 |
2072 |
| (STORE_HAMT->vec_release)(aTHX_ past); |
| 61 |
2072 |
| (STORE_HAMT->vec_release)(aTHX_ future); |
| 62 |
2072 |
▶ | if (checkpoints) (STORE_HAMT->map_release)(aTHX_ checkpoints); |
Branch: 1 branch
| Branch | True | False | Total |
| if (checkpoints) (STORE_HAMT->map_release)(aTHX_ checkpoints); |
1 |
2071 |
2072 |
|
| 63 |
2072 |
| } |
| 64 |
|
| |
| 65 |
|
| /* What a transaction kept of the checkpoints, when it is not going back to |
| 66 |
|
| * them. */ |
| 67 |
2091 |
| static void store_txn_drop_checkpoints(pTHX_ store_saved *sv) { |
| 68 |
2091 |
▶ | if (sv->checkpoints) (STORE_HAMT->map_release)(aTHX_ sv->checkpoints); |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv->checkpoints) (STORE_HAMT->map_release)(aTHX_ sv->checkpoints); |
6 |
2085 |
2091 |
|
| 69 |
2091 |
| sv->checkpoints = NULL; |
| 70 |
2091 |
| } |
| 71 |
|
| |
| 72 |
|
| /* An inner transaction that worked: what it kept is not needed. */ |
| 73 |
3 |
| static void store_txn_forget(pTHX_ store *s, store_saved *sv) { |
| 74 |
3 |
| s->txn_depth--; |
| 75 |
3 |
| store_txn_drop_checkpoints(aTHX_ sv); |
| 76 |
3 |
| store_sv_release(aTHX_ sv->value); |
| 77 |
3 |
| (STORE_HAMT->vec_release)(aTHX_ sv->past); |
| 78 |
3 |
| (STORE_HAMT->vec_release)(aTHX_ sv->future); |
| 79 |
3 |
| } |
| 80 |
|
| |
| 81 |
|
| /* The outermost transaction worked. One step, from the value it began with, |
| 82 |
|
| * onto the stacks it began with; nothing to redo; one change queued. */ |
| 83 |
2088 |
| static void store_txn_commit(pTHX_ store *s, store_saved *sv, const char *what) { |
| 84 |
2088 |
| hamt_vec *past, *future, *room = NULL, *dead_past, *dead_future; |
| 85 |
2088 |
▶ | int record = !s->limited || s->limit > 0, nomem = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| int record = !s->limited || s->limit > 0, nomem = 0; |
59 |
2029 |
2088 |
| int record = !s->limited || s->limit > 0, nomem = 0; |
55 |
4 |
59 |
|
| 86 |
|
| |
| 87 |
|
| /* No change of value: the stacks go back, but checkpoints named inside |
| 88 |
|
| * stay, since the transaction worked. */ |
| 89 |
2088 |
▶ | if ((STORE_HAMT->value_equal)(aTHX_ sv->value, s->value)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((STORE_HAMT->value_equal)(aTHX_ sv->value, s->value)) { |
11 |
2077 |
2088 |
|
| 90 |
11 |
| store_txn_drop_checkpoints(aTHX_ sv); |
| 91 |
11 |
| sv->checkpoints = s->checkpoints; |
| 92 |
11 |
| s->checkpoints = NULL; |
| 93 |
11 |
| store_txn_restore(aTHX_ s, sv); |
| 94 |
11 |
| return; |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
2077 |
| future = (STORE_HAMT->vec_new)(aTHX); |
| 98 |
2077 |
▶ | if (!future) { store_txn_restore(aTHX_ s, sv); croak("%s: out of memory", what); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!future) { store_txn_restore(aTHX_ s, sv); croak("%s: out of memory", what); } |
0 |
2077 |
2077 |
|
| 99 |
2077 |
| past = sv->past; |
| 100 |
2077 |
▶ | if (record && s->limited) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (record && s->limited) { |
2074 |
3 |
2077 |
| if (record && s->limited) { |
54 |
2020 |
2074 |
|
| 101 |
54 |
| room = store_room(aTHX_ s, past, &nomem); |
| 102 |
54 |
▶ | if (nomem) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nomem) { |
0 |
54 |
54 |
|
| 103 |
0 |
| (STORE_HAMT->vec_release)(aTHX_ future); |
| 104 |
0 |
| store_txn_restore(aTHX_ s, sv); |
| 105 |
0 |
| croak("%s: out of memory", what); |
| 106 |
|
| } |
| 107 |
54 |
▶ | if (room) { past = room; sv->past_skip = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (room) { past = room; sv->past_skip = 0; } |
0 |
54 |
54 |
|
| 108 |
|
| } |
| 109 |
2077 |
▶ | if (record && (STORE_HAMT->vec_push)(aTHX_ past, sv->value) != HAMT_ABI_OK) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (record && (STORE_HAMT->vec_push)(aTHX_ past, sv->value) != HAMT_ABI_OK) { |
2074 |
3 |
2077 |
| if (record && (STORE_HAMT->vec_push)(aTHX_ past, sv->value) != HAMT_ABI_OK) { |
0 |
2074 |
2074 |
|
| 110 |
0 |
▶ | if (room) (STORE_HAMT->vec_release)(aTHX_ room); |
Branch: 1 branch
| Branch | True | False | Total |
| if (room) (STORE_HAMT->vec_release)(aTHX_ room); |
0 |
0 |
0 |
|
| 111 |
0 |
| (STORE_HAMT->vec_release)(aTHX_ future); |
| 112 |
0 |
| store_txn_restore(aTHX_ s, sv); |
| 113 |
0 |
| croak("%s: out of memory", what); |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
2077 |
| dead_past = s->past; |
| 117 |
2077 |
| dead_future = s->future; |
| 118 |
2077 |
| s->past = past; |
| 119 |
2077 |
| s->future = future; |
| 120 |
2074 |
| s->past_skip = record ? store_skip_after(s, (STORE_HAMT->vec_count)(aTHX_ past), sv->past_skip) |
| 121 |
2077 |
▶ | : sv->past_skip; |
Branch: 1 branch
| Branch | True | False | Total |
| : sv->past_skip; |
2074 |
3 |
2077 |
|
| 122 |
2077 |
| s->txn_depth--; |
| 123 |
|
| |
| 124 |
2077 |
| store_enqueue(aTHX_ s, sv->value, s->value); |
| 125 |
2077 |
| store_txn_drop_checkpoints(aTHX_ sv); |
| 126 |
2077 |
| store_sv_release(aTHX_ sv->value); |
| 127 |
2077 |
▶ | if (room) (STORE_HAMT->vec_release)(aTHX_ sv->past); |
Branch: 1 branch
| Branch | True | False | Total |
| if (room) (STORE_HAMT->vec_release)(aTHX_ sv->past); |
0 |
2077 |
2077 |
|
| 128 |
2077 |
| (STORE_HAMT->vec_release)(aTHX_ sv->future); |
| 129 |
2077 |
| (STORE_HAMT->vec_release)(aTHX_ dead_past); |
| 130 |
2077 |
| (STORE_HAMT->vec_release)(aTHX_ dead_future); |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
|
| #endif |