switch source package format to 3.0 quilt
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_msg_queue.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <gr_msg_queue.h>
27 #include <stdexcept>
28
29 gr_msg_queue_sptr
30 gr_make_msg_queue(unsigned int limit)
31 {
32   return gr_msg_queue_sptr (new gr_msg_queue(limit));
33 }
34
35 gr_msg_queue::gr_msg_queue(unsigned int limit)
36   : d_not_empty(), d_not_full(),
37     /*d_head(0), d_tail(0),*/ d_count(0), d_limit(limit)
38 {
39 }
40
41 gr_msg_queue::~gr_msg_queue()
42 {
43   flush ();
44 }
45
46 void
47 gr_msg_queue::insert_tail(gr_message_sptr msg)
48 {
49   if (msg->d_next)
50     throw std::invalid_argument("gr_msg_queue::insert_tail: msg already in queue");
51
52   gruel::scoped_lock guard(d_mutex);
53
54   while (full_p())
55     d_not_full.wait(guard);
56
57   if (d_tail == 0){
58     d_tail = d_head = msg;
59     //msg->d_next = 0;
60     msg->d_next.reset();
61   }
62   else {
63     d_tail->d_next = msg;
64     d_tail = msg;
65     //msg->d_next = 0;
66     msg->d_next.reset();
67   }
68   d_count++;
69   d_not_empty.notify_one();
70 }
71
72 gr_message_sptr
73 gr_msg_queue::delete_head()
74 {
75   gruel::scoped_lock guard(d_mutex);
76   gr_message_sptr m;
77
78   while ((m = d_head) == 0)
79     d_not_empty.wait(guard);
80
81   d_head = m->d_next;
82   if (d_head == 0){
83     //d_tail = 0;
84     d_tail.reset();
85   }
86
87   d_count--;
88   // m->d_next = 0;
89   m->d_next.reset();
90   d_not_full.notify_one();
91   return m;
92 }
93
94 gr_message_sptr
95 gr_msg_queue::delete_head_nowait()
96 {
97   gruel::scoped_lock guard(d_mutex);
98   gr_message_sptr m;
99
100   if ((m = d_head) == 0){
101     //return 0;
102     return gr_message_sptr();
103   }
104
105   d_head = m->d_next;
106   if (d_head == 0){
107     //d_tail = 0;
108     d_tail.reset();
109   }
110
111   d_count--;
112   //m->d_next = 0;
113   m->d_next.reset();
114   d_not_full.notify_one();
115   return m;
116 }
117
118 void
119 gr_msg_queue::flush()
120 {
121   gr_message_sptr       m;
122
123   while ((m = delete_head_nowait ()) != 0)
124     ;
125 }