4 * @brief implements the scheduler, looks for jobs in event-queue
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 #include <pluto/constants.h>
27 #include <pluto/defs.h>
29 #include "scheduler.h"
30 #include "job_queue.h"
34 * Private data of a scheduler object
36 typedef struct private_scheduler_s private_scheduler_t
;
38 struct private_scheduler_s
{
40 * Public part of a scheduler object
45 * Assigned thread to the scheduler_t-object
47 pthread_t assigned_thread
;
52 * Thread function started at creation of the scheduler object
54 * @param this assigned scheduler object
55 * @return SUCCESS if thread_function ended successfully, FAILED otherwise
57 static void scheduler_thread_function(private_scheduler_t
* this)
59 /* cancellation disabled by default */
60 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
65 /* get a job, this block until one is available */
66 global_event_queue
->get(global_event_queue
, ¤t_job
);
67 /* queue the job in the job queue, workers will eat them */
68 global_job_queue
->add(global_job_queue
, current_job
);
73 * Implementation of scheduler_t's destroy function
75 static status_t
destroy(private_scheduler_t
*this)
77 pthread_cancel(this->assigned_thread
);
79 pthread_join(this->assigned_thread
, NULL
);
86 scheduler_t
* scheduler_create()
88 private_scheduler_t
*this = alloc_thing(private_scheduler_t
,"private_scheduler_t");
90 this->public.destroy
= (status_t(*)(scheduler_t
*)) destroy
;
91 if (pthread_create(&(this->assigned_thread
), NULL
, (void*(*)(void*))scheduler_thread_function
, this) != 0)
93 /* thread could not be created */
98 return &(this->public);