58c3d7e3424cfcd95073024b0be26aea3f37ddb1
[senf.git] / Scheduler / FdEvent.cc
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 FdDispatcher non-inline non-template implementation */
25
26 #include "FdEvent.hh"
27 #include "FdEvent.ih"
28
29 // Custom includes
30 #include "../Utils/senfassert.hh"
31
32 //#include "FdEvent.mpp"
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::scheduler::detail::FdDispatcher
38
39 prefix_ senf::scheduler::detail::FdDispatcher::~FdDispatcher()
40 {
41     for (FdSet::iterator i (fds_.begin()); i != fds_.end(); ++i) {
42         FdManager::instance().remove(i->fd_);
43         FIFORunner::instance().dequeue(&(*i));
44     }
45 }
46
47 prefix_ bool senf::scheduler::detail::FdDispatcher::add(FdEvent & event)
48 {
49     std::pair<FdSet::iterator,FdSet::iterator> range (fds_.equal_range(event));
50     int events (0);
51     for (FdSet::iterator i (range.first); i != range.second; ++i)
52         events |= i->events_;
53     if (event.events_ & events)
54         throw FdEvent::DuplicateEventRegistrationException();
55
56     if (! FdManager::instance().set(event.fd_, events | event.events_, &event))
57         return false;
58
59     FIFORunner::instance().enqueue(&event);
60     fds_.insert(range.first, event);
61
62     return true;
63 }
64
65 prefix_ void senf::scheduler::detail::FdDispatcher::remove(FdEvent & event)
66 {
67     fds_.erase(FdSet::current(event));
68     FIFORunner::instance().dequeue(&event);
69
70     std::pair<FdSet::iterator,FdSet::iterator> range (fds_.equal_range(event));
71     if (range.first == range.second)
72         FdManager::instance().remove(event.fd_);
73     else {
74         int events (0);
75         for (FdSet::iterator i (range.first); i != range.second; ++i)
76             events |= i->events_;
77         FdManager::instance().set(event.fd_, events, &(*range.first));
78     }
79 }
80
81 ///////////////////////////////////////////////////////////////////////////
82 // senf::scheduler::detail::FileDispatcher
83
84 prefix_ void senf::scheduler::detail::FileDispatcher::add(FdEvent & event)
85 {
86     std::pair<FdSet::iterator,FdSet::iterator> range (fds_.equal_range(event));
87     int events (0);
88     for (FdSet::iterator i (range.first); i != range.second; ++i)
89         events |= i->events_;
90     if (event.events_ & events)
91         throw FdEvent::DuplicateEventRegistrationException();
92
93     FIFORunner::instance().enqueue(&event);
94     fds_.insert(range.first, event);
95
96     FdManager::instance().timeout(0);
97 }
98
99 prefix_ senf::scheduler::detail::FileDispatcher::FileDispatcher()
100     : managerTimeout_ (scheduler::FdManager::instance().timeout())
101 {}
102
103 prefix_ senf::scheduler::detail::FileDispatcher::~FileDispatcher()
104 {
105     FdManager::instance().timeout(-1);
106     for (FdSet::iterator i (fds_.begin()); i != fds_.end(); ++i)
107         FIFORunner::instance().dequeue(&(*i));
108 }
109
110 prefix_ void senf::scheduler::detail::FileDispatcher::prepareRun()
111 {
112     for (FdSet::iterator i (fds_.begin()); i != fds_.end(); ++i) {
113         i->signaledEvents_ = i->events_;
114         i->setRunnable();
115     }
116 }
117
118 prefix_ void senf::scheduler::detail::FileDispatcher::remove(FdEvent & event)
119 {
120     fds_.erase(FdSet::current(event));
121     FIFORunner::instance().dequeue(&event);
122     if (fds_.empty())
123         FdManager::instance().timeout(managerTimeout_);
124 }
125
126 ///////////////////////////////////////////////////////////////////////////
127 // senf::scheduler::FdEvent
128
129 prefix_ void senf::scheduler::FdEvent::disable()
130 {
131     if (enabled()) {
132         if (pollable_)
133             detail::FdDispatcher::instance().remove(*this);
134         else
135             detail::FileDispatcher::instance().remove(*this);
136     }
137 }
138
139 prefix_ void senf::scheduler::FdEvent::enable()
140 {
141     if (! enabled() && events_ && fd_ != -1) {
142         if (pollable_ && detail::FdDispatcher::instance().add(*this))
143             return;
144         detail::FileDispatcher::instance().add(*this);
145         pollable_ = false;
146     }
147 }
148
149 prefix_ senf::scheduler::FdEvent & senf::scheduler::FdEvent::events(int events)
150 {
151     bool en = enabled();
152     disable();
153     events_ = events;
154     if (en)
155         enabled();
156     return *this;
157 }
158
159 prefix_ void senf::scheduler::FdEvent::signal(int events)
160 {
161     detail::FdDispatcher::FdSet::iterator i (detail::FdDispatcher::FdSet::current(*this));
162     detail::FdDispatcher::FdSet::iterator const i_end (detail::FdDispatcher::instance().fds_.end());
163     bool all ((events & (EV_ERR | EV_HUP)) && ! (events & (EV_READ | EV_PRIO | EV_WRITE)));
164     for (; i != i_end && fd_ == i->fd_; ++i) {
165         i->signaledEvents_ = events;
166         if (i->events_ & events || all)
167             i->setRunnable();
168     }
169 }
170
171 prefix_ void senf::scheduler::FdEvent::run()
172 {
173     cb_(signaledEvents_);
174 }
175
176 ///////////////////////////////cc.e////////////////////////////////////////
177 #undef prefix_
178 //#include "FdEvent.mpp"
179
180 \f
181 // Local Variables:
182 // mode: c++
183 // fill-column: 100
184 // comment-column: 40
185 // c-file-style: "senf"
186 // indent-tabs-mode: nil
187 // ispell-local-dictionary: "american"
188 // compile-command: "scons -u test"
189 // End: