-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreadqueue.h
201 lines (185 loc) · 5.55 KB
/
threadqueue.h
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
#ifndef _THREADQUEUE_H_
#define _THREADQUEUE_H_ 1
#include "platform.h"
/*
Based on code from https://stackoverflow.com/questions/4577961/pthread-synchronized-blocking-queue
*/
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @defgroup ThreadQueue ThreadQueue
*
* Little API for waitable queues, typically used for passing messages
* between threads.
*
*/
/**
* @mainpage
*/
/**
* A thread message.
*
* @ingroup ThreadQueue
*
* This is used for passing to #thread_queue_get for retreive messages.
* the date is stored in the data member, the message type in the #msgtype.
*
* Typical:
* @code
* struct threadmsg;
* struct myfoo *foo;
* while(1)
* ret = thread_queue_get(&queue,NULL,&message);
* ..
* foo = msg.data;
* switch(msg.msgtype){
* ...
* }
* }
* @endcode
*
*/
struct threadmsg
{
void *data; // Holds the data.
long msgtype; // Holds the messagetype
long qlength; // Holds the current queue lenght. Might not be meaningful if there's several readers
};
/**
* A TthreadQueue
*
* @ingroup ThreadQueue
*
* You should threat this struct as opaque, never ever set/get any
* of the variables. You have been warned.
*/
struct threadqueue
{
long length; // Length of the queue, never set this, never read this.
mutex_t mutex; // Mutex for the queue, never touch.
cond_t cond; // Condition variable for the queue, never touch.
struct msglist *first, *last; // Internal pointers for the queue, never touch.
struct msglist *msgpool; // Internal cache of msglists
long msgpool_length; // No. of elements in the msgpool
};
/**
* A TthreadQueue
*
* @ingroup ThreadQueue
*
* User provided callback function used in thread_queue_free() for freeing user data
*/
typedef void (*user_free_fn)(void* data);
/**
* Initializes a queue.
*
* @ingroup ThreadQueue
*
* thread_queue_init initializes a new threadqueue. A new queue must always
* be initialized before it is used.
*
* @param queue Pointer to the queue that should be initialized
* @return 0 on success see pthread_mutex_init
*/
int thread_queue_init(struct threadqueue *queue);
/**
* Put a message to a queue
*
* @ingroup ThreadQueue
*
* thread_queue_put adds a "message" to the specified queue, a message
* is just a pointer to a anything of the users choice. Nothing is copied
* so the user must keep track on (de)allocation of the data.
* A message type is also specified, it is not used for anything else than
* given back when a message is retreived from the queue.
* @param queue Pointer to the queue on where the message should be added.
* @param data the "message".
* @param msgtype a long specifying the message type, choice of the user.
* @return 0 on succes ENOMEM if out of memory EINVAL if queue is NULL
*/
int thread_queue_put_msg(struct threadqueue *queue, void *data, long msgtype);
/**
* Gets a message from a queue
*
* @ingroup ThreadQueue
*
* thread_queue_get gets a message from the specified queue, it will block
* the caling thread untill a message arrives, or the (optional) timeout occurs.
* If timeout is NULL, there will be no timeout, and thread_queue_get will wait
* untill a message arrives.
*
* struct timespec is defined as:
* @code
* struct timespec {
* long tv_sec; // seconds
* long tv_nsec; // nanoseconds
* };
* @endcode
*
* @param queue Pointer to the queue to wait on for a message.
* @param timeout timeout on how long to wait on a message
* @param msg pointer that is filled in with mesagetype and data
*
* @return 0 on success EINVAL if queue is NULL and ETIMEDOUT (or ERROR_TIMEOUT for windows) if timeout occurs
*/
int thread_queue_get_msg(struct threadqueue *queue, const struct timespec *timeout, struct threadmsg *msg);
/**
* Gets the length of a queue
*
* @ingroup ThreadQueue
*
* threadqueue_length returns the number of messages waiting in the queue
*
* @param queue Pointer to the queue for which to get the length
* @return the length(number of pending messages) in the queue
*/
long thread_queue_length(struct threadqueue *queue);
/**
* @ingroup ThreadQueue
* Cleans up the queue.
*
* threadqueue_cleanup cleans up and destroys the queue.
* This will remove all messages from a queue, and reset it. If
* freedata is != 0 free(3) will be called on all pending messages in the queue
* You cannot call this if there are someone currently adding or getting messages
* from the queue.
* After a queue have been cleaned, it cannot be used again untill #thread_queue_init
* has been called on the queue.
*
* @param queue Pointer to the queue that should be cleaned
* @param freedata set to nonzero if free(3) should be called on remaining
* messages
* @return 0 on success EINVAL if queue is NULL EBUSY if someone is holding any locks on the queue
*/
int thread_queue_cleanup(struct threadqueue *queue, user_free_fn freedata);
/**
* Allocate a queue.
*
* @ingroup ThreadQueue
*
* thread_queue_alloc allocate and initialize a new threadqueue.
* Do not need call thread_queue_init() when queue allocated with this function
*
* @param NONE
* @return pointer to newly allocated queue
*/
struct threadqueue* thread_queue_alloc();
/**
* Deallocate a queue.
*
* @ingroup ThreadQueue
*
* thread_queue_free deallocate and freeing the queue.
* User can provide callback function for free user data.
* See also thread_queue_cleanup() notes.
*
* @param NONE
* @return pointer to newly allocated queue
*/
void thread_queue_free(struct threadqueue* queue, user_free_fn freedata);
#ifdef __cplusplus
}
#endif
#endif