blob: c357c3fa1250a678d14555b434e8e244530d3db9 [file] [log] [blame]
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +01001/* 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
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010024#include <osmocom/core/write_queue.h>
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010025
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020026int write_queue_bfd_cb(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010027{
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
Holger Hans Peter Freyther4052c812010-04-08 10:58:20 +020035 if (what & BSC_FD_EXCEPT)
36 queue->except_cb(fd);
37
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010038 if (what & BSC_FD_WRITE) {
39 struct msgb *msg;
40
41 fd->when &= ~BSC_FD_WRITE;
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010042
Holger Hans Peter Freyther76681ba2011-02-15 00:42:19 +010043 /* the queue might have been emptied */
44 if (!llist_empty(&queue->msg_queue)) {
45 --queue->current_length;
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010046
Holger Hans Peter Freyther76681ba2011-02-15 00:42:19 +010047 msg = msgb_dequeue(&queue->msg_queue);
48 queue->write_cb(fd, msg);
49 msgb_free(msg);
50
51 if (!llist_empty(&queue->msg_queue))
52 fd->when |= BSC_FD_WRITE;
53 }
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010054 }
55
56 return 0;
57}
58
59void write_queue_init(struct write_queue *queue, int max_length)
60{
61 queue->max_length = max_length;
62 queue->current_length = 0;
63 queue->read_cb = NULL;
64 queue->write_cb = NULL;
Holger Hans Peter Freythera49951f2010-03-05 19:42:09 +010065 queue->bfd.cb = write_queue_bfd_cb;
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010066 INIT_LLIST_HEAD(&queue->msg_queue);
67}
68
69int write_queue_enqueue(struct write_queue *queue, struct msgb *data)
70{
71// if (queue->current_length + 1 >= queue->max_length)
72// LOGP(DMSC, LOGL_ERROR, "The queue is full. Dropping not yet implemented.\n");
73
74 ++queue->current_length;
75 msgb_enqueue(&queue->msg_queue, data);
76 queue->bfd.when |= BSC_FD_WRITE;
77
78 return 0;
79}
Holger Hans Peter Freyther99a263f2010-03-26 09:20:22 +010080
81void write_queue_clear(struct write_queue *queue)
82{
83 while (!llist_empty(&queue->msg_queue)) {
84 struct msgb *msg = msgb_dequeue(&queue->msg_queue);
85 msgb_free(msg);
86 }
87
88 queue->current_length = 0;
89 queue->bfd.when &= ~BSC_FD_WRITE;
90}