Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 1 | /* Generic write queue implementation */ |
| 2 | /* |
| 3 | * (C) 2010 by Holger Hans Peter Freyther |
| 4 | * (C) 2010 by On-Waves |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <osmocore/write_queue.h> |
| 25 | |
| 26 | int write_queue_bfd_cb(struct bsc_fd *fd, unsigned int what) |
| 27 | { |
| 28 | struct write_queue *queue; |
| 29 | |
| 30 | queue = container_of(fd, struct write_queue, bfd); |
| 31 | |
| 32 | if (what & BSC_FD_READ) |
| 33 | queue->read_cb(fd); |
| 34 | |
Harald Welte | f3f9002 | 2010-04-30 14:29:56 +0200 | [diff] [blame] | 35 | if (what & BSC_FD_EXCEPT) |
| 36 | queue->except_cb(fd); |
| 37 | |
Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 38 | if (what & BSC_FD_WRITE) { |
| 39 | struct msgb *msg; |
| 40 | |
| 41 | fd->when &= ~BSC_FD_WRITE; |
| 42 | msg = msgb_dequeue(&queue->msg_queue); |
| 43 | if (!msg) |
| 44 | return -1; |
| 45 | |
| 46 | --queue->current_length; |
| 47 | queue->write_cb(fd, msg); |
| 48 | msgb_free(msg); |
| 49 | |
| 50 | if (!llist_empty(&queue->msg_queue)) |
| 51 | fd->when |= BSC_FD_WRITE; |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | void write_queue_init(struct write_queue *queue, int max_length) |
| 58 | { |
| 59 | queue->max_length = max_length; |
| 60 | queue->current_length = 0; |
| 61 | queue->read_cb = NULL; |
| 62 | queue->write_cb = NULL; |
| 63 | queue->bfd.cb = write_queue_bfd_cb; |
| 64 | INIT_LLIST_HEAD(&queue->msg_queue); |
| 65 | } |
| 66 | |
| 67 | int write_queue_enqueue(struct write_queue *queue, struct msgb *data) |
| 68 | { |
| 69 | // if (queue->current_length + 1 >= queue->max_length) |
| 70 | // LOGP(DMSC, LOGL_ERROR, "The queue is full. Dropping not yet implemented.\n"); |
| 71 | |
| 72 | ++queue->current_length; |
| 73 | msgb_enqueue(&queue->msg_queue, data); |
| 74 | queue->bfd.when |= BSC_FD_WRITE; |
| 75 | |
| 76 | return 0; |
| 77 | } |
Harald Welte | 411e86f | 2010-03-26 21:35:28 +0800 | [diff] [blame] | 78 | |
| 79 | void write_queue_clear(struct write_queue *queue) |
| 80 | { |
| 81 | while (!llist_empty(&queue->msg_queue)) { |
| 82 | struct msgb *msg = msgb_dequeue(&queue->msg_queue); |
| 83 | msgb_free(msg); |
| 84 | } |
| 85 | |
| 86 | queue->current_length = 0; |
| 87 | queue->bfd.when &= ~BSC_FD_WRITE; |
| 88 | } |