| 1 |
|
| #ifndef STORE_WATCH_H |
| 2 |
|
| #define STORE_WATCH_H |
| 3 |
|
| |
| 4 |
|
| /* Watchers: subs told of every change, after the store has settled. |
| 5 |
|
| * |
| 6 |
|
| * A change is queued where it is made and told once the operation that made |
| 7 |
|
| * it is over. A watcher that changes the store queues another change behind |
| 8 |
|
| * the one being told, so every watcher sees every change, in order, and |
| 9 |
|
| * none is told a `now` older than its `was`. A store that keeps changing |
| 10 |
|
| * itself from its watchers is a loop, and is stopped after a fixed number |
| 11 |
|
| * of changes told in one round. |
| 12 |
|
| * |
| 13 |
|
| * A watcher that dies does not stop the others, and does not undo the |
| 14 |
|
| * change: the change has happened. The first error is thrown again once |
| 15 |
|
| * every watcher has been told every change. */ |
| 16 |
|
| |
| 17 |
|
| #define STORE_WATCH_LOOP 10000 |
| 18 |
|
| |
| 19 |
|
| /* Removed watchers out of the list. Only between rounds: during one, a |
| 20 |
|
| * removed watcher keeps its place so that the indices being walked hold. */ |
| 21 |
4562 |
| static void store_watch_sweep(store *s) { |
| 22 |
4562 |
| size_t i, j = 0; |
| 23 |
15168 |
▶ | for (i = 0; i < s->nwatchers; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < s->nwatchers; i++) |
10606 |
4562 |
15168 |
|
| 24 |
10606 |
▶ | if (s->watchers[i].code || s->watchers[i].cfn) s->watchers[j++] = s->watchers[i]; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s->watchers[i].code || s->watchers[i].cfn) s->watchers[j++] = s->watchers[i]; |
1327 |
9279 |
10606 |
| if (s->watchers[i].code || s->watchers[i].cfn) s->watchers[j++] = s->watchers[i]; |
255 |
1072 |
1327 |
|
| 25 |
4562 |
| s->nwatchers = j; |
| 26 |
4562 |
| } |
| 27 |
|
| |
| 28 |
|
| /* A Perl sub, or a C function and its data. */ |
| 29 |
1118 |
| static UV store_watch_add(pTHX_ store *s, SV *code, state_store_watch_fn cfn, void *ud) { |
| 30 |
1118 |
▶ | if (s->nwatchers == s->cwatchers) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->nwatchers == s->cwatchers) { |
91 |
1027 |
1118 |
|
| 31 |
91 |
▶ | s->cwatchers = s->cwatchers ? s->cwatchers * 2 : 4; |
Branch: 1 branch
| Branch | True | False | Total |
| s->cwatchers = s->cwatchers ? s->cwatchers * 2 : 4; |
1 |
90 |
91 |
|
| 32 |
91 |
▶ | Renew(s->watchers, s->cwatchers, store_watcher); |
Branch: 1 branch
| Branch | True | False | Total |
| Renew(s->watchers, s->cwatchers, store_watcher); |
0 |
91 |
91 |
|
| 33 |
|
| } |
| 34 |
1118 |
| s->watchers[s->nwatchers].id = ++s->next_id; |
| 35 |
1118 |
▶ | s->watchers[s->nwatchers].code = code ? SvREFCNT_inc_simple_NN(code) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| s->watchers[s->nwatchers].code = code ? SvREFCNT_inc_simple_NN(code) : NULL; |
1067 |
51 |
1118 |
|
| 36 |
1118 |
▶ | s->watchers[s->nwatchers].cfn = code ? NULL : cfn; |
Branch: 1 branch
| Branch | True | False | Total |
| s->watchers[s->nwatchers].cfn = code ? NULL : cfn; |
51 |
1067 |
1118 |
|
| 37 |
1118 |
| s->watchers[s->nwatchers].ud = ud; |
| 38 |
1118 |
| s->nwatchers++; |
| 39 |
1118 |
| s->live_watchers++; |
| 40 |
1118 |
| return s->next_id; |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| /* 1 if there was a watcher with that id. Its code is let go now; a round |
| 44 |
|
| * that is calling it holds a reference of its own. */ |
| 45 |
1128 |
| static int store_watch_remove(pTHX_ store *s, UV id) { |
| 46 |
|
| size_t i; |
| 47 |
1154 |
▶ | for (i = 0; i < s->nwatchers; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < s->nwatchers; i++) { |
1098 |
56 |
1154 |
|
| 48 |
1098 |
| SV *code = s->watchers[i].code; |
| 49 |
1098 |
▶ | if (s->watchers[i].id != id || (!code && !s->watchers[i].cfn)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (s->watchers[i].id != id || (!code && !s->watchers[i].cfn)) continue; |
1073 |
25 |
1098 |
| if (s->watchers[i].id != id || (!code && !s->watchers[i].cfn)) continue; |
52 |
1021 |
1073 |
| if (s->watchers[i].id != id || (!code && !s->watchers[i].cfn)) continue; |
1 |
51 |
52 |
|
| 50 |
1072 |
| s->watchers[i].code = NULL; |
| 51 |
1072 |
| s->watchers[i].cfn = NULL; |
| 52 |
1072 |
| s->live_watchers--; |
| 53 |
1072 |
▶ | if (!s->delivering) store_watch_sweep(s); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->delivering) store_watch_sweep(s); |
1064 |
8 |
1072 |
|
| 54 |
1072 |
▶ | if (code) SvREFCNT_dec(code); |
Branch: 1 branch
| Branch | True | False | Total |
| if (code) SvREFCNT_dec(code); |
1021 |
51 |
1072 |
|
| 55 |
1072 |
| return 1; |
| 56 |
|
| } |
| 57 |
56 |
| return 0; |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
|
| /* Tell the watchers every change queued. `storerv` is a reference to the |
| 61 |
|
| * store, held by the caller for as long as this runs, and handed to each |
| 62 |
|
| * watcher. Nothing happens if a round is already running further up: that |
| 63 |
|
| * round will tell what was queued here. */ |
| 64 |
262375 |
| static void store_deliver(pTHX_ store *s, SV *storerv) { |
| 65 |
262375 |
| SV *first_err = NULL; |
| 66 |
262375 |
| size_t told = 0; |
| 67 |
|
| |
| 68 |
262375 |
▶ | if (s->delivering || !s->qlen) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s->delivering || !s->qlen) return; |
232364 |
30011 |
262375 |
| if (s->delivering || !s->qlen) return; |
228866 |
3498 |
232364 |
|
| 69 |
3498 |
▶ | if (PL_dirty) { store_queue_clear(aTHX_ s); return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_dirty) { store_queue_clear(aTHX_ s); return; } |
0 |
3498 |
3498 |
|
| 70 |
3498 |
| s->delivering = 1; |
| 71 |
|
| |
| 72 |
27002 |
▶ | while (s->qlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (s->qlen) { |
23506 |
3496 |
27002 |
|
| 73 |
23506 |
| store_change c = s->queue[s->qhead++]; |
| 74 |
23506 |
| size_t n = s->nwatchers, i; |
| 75 |
23506 |
| s->qlen--; |
| 76 |
23506 |
▶ | if (!s->qlen) s->qhead = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->qlen) s->qhead = 0; |
13506 |
10000 |
23506 |
|
| 77 |
|
| |
| 78 |
23506 |
▶ | if (++told > STORE_WATCH_LOOP) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (++told > STORE_WATCH_LOOP) { |
2 |
23504 |
23506 |
|
| 79 |
2 |
| SvREFCNT_dec(c.was); |
| 80 |
2 |
| SvREFCNT_dec(c.now); |
| 81 |
2 |
| store_queue_clear(aTHX_ s); |
| 82 |
2 |
| s->delivering = 0; |
| 83 |
2 |
| store_watch_sweep(s); |
| 84 |
2 |
▶ | if (first_err) SvREFCNT_dec(first_err); |
Branch: 1 branch
| Branch | True | False | Total |
| if (first_err) SvREFCNT_dec(first_err); |
0 |
2 |
2 |
|
| 85 |
2 |
| croak("State::Store: the watchers changed the store %d times in one round;" |
| 86 |
|
| " one of them changes it every time it is called", STORE_WATCH_LOOP); |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| /* Those there when this change is told, and still there when their |
| 90 |
|
| * turn comes. One added now is told the next change. */ |
| 91 |
63041 |
▶ | for (i = 0; i < n && i < s->nwatchers; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < n && i < s->nwatchers; i++) { |
39537 |
23504 |
63041 |
| for (i = 0; i < n && i < s->nwatchers; i++) { |
39537 |
0 |
39537 |
|
| 92 |
39537 |
| SV *code = s->watchers[i].code, *args[3], *err; |
| 93 |
39537 |
▶ | if (!code) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!code) { |
261 |
39276 |
39537 |
|
| 94 |
261 |
| state_store_watch_fn cfn = s->watchers[i].cfn; |
| 95 |
261 |
▶ | if (cfn) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cfn) { |
255 |
6 |
261 |
|
| 96 |
255 |
| ENTER; |
| 97 |
255 |
| SAVETMPS; |
| 98 |
255 |
| cfn(aTHX_ s->watchers[i].ud, c.was, c.now, storerv); |
| 99 |
255 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
0 |
255 |
255 |
|
| 100 |
255 |
| LEAVE; |
| 101 |
|
| } |
| 102 |
261 |
| continue; |
| 103 |
|
| } |
| 104 |
39276 |
| args[0] = c.was; |
| 105 |
39276 |
| args[1] = c.now; |
| 106 |
39276 |
| args[2] = storerv; |
| 107 |
39276 |
| SvREFCNT_inc_simple_void_NN(code); |
| 108 |
39276 |
| ENTER; |
| 109 |
39276 |
| SAVETMPS; |
| 110 |
39276 |
| err = store_call(aTHX_ code, args, 3, G_VOID, NULL, NULL); |
| 111 |
39276 |
▶ | if (err && !first_err) first_err = newSVsv(err); |
Branch: 2 branches
| Branch | True | False | Total |
| if (err && !first_err) first_err = newSVsv(err); |
7 |
39269 |
39276 |
| if (err && !first_err) first_err = newSVsv(err); |
6 |
1 |
7 |
|
| 112 |
39276 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
30030 |
9246 |
39276 |
|
| 113 |
39276 |
| LEAVE; |
| 114 |
39276 |
| SvREFCNT_dec(code); |
| 115 |
|
| } |
| 116 |
23504 |
| SvREFCNT_dec(c.was); |
| 117 |
23504 |
| SvREFCNT_dec(c.now); |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
3496 |
| s->delivering = 0; |
| 121 |
3496 |
| store_watch_sweep(s); |
| 122 |
3496 |
▶ | if (first_err) store_rethrow(aTHX_ sv_2mortal(first_err)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (first_err) store_rethrow(aTHX_ sv_2mortal(first_err)); |
6 |
3490 |
3496 |
|
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
|
| #endif |