- cancellation disabled by default
[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 /* cancellation disabled by default */
60 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
61
62 for (;;) {
63 job_t *job;
64 job_queue->get(job_queue, &job);
65
66 /* process them here */
67
68 job->destroy(job);
69 }
70 }
71
72 /**
73 * Implementation of thread_pool_t.get_pool_size
74 */
75 static status_t get_pool_size(private_thread_pool_t *this, size_t *size)
76 {
77 *size = this->pool_size;
78 return SUCCESS;
79 }
80
81 /**
82 * Implementation of thread_pool_t.destroy
83 */
84 static status_t destroy(private_thread_pool_t *this)
85 {
86 int current;
87 /* flag thread for termination */
88 for (current = 0; current < this->pool_size; current++) {
89 pthread_cancel(this->threads[current]);
90 }
91
92 /* wait for all threads */
93 for (current = 0; current < this->pool_size; current++) {
94 pthread_join(this->threads[current], NULL);
95 }
96
97 /* free mem */
98 pfree(this->threads);
99 pfree(this);
100 return SUCCESS;
101 }
102
103 /**
104 * Implementation of default constructor for thread_pool_t
105 */
106 thread_pool_t *thread_pool_create(size_t pool_size)
107 {
108 int current;
109
110 private_thread_pool_t *this = alloc_thing(private_thread_pool_t, "private_thread_pool_t");
111
112 /* fill in public fields */
113 this->public.destroy = (status_t(*)(thread_pool_t*))destroy;
114 this->public.get_pool_size = (status_t(*)(thread_pool_t*, size_t*))get_pool_size;
115
116 this->pool_size = pool_size;
117 this->threads = alloc_bytes(sizeof(pthread_t) * pool_size, "pthread_t[] of private_thread_pool_t");
118
119
120 /* try to create as many threads as possible, up tu pool_size */
121 for (current = 0; current < pool_size; current++) {
122 if (pthread_create(&(this->threads[current]), NULL, (void*(*)(void*))job_processing, this)) {
123 /* did we get any? */
124 if (current == 0) {
125 pfree(this->threads);
126 pfree(this);
127 return NULL;
128 }
129 /* not all threads could be created, but at least one :-/ */
130 this->pool_size = current;
131 return (thread_pool_t*)this;
132 }
133 }
134
135 return (thread_pool_t*)this;
136 }