Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 548 lines (359 sloc) 14.328 kb
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
1 /*
ab0dec3 @petdance consting
petdance authored
2 Copyright (C) 2001-2010, Parrot Foundation.
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
3 $Id$
4
5 =head1 NAME
6
7 src/pmc/scheduler.pmc - The concurrency scheduler
8
9 =head1 DESCRIPTION
10
11 Implements the core concurrency scheduler.
12
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
13 =head2 Vtable Functions
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
14
15 =over 4
16
17 =cut
18
19 */
20
ec73fb3 @allisonrandal [concurrency] Divide scheduler headers into public and private (for codi...
allisonrandal authored
21 #include "parrot/scheduler_private.h"
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
22
6b81760 @Whiteknight [pmc] we don't need need_ext anymore, so BALEETED
Whiteknight authored
23 pmclass Scheduler auto_attrs {
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
24
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
25 ATTR INTVAL id; /* The scheduler's ID. */
26 ATTR INTVAL max_tid; /* The highest assigned task ID. */
27 ATTR INTVAL pending; /* A count of pending tasks (cached for fast
28 lookup). */
29 ATTR PMC *task_list; /* The current list of tasks. */
30 ATTR PMC *task_index; /* An index into the current list of tasks,
31 ordered by priority. */
32 ATTR PMC *wait_index; /* An unordered index of inactive tasks. */
33 ATTR PMC *handlers; /* The list of currently active handlers. */
34 ATTR PMC *messages; /* A message queue used for communication
35 between schedulers. */
36 ATTR Parrot_mutex msg_lock; /* Lock to synchronize the message queue. */
37 ATTR Parrot_Interp interp; /* A link to the scheduler's interpreter. */
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
38
39 /*
40
41 =item C<void init()>
42
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
43 Initializes a concurrency scheduler object.
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
44
45 =cut
46
47 */
48
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
49 VTABLE void init() {
971e997 @NotFound rename pmc attributes struct from Parrot_<type> to Parrot_<type>_attribu...
NotFound authored
50 Parrot_Scheduler_attributes * const core_struct =
5a7584e @NotFound merge auto_attrs branch into trunk
NotFound authored
51 (Parrot_Scheduler_attributes *) PMC_data(SELF);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
52
1997c49 @cotto [gc] remove/replace DOD references in core PMCs
cotto authored
53 /* Set flags for custom GC mark and destroy. */
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
54 PObj_custom_mark_SET(SELF);
4591dbb @chromatic [HLL] Fixed two compilation-breaking typos accidentally committed in r40...
chromatic authored
55 PObj_custom_destroy_SET(SELF);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
56
57 /* Set up the core struct. */
58 core_struct->id = 0;
59 core_struct->max_tid = 0;
083213a @Whiteknight start rename of pmc_new to Parrot_pmc_new
Whiteknight authored
60 core_struct->task_list = Parrot_pmc_new(interp, enum_class_Hash);
61 core_struct->task_index = Parrot_pmc_new(interp, enum_class_ResizableIntegerArray);
62 core_struct->wait_index = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
63 core_struct->handlers = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
64 core_struct->messages = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
2f43c23 @allisonrandal [concurrency] Initialize interp struct member in Scheduler. Add a way to
allisonrandal authored
65 core_struct->interp = INTERP;
4d9c960 @allisonrandal [concurrency] Next stage of concurrency implementation, message passing ...
allisonrandal authored
66 MUTEX_INIT(core_struct->msg_lock);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
67 }
68
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
69
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
70 /*
71
96858ee @chromatic [PMC] Added init_pmc() to Scheduler PMC, with tests.
chromatic authored
72 =item C<void init_pmc(PMC *data)>
73
74 Initializes a new Scheduler with a C<Hash> PMC with any or all of the keys:
75
76 =over 4
77
78 =item C<id>
79
80 An C<Integer> representing the unique identifier for this scheduler.
81
82 =back
83
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
84 =cut
85
96858ee @chromatic [PMC] Added init_pmc() to Scheduler PMC, with tests.
chromatic authored
86 */
87
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
88 VTABLE void init_pmc(PMC *data) {
96858ee @chromatic [PMC] Added init_pmc() to Scheduler PMC, with tests.
chromatic authored
89 PMC *elem;
971e997 @NotFound rename pmc attributes struct from Parrot_<type> to Parrot_<type>_attribu...
NotFound authored
90 Parrot_Scheduler_attributes *core_struct;
96858ee @chromatic [PMC] Added init_pmc() to Scheduler PMC, with tests.
chromatic authored
91
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
92 if (!VTABLE_isa(INTERP, data, CONST_STRING(INTERP, "Hash")))
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
93 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
4d9c960 @allisonrandal [concurrency] Next stage of concurrency implementation, message passing ...
allisonrandal authored
94 "Scheduler initializer must be a Hash");
96858ee @chromatic [PMC] Added init_pmc() to Scheduler PMC, with tests.
chromatic authored
95
4d9c960 @allisonrandal [concurrency] Next stage of concurrency implementation, message passing ...
allisonrandal authored
96 SELF.init();
96858ee @chromatic [PMC] Added init_pmc() to Scheduler PMC, with tests.
chromatic authored
97
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
98 core_struct = PARROT_SCHEDULER(SELF);
99 elem = VTABLE_get_pmc_keyed_str(INTERP, data, CONST_STRING(INTERP, "id"));
96858ee @chromatic [PMC] Added init_pmc() to Scheduler PMC, with tests.
chromatic authored
100
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
101 if (!PMC_IS_NULL(elem))
102 core_struct->id = VTABLE_get_integer(INTERP, elem);
96858ee @chromatic [PMC] Added init_pmc() to Scheduler PMC, with tests.
chromatic authored
103 }
104
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
105
96858ee @chromatic [PMC] Added init_pmc() to Scheduler PMC, with tests.
chromatic authored
106 /*
107
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
108 =item C<void push_pmc(PMC *value)>
109
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
110 Inserts a task into the task list, giving it a task ID one higher than the
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
111 current maximum, and a birthtime of the current time.
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
112
113 =cut
114
115 */
116
e922116 @allisonrandal [concurrency] Initial implementation of concurrency scheduler runloop.
allisonrandal authored
117 void push_pmc(PMC *task) {
971e997 @NotFound rename pmc attributes struct from Parrot_<type> to Parrot_<type>_attribu...
NotFound authored
118 Parrot_Scheduler_attributes * const core_struct = PARROT_SCHEDULER(SELF);
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
119 STRING *task_id_str;
120 INTVAL new_tid;
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
121
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
122 task = VTABLE_share_ro(INTERP, task);
123 VTABLE_set_number_native(INTERP, task, Parrot_floatval_time());
d0fb13e @allisonrandal [concurrency] Tasks are marked as shared before being inserted into the ...
allisonrandal authored
124
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
125 new_tid = ++(core_struct->max_tid);
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
126 VTABLE_set_integer_native(INTERP, task, new_tid);
b6bc0c3 @allisonrandal [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allisonrandal authored
127 task_id_str = Parrot_str_from_int(INTERP, new_tid);
245b0a8 @allisonrandal [concurrency] Change an array of task IDs to an integer array rather tha...
allisonrandal authored
128
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
129 VTABLE_set_pmc_keyed_str(INTERP, core_struct->task_list,
130 task_id_str, task);
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
131
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
132 if (task->vtable->base_type == enum_class_Timer)
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
133 VTABLE_push_integer(INTERP, core_struct->wait_index, new_tid);
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
134 else
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
135 VTABLE_push_integer(INTERP, core_struct->task_index, new_tid);
136
137 SCHEDULER_cache_valid_CLEAR(SELF);
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
138
139 if (task->vtable->base_type != enum_class_Exception)
140 Parrot_cx_runloop_wake(core_struct->interp, SELF);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
141 }
142
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
143
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
144 /*
145
146 =item C<PMC *pop_pmc()>
147
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
148 Retrieves the next task from the task list. If the task index is invalid,
149 recalculates it before retrieving the next task.
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
150
151 =cut
152
153 */
154
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
155 VTABLE PMC *pop_pmc() {
971e997 @NotFound rename pmc attributes struct from Parrot_<type> to Parrot_<type>_attribu...
NotFound authored
156 Parrot_Scheduler_attributes * core_struct = PARROT_SCHEDULER(SELF);
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
157 PMC *task = PMCNULL;
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
158
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
159 /* Pull the next valid task off the task list, skipping expired and
160 * deleted tasks. */
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
161 while (PMC_IS_NULL(task)
162 && VTABLE_elements(INTERP, core_struct->task_index) > 0) {
ab0dec3 @petdance consting
petdance authored
163 const INTVAL tid = VTABLE_shift_integer(INTERP, core_struct->task_index);
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
164
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
165 if (tid > 0)
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
166 task = VTABLE_get_pmc_keyed_int(INTERP,
167 core_struct->task_list, tid);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
168 }
169
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
170 return task;
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
171 }
172
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
173
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
174 /*
175
2f43c23 @allisonrandal [concurrency] Initialize interp struct member in Scheduler. Add a way to
allisonrandal authored
176 =item C<INTVAL get_integer()>
177
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
178 Retrieves the number of pending tasks in the scheduler's task list.
2f43c23 @allisonrandal [concurrency] Initialize interp struct member in Scheduler. Add a way to
allisonrandal authored
179
180 =cut
181
182 */
183
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
184 VTABLE INTVAL get_integer() {
971e997 @NotFound rename pmc attributes struct from Parrot_<type> to Parrot_<type>_attribu...
NotFound authored
185 Parrot_Scheduler_attributes * core_struct = PARROT_SCHEDULER(SELF);
2f43c23 @allisonrandal [concurrency] Initialize interp struct member in Scheduler. Add a way to
allisonrandal authored
186 return VTABLE_elements(INTERP, core_struct->task_index);
187 }
188
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
189
2f43c23 @allisonrandal [concurrency] Initialize interp struct member in Scheduler. Add a way to
allisonrandal authored
190 /*
191
192 =item C<void delete_keyed_int(INTVAL key)>
193
194 Removes the task with the given task ID from the task list.
195
196 =cut
197
198 */
199
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
200 VTABLE void delete_keyed_int(INTVAL key) {
ab0dec3 @petdance consting
petdance authored
201 Parrot_Scheduler_attributes * const core_struct = PARROT_SCHEDULER(SELF);
202 STRING * const task_id_str = Parrot_str_from_int(INTERP, key);
203
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
204 VTABLE_delete_keyed_str(INTERP, core_struct->task_list, task_id_str);
2f43c23 @allisonrandal [concurrency] Initialize interp struct member in Scheduler. Add a way to
allisonrandal authored
205 SCHEDULER_cache_valid_CLEAR(SELF);
206 }
207
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
208
2f43c23 @allisonrandal [concurrency] Initialize interp struct member in Scheduler. Add a way to
allisonrandal authored
209 /*
210
e922116 @allisonrandal [concurrency] Initial implementation of concurrency scheduler runloop.
allisonrandal authored
211 =item C<PMC *share_ro()>
212
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
213 Sets this PMC as shared.
e922116 @allisonrandal [concurrency] Initial implementation of concurrency scheduler runloop.
allisonrandal authored
214
215 =cut
216
217 */
218
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
219 VTABLE PMC *share_ro() {
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
220 PMC *shared_self;
971e997 @NotFound rename pmc attributes struct from Parrot_<type> to Parrot_<type>_attribu...
NotFound authored
221 Parrot_Scheduler_attributes *sched;
e922116 @allisonrandal [concurrency] Initial implementation of concurrency scheduler runloop.
allisonrandal authored
222
223 if (PObj_is_PMC_shared_TEST(SELF))
224 return SELF;
225
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
226 shared_self = pt_shared_fixup(INTERP, SELF);
227 sched = PARROT_SCHEDULER(shared_self);
e922116 @allisonrandal [concurrency] Initial implementation of concurrency scheduler runloop.
allisonrandal authored
228
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
229 sched->task_list = pt_shared_fixup(INTERP, sched->task_list);
230 sched->task_index = pt_shared_fixup(INTERP, sched->task_index);
231 sched->wait_index = pt_shared_fixup(INTERP, sched->wait_index);
232 sched->handlers = pt_shared_fixup(INTERP, sched->handlers);
233 sched->messages = pt_shared_fixup(INTERP, sched->messages);
e922116 @allisonrandal [concurrency] Initial implementation of concurrency scheduler runloop.
allisonrandal authored
234
235 return shared_self;
236 }
237
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
238
e922116 @allisonrandal [concurrency] Initial implementation of concurrency scheduler runloop.
allisonrandal authored
239 /*
240
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
241 =item C<void destroy()>
242
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
243 Frees the scheduler's underlying struct.
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
244
245 =cut
246
247 */
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
248 VTABLE void destroy() {
971e997 @NotFound rename pmc attributes struct from Parrot_<type> to Parrot_<type>_attribu...
NotFound authored
249 Parrot_Scheduler_attributes * const core_struct = PARROT_SCHEDULER(SELF);
540d4c3 @bacek Merge context_pmc3 branch into trunk.
bacek authored
250 /* TT #946: this line is causing an order-of-destruction error
251 because the scheduler is being freed before it's tasks.
252 Commenting this out till we get a real fix (although it's a hack) */
253 /* MUTEX_DESTROY(core_struct->msg_lock); */
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
254 }
255
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
256
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
257 /*
258
259 =item C<void mark()>
260
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
261 Marks any referenced strings and PMCs as live.
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
262
263 =cut
264
265 */
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
266 VTABLE void mark() {
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
267 if (PARROT_SCHEDULER(SELF)) {
971e997 @NotFound rename pmc attributes struct from Parrot_<type> to Parrot_<type>_attribu...
NotFound authored
268 Parrot_Scheduler_attributes * const core_struct = PARROT_SCHEDULER(SELF);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
269
18dd147 @NotFound [gc] add new mark_... _alive functions, use it in a lot of mark vtable f...
NotFound authored
270 Parrot_gc_mark_PMC_alive(interp, core_struct->task_list);
271 Parrot_gc_mark_PMC_alive(interp, core_struct->task_index);
272 Parrot_gc_mark_PMC_alive(interp, core_struct->wait_index);
273 Parrot_gc_mark_PMC_alive(interp, core_struct->handlers);
274 Parrot_gc_mark_PMC_alive(interp, core_struct->messages);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
275 }
276 }
277
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
278
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
279 /*
280
b101f2c @darbelo Merge pmc_freeze_with_pmcs into trunk.
darbelo authored
281 =item C<void visit(PMC *info)>
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
282
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
283 Visits the contents of the scheduler (used by freeze/thaw).
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
284
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
285 C<*info> is the visit info (see F<include/parrot/pmc_freeze.h>).
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
286
287 =cut
288
289 */
290
b101f2c @darbelo Merge pmc_freeze_with_pmcs into trunk.
darbelo authored
291 VTABLE void visit(PMC *info) {
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
292 /* 1) visit task list */
4c43475 @plobsing merge branch pmc_freeze_cleanup
plobsing authored
293 VISIT_PMC_ATTR(INTERP, info, SELF, Scheduler, task_list);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
294
295 /* 2) visit the handlers */
4c43475 @plobsing merge branch pmc_freeze_cleanup
plobsing authored
296 VISIT_PMC_ATTR(INTERP, info, SELF, Scheduler, handlers);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
297 }
298
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
299
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
300 /*
301
b101f2c @darbelo Merge pmc_freeze_with_pmcs into trunk.
darbelo authored
302 =item C<void freeze(PMC *info)>
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
303
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
304 Archives the scheduler.
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
305
306 =cut
307
308 */
309
b101f2c @darbelo Merge pmc_freeze_with_pmcs into trunk.
darbelo authored
310 VTABLE void freeze(PMC *info) {
971e997 @NotFound rename pmc attributes struct from Parrot_<type> to Parrot_<type>_attribu...
NotFound authored
311 Parrot_Scheduler_attributes * const core_struct = PARROT_SCHEDULER(SELF);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
312
313 /* 1) freeze scheduler id */
4c43475 @plobsing merge branch pmc_freeze_cleanup
plobsing authored
314 VTABLE_push_integer(INTERP, info, core_struct->id);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
315
316 /* 2) freeze maximum task id */
4c43475 @plobsing merge branch pmc_freeze_cleanup
plobsing authored
317 VTABLE_push_integer(INTERP, info, core_struct->max_tid);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
318 }
319
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
320
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
321 /*
322
b101f2c @darbelo Merge pmc_freeze_with_pmcs into trunk.
darbelo authored
323 =item C<void thaw(PMC *info)>
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
324
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
325 Unarchives the scheduler.
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
326
327 =cut
328
329 */
330
b101f2c @darbelo Merge pmc_freeze_with_pmcs into trunk.
darbelo authored
331 VTABLE void thaw(PMC *info) {
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
332 /* 1. thaw scheduler id */
4c43475 @plobsing merge branch pmc_freeze_cleanup
plobsing authored
333 const INTVAL id = VTABLE_shift_integer(INTERP, info);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
334
335 /* 2. thaw maximum task id */
4c43475 @plobsing merge branch pmc_freeze_cleanup
plobsing authored
336 const INTVAL max_tid = VTABLE_shift_integer(INTERP, info);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
337
338 /* Allocate the scheduler's core data struct and set custom flags. */
339 SELF.init();
340
341 /* Set the scheduler's id to the frozen id */
342 PARROT_SCHEDULER(SELF)->id = id;
343
344 /* Set the scheduler's maximum task id to the frozen tid */
345 PARROT_SCHEDULER(SELF)->max_tid = max_tid;
346 }
347
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
348
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
349 /*
350
b101f2c @darbelo Merge pmc_freeze_with_pmcs into trunk.
darbelo authored
351 =item C<void thawfinish(PMC *info)>
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
352
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
353 Finishes thawing the scheduler.
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
354
355 =cut
356
357 */
358
b101f2c @darbelo Merge pmc_freeze_with_pmcs into trunk.
darbelo authored
359 VTABLE void thawfinish(PMC *info) {
4d9c960 @allisonrandal [concurrency] Next stage of concurrency implementation, message passing ...
allisonrandal authored
360 Parrot_cx_refresh_task_list(INTERP, SELF);
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
361 }
362
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
363
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
364 /*
365
366 =back
367
368 =head2 Methods
369
370 =over 4
371
372 =cut
373
374 */
375
376 /*
377
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
378 =item C<METHOD add_handler(PMC *handler)>
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
379
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
380 Adds a handler to the scheduler.
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
381
382 =cut
383
384 */
385
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
386 METHOD add_handler(PMC *handler) {
971e997 @NotFound rename pmc attributes struct from Parrot_<type> to Parrot_<type>_attribu...
NotFound authored
387 Parrot_Scheduler_attributes *core_struct = PARROT_SCHEDULER(SELF);
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
388 VTABLE_unshift_pmc(INTERP, core_struct->handlers, handler);
389 }
390
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
391
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
392 /*
393
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
394 =item C<METHOD delete_handler(STRING *type :optional, INTVAL have_type :opt_flag)>
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
395
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
396 Deletes a handler from the scheduler.
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
397
398 =cut
399
400 */
401
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
402 METHOD delete_handler(STRING *type :optional, INTVAL have_type :opt_flag) {
403 PMC *handlers;
404 INTVAL elements, index;
ab0dec3 @petdance consting
petdance authored
405 STRING * const except_str = CONST_STRING(INTERP, "exception");
406 STRING * const event_str = CONST_STRING(INTERP, "event");
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
407
408 GET_ATTR_handlers(INTERP, SELF, handlers);
409 elements = VTABLE_elements(INTERP, handlers);
410
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
411 if (!have_type)
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
412 VTABLE_shift_pmc(INTERP, handlers);
413
414 /* Loop from newest handler to oldest handler. */
415 for (index = 0; index < elements; ++index) {
ab0dec3 @petdance consting
petdance authored
416 const PMC * const handler = VTABLE_get_pmc_keyed_int(INTERP, handlers, index);
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
417 if (!PMC_IS_NULL(handler)) {
4b380b1 @Util [cage] (TT #397) PDD07 enforcement for Parrot_str_not_equal(foo,bar)==0
Util authored
418 if (Parrot_str_equal(INTERP, type, except_str)
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
419 && handler->vtable->base_type == enum_class_ExceptionHandler) {
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
420 VTABLE_set_pmc_keyed_int(INTERP, handlers, index, PMCNULL);
421 RETURN(void);
422 }
4b380b1 @Util [cage] (TT #397) PDD07 enforcement for Parrot_str_not_equal(foo,bar)==0
Util authored
423 else if (Parrot_str_equal(INTERP, type, event_str)
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
424 && handler->vtable->base_type == enum_class_EventHandler) {
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
425 VTABLE_set_pmc_keyed_int(INTERP, handlers, index, PMCNULL);
426 RETURN(void);
427 }
428 }
429 }
430
431 Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
432 "No handler to delete.");
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
433 }
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
434
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
435
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
436 /*
437
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
438 =item C<METHOD find_handler(PMC *task)>
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
439
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
440 Searchs for a handler for the given task. If no handler is found, returns
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
441 PMCNULL.
442
443 =cut
444
445 */
446
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
447 METHOD find_handler(PMC *task) {
0006ce7 @chromatic [src] Fixed all CONST_STRING macros so that they don't break across line...
chromatic authored
448 STRING * const handled_str = CONST_STRING(interp, "handled");
449 STRING * const iter_str = CONST_STRING(interp, "handler_iter");
450 PMC *iter;
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
451
452 /* Exceptions store the handler iterator for rethrow, other kinds of
453 * tasks don't (though they could). */
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
454 if (task->vtable->base_type == enum_class_Exception
0006ce7 @chromatic [src] Fixed all CONST_STRING macros so that they don't break across line...
chromatic authored
455 && VTABLE_get_integer_keyed_str(interp, task, handled_str) == -1) {
456 iter = VTABLE_get_attr_str(interp, task, iter_str);
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
457 }
458 else {
459 PMC *handlers;
460 GET_ATTR_handlers(interp, SELF, handlers);
461 iter = VTABLE_get_iter(interp, handlers);
462
463 if (task->vtable->base_type == enum_class_Exception)
0006ce7 @chromatic [src] Fixed all CONST_STRING macros so that they don't break across line...
chromatic authored
464 VTABLE_set_attr_str(interp, task, iter_str, iter);
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
465 }
466
467 /* Loop from newest handler to oldest handler. */
468 while (VTABLE_get_bool(interp, iter)) {
ab0dec3 @petdance consting
petdance authored
469 PMC * const handler = VTABLE_shift_pmc(INTERP, iter);
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
470
471 INTVAL valid_handler = 0;
472 if (!PMC_IS_NULL(handler)) {
ab0dec3 @petdance consting
petdance authored
473 (const INTVAL valid_handler) = PCCINVOKE(INTERP, handler, "can_handle", PMC *task);
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
474 if (valid_handler) {
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
475 if (task->vtable->base_type == enum_class_Exception)
476 VTABLE_set_integer_native(interp, handler, 1);
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
477 RETURN(PMC *handler);
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
478 }
479 }
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
480
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
481 }
482
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
483 RETURN(PMC *PMCNULL);
98ca8df @allisonrandal [concurrency] Further work on the new concurrency scheduler. (With apolo...
allisonrandal authored
484 }
485
d6808fe @allisonrandal [events] Add interface for event handling, add subtype attribute to task...
allisonrandal authored
486
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
487 /*
488
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
489 =item C<METHOD count_handlers(STRING *type :optional, INTVAL have_type :opt_flag)>
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
490
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
491 Returns the number of handlers currently held by the scheduler. If a type
492 argument is passed, only counts handlers of that type (C<event>, C<exception>).
493 If no type argument is passed, counts all handlers.
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
494
495 =cut
496
497 */
498
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
499 METHOD count_handlers(STRING *type :optional, INTVAL have_type :opt_flag) {
500 /* avoid uninitialized value warning */
501 PMC *handlers = NULL;
9f26f43 @NotFound fix count_eh opcode and add test for it, TT #212 rg++
NotFound authored
502 INTVAL elements;
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
503 INTVAL count = 0;
504 INTVAL index;
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
505
506 GET_ATTR_handlers(INTERP, SELF, handlers);
9f26f43 @NotFound fix count_eh opcode and add test for it, TT #212 rg++
NotFound authored
507 elements = VTABLE_elements(INTERP, handlers);
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
508
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
509 if (!have_type)
510 RETURN(INTVAL elements);
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
511
512 for (index = 0; index < elements; ++index) {
ab0dec3 @petdance consting
petdance authored
513 const PMC * const handler = VTABLE_get_pmc_keyed_int(INTERP, handlers, index);
514 STRING * const exception = CONST_STRING(INTERP, "exception");
515 STRING * const event = CONST_STRING(INTERP, "event");
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
516
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
517 if (!PMC_IS_NULL(handler)) {
4b380b1 @Util [cage] (TT #397) PDD07 enforcement for Parrot_str_not_equal(foo,bar)==0
Util authored
518 if ((Parrot_str_equal(INTERP, type, exception)
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
519 && handler->vtable->base_type == enum_class_ExceptionHandler)
4b380b1 @Util [cage] (TT #397) PDD07 enforcement for Parrot_str_not_equal(foo,bar)==0
Util authored
520 || (Parrot_str_equal(INTERP, type, event)
be7602b @chromatic [PMC] Fixed compiler warnings (use of uninitialized variables) and tided...
chromatic authored
521 && handler->vtable->base_type == enum_class_EventHandler))
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
522 count++;
523 }
524 }
525
526 RETURN(INTVAL count);
527 }
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
528 }
529
530 /*
531
532 =back
533
534 =head1 SEE ALSO
535
4d9c960 @allisonrandal [concurrency] Next stage of concurrency implementation, message passing ...
allisonrandal authored
536 F<docs/pdds/pdd25_concurrency.pod>.
1d259c6 @allisonrandal [concurrency] Initial implementations of the Scheduler and Task PMCs.
allisonrandal authored
537
538 =cut
539
540 */
541
542 /*
543 * Local variables:
544 * c-file-style: "parrot"
545 * End:
546 * vim: expandtab shiftwidth=4:
547 */
Something went wrong with that request. Please try again.