[BACK]Return to Scheduler.h CVS log [TXT][DIR] Up to [Development] / fam / fam

Annotation of fam/fam/Scheduler.h, Revision 1.1.1.1

1.1       trev        1: //  Copyright (C) 1999 Silicon Graphics, Inc.  All Rights Reserved.
                      2: //
                      3: //  This program is free software; you can redistribute it and/or modify it
                      4: //  under the terms of version 2 of the GNU General Public License as
                      5: //  published by the Free Software Foundation.
                      6: //
                      7: //  This program is distributed in the hope that it would be useful, but
                      8: //  WITHOUT ANY WARRANTY; without even the implied warranty of
                      9: //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  Further, any
                     10: //  license provided herein, whether implied or otherwise, is limited to
                     11: //  this program in accordance with the express provisions of the GNU
                     12: //  General Public License.  Patent licenses, if any, provided herein do not
                     13: //  apply to combinations of this program with other product or programs, or
                     14: //  any other product whatsoever.  This program is distributed without any
                     15: //  warranty that the program is delivered free of the rightful claim of any
                     16: //  third person by way of infringement or the like.  See the GNU General
                     17: //  Public License for more details.
                     18: //
                     19: //  You should have received a copy of the GNU General Public License along
                     20: //  with this program; if not, write the Free Software Foundation, Inc., 59
                     21: //  Temple Place - Suite 330, Boston MA 02111-1307, USA.
                     22:
                     23: #ifndef Scheduler_included
                     24: #define Scheduler_included
                     25:
                     26: #include <sys/time.h>
                     27: #include <string.h>
                     28:
                     29: #include "Boolean.h"
                     30:
                     31: //  Scheduler is a class encapsulating a simple event and timer based
                     32: //  task scheduler.  Tasks are simply subroutines that are activated
                     33: //  by calling them.
                     34: //
                     35: //  A recurring task will be activated at fixed intervals of real
                     36: //  time.  It is unpredictable when the Scheduler will first activate
                     37: //  a recurring task.
                     38: //
                     39: //  A onetime task will be activated at a particular time.
                     40: //
                     41: //  Events are those defined by select(2) -- file descriptors that are
                     42: //  ready to be read, written or checked for exceptional conditions.
                     43: //  A handler may be installed to be called for any event on any
                     44: //  descriptor.  Only one handler may be installed for each event.
                     45: //  The installation and removal routines return the previously
                     46: //  installed handler.
                     47: //
                     48: //  Scheduler has fixed priorities -- write events precede exception
                     49: //  events precede read events precede timed tasks.
                     50: //
                     51: //  USE: There are no instances of Scheduler; all its interface
                     52: //  routines are static, and are called, e.g., "Scheduler::loop();"
                     53: //
                     54: //  RESTRICTIONS: Because we currently need only a single recurring
                     55: //  task, multiple recurring tasks are not implemented.
                     56:
                     57: class Scheduler {
                     58:
                     59: public:
                     60:
                     61:     typedef void (*IOHandler)(int fd, void *closure);
                     62:     typedef void (*TimedProc)(void *closure);
                     63:
                     64:     //  One-time tasks.
                     65:
                     66:     static void install_onetime_task(const timeval& when,
                     67: 				     TimedProc, void *closure);
                     68:     static void remove_onetime_task(TimedProc, void *closure);
                     69:
                     70:     //  Recurring tasks.
                     71:
                     72:     static void install_recurring_task(const timeval& interval,
                     73: 				       TimedProc, void *closure);
                     74:     static void remove_recurring_task(TimedProc, void *closure);
                     75:
                     76:     //  I/O handlers.
                     77:
                     78:     static IOHandler install_read_handler(int fd, IOHandler, void *closure);
                     79:     static IOHandler remove_read_handler(int fd);
                     80:
                     81:     static IOHandler install_write_handler(int fd, IOHandler, void *closure);
                     82:     static IOHandler remove_write_handler(int fd);
                     83:
                     84:     //  Mainline code.
                     85:
                     86:     static void select();
                     87:     static void exit()			{ running = false; }
                     88:     static void loop()			{ running = true;
                     89: 					  while (running) select(); }
                     90:
                     91: private:
                     92:
                     93:     //  Per-filedescriptor info is the set of three handlers and their
                     94:     //  closures.
                     95:
                     96:     struct FDInfo {
                     97: 	struct FDIOHandler {
                     98: 	    IOHandler handler;
                     99: 	    void *closure;
                    100: 	} read, write;
                    101:     };
                    102:
                    103:     //  Per-I/O type info is the file descriptor set passed to select(),
                    104:     //  the number of bits set there, and the offset into the FDInfo
                    105:     //  for the corresponding I/O type.
                    106:
                    107:     struct IOTypeInfo {
                    108: 	FDInfo::FDIOHandler FDInfo::*iotype;
                    109: 	unsigned int nbitsset;		// number of bits set in fds
                    110: 	fd_set fds;
                    111: 	IOTypeInfo(FDInfo::FDIOHandler FDInfo::* a_iotype) :
                    112:             iotype(a_iotype), nbitsset(0) { FD_ZERO(&fds); }
                    113:     };
                    114:
                    115:     struct onetime_task {
                    116: 	onetime_task *next;
                    117: 	timeval when;
                    118: 	Scheduler::TimedProc proc;
                    119: 	void *closure;
                    120:     };
                    121:
                    122:     // I/O event related variables
                    123:
                    124:     static IOTypeInfo read, write;
                    125:     static FDInfo *fdinfo;
                    126:     static unsigned int nfds;
                    127:     static unsigned int nfdinfo_alloc;
                    128:
                    129:     // Timed task related variables
                    130:
                    131:     static unsigned int ntasks;
                    132:     static timeval next_task_time;
                    133:     static TimedProc recurring_proc;
                    134:     static void *recurring_closure;
                    135:     static timeval recurring_interval;
                    136:     static onetime_task *first_task;
                    137:     static bool running;
                    138:
                    139:     // I/O event related functions
                    140:
                    141:     static FDInfo *fd_to_info(int fd);
                    142:     static void trim_fdinfo();
                    143:     static IOHandler install_io_handler(int fd,
                    144: 					IOHandler handler, void *closure,
                    145: 					IOTypeInfo *iotype);
                    146:     static IOHandler remove_io_handler(int fd, IOTypeInfo *iotype);
                    147:     static void handle_io(const fd_set *fds, FDInfo::FDIOHandler FDInfo::* iotype);
                    148:
                    149:     // Timed task related functions
                    150:
                    151:     static void do_tasks();
                    152:     static timeval *calc_timeout();
                    153:
                    154:     Scheduler();			// Never instantiate a Scheduler.
                    155:
                    156: };
                    157:
                    158: #endif /* !Scheduler_included */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>