4 * @brief Implementation of scheduler_t.
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 #include "scheduler.h"
30 #include <definitions.h>
31 #include <queues/job_queue.h>
34 typedef struct private_scheduler_t private_scheduler_t
;
37 * Private data of a scheduler_t object.
39 struct private_scheduler_t
{
41 * Public part of a scheduler_t object.
48 pthread_t assigned_thread
;
52 * Implementation of private_scheduler_t.get_events.
54 static void get_events(private_scheduler_t
* this)
58 /* cancellation disabled by default */
59 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
61 DBG1(DBG_JOB
, "scheduler thread running, thread_ID: %06u",
66 DBG2(DBG_JOB
, "waiting for next event...");
67 /* get a job, this block until one is available */
68 current_job
= charon
->event_queue
->get(charon
->event_queue
);
69 /* queue the job in the job queue, workers will eat them */
70 DBG2(DBG_JOB
, "got event, adding job %N to job-queue",
71 job_type_names
, current_job
->get_type(current_job
));
72 charon
->job_queue
->add(charon
->job_queue
, current_job
);
77 * Implementation of scheduler_t.destroy.
79 static void destroy(private_scheduler_t
*this)
81 pthread_cancel(this->assigned_thread
);
82 pthread_join(this->assigned_thread
, NULL
);
87 * Described in header.
89 scheduler_t
* scheduler_create()
91 private_scheduler_t
*this = malloc_thing(private_scheduler_t
);
93 this->public.destroy
= (void(*)(scheduler_t
*)) destroy
;
95 if (pthread_create(&(this->assigned_thread
), NULL
, (void*(*)(void*))get_events
, this) != 0)
97 /* thread could not be created */
99 charon
->kill(charon
, "unable to create scheduler thread");
102 return &(this->public);