6da7072e75ce06f2e1b3e6197f289d9ad9be63e4
[senf.git] / senf / Scheduler / SignalEvent.hh
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
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
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief SignalDispatcher public header */
25
26 #ifndef HH_SENF_Scheduler_SignalEvent_
27 #define HH_SENF_Scheduler_SignalEvent_ 1
28
29 // Custom includes
30 #include <signal.h>
31 #include <boost/function.hpp>
32 #include "FIFORunner.hh"
33 #include <senf/boost_intrusive/iset_hook.hpp>
34
35 //#include "SignalEvent.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace senf {
39
40     class Scheduler;
41
42 namespace scheduler {
43
44     namespace detail {
45         struct SignalSetTag;
46         typedef boost::intrusive::iset_base_hook<SignalSetTag> SignalSetBase;
47         struct SignalSetCompare;
48         struct FindNumericSignal;
49         struct SignalDispatcher;
50     }
51
52     /** \brief UNIX signal event
53
54         The SignalEvent class registers a callback for UNIX signals. The callback will be called \e
55         synchronously (not from within the UNIX signal handler) by the scheduler.
56
57         The SignalEvent class is an implementation of the RAII idiom: The event will be
58         automatically unregistered in the SignalEvent destructor. The SignalEvent instance should be
59         created within the same scope or on a scope below where the callback is defined (e.g. if the
60         callback is a member function it should be defined as a class member).
61      */
62     class SignalEvent
63         : public detail::FIFORunner::TaskInfo,
64           public detail::SignalSetBase
65     {
66     public:
67         ///////////////////////////////////////////////////////////////////////////
68         // Types
69
70         typedef boost::function<void (siginfo_t const &)> Callback;
71                                         ///< Callback type
72
73         ///////////////////////////////////////////////////////////////////////////
74         ///\name Structors and default members
75         ///@{
76
77         SignalEvent(int signal, Callback const & cb, bool initiallyEnabled=true);
78                                         ///< Register a signal event
79                                         /**< Registers \a cb as callback for the UNIX signal \a
80                                              signal. If \a initiallyEnabled is set \c false, the
81                                              callback will not be enabled automatically. Use
82                                              enable() to do so.
83                                              \param[in] signal UNIX signal to register callback for
84                                              \param[in] cb Callback to call
85                                              \param[in] initiallyEnabled if set \c false, do not
86                                                  enable callback automatically. */
87         ~SignalEvent();
88
89         ///@}
90         ///////////////////////////////////////////////////////////////////////////
91
92         void disable();                 ///< Enable signal event registration
93         void enable();                  ///< Disable the signal event registration
94
95         void action(Callback const & cb); ///< Change signal event callback
96
97     private:
98         virtual void v_run();
99         virtual char const * v_type() const;
100         virtual std::string v_info() const;
101
102         int signal_;
103         Callback cb_;
104         siginfo_t siginfo_;
105
106         friend class detail::SignalSetCompare;
107         friend class detail::FindNumericSignal;
108         friend class detail::SignalDispatcher;
109     };
110
111
112 }}
113
114 ///////////////////////////////hh.e////////////////////////////////////////
115 #include "SignalEvent.cci"
116 //#include "SignalEvent.ct"
117 //#include "SignalEvent.cti"
118 #endif
119
120 \f
121 // Local Variables:
122 // mode: c++
123 // fill-column: 100
124 // comment-column: 40
125 // c-file-style: "senf"
126 // indent-tabs-mode: nil
127 // ispell-local-dictionary: "american"
128 // compile-command: "scons -u test"
129 // End: