fixed a at-least-one-year-old bug which caused crashed in the scheduler
[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 * Assigned thread.
48 */
49 pthread_t assigned_thread;
50
51 /**
52 * A logger.
53 */
54 logger_t *logger;
55 };
56
57 /**
58 * Implementation of private_scheduler_t.get_events.
59 */
60 static void get_events(private_scheduler_t * this)
61 {
62 job_t *current_job;
63
64 /* cancellation disabled by default */
65 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
66
67 this->logger->log(this->logger, CONTROL, "scheduler thread running, thread_ID: %06u", (int)pthread_self());
68
69 while (TRUE)
70 {
71 this->logger->log(this->logger, CONTROL|LEVEL2, "waiting for next event...");
72 /* get a job, this block until one is available */
73 current_job = charon->event_queue->get(charon->event_queue);
74 /* queue the job in the job queue, workers will eat them */
75 this->logger->log(this->logger, CONTROL | LEVEL1, "got event, adding job %s to job-queue.",
76 mapping_find(job_type_m, current_job->get_type(current_job)));
77 charon->job_queue->add(charon->job_queue, current_job);
78 }
79 }
80
81 /**
82 * Implementation of scheduler_t.destroy.
83 */
84 static void destroy(private_scheduler_t *this)
85 {
86 this->logger->log(this->logger, CONTROL | LEVEL1, "going to terminate scheduler thread");
87 pthread_cancel(this->assigned_thread);
88
89 pthread_join(this->assigned_thread, NULL);
90 this->logger->log(this->logger, CONTROL | LEVEL1, "scheduler thread terminated");
91
92 free(this);
93 }
94
95 /*
96 * Described in header.
97 */
98 scheduler_t * scheduler_create()
99 {
100 private_scheduler_t *this = malloc_thing(private_scheduler_t);
101
102 this->public.destroy = (void(*)(scheduler_t*)) destroy;
103
104 this->logger = logger_manager->get_logger(logger_manager, SCHEDULER);
105
106 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))get_events, this) != 0)
107 {
108 /* thread could not be created */
109 this->logger->log(this->logger, ERROR, "scheduler thread could not be created!");
110 free(this);
111 charon->kill(charon, "unable to create scheduler thread");
112 }
113
114 return &(this->public);
115 }