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