Skip to content
This repository
branch: whiteknight/gc…
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 344 lines (215 sloc) 7.849 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
/*
Copyright (C) 2001-2011, Parrot Foundation.

=head1 NAME

src/pmc/scheduler.pmc - The concurrency scheduler

=head1 DESCRIPTION

Implements the core concurrency scheduler.

=head2 Vtable Functions

=over 4

=cut

*/

#include "parrot/scheduler_private.h"

/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */

pmclass Scheduler auto_attrs {
    ATTR INTVAL id; /* The scheduler's ID. */
    ATTR PMC *handlers; /* The list of currently active handlers. */
    ATTR PMC *messages; /* A message queue used for communication
                                        between schedulers. */

    ATTR PMC *task_queue; /* List of tasks/green threads waiting to run */
    ATTR PMC *alarms; /* List of future alarms ordered by time */

    ATTR PMC *all_tasks; /* Hash of all active tasks by ID */
    ATTR UINTVAL next_task_id; /* ID to assign to the next created task */
    ATTR INTVAL enable_preemption;
                                     /* Can perform uncooperative task preemption */
    ATTR INTVAL enable_scheduling;
                                     /* Can perform task scheduling behaviors */

    ATTR Parrot_Interp interp; /* A link to the scheduler's interpreter. */

/*

=item C<void init()>

Initializes a concurrency scheduler object.

=cut

*/

    VTABLE void init() {
        Parrot_Scheduler_attributes * const core_struct =
            (Parrot_Scheduler_attributes *) PMC_data(SELF);

        /* Set flags for custom GC mark and destroy. */
        PObj_custom_mark_SET(SELF);
        PObj_custom_destroy_SET(SELF);

        /* Set up the core struct. */
        core_struct->id = 0;
        core_struct->handlers = Parrot_pmc_new(INTERP, enum_class_ResizablePMCArray);
        core_struct->messages = Parrot_pmc_new(INTERP, enum_class_ResizablePMCArray);
        core_struct->task_queue = Parrot_pmc_new(INTERP, enum_class_PMCList);
        core_struct->alarms = Parrot_pmc_new(INTERP, enum_class_PMCList);
        core_struct->all_tasks = Parrot_pmc_new(INTERP, enum_class_Hash);
        core_struct->enable_scheduling = 0;
        core_struct->enable_preemption = 0;
        core_struct->next_task_id = 0;
        core_struct->interp = INTERP;

        /* Chandon TODO: Delete from int-keyed hash doesn't like me. */
        /* VTABLE_set_integer_native(interp, core_struct->all_tasks, Hash_key_type_int); */

    }


/*

=item C<void init_pmc(PMC *data)>

Initializes a new Scheduler with a C<Hash> PMC with any or all of the keys:

=over 4

=item C<id>

An C<Integer> representing the unique identifier for this scheduler.

=back

=cut

*/

    VTABLE void init_pmc(PMC *data) {
        PMC *elem;
        Parrot_Scheduler_attributes *core_struct;

        if (!VTABLE_isa(INTERP, data, CONST_STRING(INTERP, "Hash")))
            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                "Scheduler initializer must be a Hash");

        SELF.init();

        core_struct = PARROT_SCHEDULER(SELF);
        elem = VTABLE_get_pmc_keyed_str(INTERP, data, CONST_STRING(INTERP, "id"));

        if (!PMC_IS_NULL(elem))
            core_struct->id = VTABLE_get_integer(INTERP, elem);
    }

/*

=item C<void push_pmc(PMC *value)>

Inserts a task into the task list, giving it a task ID one higher than the
current maximum, and a birthtime of the current time.

=cut

*/

    void push_pmc(PMC *task) {
        /* TODO: This doesn't appear to do anything */
        Parrot_Scheduler_attributes * const core_struct = PARROT_SCHEDULER(SELF);
        PMC * const type_pmc = VTABLE_get_attr_str(interp, task, CONST_STRING(interp, "type"));
        STRING * const type = VTABLE_get_string(interp, type_pmc);
    }


/*

=item C<PMC *pop_pmc()>

Retrieves the next task from the task list. If the task index is invalid,
recalculates it before retrieving the next task.

=cut

*/

    VTABLE PMC *pop_pmc() {
        Parrot_Scheduler_attributes * const core_struct = PARROT_SCHEDULER(SELF);
        PMC * const task = VTABLE_shift_pmc(INTERP, core_struct->task_queue);
        return task;
    }


/*

=item C<INTVAL get_integer()>

Retrieves the number of pending tasks in the scheduler's task list.

=cut

*/

    VTABLE INTVAL get_integer() {
        Parrot_Scheduler_attributes * const core_struct = PARROT_SCHEDULER(SELF);
        return VTABLE_elements(INTERP, core_struct->task_queue);
    }


/*

=item C<void destroy()>

Frees the scheduler's underlying struct.

=cut

*/
    VTABLE void destroy() {
        UNUSED(SELF);
        UNUSED(interp);
    }


/*

=item C<void mark()>

Marks any referenced strings and PMCs as live.

=cut

*/
    VTABLE void mark() {
        if (PARROT_SCHEDULER(SELF)) {
            Parrot_Scheduler_attributes * const core_struct = PARROT_SCHEDULER(SELF);

            Parrot_gc_mark_PMC_alive(INTERP, core_struct->handlers);
            Parrot_gc_mark_PMC_alive(INTERP, core_struct->messages);
            Parrot_gc_mark_PMC_alive(INTERP, core_struct->task_queue);
            Parrot_gc_mark_PMC_alive(INTERP, core_struct->alarms);
            Parrot_gc_mark_PMC_alive(INTERP, core_struct->all_tasks);
       }
    }


/*

=item C<void visit(PMC *info)>

Visits the contents of the scheduler (used by freeze/thaw).

C<*info> is the visit info (see F<include/parrot/pmc_freeze.h>).

=cut

*/

    VTABLE void visit(PMC *info) {
        /* 1) visit task list */
        VISIT_PMC_ATTR(INTERP, info, SELF, Scheduler, task_queue);

        /* 2) visit the handlers */
        VISIT_PMC_ATTR(INTERP, info, SELF, Scheduler, handlers);

        /* 3) visit the alarms */
        VISIT_PMC_ATTR(INTERP, info, SELF, Scheduler, alarms);

        /* 3) visit all tasks */
        VISIT_PMC_ATTR(INTERP, info, SELF, Scheduler, all_tasks);
    }


/*

=item C<void freeze(PMC *info)>

Archives the scheduler.

=cut

*/

    VTABLE void freeze(PMC *info) {
        Parrot_Scheduler_attributes * const core_struct = PARROT_SCHEDULER(SELF);

        /* 1) freeze scheduler id */
        VTABLE_push_integer(INTERP, info, core_struct->id);
    }

/*

=item C<void thaw(PMC *info)>

Unarchives the scheduler.

=cut

*/

    VTABLE void thaw(PMC *info) {
        /* 1. thaw scheduler id */
        const INTVAL id = VTABLE_shift_integer(INTERP, info);

        /* Allocate the scheduler's core data struct and set custom flags. */
        SELF.init();

        /* Set the scheduler's id to the frozen id */
        PARROT_SCHEDULER(SELF)->id = id;
    }


/*

=item C<void thawfinish(PMC *info)>

Finishes thawing the scheduler.

=cut

*/

    VTABLE void thawfinish(PMC *info) {
        /* Parrot_cx_refresh_task_list(INTERP, SELF); */
    }


/*

=back

=head2 Methods

=over 4

=cut

*/

/*

=item C<METHOD active_tasks()>

Returns a ResizablePMCArray containing pointers to all active tasks.

=cut

*/

    METHOD active_tasks() {
        Parrot_Scheduler_attributes *sdata = PARROT_SCHEDULER(SELF);
        PMC * const tasks = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
        PMC * const iter = Parrot_pmc_new_init(interp, enum_class_HashIterator, sdata->all_tasks);

        while (!VTABLE_get_bool(interp, iter)) {
            PMC * const pair = VTABLE_shift_pmc(interp, iter);
            PMC * task = PMCNULL;
            STRING * const value = CONST_STRING(interp, "value");
            Parrot_pcc_invoke_method_from_c_args(interp, pair,
                value, "->P", &task);
            VTABLE_push_pmc(interp, tasks, task);
        }

        RETURN(PMC* tasks);
    }
}

/*

=back

=head1 SEE ALSO

F<docs/pdds/pdd25_concurrency.pod>.

=cut

*/

/*
 * Local variables:
 * c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
 */
Something went wrong with that request. Please try again.