]>
Commit | Line | Data |
---|---|---|
2dde79ac TB |
1 | /* |
2 | * Copyright (C) 2012 Tobias Brunner | |
3 | * Copyright (C) 2012 Giuliano Grassi | |
4 | * Copyright (C) 2012 Ralf Sager | |
19ef2aec TB |
5 | * |
6 | * Copyright (C) secunet Security Networks AG | |
2dde79ac TB |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2 of the License, or (at your | |
11 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | * for more details. | |
17 | */ | |
18 | ||
19 | /** | |
20 | * @defgroup blocking_queue blocking_queue | |
12642a68 | 21 | * @{ @ingroup collections |
2dde79ac TB |
22 | */ |
23 | ||
24 | #ifndef BLOCKING_QUEUE_H_ | |
25 | #define BLOCKING_QUEUE_H_ | |
26 | ||
27 | typedef struct blocking_queue_t blocking_queue_t; | |
28 | ||
29 | #include <library.h> | |
30 | ||
31 | /** | |
32 | * Class implementing a synchronized blocking queue based on linked_list_t | |
33 | */ | |
34 | struct blocking_queue_t { | |
35 | ||
36 | /** | |
37 | * Inserts a new item at the tail of the queue | |
38 | * | |
39 | * @param item item to insert in queue | |
40 | */ | |
41 | void (*enqueue)(blocking_queue_t *this, void *item); | |
42 | ||
43 | /** | |
44 | * Removes the first item in the queue and returns its value. | |
45 | * If the queue is empty, this call blocks until a new item is inserted. | |
46 | * | |
47 | * @note This is a thread cancellation point | |
48 | * | |
49 | * @return removed item | |
50 | */ | |
51 | void *(*dequeue)(blocking_queue_t *this); | |
52 | ||
53 | /** | |
54 | * Destroys a blocking_queue_t object. | |
55 | * | |
56 | * @note No thread must wait in dequeue() when this function is called | |
57 | */ | |
58 | void (*destroy)(blocking_queue_t *this); | |
59 | ||
60 | /** | |
61 | * Destroys a queue and its objects using the given destructor. | |
62 | * | |
63 | * If a queue and the contained objects should be destroyed, use | |
64 | * destroy_offset. The supplied offset specifies the destructor to | |
65 | * call on each object. The offset may be calculated using the offsetof | |
66 | * macro, e.g.: queue->destroy_offset(queue, offsetof(object_t, destroy)); | |
67 | * | |
68 | * @note No thread must wait in dequeue() when this function is called | |
69 | * | |
70 | * @param offset offset of the objects destructor | |
71 | */ | |
72 | void (*destroy_offset)(blocking_queue_t *this, size_t offset); | |
73 | ||
74 | /** | |
75 | * Destroys a queue and its objects using a cleanup function. | |
76 | * | |
77 | * If a queue and its contents should get destroyed using a specific | |
78 | * cleanup function, use destroy_function. This is useful when the | |
79 | * list contains malloc()-ed blocks which should get freed, | |
80 | * e.g.: queue->destroy_function(queue, free); | |
81 | * | |
82 | * @note No thread must wait in dequeue() when this function is called | |
83 | * | |
84 | * @param function function to call on each object | |
85 | */ | |
86 | void (*destroy_function)(blocking_queue_t *this, void (*)(void*)); | |
87 | ||
88 | }; | |
89 | ||
90 | /** | |
91 | * Creates an empty queue object. | |
92 | * | |
93 | * @return blocking_queue_t object. | |
94 | */ | |
95 | blocking_queue_t *blocking_queue_create(); | |
96 | ||
97 | #endif /** BLOCKING_QUEUE_H_ @}*/ | |
98 |