Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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