about summary refs log tree commit diff stats
path: root/072scheduler.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-08-16 17:03:27 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-08-16 17:03:27 -0700
commit78a12c9d706f6197d3710ece04e9bd8d4eebe713 (patch)
tree3951e166cb933c608eeb1a233e7a218497870751 /072scheduler.cc
parent19695cc7ca16d9129ed3ef2b46bf3460452dd6db (diff)
downloadmu-78a12c9d706f6197d3710ece04e9bd8d4eebe713.tar.gz
3202 - bugfix: 'start-running' and refcounts
When you pass an ingredient to a recipe using 'start-running' it mostly
behaves identically to performing a regular function call. However, if
the calling function completed before the new routine had a chance to
run, the ingredients passed in ran the risk of being reclaimed.

In response, let's always increment refcounts at the time of a function
call rather than when the ingredients are read inside the callee.

Now the summary of commit 3197 is modified to this:

  Update refcounts of products after every instruction, EXCEPT:

    a) when instruction is a non-primitive and the callee starts with
    'local-scope' (because it's already not decremented in 'return')

  OR:

    b) when instruction is primitive 'next-ingredient' or
    'next-ingredient-without-typechecking'
Diffstat (limited to '072scheduler.cc')
-rw-r--r--072scheduler.cc59
1 files changed, 59 insertions, 0 deletions
diff --git a/072scheduler.cc b/072scheduler.cc
index 61d759ea..068e1617 100644
--- a/072scheduler.cc
+++ b/072scheduler.cc
@@ -171,6 +171,7 @@ case START_RUNNING: {
     reagent/*copy*/ ingredient = current_instruction().ingredients.at(i);
     canonize_type(ingredient);
     new_routine->calls.front().ingredients.push_back(ingredient);
+    // End Populate start-running Ingredient
   }
   Routines.push_back(new_routine);
   products.resize(1);
@@ -225,6 +226,64 @@ def f2 [
 ]
 +mem: storing 4 in location 2
 
+//: more complex: refcounting management when starting up new routines
+
+:(scenario start_running_immediately_updates_refcounts_of_ingredients)
+def main [
+  local-scope
+  create-new-routine
+  switch  # make sure we run new routine before returning
+]
+def create-new-routine [
+  local-scope
+  n:address:number <- new number:type
+  *n <- copy 34
+  start-running new-routine, n
+  # refcount of n decremented
+]
+def new-routine n:address:number [
+  local-scope
+  load-ingredients
+  1:number/raw <- copy *n
+]
+# check that n wasn't reclaimed when create-new-routine returned
++mem: storing 34 in location 1
+
+//: to support the previous scenario we'll increment refcounts for all call
+//: ingredients right at call time, and stop incrementing refcounts inside
+//: next-ingredient
+:(before "End Populate Call Ingredient")
+increment_any_refcounts(ingredient, ingredients.at(i));
+:(before "End Populate start-running Ingredient")
+increment_any_refcounts(ingredient, ingredients.at(i));
+:(before "End should_update_refcounts_in_write_memory Special-cases For Primitives")
+if (inst.operation == NEXT_INGREDIENT || inst.operation == NEXT_INGREDIENT_WITHOUT_TYPECHECKING)
+  return false;
+:(code)
+void increment_any_refcounts(const reagent& x, const vector<double>& data) {
+  if (is_mu_address(x)) {
+    assert(scalar(data));
+    assert(x.value);
+    assert(!x.metadata.size);
+    increment_refcount(data.at(0));
+  }
+  if (is_mu_container(x) || is_mu_exclusive_container(x)) {
+    const container_metadata& metadata = get(Container_metadata, x.type);
+    for (map<set<tag_condition_info>, set<address_element_info> >::const_iterator p = metadata.address.begin(); p != metadata.address.end(); ++p) {
+      if (!all_match(data, p->first)) continue;
+      for (set<address_element_info>::const_iterator info = p->second.begin(); info != p->second.end(); ++info)
+        increment_refcount(data.at(info->offset));
+    }
+  }
+}
+
+void increment_refcount(int address) {
+  if (address == 0) return;
+  int refcount = get_or_insert(Memory, address);
+  trace(9999, "mem") << "incrementing refcount of " << address << ": " << refcount << " -> " << refcount+1 << end();
+  put(Memory, address, refcount+1);
+}
+
 :(scenario start_running_returns_routine_id)
 def f1 [
   1:number <- start-running f2
com> 2020-07-10 21:08:26 -0700 committer Kartik Agaram <vc@akkartik.com> 2020-07-10 21:08:26 -0700 6627' href='/akkartik/mu/commit/103kernel-string-equal.subx?h=hlt&id=34dd228eb2706674f3ce06297997ce11f42768c8'>34dd228e ^
6030d7e2 ^
bfcc0f85 ^
1639687b ^

6030d7e2 ^
1639687b ^
6030d7e2 ^

1639687b ^


6030d7e2 ^

1639687b ^
33352536 ^





1639687b ^
7a583220 ^
33352536 ^

aff782c4 ^
33352536 ^




71eb22a5 ^
33352536 ^
bfcc0f85 ^
33352536 ^

71eb22a5 ^
33352536 ^

71eb22a5 ^
33352536 ^
71eb22a5 ^
33352536 ^
71eb22a5 ^
33352536 ^
71ee78f2 ^
1639687b ^
33352536 ^
6070c23e ^
1639687b ^
33352536 ^
1639687b ^
33352536 ^
57628c0e ^
6323661c ^
6070c23e ^
57628c0e ^
33352536 ^
6070c23e ^
1639687b ^
33352536 ^
1639687b ^
33352536 ^
1639687b ^
33352536 ^
71ee78f2 ^

1639687b ^
33352536 ^
6323661c ^
6070c23e ^
71ee78f2 ^
33352536 ^
71ee78f2 ^
71ee78f2 ^
33352536 ^
71ee78f2 ^
ee9a9237 ^
33352536 ^




7a583220 ^
33352536 ^

6030d7e2 ^
57628c0e ^
9d27e966 ^
57628c0e ^
03d50cc8 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
71ee78f2 ^
ee9a9237 ^
33352536 ^

ee9a9237 ^
6030d7e2 ^

33352536 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
33352536 ^
6030d7e2 ^
57628c0e ^
03d50cc8 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
71ee78f2 ^
ee9a9237 ^
33352536 ^

ee9a9237 ^
6030d7e2 ^

33352536 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
33352536 ^
6030d7e2 ^
57628c0e ^
03d50cc8 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
8b9dd2d1 ^
ee9a9237 ^
71ee78f2 ^
ee9a9237 ^
33352536 ^

ee9a9237 ^
6030d7e2 ^

33352536 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
33352536 ^
6030d7e2 ^
57628c0e ^
03d50cc8 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
8b9dd2d1 ^
ee9a9237 ^
71ee78f2 ^
ee9a9237 ^
33352536 ^

ee9a9237 ^
6030d7e2 ^

33352536 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
33352536 ^
6030d7e2 ^
57628c0e ^
03d50cc8 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
8b9dd2d1 ^
ee9a9237 ^
71ee78f2 ^
ee9a9237 ^
33352536 ^

ee9a9237 ^
6030d7e2 ^

33352536 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
33352536 ^
6030d7e2 ^
57628c0e ^
03d50cc8 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
8b9dd2d1 ^
ee9a9237 ^
71ee78f2 ^
ee9a9237 ^
33352536 ^

ee9a9237 ^
6030d7e2 ^

33352536 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
33352536 ^
6030d7e2 ^
57628c0e ^
03d50cc8 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
8b9dd2d1 ^
ee9a9237 ^
71ee78f2 ^
ee9a9237 ^
33352536 ^

ee9a9237 ^
6030d7e2 ^

33352536 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
33352536 ^
6030d7e2 ^
57628c0e ^


7e7a8a6e ^
6030d7e2 ^
1639687b ^
7e7a8a6e ^
6030d7e2 ^
4224ec81 ^
ee9a9237 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269