435e310cccacef9c33c0757e1e1ad766c287de70
[strongswan.git] / Source / charon / thread_pool.c
1 /**
2 * @file thread_pool.c
3 *
4 * @brief Thread-pool with some threads processing the job_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
24 #include "thread_pool.h"
25
26 #include "job_queue.h"
27
28 #include <stdlib.h>
29 #include <freeswan.h>
30 #include <pluto/constants.h>
31 #include <pluto/defs.h>
32
33 #include <pthread.h>
34
35 extern job_queue_t *job_queue;
36
37
38 /**
39 * structure with private members for thread_pool
40 */
41 typedef struct {
42 /**
43 * inclusion of public members
44 */
45 thread_pool_t public;
46 /**
47 * number of running threads
48 */
49 size_t pool_size;
50 /**
51 * array of thread ids
52 */
53 pthread_t *threads;
54 } private_thread_pool_t;
55
56
57 void job_processing(private_thread_pool_t *this)
58 {
59 for (;;) {
60 job_t *job;
61 job_queue->get(job_queue, &job);
62
63 /* process them here */
64
65 job->destroy(job);
66 }
67 }
68
69 /**
70 * Implementation of thread_pool_t.get_pool_size
71 */
72 static status_t get_pool_size(private_thread_pool_t *this, size_t *size)
73 {
74 *size = this->pool_size;
75 return SUCCESS;
76 }
77
78 /**
79 * Implementation of thread_pool_t.destroy
80 */
81 static status_t destroy(private_thread_pool_t *this)
82 {
83 int current;
84 /* flag thread for termination */
85 for (current = 0; current < this->pool_size; current++) {
86 pthread_cancel(this->threads[current]);
87 }
88
89 /* wait for all threads */
90 for (current = 0; current < this->pool_size; current++) {
91 pthread_join(this->threads[current], NULL);
92 }
93
94 /* free mem */
95 pfree(this->threads);
96 pfree(this);
97 return SUCCESS;
98 }
99
100 /**
101 * Implementation of default constructor for thread_pool_t
102 */
103 thread_pool_t *thread_pool_create(size_t pool_size)
104 {
105 int current;
106
107 private_thread_pool_t *this = alloc_thing(private_thread_pool_t, "private_thread_pool_t");
108
109 /* fill in public fields */
110 this->public.destroy = (status_t(*)(thread_pool_t*))destroy;
111 this->public.get_pool_size = (status_t(*)(thread_pool_t*, size_t*))get_pool_size;
112
113 this->pool_size = pool_size;
114 this->threads = alloc_bytes(sizeof(pthread_t) * pool_size, "pthread_t[] of private_thread_pool_t");
115
116 /* try to create as many threads as possible, up tu pool_size */
117 for (current = 0; current < pool_size; current++) {
118 if (pthread_create(&(this->threads[current]), NULL, (void*(*)(void*))job_processing, this)) {
119 /* did we get any? */
120 if (current == 0) {
121 pfree(this->threads);
122 pfree(this);
123 return NULL;
124 }
125 /* not all threads could be created, but at least one :-/ */
126 this->pool_size = current;
127 return (thread_pool_t*)this;
128 }
129 }
130
131 return (thread_pool_t*)this;
132 }