summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/scheduler/scheduler.cc
diff options
context:
space:
mode:
authorschodet2005-01-17 15:04:29 +0000
committerschodet2005-01-17 15:04:29 +0000
commit86c65de3ae99420ca7a2af406a97ade6aa989774 (patch)
tree6023b98ae17f991ba92e6f9a0dc698ee8beb524d /2005/i/robert/src/scheduler/scheduler.cc
parented3f0dae905fad5a6659657e7b4ee5f08c00ccd8 (diff)
Initial revision
Diffstat (limited to '2005/i/robert/src/scheduler/scheduler.cc')
-rw-r--r--2005/i/robert/src/scheduler/scheduler.cc96
1 files changed, 96 insertions, 0 deletions
diff --git a/2005/i/robert/src/scheduler/scheduler.cc b/2005/i/robert/src/scheduler/scheduler.cc
new file mode 100644
index 0000000..e37504f
--- /dev/null
+++ b/2005/i/robert/src/scheduler/scheduler.cc
@@ -0,0 +1,96 @@
+// scheduler.cc
+// {{{
+//
+// Copyright (C) 2004 Nicolas Schodet
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Contact :
+// Web: http://perso.efrei.fr/~schodet/
+// Email: <contact@ni.fr.eu.org>
+// }}}
+#include "scheduler.hh"
+#include "utils/fd_set.hh"
+
+#include <stdlib.h> //XXX
+#include <sys/types.h>
+#include <sys/time.h>
+
+namespace scheduler {
+
+/// Destructeur.
+Scheduler::~Scheduler (void)
+{
+ // TODO: Test for schedulables_ empty ?
+}
+
+/// Ajoute un élément schedulable.
+void
+Scheduler::insert (Schedulable &schedulable)
+{
+ schedulables_.insert (&schedulable);
+}
+
+/// Enlève un élément schedulable.
+void
+Scheduler::erase (Schedulable &schedulable)
+{
+ schedulables_.erase (&schedulable);
+}
+
+/// Lance le scheduler. Si timeout est différent de -1, c'est le temps
+/// maximal que prend le scheduler avant de rendre la main. Si
+/// returnOnEvent est true, le premier évenement arrète le scheduler.
+/// Renvois true si au moins un événement a été traité.
+bool
+Scheduler::schedule (int timeout/*-1*/, bool returnOnEvent/*false*/)
+{
+ bool event = false;
+ int t = time (0) * 1000; //XXX
+ int start = t;
+ do
+ {
+ // Prépare le select.
+ int to = timeout == -1 ? -1 : start + timeout - t;
+ FdSet readFds;
+ for (Schedulables::const_iterator i = schedulables_.begin ();
+ i != schedulables_.end ();
+ ++i)
+ {
+ int top = -1;
+ (*i)->setup (*this, t, readFds, top);
+ if (to == -1 || top != -1 && top < to)
+ {
+ to = top;
+ }
+ }
+ // Select.
+ timeval tv;
+ tv.tv_sec = to / 1000;
+ tv.tv_usec = to % 1000 * 1000;
+ select (FD_SETSIZE, readFds.get (), 0, 0, &tv);
+ // Run.
+ t = time (0) * 1000; //XXX
+ for (Schedulables::const_iterator i = schedulables_.begin ();
+ i != schedulables_.end ();
+ ++i)
+ event |= (*i)->run (*this, t, readFds);
+ t = time (0) * 1000; //XXX
+ } while (!(returnOnEvent && event) && start + timeout > t); //XXX
+ return event;
+}
+
+} // namespace scheduler
+