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