GNU Radio 3.4.0 C++ API
gr_tpb_detail.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2008,2009 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License along
00018  * with this program; if not, write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00020  */
00021 #ifndef INCLUDED_GR_TPB_DETAIL_H
00022 #define INCLUDED_GR_TPB_DETAIL_H
00023 
00024 #include <gruel/thread.h>
00025 #include <deque>
00026 #include <gruel/pmt.h>
00027 
00028 class gr_block_detail;
00029 
00030 /*!
00031  * \brief used by thread-per-block scheduler
00032  */
00033 struct gr_tpb_detail {
00034 
00035   gruel::mutex                  mutex;                  //< protects all vars
00036   bool                          input_changed;
00037   gruel::condition_variable     input_cond;
00038   bool                          output_changed;
00039   gruel::condition_variable     output_cond;
00040 
00041 private:
00042   std::deque<pmt::pmt_t>        msg_queue;
00043 
00044 public:
00045   gr_tpb_detail()
00046     : input_changed(false), output_changed(false) { }
00047 
00048   //! Called by us to tell all our upstream blocks that their output may have changed.
00049   void notify_upstream(gr_block_detail *d);
00050 
00051   //! Called by us to tell all our downstream blocks that their input may have changed.
00052   void notify_downstream(gr_block_detail *d);
00053 
00054   //! Called by us to notify both upstream and downstream
00055   void notify_neighbors(gr_block_detail *d);
00056 
00057   //! Called by us
00058   void clear_changed()
00059   {
00060     gruel::scoped_lock guard(mutex);
00061     input_changed = false;
00062     output_changed = false;
00063   }
00064   
00065   //! is the queue empty?
00066   bool empty_p() const { return msg_queue.empty(); }
00067 
00068   //| Acquires and release the mutex    
00069   void insert_tail(pmt::pmt_t msg);
00070 
00071   /*!
00072    * \returns returns pmt at head of queue or pmt_t() if empty.
00073    */
00074   pmt::pmt_t delete_head_nowait();
00075 
00076   /*!
00077    * \returns returns pmt at head of queue or pmt_t() if empty.
00078    * Caller must already be holding the mutex
00079    */
00080   pmt::pmt_t delete_head_nowait_already_holding_mutex();
00081 
00082 private:
00083 
00084   //! Used by notify_downstream
00085   void set_input_changed()
00086   {
00087     gruel::scoped_lock guard(mutex);
00088     input_changed = true;
00089     input_cond.notify_one();
00090   }
00091 
00092   //! Used by notify_upstream
00093   void set_output_changed()
00094   {
00095     gruel::scoped_lock guard(mutex);
00096     output_changed = true;
00097     output_cond.notify_one();
00098   }
00099 
00100 };
00101 
00102 #endif /* INCLUDED_GR_TPB_DETAIL_H */