comparison src/neon/rb.c @ 2151:9a9f406374c6

- Update ring buffer code to newer version
author Ralf Ertzinger <ralf@skytale.net>
date Sun, 04 Nov 2007 14:04:37 +0100
parents dc83901850df
children b73ea297d197
comparison
equal deleted inserted replaced
2150:04421592e6a3 2151:9a9f406374c6
91 */ 91 */
92 void reset_rb(struct ringbuf* rb) { 92 void reset_rb(struct ringbuf* rb) {
93 93
94 _ENTER; 94 _ENTER;
95 95
96 pthread_mutex_lock(&rb->lock); 96 _RB_LOCK(rb->lock);
97 97
98 rb->wp = rb->buf; 98 rb->wp = rb->buf;
99 rb->rp = rb->buf; 99 rb->rp = rb->buf;
100 rb->free = rb->size; 100 rb->free = rb->size;
101 rb->used = 0; 101 rb->used = 0;
102 rb->end = rb->buf+(rb->size-1); 102 rb->end = rb->buf+(rb->size-1);
103 103
104 pthread_mutex_unlock(&rb->lock); 104 _RB_UNLOCK(rb->lock);
105 105
106 _LEAVE; 106 _LEAVE;
107 } 107 }
108 108
109 /* 109 /*
118 118
119 if (0 == size) { 119 if (0 == size) {
120 _LEAVE -1; 120 _LEAVE -1;
121 } 121 }
122 122
123 if (0 != pthread_mutex_init(&rb->lock, NULL)) { 123 if (NULL == (rb->buf = malloc(size))) {
124 _LEAVE -1; 124 _LEAVE -1;
125 } 125 }
126 rb->size = size;
127
128 #ifdef _RB_USE_GLIB
129 if (NULL == (rb->lock = g_mutex_new())) {
130 _LEAVE -1;
131 }
132 #else
133 if (NULL == (rb->lock = malloc(sizeof(pthread_mutex_t)))) {
134 _LEAVE -1;
135 }
136
137 if (0 != pthread_mutex_init(rb->lock, NULL)) {
138 free(rb->lock);
139 _LEAVE -1;
140 }
141 #endif
142 rb->_free_lock = 1;
143
144 reset_rb(rb);
145
146 ASSERT_RB(rb);
147
148 _LEAVE 0;
149 }
150
151 /*
152 * Initialize a ringbuffer structure (including
153 * memory allocation.
154 * The mutex to be used is passed in the function call.
155 * The mutex must not be held while calling this function.
156 *
157 * Return -1 on error
158 */
159 int init_rb_with_lock(struct ringbuf* rb, unsigned int size, rb_mutex_t* lock) {
160
161 _ENTER;
162
163 if (0 == size) {
164 _LEAVE -1;
165 }
166
167 rb->lock = lock;
168 rb->_free_lock = 0;
126 169
127 if (NULL == (rb->buf = malloc(size))) { 170 if (NULL == (rb->buf = malloc(size))) {
128 _LEAVE -1; 171 _LEAVE -1;
129 } 172 }
130 rb->size = size; 173 rb->size = size;
144 int ret = -1; 187 int ret = -1;
145 int endfree; 188 int endfree;
146 189
147 _ENTER; 190 _ENTER;
148 191
149 pthread_mutex_lock(&rb->lock); 192 _RB_LOCK(rb->lock);
150 193
151 ASSERT_RB(rb); 194 ASSERT_RB(rb);
152 195
153 if (rb->free < size) { 196 if (rb->free < size) {
154 ret = -1; 197 ret = -1;
184 227
185 ret = 0; 228 ret = 0;
186 229
187 out: 230 out:
188 ASSERT_RB(rb); 231 ASSERT_RB(rb);
189 pthread_mutex_unlock(&rb->lock); 232 _RB_UNLOCK(rb->lock);
190 233
191 _LEAVE ret; 234 _LEAVE ret;
192 } 235 }
193 236
194 /* 237 /*
199 242
200 int ret; 243 int ret;
201 244
202 _ENTER; 245 _ENTER;
203 246
204 pthread_mutex_lock(&rb->lock); 247 _RB_LOCK(rb->lock);
205 ret = read_rb_locked(rb, buf, size); 248 ret = read_rb_locked(rb, buf, size);
206 pthread_mutex_unlock(&rb->lock); 249 _RB_UNLOCK(rb->lock);
207 250
208 _LEAVE ret; 251 _LEAVE ret;
209 } 252 }
210 253
211 /* 254 /*
226 _LEAVE -1; 269 _LEAVE -1;
227 } 270 }
228 271
229 if (rb->rp < rb->wp) { 272 if (rb->rp < rb->wp) {
230 /* 273 /*
231 Read pointer is behind write pointer, all the data is available in one cunk 274 Read pointer is behind write pointer, all the data is available in one chunk
232 */ 275 */
233 memcpy(buf, rb->rp, size); 276 memcpy(buf, rb->rp, size);
234 rb->rp += size; 277 rb->rp += size;
235 } else { 278 } else {
236 /* 279 /*
269 312
270 unsigned int f; 313 unsigned int f;
271 314
272 _ENTER; 315 _ENTER;
273 316
274 pthread_mutex_lock(&rb->lock); 317 _RB_LOCK(rb->lock);
275 f = rb->free; 318 f = free_rb_locked(rb);
276 pthread_mutex_unlock(&rb->lock); 319 _RB_UNLOCK(rb->lock);
277 320
278 _LEAVE f; 321 _LEAVE f;
279 } 322 }
280 323
324 /*
325 * Return the amount of free space currently in the rb.
326 * Assume the rb lock is already being held.
327 */
328 unsigned int free_rb_locked(struct ringbuf* rb) {
329
330 _ENTER;
331
332 _LEAVE rb->free;
333 }
334
281 335
282 /* 336 /*
283 * Return the amount of used space currently in the rb 337 * Return the amount of used space currently in the rb
284 */ 338 */
285 unsigned int used_rb(struct ringbuf* rb) { 339 unsigned int used_rb(struct ringbuf* rb) {
286 340
287 unsigned int u; 341 unsigned int u;
288 342
289 _ENTER; 343 _ENTER;
290 344
291 pthread_mutex_lock(&rb->lock); 345 _RB_LOCK(rb->lock);
292 u = rb->used; 346 u = rb->used;
293 pthread_mutex_unlock(&rb->lock); 347 _RB_UNLOCK(rb->lock);
294 348
295 _LEAVE u; 349 _LEAVE u;
296 } 350 }
297 351
298 352
300 * destroy a ringbuffer 354 * destroy a ringbuffer
301 */ 355 */
302 void destroy_rb(struct ringbuf* rb) { 356 void destroy_rb(struct ringbuf* rb) {
303 357
304 _ENTER; 358 _ENTER;
305 pthread_mutex_lock(&rb->lock);
306 free(rb->buf); 359 free(rb->buf);
307 pthread_mutex_unlock(&rb->lock); 360 if (rb->_free_lock) {
361 #ifdef _RB_USE_GLIB
362 g_mutex_free(rb->lock);
363 #else
364 pthread_mutex_destroy(rb->lock);
365 free(rb->lock);
366 #endif
367 }
308 368
309 _LEAVE; 369 _LEAVE;
310 } 370 }