|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 jibi <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation; either version 2 |
| 7 | + * of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <stdio.h> |
| 20 | +#include <stdlib.h> |
| 21 | +#include <string.h> |
| 22 | +#include <stdint.h> |
| 23 | +#include <stdbool.h> |
| 24 | + |
| 25 | +#include <pthread.h> |
| 26 | + |
| 27 | +#include <eth/datastruct/async_queue.h> |
| 28 | + |
| 29 | +#define ASYNC_QUEUE_INIT_SIZE 2048 |
| 30 | + |
| 31 | +static inline void async_queue_grow(async_queue_t *q); |
| 32 | + |
| 33 | +async_queue_t * |
| 34 | +async_queue_new() |
| 35 | +{ |
| 36 | + async_queue_t *q = malloc(sizeof(async_queue_t)); |
| 37 | + |
| 38 | + q->size = ASYNC_QUEUE_INIT_SIZE; |
| 39 | + q->items = malloc(q->size * sizeof(void *)); |
| 40 | + |
| 41 | + q->count = 0; |
| 42 | + q->begin = 0; |
| 43 | + q->end = 0; |
| 44 | + |
| 45 | + pthread_mutex_init(&q->rw_lock, NULL); |
| 46 | + pthread_cond_init(&q->full_cond, NULL); |
| 47 | + |
| 48 | + return q; |
| 49 | +} |
| 50 | + |
| 51 | +void |
| 52 | +async_queue_push(async_queue_t *q, void *item) |
| 53 | +{ |
| 54 | + bool empty; |
| 55 | + pthread_mutex_lock(&q->rw_lock); |
| 56 | + |
| 57 | + empty = q->count == 0; |
| 58 | + |
| 59 | + if (q->count == q->size) { |
| 60 | + async_queue_grow(q); |
| 61 | + } |
| 62 | + |
| 63 | + q->items[q->end] = item; |
| 64 | + q->end = (q->end + 1) % q->size; |
| 65 | + q->count++; |
| 66 | + |
| 67 | + pthread_mutex_unlock(&q->rw_lock); |
| 68 | + |
| 69 | + if (empty) { |
| 70 | + pthread_cond_broadcast(&q->full_cond); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void * |
| 75 | +async_queue_pop(async_queue_t *q) |
| 76 | +{ |
| 77 | + void *item; |
| 78 | + |
| 79 | + pthread_mutex_lock(&q->rw_lock); |
| 80 | + |
| 81 | + while (q->count == 0) { |
| 82 | + pthread_cond_wait(&q->full_cond, &q->rw_lock); |
| 83 | + } |
| 84 | + |
| 85 | + item = q->items[q->begin]; |
| 86 | + q->begin = (q->begin + 1) % q->size; |
| 87 | + q->count--; |
| 88 | + |
| 89 | + pthread_mutex_unlock(&q->rw_lock); |
| 90 | + |
| 91 | + return item; |
| 92 | +} |
| 93 | + |
| 94 | +static inline |
| 95 | +void |
| 96 | +async_queue_grow(async_queue_t *q) |
| 97 | +{ |
| 98 | + uint32_t new_size = q->size * 2; |
| 99 | + void **new_items = malloc(new_size * sizeof(void **)); |
| 100 | + |
| 101 | + memcpy(new_items, q->items, q->size); |
| 102 | + free(q->items); |
| 103 | + |
| 104 | + q->items = new_items; |
| 105 | + q->size = new_size; |
| 106 | +} |
0 commit comments