updated copyright information
[strongswan.git] / src / charon / threads / scheduler.c
1 /**
2 * @file scheduler.c
3 *
4 * @brief Implementation of scheduler_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
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>.
17 *
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
21 * for more details.
22 */
23
24 #include <stdlib.h>
25 #include <pthread.h>
26
27 #include "scheduler.h"
28
29 #include <daemon.h>
30 #include <definitions.h>
31 #include <utils/logger_manager.h>
32 #include <queues/job_queue.h>
33
34
35 typedef struct private_scheduler_t private_scheduler_t;
36
37 /**
38 * Private data of a scheduler_t object.
39 */
40 struct private_scheduler_t {
41 /**
42 * Public part of a scheduler_t object.
43 */
44 scheduler_t public;
45
46 /**
47 * @brief Get events from the event queue and add them to to job queue.
48 *
49 * Thread function started at creation of the scheduler object.
50 *
51 * @param this calling object
52 */
53 void (*get_events) (private_scheduler_t *this);
54
55 /**
56 * Assigned thread.
57 */
58 pthread_t assigned_thread;
59
60 /**
61 * A logger.
62 */
63 logger_t *logger;
64 };
65
66 /**
67 * Implementation of private_scheduler_t.get_events.
68 */
69 static void get_events(private_scheduler_t * this)
70 {
71 job_t *current_job;
72
73 /* cancellation disabled by default */
74 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
75
76 this->logger->log(this->logger, CONTROL, "scheduler thread running, thread_ID: %06u", (int)pthread_self());
77
78 for (;;)
79 {
80 this->logger->log(this->logger, CONTROL|LEVEL2, "Waiting for next event...");
81 /* get a job, this block until one is available */
82 current_job = charon->event_queue->get(charon->event_queue);
83 /* queue the job in the job queue, workers will eat them */
84 charon->job_queue->add(charon->job_queue, current_job);
85 this->logger->log(this->logger, CONTROL | LEVEL1, "Got event, added job %s to job-queue.",
86 mapping_find(job_type_m, current_job->get_type(current_job)));
87 }
88 }
89
90 /**
91 * Implementation of scheduler_t.destroy.
92 */
93 static void destroy(private_scheduler_t *this)
94 {
95 this->logger->log(this->logger, CONTROL | LEVEL1, "Going to terminate scheduler thread");
96 pthread_cancel(this->assigned_thread);
97
98 pthread_join(this->assigned_thread, NULL);
99 this->logger->log(this->logger, CONTROL | LEVEL1, "Scheduler thread terminated");
100
101 free(this);
102 }
103
104 /*
105 * Described in header.
106 */
107 scheduler_t * scheduler_create()
108 {
109 private_scheduler_t *this = malloc_thing(private_scheduler_t);
110
111 this->public.destroy = (void(*)(scheduler_t*)) destroy;
112 this->get_events = get_events;
113
114 this->logger = logger_manager->get_logger(logger_manager, SCHEDULER);
115
116 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->get_events, this) != 0)
117 {
118 /* thread could not be created */
119 this->logger->log(this->logger, ERROR, "Scheduler thread could not be created!");
120 free(this);
121 charon->kill(charon, "Unable to create scheduler thread");
122 }
123
124 return &(this->public);
125 }