moved interface enumeration code to socket, where it belongs
[strongswan.git] / src / charon / threads / thread_pool.c
1 /**
2 * @file thread_pool.c
3 *
4 * @brief Implementation of thread_pool_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 #include <string.h>
27 #include <errno.h>
28
29 #include "thread_pool.h"
30
31 #include <daemon.h>
32 #include <queues/job_queue.h>
33 #include <utils/logger.h>
34
35
36 typedef struct private_thread_pool_t private_thread_pool_t;
37
38 /**
39 * @brief Private data of thread_pool_t class.
40 */
41 struct private_thread_pool_t {
42 /**
43 * Public thread_pool_t interface.
44 */
45 thread_pool_t public;
46
47 /**
48 * Number of running threads.
49 */
50 size_t pool_size;
51
52 /**
53 * Array of thread ids.
54 */
55 pthread_t *threads;
56
57 /**
58 * Logger of the thread pool.
59 */
60 logger_t *logger;
61 } ;
62
63 /**
64 * Implementation of private_thread_pool_t.process_jobs.
65 */
66 static void process_jobs(private_thread_pool_t *this)
67 {
68 job_t *job;
69 status_t status;
70
71 /* cancellation disabled by default */
72 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
73
74 this->logger->log(this->logger, CONTROL,
75 "worker thread running, thread_ID: %06u",
76 (int)pthread_self());
77
78 while (TRUE)
79 {
80 job = charon->job_queue->get(charon->job_queue);
81
82 status = job->execute(job);
83
84 if (status == DESTROY_ME)
85 {
86 job->destroy(job);
87 }
88 }
89 }
90
91 /**
92 * Implementation of thread_pool_t.get_pool_size.
93 */
94 static size_t get_pool_size(private_thread_pool_t *this)
95 {
96 return this->pool_size;
97 }
98
99 /**
100 * Implementation of thread_pool_t.destroy.
101 */
102 static void destroy(private_thread_pool_t *this)
103 {
104 int current;
105 /* flag thread for termination */
106 for (current = 0; current < this->pool_size; current++) {
107 this->logger->log(this->logger, CONTROL,
108 "cancelling worker thread #%d", current+1);
109 pthread_cancel(this->threads[current]);
110 }
111
112 /* wait for all threads */
113 for (current = 0; current < this->pool_size; current++) {
114 if (pthread_join(this->threads[current], NULL) == 0)
115 {
116 this->logger->log(this->logger, CONTROL,
117 "worker thread #%d terminated", current+1);
118 }
119 else
120 {
121 this->logger->log(this->logger, ERROR,
122 "could not terminate worker thread #%d", current+1);
123 }
124 }
125
126 /* free mem */
127 free(this->threads);
128 free(this);
129 }
130
131 /*
132 * Described in header.
133 */
134 thread_pool_t *thread_pool_create(size_t pool_size)
135 {
136 int current;
137 private_thread_pool_t *this = malloc_thing(private_thread_pool_t);
138
139 /* fill in public fields */
140 this->public.destroy = (void(*)(thread_pool_t*))destroy;
141 this->public.get_pool_size = (size_t(*)(thread_pool_t*))get_pool_size;
142
143 /* initialize member */
144 this->pool_size = pool_size;
145 this->threads = malloc(sizeof(pthread_t) * pool_size);
146 this->logger = logger_manager->get_logger(logger_manager, THREAD_POOL);
147
148 /* try to create as many threads as possible, up to pool_size */
149 for (current = 0; current < pool_size; current++)
150 {
151 if (pthread_create(&(this->threads[current]), NULL,
152 (void*(*)(void*))process_jobs, this) == 0)
153 {
154 this->logger->log(this->logger, CONTROL,
155 "created worker thread #%d", current+1);
156 }
157 else
158 {
159 /* creation failed, is it the first one? */
160 if (current == 0)
161 {
162 this->logger->log(this->logger, ERROR, "Could not create any thread");
163 free(this->threads);
164 free(this);
165 return NULL;
166 }
167 /* not all threads could be created, but at least one :-/ */
168 this->logger->log(this->logger, ERROR,
169 "Could only create %d from requested %d threads!",
170 current, pool_size);
171
172 this->pool_size = current;
173 return (thread_pool_t*)this;
174 }
175 }
176 return (thread_pool_t*)this;
177 }