4 * @brief Thread pool with some threads processing the job_queue.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
28 #include "allocator.h"
30 #include "thread_pool.h"
31 #include "job_queue.h"
35 * @brief structure with private members for thread_pool_t
37 typedef struct private_thread_pool_s private_thread_pool_t
;
39 struct private_thread_pool_s
{
41 * inclusion of public members
45 * @brief Processing function of a worker thread
47 * @param this private_thread_pool_t-Object
49 void (*function
) (private_thread_pool_t
*this);
51 * number of running threads
59 * logger of the threadpool
67 * implements private_thread_pool_t.function
69 static void job_processing(private_thread_pool_t
*this)
71 /* cancellation disabled by default */
72 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
74 this->logger
->log(this->logger
, CONTROL_MORE
, "thread %u started working", pthread_self());
79 global_job_queue
->get(global_job_queue
, &job
);
80 this->logger
->log(this->logger
, CONTROL_MORE
, "thread %u got a job", pthread_self());
82 /* process them here */
90 * implementation of thread_pool_t.get_pool_size
92 static size_t get_pool_size(private_thread_pool_t
*this)
94 return this->pool_size
;
98 * Implementation of thread_pool_t.destroy
100 static status_t
destroy(private_thread_pool_t
*this)
103 /* flag thread for termination */
104 for (current
= 0; current
< this->pool_size
; current
++) {
105 this->logger
->log(this->logger
, CONTROL
, "cancelling thread %u", this->threads
[current
]);
106 pthread_cancel(this->threads
[current
]);
109 /* wait for all threads */
110 for (current
= 0; current
< this->pool_size
; current
++) {
111 pthread_join(this->threads
[current
], NULL
);
112 this->logger
->log(this->logger
, CONTROL
, "thread %u terminated", this->threads
[current
]);
116 this->logger
->destroy(this->logger
);
117 allocator_free(this->threads
);
118 allocator_free(this);
127 thread_pool_t
*thread_pool_create(size_t pool_size
)
131 private_thread_pool_t
*this = allocator_alloc_thing(private_thread_pool_t
);
133 /* fill in public fields */
134 this->public.destroy
= (status_t(*)(thread_pool_t
*))destroy
;
135 this->public.get_pool_size
= (size_t(*)(thread_pool_t
*))get_pool_size
;
137 this->function
= job_processing
;
138 this->pool_size
= pool_size
;
140 this->threads
= allocator_alloc(sizeof(pthread_t
) * pool_size
);
141 if (this->threads
== NULL
)
143 allocator_free(this);
146 this->logger
= logger_create("thread_pool", ALL
,NULL
);
147 if (this->threads
== NULL
)
149 allocator_free(this);
150 allocator_free(this->threads
);
154 /* try to create as many threads as possible, up tu pool_size */
155 for (current
= 0; current
< pool_size
; current
++)
157 if (pthread_create(&(this->threads
[current
]), NULL
, (void*(*)(void*))this->function
, this) == 0)
159 this->logger
->log(this->logger
, CONTROL
, "thread %u created", this->threads
[current
]);
163 /* creation failed, is it the first one? */
166 this->logger
->log(this->logger
, CONTROL
, "could not create any thread: %s\n", strerror(errno
));
167 allocator_free(this->threads
);
168 allocator_free(this->logger
);
169 allocator_free(this);
172 /* not all threads could be created, but at least one :-/ */
173 this->logger
->log(this->logger
, CONTROL
, "could only create %d from requested %d threads: %s\n", current
, pool_size
, strerror(errno
));
175 this->pool_size
= current
;
176 return (thread_pool_t
*)this;
179 return (thread_pool_t
*)this;