]> gitweb.factorcode.org Git - factor.git/blob - vm/contexts.cpp
threads: delete old contexts immediately instead of handing them off to a 'context...
[factor.git] / vm / contexts.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 context::context(cell datastack_size, cell retainstack_size, cell callstack_size) :
7         callstack_top(NULL),
8         callstack_bottom(NULL),
9         datastack(0),
10         retainstack(0),
11         callstack_save(0),
12         datastack_seg(new segment(datastack_size,false)),
13         retainstack_seg(new segment(retainstack_size,false)),
14         callstack_seg(new segment(callstack_size,false))
15 {
16         reset();
17 }
18
19 void context::reset_datastack()
20 {
21         datastack = datastack_seg->start - sizeof(cell);
22 }
23
24 void context::reset_retainstack()
25 {
26         retainstack = retainstack_seg->start - sizeof(cell);
27 }
28
29 void context::reset_callstack()
30 {
31         callstack_top = callstack_bottom = CALLSTACK_BOTTOM(this);
32 }
33
34 void context::reset_context_objects()
35 {
36         memset_cell(context_objects,false_object,context_object_count * sizeof(cell));
37 }
38
39 void context::reset()
40 {
41         reset_datastack();
42         reset_retainstack();
43         reset_callstack();
44         reset_context_objects();
45 }
46
47 void context::fix_stacks()
48 {
49         if(datastack + sizeof(cell) < datastack_seg->start
50                 || datastack + stack_reserved >= datastack_seg->end)
51                 reset_datastack();
52
53         if(retainstack + sizeof(cell) < retainstack_seg->start
54                 || retainstack + stack_reserved >= retainstack_seg->end)
55                 reset_retainstack();
56 }
57
58 context::~context()
59 {
60         delete datastack_seg;
61         delete retainstack_seg;
62         delete callstack_seg;
63 }
64
65 /* called on startup */
66 void factor_vm::init_contexts(cell datastack_size_, cell retainstack_size_, cell callstack_size_)
67 {
68         datastack_size = datastack_size_;
69         retainstack_size = retainstack_size_;
70         callstack_size = callstack_size_;
71
72         ctx = NULL;
73         spare_ctx = new_context();
74 }
75
76 void factor_vm::delete_contexts()
77 {
78         assert(!ctx);
79         std::vector<context *>::const_iterator iter = unused_contexts.begin();
80         std::vector<context *>::const_iterator end = unused_contexts.end();
81         while(iter != end)
82         {
83                 delete *iter;
84                 iter++;
85         }
86 }
87
88 context *factor_vm::new_context()
89 {
90         context *new_context;
91
92         if(unused_contexts.empty())
93         {
94                 new_context = new context(datastack_size,
95                         retainstack_size,
96                         callstack_size);
97         }
98         else
99         {
100                 new_context = unused_contexts.back();
101                 unused_contexts.pop_back();
102         }
103
104         new_context->reset();
105
106         active_contexts.insert(new_context);
107
108         return new_context;
109 }
110
111 context *new_context(factor_vm *parent)
112 {
113         return parent->new_context();
114 }
115
116 void factor_vm::delete_context(context *old_context)
117 {
118         unused_contexts.push_back(old_context);
119         active_contexts.erase(old_context);
120 }
121
122 VM_C_API void delete_context(factor_vm *parent, context *old_context)
123 {
124         parent->delete_context(old_context);
125 }
126
127 void factor_vm::begin_callback()
128 {
129         ctx->reset();
130         spare_ctx = new_context();
131         callback_ids.push_back(callback_id++);
132 }
133
134 void begin_callback(factor_vm *parent)
135 {
136         parent->begin_callback();
137 }
138
139 void factor_vm::end_callback()
140 {
141         callback_ids.pop_back();
142         delete_context(ctx);
143 }
144
145 void end_callback(factor_vm *parent)
146 {
147         parent->end_callback();
148 }
149
150 void factor_vm::primitive_current_callback()
151 {
152         ctx->push(tag_fixnum(callback_ids.back()));
153 }
154
155 void factor_vm::primitive_context_object()
156 {
157         fixnum n = untag_fixnum(ctx->peek());
158         ctx->replace(ctx->context_objects[n]);
159 }
160
161 void factor_vm::primitive_set_context_object()
162 {
163         fixnum n = untag_fixnum(ctx->pop());
164         cell value = ctx->pop();
165         ctx->context_objects[n] = value;
166 }
167
168 void factor_vm::primitive_context_object_for()
169 {
170         context *other_ctx = (context *)pinned_alien_offset(ctx->pop());
171         fixnum n = untag_fixnum(ctx->pop());
172         ctx->push(other_ctx->context_objects[n]);
173 }
174
175 cell factor_vm::stack_to_array(cell bottom, cell top)
176 {
177         fixnum depth = (fixnum)(top - bottom + sizeof(cell));
178
179         if(depth < 0)
180                 return false_object;
181         else
182         {
183                 array *a = allot_uninitialized_array<array>(depth / sizeof(cell));
184                 memcpy(a + 1,(void*)bottom,depth);
185                 return tag<array>(a);
186         }
187 }
188
189 cell factor_vm::datastack_to_array(context *ctx)
190 {
191         cell array = stack_to_array(ctx->datastack_seg->start,ctx->datastack);
192         if(array == false_object)
193         {
194                 general_error(ERROR_DATASTACK_UNDERFLOW,false_object,false_object);
195                 return false_object;
196         }
197         else
198                 return array;
199 }
200
201 void factor_vm::primitive_datastack()
202 {
203         ctx->push(datastack_to_array(ctx));
204 }
205
206 void factor_vm::primitive_datastack_for()
207 {
208         context *other_ctx = (context *)pinned_alien_offset(ctx->pop());
209         ctx->push(datastack_to_array(other_ctx));
210 }
211
212 cell factor_vm::retainstack_to_array(context *ctx)
213 {
214         cell array = stack_to_array(ctx->retainstack_seg->start,ctx->retainstack);
215         if(array == false_object)
216         {
217                 general_error(ERROR_RETAINSTACK_UNDERFLOW,false_object,false_object);
218                 return false_object;
219         }
220         else
221                 return array;
222 }
223
224 void factor_vm::primitive_retainstack()
225 {
226         ctx->push(retainstack_to_array(ctx));
227 }
228
229 void factor_vm::primitive_retainstack_for()
230 {
231         context *other_ctx = (context *)pinned_alien_offset(ctx->pop());
232         ctx->push(retainstack_to_array(other_ctx));
233 }
234
235 /* returns pointer to top of stack */
236 cell factor_vm::array_to_stack(array *array, cell bottom)
237 {
238         cell depth = array_capacity(array) * sizeof(cell);
239         memcpy((void*)bottom,array + 1,depth);
240         return bottom + depth - sizeof(cell);
241 }
242
243 void factor_vm::set_datastack(context *ctx, array *array)
244 {
245         ctx->datastack = array_to_stack(array,ctx->datastack_seg->start);
246 }
247
248 void factor_vm::primitive_set_datastack()
249 {
250         set_datastack(ctx,untag_check<array>(ctx->pop()));
251 }
252
253 void factor_vm::set_retainstack(context *ctx, array *array)
254 {
255         ctx->retainstack = array_to_stack(array,ctx->retainstack_seg->start);
256 }
257
258 void factor_vm::primitive_set_retainstack()
259 {
260         set_retainstack(ctx,untag_check<array>(ctx->pop()));
261 }
262
263 /* Used to implement call( */
264 void factor_vm::primitive_check_datastack()
265 {
266         fixnum out = to_fixnum(ctx->pop());
267         fixnum in = to_fixnum(ctx->pop());
268         fixnum height = out - in;
269         array *saved_datastack = untag_check<array>(ctx->pop());
270         fixnum saved_height = array_capacity(saved_datastack);
271         fixnum current_height = (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) / sizeof(cell);
272         if(current_height - height != saved_height)
273                 ctx->push(false_object);
274         else
275         {
276                 cell *ds_bot = (cell *)ctx->datastack_seg->start;
277                 for(fixnum i = 0; i < saved_height - in; i++)
278                 {
279                         if(ds_bot[i] != array_nth(saved_datastack,i))
280                         {
281                                 ctx->push(false_object);
282                                 return;
283                         }
284                 }
285                 ctx->push(true_object);
286         }
287 }
288
289 void factor_vm::primitive_load_locals()
290 {
291         fixnum count = untag_fixnum(ctx->pop());
292         memcpy((cell *)(ctx->retainstack + sizeof(cell)),
293                 (cell *)(ctx->datastack - sizeof(cell) * (count - 1)),
294                 sizeof(cell) * count);
295         ctx->datastack -= sizeof(cell) * count;
296         ctx->retainstack += sizeof(cell) * count;
297 }
298
299 void factor_vm::primitive_context()
300 {
301         ctx->push(allot_alien(ctx));
302 }
303
304 }