switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Scheduler / TimerSource.hh
1 // $Id$
2 //
3 // Copyright (C) 2009
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 TimerSource public header */
30
31 #ifndef HH_SENF_Scheduler_TimerSource_
32 #define HH_SENF_Scheduler_TimerSource_ 1
33
34 // Custom includes
35 #include <boost/utility.hpp>
36 #include <signal.h>
37 #include "ClockService.hh"
38 #include "FdManager.hh"
39
40 //#include "TimerSource.mpp"
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 namespace senf {
44 namespace scheduler {
45 namespace detail {
46
47     class TimerSource
48         : boost::noncopyable
49     {
50     public:
51         virtual ~TimerSource();
52
53         virtual void timeout(ClockService::clock_type timeout) = 0;
54         virtual void notimeout() = 0;
55
56         virtual void enable() = 0;
57         virtual void disable() = 0;
58
59     protected:
60         TimerSource();
61     };
62
63     class POSIXTimerSource
64         : public detail::FdManager::Event, public TimerSource
65     {
66     public:
67         POSIXTimerSource();
68         ~POSIXTimerSource();
69
70         virtual void timeout(ClockService::clock_type timeout);
71         virtual void notimeout();
72
73         virtual void enable();
74         virtual void disable();
75
76     private:
77         static void sigHandler(int signal, ::siginfo_t * siginfo, void *);
78         virtual void signal(int events);
79         void reschedule();
80
81         bool timeoutEnabled_;
82         ClockService::clock_type timeout_;
83         int timerPipe_[2];
84         sigset_t sigSet_;
85         bool signalEnabled_;
86         timer_t timerId_;
87     };
88
89     class PollTimerSource
90         : public TimerSource
91     {
92     public:
93         virtual void timeout(ClockService::clock_type timeout);
94         virtual void notimeout();
95
96         virtual void enable();
97         virtual void disable();
98     };
99
100 #ifdef HAVE_TIMERFD_CREATE
101     class TimerFDTimerSource
102         : public detail::FdManager::Event, public TimerSource
103     {
104     public:
105         TimerFDTimerSource();
106         ~TimerFDTimerSource();
107
108         virtual void timeout(ClockService::clock_type timeout);
109         virtual void notimeout();
110
111         virtual void enable();
112         virtual void disable();
113
114         static bool haveTimerFD();
115
116     private:
117         virtual void signal(int events);
118         void reschedule();
119
120         int timerfd_;
121         bool timeoutEnabled_;
122         ClockService::clock_type timeout_;
123     };
124 #endif
125
126 }}}
127
128 //-/////////////////////////////////////////////////////////////////////////////////////////////////
129 #include "TimerSource.cci"
130 //#include "TimerSource.ct"
131 //#include "TimerSource.cti"
132 #endif
133
134 \f
135 // Local Variables:
136 // mode: c++
137 // fill-column: 100
138 // comment-column: 40
139 // c-file-style: "senf"
140 // indent-tabs-mode: nil
141 // ispell-local-dictionary: "american"
142 // compile-command: "scons -u test"
143 // End: