4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief FdDispatcher non-inline non-template implementation */
30 #include "../Utils/senfassert.hh"
32 //#include "FdEvent.mpp"
34 ///////////////////////////////cc.p////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::scheduler::detail::FdDispatcher
39 prefix_ senf::scheduler::detail::FdDispatcher::~FdDispatcher()
41 for (FdSet::iterator i (fds_.begin()); i != fds_.end(); ++i) {
42 detail::FdManager::instance().remove(i->fd_);
43 detail::FIFORunner::instance().dequeue(&(*i));
47 prefix_ bool senf::scheduler::detail::FdDispatcher::add(FdEvent & event)
49 std::pair<FdSet::iterator,FdSet::iterator> range (fds_.equal_range(event));
51 for (FdSet::iterator i (range.first); i != range.second; ++i)
53 if (event.events_ & events)
54 throw FdEvent::DuplicateEventRegistrationException();
56 if (! detail::FdManager::instance().set(event.fd_, events | event.events_, &event))
59 detail::FIFORunner::instance().enqueue(&event);
60 fds_.insert(range.first, event);
65 prefix_ void senf::scheduler::detail::FdDispatcher::remove(FdEvent & event)
67 fds_.erase(FdSet::current(event));
68 detail::FIFORunner::instance().dequeue(&event);
70 std::pair<FdSet::iterator,FdSet::iterator> range (fds_.equal_range(event));
71 if (range.first == range.second)
72 detail::FdManager::instance().remove(event.fd_);
75 for (FdSet::iterator i (range.first); i != range.second; ++i)
77 detail::FdManager::instance().set(event.fd_, events, &(*range.first));
81 ///////////////////////////////////////////////////////////////////////////
82 // senf::scheduler::detail::FileDispatcher
84 prefix_ void senf::scheduler::detail::FileDispatcher::add(FdEvent & event)
86 std::pair<FdSet::iterator,FdSet::iterator> range (fds_.equal_range(event));
88 for (FdSet::iterator i (range.first); i != range.second; ++i)
90 if (event.events_ & events)
91 throw FdEvent::DuplicateEventRegistrationException();
93 detail::FIFORunner::instance().enqueue(&event);
94 fds_.insert(range.first, event);
96 detail::FdManager::instance().timeout(0);
99 prefix_ senf::scheduler::detail::FileDispatcher::FileDispatcher()
100 : managerTimeout_ (scheduler::detail::FdManager::instance().timeout())
103 prefix_ senf::scheduler::detail::FileDispatcher::~FileDispatcher()
105 detail::FdManager::instance().timeout(-1);
106 for (FdSet::iterator i (fds_.begin()); i != fds_.end(); ++i)
107 detail::FIFORunner::instance().dequeue(&(*i));
110 prefix_ void senf::scheduler::detail::FileDispatcher::prepareRun()
112 for (FdSet::iterator i (fds_.begin()); i != fds_.end(); ++i) {
113 i->signaledEvents_ = i->events_;
118 prefix_ void senf::scheduler::detail::FileDispatcher::remove(FdEvent & event)
120 fds_.erase(FdSet::current(event));
121 detail::FIFORunner::instance().dequeue(&event);
123 detail::FdManager::instance().timeout(managerTimeout_);
126 ///////////////////////////////////////////////////////////////////////////
127 // senf::scheduler::FdEvent
129 prefix_ void senf::scheduler::FdEvent::disable()
133 detail::FdDispatcher::instance().remove(*this);
135 detail::FileDispatcher::instance().remove(*this);
139 prefix_ void senf::scheduler::FdEvent::enable()
141 if (! enabled() && events_ && fd_ != -1) {
142 if (pollable_ && detail::FdDispatcher::instance().add(*this))
144 detail::FileDispatcher::instance().add(*this);
149 prefix_ senf::scheduler::FdEvent & senf::scheduler::FdEvent::events(int events)
159 prefix_ void senf::scheduler::FdEvent::signal(int events)
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)
171 prefix_ void senf::scheduler::FdEvent::run()
173 cb_(signaledEvents_);
176 ///////////////////////////////cc.e////////////////////////////////////////
178 //#include "FdEvent.mpp"
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"