1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/**
* @file prime_pool.c
*
* @brief Implementation of prime_pool_t.
*
*/
/*
* Copyright (C) 2005 Jan Hutter, Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <pthread.h>
#include "prime_pool.h"
#include <utils/allocator.h>
#include <utils/linked_list.h>
#include <utils/randomizer.h>
typedef struct prime_list_t prime_list_t;
/**
* A prime_list_t contains prime for a specific size.
*/
struct prime_list_t {
/**
* Size of the stored primes
*/
size_t prime_size;
/**
* is this much used prime_size ?
*/
u_int32_t usage;
/**
* list of primes
*/
linked_list_t *primes;
};
typedef struct private_prime_pool_t private_prime_pool_t;
/**
* @brief Private Variables and Functions of prime_pool class
*
*/
struct private_prime_pool_t {
/**
* Public part of the prime_pool_t object
*/
prime_pool_t public;
/**
* A list which contains a set of prime_list_t's
*/
linked_list_t *prime_lists;
/**
* prime generation is stopped if more than
* that primes of a kind are already generated
*/
int generation_limit;
/**
* access to prime_lists is locked through this mutex
*/
pthread_mutex_t mutex;
/**
* If the queue is empty a thread has to wait
* This condvar is used to wake up such a thread
*/
pthread_cond_t condvar;
/**
* prime generation thread
*/
pthread_t thread;
/**
* Function for the prime thread, generate primes
*/
void (*generate_primes) (private_prime_pool_t *this);
/**
* calculate a prime of requested size
*/
void (*compute_prime) (private_prime_pool_t *this, size_t prime_size, mpz_t *prime);
};
/**
* implements prime_pool_t.get_count
*/
static int get_count(private_prime_pool_t *this, size_t prime_size)
{
int count = 0;
iterator_t *iterator;
pthread_mutex_lock(&(this->mutex));
iterator = this->prime_lists->create_iterator(this->prime_lists, TRUE);
while (iterator->has_next(iterator))
{
prime_list_t *prime_list;
iterator->current(iterator, (void*)&prime_list);
if (prime_list->prime_size == prime_size)
{
count = prime_list->primes->get_count(prime_list->primes);
break;
}
}
iterator->destroy(iterator);
pthread_mutex_unlock(&(this->mutex));
return count;
}
/**
* implements prime_pool_t.get_prime
*/
static void get_prime(private_prime_pool_t *this, size_t prime_size, mpz_t *prime)
{
bool prime_found = FALSE;
iterator_t *iterator;
bool create_new_list = TRUE;
pthread_mutex_lock(&(this->mutex));
iterator = this->prime_lists->create_iterator(this->prime_lists, TRUE);
while (iterator->has_next(iterator))
{
prime_list_t *prime_list;
iterator->current(iterator, (void*)&prime_list);
if (prime_list->prime_size == prime_size)
{
mpz_t *removed_prime;
create_new_list = FALSE;
prime_list->usage += 2;
if (prime_list->primes->remove_first(prime_list->primes, (void*)&removed_prime) == SUCCESS)
{
mpz_init_set(*prime, *removed_prime);
mpz_clear(*removed_prime);
allocator_free(removed_prime);
prime_found = TRUE;
}
/* wake up prime thread, he may be sleeping */
pthread_cond_signal(&(this->condvar));
break;
}
}
iterator->destroy(iterator);
if (create_new_list)
{
/* there is no list for this prime size, create one */
prime_list_t *prime_list;
prime_list = allocator_alloc_thing(prime_list_t);
prime_list->usage = 1;
prime_list->primes = linked_list_create();
prime_list->prime_size = prime_size;
this->prime_lists->insert_last(this->prime_lists, (void*)prime_list);
/* wake up prime thread, he may be sleeping */
pthread_cond_signal(&(this->condvar));
}
pthread_mutex_unlock(&(this->mutex));
if (!prime_found)
{
/* no prime found, create one ourself */
this->compute_prime(this, prime_size, prime);
}
}
/**
* implements private_prime_pool_t.compute_prime
*/
void compute_prime(private_prime_pool_t *this, size_t prime_size, mpz_t *prime)
{
randomizer_t *randomizer;
chunk_t random_bytes;
randomizer = randomizer_create();
mpz_init(*prime);
do
{
/* TODO change to true random device ? */
randomizer->allocate_pseudo_random_bytes(randomizer, prime_size, &random_bytes);
/* make sure most significant bit is set */
random_bytes.ptr[0] = random_bytes.ptr[0] | 0x80;
/* convert chunk to mpz value */
mpz_import(*prime, random_bytes.len, 1, 1, 1, 0, random_bytes.ptr);
/* get next prime */
mpz_nextprime (*prime, *prime);
allocator_free(random_bytes.ptr);
}
/* check if it isnt too large */
while (((mpz_sizeinbase(*prime, 2) + 7) / 8) > prime_size);
randomizer->destroy(randomizer);
}
/**
* implements private_prime_pool_t.generate_primes
*/
void generate_primes(private_prime_pool_t *this)
{
/* allow cancellation */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
/* reduce priority */
while (TRUE)
{
prime_list_t *selected_prime_list = NULL;
u_int32_t max_usage = 0;
iterator_t *iterator;
mpz_t *prime;
pthread_mutex_lock(&(this->mutex));
/* get aprime to generate */
iterator = this->prime_lists->create_iterator(this->prime_lists, TRUE);
while (iterator->has_next(iterator))
{
prime_list_t *prime_list;
iterator->current(iterator, (void*)&prime_list);
if (prime_list->usage > max_usage)
{
if (prime_list->primes->get_count(prime_list->primes) < this->generation_limit)
{
/* there is work to do */
max_usage = prime_list->usage;
selected_prime_list = prime_list;
}
}
}
iterator->destroy(iterator);
if (selected_prime_list == NULL)
{
/* wait, be able to cancel */
pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex));
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_cond_wait(&(this->condvar), &(this->mutex));
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
pthread_cleanup_pop(0);
}
pthread_mutex_unlock(&(this->mutex));
if (selected_prime_list != NULL)
{
/* generate the prime of requested size */
prime = allocator_alloc_thing(mpz_t);
compute_prime(this, selected_prime_list->prime_size, prime);
/* insert prime */
pthread_mutex_lock(&(this->mutex));
selected_prime_list->primes->insert_last(selected_prime_list->primes, (void*)prime);
pthread_mutex_unlock(&(this->mutex));
}
}
}
/**
* implements prime_pool_t.destroy
*/
static void destroy (private_prime_pool_t *this)
{
if (this->generation_limit > 0)
{
pthread_cancel(this->thread);
pthread_join(this->thread, NULL);
}
while ((this->prime_lists->get_count(this->prime_lists) > 0))
{
prime_list_t *prime_list;
this->prime_lists->remove_last(this->prime_lists, (void*)&prime_list);
/* clear every mpz */
while (prime_list->primes->get_count(prime_list->primes) > 0)
{
mpz_t *prime;
prime_list->primes->remove_last(prime_list->primes, (void**)&prime);
mpz_clear(*prime);
allocator_free(prime);
}
prime_list->primes->destroy(prime_list->primes);
allocator_free(prime_list);
}
this->prime_lists->destroy(this->prime_lists);
pthread_mutex_destroy(&(this->mutex));
pthread_cond_destroy(&(this->condvar));
allocator_free(this);
}
/*
*
* Documented in header
*/
prime_pool_t *prime_pool_create(int generation_limit)
{
private_prime_pool_t *this = allocator_alloc_thing(private_prime_pool_t);
/* public functions */
this->public.get_count = (int(*)(prime_pool_t*,size_t)) get_count;
this->public.get_prime = (void(*)(prime_pool_t*,size_t,mpz_t*)) get_prime;
this->public.destroy = (void(*)(prime_pool_t*)) destroy;
/* private members */
this->generate_primes = generate_primes;
this->compute_prime = compute_prime;
this->generation_limit = generation_limit;
this->prime_lists = linked_list_create();
pthread_mutex_init(&(this->mutex), NULL);
pthread_cond_init(&(this->condvar), NULL);
if (generation_limit > 0)
{
if (pthread_create(&(this->thread), NULL, (void*(*)(void*))this->generate_primes, this) != 0)
{
pthread_mutex_destroy(&(this->mutex));
pthread_cond_destroy(&(this->condvar));
this->prime_lists->destroy(this->prime_lists);
allocator_free(this);
return NULL;
}
}
return (&this->public);
}
|