- added logger
[strongswan.git] / Source / charon / scheduler.c
1 /**
2 * @file scheduler.c
3 *
4 * @brief implements the scheduler, looks for jobs in event-queue
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 "globals.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_s private_scheduler_t;
38
39 struct private_scheduler_s {
40 /**
41 * Public part of a scheduler object
42 */
43 scheduler_t public;
44
45 /**
46 * Assigned thread to the scheduler_t object
47 */
48 pthread_t assigned_thread;
49
50 /**
51 * logger for this scheduler
52 */
53 logger_t *logger;
54
55 };
56
57 /**
58 * Thread function started at creation of the scheduler object
59 *
60 * @param this assigned scheduler object
61 * @return SUCCESS if thread_function ended successfully, FAILED otherwise
62 */
63 static void scheduler_thread_function(private_scheduler_t * this)
64 {
65 /* cancellation disabled by default */
66 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
67 job_t *current_job;
68
69 for (;;)
70 {
71 this->logger->log(this->logger, CONTROL, "waiting for next event...");
72 /* get a job, this block until one is available */
73 global_event_queue->get(global_event_queue, &current_job);
74 /* queue the job in the job queue, workers will eat them */
75 global_job_queue->add(global_job_queue, current_job);
76 this->logger->log(this->logger, CONTROL, "got event, added job %s to job-queue.", mapping_find(job_type_m, current_job->get_type(current_job)));
77 }
78 }
79
80 /**
81 * Implementation of scheduler_t's destroy function
82 */
83 static status_t destroy(private_scheduler_t *this)
84 {
85 pthread_cancel(this->assigned_thread);
86
87 pthread_join(this->assigned_thread, NULL);
88
89 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
90
91 allocator_free(this);
92 return SUCCESS;
93 }
94
95
96 scheduler_t * scheduler_create()
97 {
98 private_scheduler_t *this = allocator_alloc_thing(private_scheduler_t);
99
100 this->public.destroy = (status_t(*)(scheduler_t*)) destroy;
101
102 this->logger = global_logger_manager->create_logger(global_logger_manager, SCHEDULER_THREAD, NULL);
103 if (this->logger == NULL)
104 {
105 allocator_free(this);
106 return NULL;
107 }
108
109 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))scheduler_thread_function, this) != 0)
110 {
111 /* thread could not be created */
112 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
113 allocator_free(this);
114 return NULL;
115 }
116
117 return &(this->public);
118 }