1 /* Generic write queue implementation */
3 * (C) 2010 by Holger Hans Peter Freyther
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.
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.
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.
24 #include <osmocom/core/write_queue.h>
26 int osmo_wqueue_bfd_cb(struct osmo_fd *fd, unsigned int what)
28 struct osmo_wqueue *queue;
30 queue = container_of(fd, struct osmo_wqueue, bfd);
32 if (what & BSC_FD_READ)
35 if (what & BSC_FD_EXCEPT)
38 if (what & BSC_FD_WRITE) {
41 fd->when &= ~BSC_FD_WRITE;
43 /* the queue might have been emptied */
44 if (!llist_empty(&queue->msg_queue)) {
45 --queue->current_length;
47 msg = msgb_dequeue(&queue->msg_queue);
48 queue->write_cb(fd, msg);
51 if (!llist_empty(&queue->msg_queue))
52 fd->when |= BSC_FD_WRITE;
59 void osmo_wqueue_init(struct osmo_wqueue *queue, int max_length)
61 queue->max_length = max_length;
62 queue->current_length = 0;
63 queue->read_cb = NULL;
64 queue->write_cb = NULL;
65 queue->bfd.cb = osmo_wqueue_bfd_cb;
66 INIT_LLIST_HEAD(&queue->msg_queue);
69 int osmo_wqueue_enqueue(struct osmo_wqueue *queue, struct msgb *data)
71 // if (queue->current_length + 1 >= queue->max_length)
72 // LOGP(DMSC, LOGL_ERROR, "The queue is full. Dropping not yet implemented.\n");
74 ++queue->current_length;
75 msgb_enqueue(&queue->msg_queue, data);
76 queue->bfd.when |= BSC_FD_WRITE;
81 void osmo_wqueue_clear(struct osmo_wqueue *queue)
83 while (!llist_empty(&queue->msg_queue)) {
84 struct msgb *msg = msgb_dequeue(&queue->msg_queue);
88 queue->current_length = 0;
89 queue->bfd.when &= ~BSC_FD_WRITE;