aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/threads/prime_pool.h
blob: bcbf04ee023b070ca45b45f50173ccdb320fd0db (plain)
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
/**
 * @file prime_pool.h
 *
 * @brief Interface 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.
 */

#ifndef PRIME_POOL_H_
#define PRIME_POOL_H_

#include <gmp.h>

#include <types.h>
#include <network/packet.h>


typedef struct prime_pool_t prime_pool_t;

/**
 * @brief Prime generation thread.
 * 
 * Starts a low-priority thread which will
 * preallocate primes in the background.
 * This increases responsibility, since prime generation
 * is the most time-consuming task.
 * 
 * @ingroup threads
 */
struct prime_pool_t {

	/**
	 * @brief Get the number of available primes for the given prime size.
	 *
	 * @param prime_pool_t 	calling object
 	 * @param 				size of the prime
	 * @returns 			number of primes
	 */
	int (*get_count) (prime_pool_t *prime_pool, size_t prime_size);

	/**
	 * @brief Get a prime of the given size.
	 *
	 * If no primes are available, the threads generates one of its own.
	 * Supplied mpz will be initialized to a prime and must be cleared
	 * after usage.
	 *
	 * @param prime_pool_t 	calling object
	 * @return 				chunk containing the prime
	 */
	void (*get_prime) (prime_pool_t *prime_pool, size_t prime_size, mpz_t *prime);

	/**
	 * @brief Destroys a prime_pool object.
	 *
	 * Stopps the prime thread and destroys the pool.
	 *
	 * @param prime_pool_t 	calling object
	 */
	void (*destroy) (prime_pool_t *prime_pool);
};

/**
 * @brief Creates a prime pool with a thread in it.
 * 
 * The specified limit limits the preallocation of primes
 * for a specific prime_size. If limit is zero, no thread
 * will be created, prime computation is done from
 * the get_prime-calling thread.
 *
 * @param generation_limit	generation limit to use
 * @return 					created prime pool
 * 
 * @ingroup threads
 */
prime_pool_t *prime_pool_create(int generation_limit);

#endif /*PRIME_POOL_H_*/