summaryrefslogtreecommitdiff
path: root/src/libstrongswan/collections/blocking_queue.h
blob: 9b014f71999151ec8331bcc0b0b9e16fd6812606 (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
93
94
95
96
97
/*
 * Copyright (C) 2012 Tobias Brunner
 * Copyright (C) 2012 Giuliano Grassi
 * Copyright (C) 2012 Ralf Sager
 * 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.
 */

/**
 * @defgroup blocking_queue blocking_queue
 * @{ @ingroup collections
 */

#ifndef BLOCKING_QUEUE_H_
#define BLOCKING_QUEUE_H_

typedef struct blocking_queue_t blocking_queue_t;

#include <library.h>

/**
 * Class implementing a synchronized blocking queue based on linked_list_t
 */
struct blocking_queue_t {

	/**
	 * Inserts a new item at the tail of the queue
	 *
	 * @param item		item to insert in queue
	 */
	void (*enqueue)(blocking_queue_t *this, void *item);

	/**
	 * Removes the first item in the queue and returns its value.
	 * If the queue is empty, this call blocks until a new item is inserted.
	 *
	 * @note This is a thread cancellation point
	 *
	 * @return			removed item
	 */
	void *(*dequeue)(blocking_queue_t *this);

	/**
	 * Destroys a blocking_queue_t object.
	 *
	 * @note No thread must wait in dequeue() when this function is called
	 */
	void (*destroy)(blocking_queue_t *this);

	/**
	 * Destroys a queue and its objects using the given destructor.
	 *
	 * If a queue and the contained objects should be destroyed, use
	 * destroy_offset. The supplied offset specifies the destructor to
	 * call on each object. The offset may be calculated using the offsetof
	 * macro, e.g.: queue->destroy_offset(queue, offsetof(object_t, destroy));
	 *
	 * @note No thread must wait in dequeue() when this function is called
	 *
	 * @param offset	offset of the objects destructor
	 */
	void (*destroy_offset)(blocking_queue_t *this, size_t offset);

	/**
	 * Destroys a queue and its objects using a cleanup function.
	 *
	 * If a queue and its contents should get destroyed using a specific
	 * cleanup function, use destroy_function. This is useful when the
	 * list contains malloc()-ed blocks which should get freed,
	 * e.g.: queue->destroy_function(queue, free);
	 *
	 * @note No thread must wait in dequeue() when this function is called
	 *
	 * @param function	function to call on each object
	 */
	void (*destroy_function)(blocking_queue_t *this, void (*)(void*));

};

/**
 * Creates an empty queue object.
 *
 * @return		blocking_queue_t object.
 */
blocking_queue_t *blocking_queue_create();

#endif /** BLOCKING_QUEUE_H_ @}*/