switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Scheduler / SignalEvent.hh
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief SignalDispatcher public header */
30
31 #ifndef HH_SENF_Scheduler_SignalEvent_
32 #define HH_SENF_Scheduler_SignalEvent_ 1
33
34 // Custom includes
35 #include <signal.h>
36 #include <boost/function.hpp>
37 #include "FIFORunner.hh"
38 #include <senf/boost_intrusive/iset_hook.hpp>
39
40 //#include "SignalEvent.mpp"
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 namespace senf {
44
45     class Scheduler;
46
47 namespace scheduler {
48
49     namespace detail {
50         struct SignalSetTag;
51         typedef boost::intrusive::iset_base_hook<SignalSetTag> SignalSetBase;
52         struct SignalSetCompare;
53         struct FindNumericSignal;
54         struct SignalDispatcher;
55     }
56
57     /** \brief UNIX signal event
58
59         The SignalEvent class registers a callback for UNIX signals. The callback will be called \e
60         synchronously (not from within the UNIX signal handler) by the scheduler.
61
62         The SignalEvent class is an implementation of the RAII idiom: The event will be
63         automatically unregistered in the SignalEvent destructor. The SignalEvent instance should be
64         created within the same scope or on a scope below where the callback is defined (e.g. if the
65         callback is a member function it should be defined as a class member).
66      */
67     class SignalEvent
68         : public detail::FIFORunner::TaskInfo,
69           public detail::SignalSetBase
70     {
71     public:
72         //-////////////////////////////////////////////////////////////////////////
73         // Types
74
75         typedef boost::function<void (siginfo_t const &)> Callback;
76                                         ///< Callback type
77
78         //-////////////////////////////////////////////////////////////////////////
79         ///\name Structors and default members
80         //\{
81
82         SignalEvent(int signal, Callback const & cb, bool initiallyEnabled=true);
83                                         ///< Register a signal event
84                                         /**< Registers \a cb as callback for the UNIX signal \a
85                                              signal. If \a initiallyEnabled is set \c false, the
86                                              callback will not be enabled automatically. Use
87                                              enable() to do so.
88                                              \param[in] signal UNIX signal to register callback for
89                                              \param[in] cb Callback to call
90                                              \param[in] initiallyEnabled if set \c false, do not
91                                                  enable callback automatically. */
92         ~SignalEvent();
93
94         //\}
95         //-////////////////////////////////////////////////////////////////////////
96
97         void disable();                 ///< Enable signal event registration
98         void enable();                  ///< Disable the signal event registration
99
100         void action(Callback const & cb); ///< Change signal event callback
101
102     private:
103         virtual void v_run();
104         virtual char const * v_type() const;
105         virtual std::string v_info() const;
106
107         int signal_;
108         Callback cb_;
109         siginfo_t siginfo_;
110
111         friend class detail::SignalSetCompare;
112         friend class detail::FindNumericSignal;
113         friend class detail::SignalDispatcher;
114     };
115
116
117 }}
118
119 //-/////////////////////////////////////////////////////////////////////////////////////////////////
120 #include "SignalEvent.cci"
121 //#include "SignalEvent.ct"
122 //#include "SignalEvent.cti"
123 #endif
124
125 \f
126 // Local Variables:
127 // mode: c++
128 // fill-column: 100
129 // comment-column: 40
130 // c-file-style: "senf"
131 // indent-tabs-mode: nil
132 // ispell-local-dictionary: "american"
133 // compile-command: "scons -u test"
134 // End: