Imported Upstream version 3.2.2
[debian/gnuradio] / mblock / src / lib / mb_timer_queue.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifndef INCLUDED_MB_TIMER_QUEUE_H
23 #define INCLUDED_MB_TIMER_QUEUE_H
24
25 #include <mblock/time.h>
26 #include <vector>
27 #include <queue>
28 #include <pmt.h>
29 #include <mblock/msg_accepter.h>
30
31 class mb_timeout {
32 public:
33   mb_time               d_when;         // absolute time to fire timeout
34   mb_time               d_delta;        // if periodic, delta_t to next timeout
35   bool                  d_is_periodic;  // true iff this is a periodic timeout
36   pmt_t                 d_user_data;    // data from %timeout msg
37   pmt_t                 d_handle;       // handle for cancellation
38   mb_msg_accepter_sptr  d_accepter;     // where to send the message
39
40   // one-shot constructor
41   mb_timeout(const mb_time &abs_time,
42              pmt_t user_data, mb_msg_accepter_sptr accepter);
43
44   // periodic constructor
45   mb_timeout(const mb_time &first_abs_time, const mb_time &delta_time,
46              pmt_t user_data, mb_msg_accepter_sptr accepter);
47
48   pmt_t handle() const { return d_handle; }
49 };
50
51 typedef boost::shared_ptr<mb_timeout> mb_timeout_sptr;
52
53
54 //! Sort criterion for priority_queue
55 class timeout_later
56 {
57 public:
58   bool operator() (const mb_timeout_sptr t1, const mb_timeout_sptr t2)
59   {
60     return t1->d_when > t2->d_when;
61   }
62 };
63
64
65 class mb_timer_queue : public std::priority_queue<mb_timeout_sptr,
66                                                   std::vector<mb_timeout_sptr>,
67                                                   timeout_later>
68 {
69 public:
70   void cancel(pmt_t handle);
71 };
72
73 #endif /* INCLUDED_MB_TIMER_QUEUE_H */