modules/up/src/Core/sched/Dispatcher.cc

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. Dispatcher
  2. process
  3. process
  4. nextTimer
  5. process
  6. mark
  7. process
  8. loop

   1 //
   2 // $Id: Dispatcher.cc,v 1.1.1.1 2000/03/10 16:32:22 engin Exp $
   3 //
   4 // Dispatcher.cc
   5 // Author(s): Ramesh Govindan <govindan@isi.edu>
   6 
   7 #ifdef HAVE_CONFIG_H
   8 #include <config.h>
   9 #endif
  10 
  11 #include "util/Types.hh"
  12 #include "util/Trail.hh"
  13 #include "sys/File.hh"
  14 #include "sys/Signal.hh"
  15 
  16 #include "sched/Timer.hh"
  17 #include "sched/Job.hh"
  18 #include "sched/Dispatcher.hh"
  19 
  20 // Constants
  21 static const int MaxFiles = 4096;
  22 
  23 // File local variables
  24 static File *readableFiles[MaxFiles];
  25 static File *writableFiles[MaxFiles];
  26 static TraceCode traceDispatcher("dispatcher");
  27 
  28 // The dispatcher class. There is only one instance of
  29 // this class in the system.
  30 Dispatcher dispatcher;
  31 TimeLong& sysClock = dispatcher.systemClock;
  32 
  33 Dispatcher::Dispatcher()
     /* [<][>][^][v][top][bottom][index][help] */
  34 {
  35     systemClock.sync();
  36 }
  37 
  38 void
  39 Dispatcher::JobList::process()
     /* [<][>][^][v][top][bottom][index][help] */
  40 {
  41     Job*        job;
  42 
  43     if (isEmpty()) {
  44         return;
  45     }
  46 
  47     TRACE(traceDispatcher,
  48           "processing a job\n");
  49     job = head();
  50     dispatcher.systemClock.sync();
  51     job->handle();
  52     delete job; // removes the job from the list
  53     return;
  54 }
  55     
  56 void
  57 Dispatcher::TimerList::process()
     /* [<][>][^][v][top][bottom][index][help] */
  58 {
  59     Timer*      timer;
  60     
  61     if (isEmpty()) {
  62         return;
  63     }
  64 
  65     while ((timer= head()) && (dispatcher.systemClock > timer->time())) {
  66         TRACE(traceDispatcher,
  67               "processing a timer\n");
  68         dispatcher.systemClock.sync();
  69         timer->handle();
  70     }
  71     return;
  72 }
  73 
  74 void
  75 Dispatcher::TimerList::nextTimer(TimeLong& next)
     /* [<][>][^][v][top][bottom][index][help] */
  76 {
  77     if (isEmpty()) {
  78         next = InfiniteTime;
  79         return;
  80     }
  81 
  82     next = head()->time();
  83 }
  84 
  85 void
  86 Dispatcher::FileList::process(TimeShort& waitFor)
     /* [<][>][^][v][top][bottom][index][help] */
  87 {
  88     File*       file;
  89     int         nr, nw;
  90     
  91     if (select(waitFor) <= 0) {
  92         return;
  93     }
  94 
  95     nr = 0; nw = 0;
  96     for (file = head(); file != NULL; file = next(file)) {
  97         if (issetRead(file)) {
  98             ASSERT(nr < MaxFiles);
  99             readableFiles[nr++] = file;
 100         }
 101     }
 102 
 103     TRACE(traceDispatcher,
 104           "%d file descriptor(s) readable\n",
 105           nr);
 106 
 107     while (--nr >= 0) {
 108         TRACE(traceDispatcher,
 109               "calling read handler\n");
 110         dispatcher.systemClock.sync();
 111         readableFiles[nr]->handle_r();
 112         readableFiles[nr] = (File *)NULL;
 113     }
 114 
 115     for (file = head(); file != NULL; file = next(file)) {
 116         if (issetWrite(file)) {
 117             ASSERT(nw < MaxFiles);
 118             writableFiles[nw++] = file;
 119         }
 120     }
 121 
 122 
 123     TRACE(traceDispatcher,
 124           "%d file descriptor(s) writable\n",
 125           nw);
 126 
 127     while (--nw >= 0) {
 128         TRACE(traceDispatcher,
 129               "performing callback\n");
 130         dispatcher.systemClock.sync();
 131         writableFiles[nw]->handle_w();
 132         writableFiles[nw] = (File *)NULL;
 133     }
 134 
 135     return;
 136 }
 137 
 138 void
 139 Dispatcher::SignalList::mark(const char* name)
     /* [<][>][^][v][top][bottom][index][help] */
 140 {
 141     Signal*     signal;
 142     
 143     if (isEmpty()) {
 144         return;
 145     }
 146 
 147     for (signal = head(); signal != NULL; signal = next(signal)) {
 148         if (!strcmp(name, signal->name())) {
 149             TRACE(traceDispatcher,
 150                   "marking the %s signal handler\n",
 151                   signal->name());
 152             signal->pending(true);
 153         }
 154     }
 155 }
 156 
 157 void
 158 Dispatcher::SignalList::process()
     /* [<][>][^][v][top][bottom][index][help] */
 159 {
 160     Signal*     signal;
 161     
 162     if (isEmpty()) {
 163         return;
 164     }
 165 
 166     for (signal = head(); signal != NULL; signal = next(signal)) {
 167         if (signal->pending()) {
 168             TRACE(traceDispatcher,
 169                   "calling the %s signal handler\n",
 170                   signal->name());
 171             signal->pending(false);
 172             signal->handle(NULL);
 173         }
 174     }
 175 }
 176     
 177 void
 178 Dispatcher::loop()
     /* [<][>][^][v][top][bottom][index][help] */
 179 {
 180     TimeLong    next;
 181     TimeShort   wait;
 182     TimeShort   maxWait(8,0);           // 8 second wait
 183     
 184     while (1) {
 185         timers.process();
 186         signals.process();
 187         jobs.process();
 188         signals.process();
 189 
 190         if (!jobs.isEmpty()) {
 191             wait.set(0);        // Poll
 192         } else {
 193             timers.nextTimer(next);
 194             systemClock.sync();
 195             wait = next - systemClock;
 196             if (wait > maxWait) {
 197                 wait = maxWait;
 198             }
 199         }
 200         files.process(wait);
 201         signals.process();
 202     }
 203 }
 204 
 205 // 
 206 //  Copyright (c) 1994 by the University of Southern California.
 207 //  All rights reserved.
 208 //
 209 //  Permission to use, copy, modify, and distribute this software and
 210 //  its documentation in source and binary forms for lawful
 211 //  non-commercial purposes and without fee is hereby granted, provided
 212 //  that the above copyright notice appear in all copies and that both
 213 //  the copyright notice and this permission notice appear in supporting
 214 //  documentation, and that any documentation, advertising materials,
 215 //  and other materials related to such distribution and use acknowledge
 216 //  that the software was developed by the University of Southern
 217 //  California and/or Information Sciences Institute.
 218 //  The name of the University of Southern California may not
 219 //  be used to endorse or promote products derived from this software
 220 //  without specific prior written permission.
 221 //
 222 //  THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
 223 //  ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  THIS SOFTWARE IS
 224 //  PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
 225 //  INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 226 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND 
 227 //  NON-INFRINGEMENT.
 228 //
 229 //  IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
 230 //  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
 231 //  TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
 232 //  THE USE OR PERFORMANCE OF THIS SOFTWARE.
 233 //
 234 //  Questions concerning this software should be directed to 
 235 //  info-ra@isi.edu.
 236 //

/* [<][>][^][v][top][bottom][index][help] */