GNU Radio 3.5.1 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 <gr_core_api.h>
00025 #include <gruel/thread.h>
00026 #include <deque>
00027 #include <gruel/pmt.h>
00028 
00029 class gr_block_detail;
00030 
00031 /*!
00032  * \brief used by thread-per-block scheduler
00033  */
00034 struct GR_CORE_API gr_tpb_detail {
00035 
00036   gruel::mutex                  mutex;                  //< protects all vars
00037   bool                          input_changed;
00038   gruel::condition_variable     input_cond;
00039   bool                          output_changed;
00040   gruel::condition_variable     output_cond;
00041 
00042 private:
00043   std::deque<pmt::pmt_t>        msg_queue;
00044 
00045 public:
00046   gr_tpb_detail()
00047     : input_changed(false), output_changed(false) { }
00048 
00049   //! Called by us to tell all our upstream blocks that their output may have changed.
00050   void notify_upstream(gr_block_detail *d);
00051 
00052   //! Called by us to tell all our downstream blocks that their input may have changed.
00053   void notify_downstream(gr_block_detail *d);
00054 
00055   //! Called by us to notify both upstream and downstream
00056   void notify_neighbors(gr_block_detail *d);
00057 
00058   //! Called by us
00059   void clear_changed()
00060   {
00061     gruel::scoped_lock guard(mutex);
00062     input_changed = false;
00063     output_changed = false;
00064   }
00065   
00066   //! is the queue empty?
00067   bool empty_p() const { return msg_queue.empty(); }
00068 
00069   //| Acquires and release the mutex    
00070   void insert_tail(pmt::pmt_t msg);
00071 
00072   /*!
00073    * \returns returns pmt at head of queue or pmt_t() if empty.
00074    */
00075   pmt::pmt_t delete_head_nowait();
00076 
00077   /*!
00078    * \returns returns pmt at head of queue or pmt_t() if empty.
00079    * Caller must already be holding the mutex
00080    */
00081   pmt::pmt_t delete_head_nowait_already_holding_mutex();
00082 
00083 private:
00084 
00085   //! Used by notify_downstream
00086   void set_input_changed()
00087   {
00088     gruel::scoped_lock guard(mutex);
00089     input_changed = true;
00090     input_cond.notify_one();
00091   }
00092 
00093   //! Used by notify_upstream
00094   void set_output_changed()
00095   {
00096     gruel::scoped_lock guard(mutex);
00097     output_changed = true;
00098     output_cond.notify_one();
00099   }
00100 
00101 };
00102 
00103 #endif /* INCLUDED_GR_TPB_DETAIL_H */