00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 #ifndef ns_scheduler_h
00038 #define ns_scheduler_h
00039 
00040 #include "config.h"
00041 
00042 
00043 #ifdef HAVE_INT64
00044 typedef int64_t scheduler_uid_t;
00045 #define UID_PRINTF_FORMAT STRTOI64_FMTSTR
00046 #define STRTOUID(S) STRTOI64((S), NULL, 0)
00047 #else
00048 typedef int scheduler_uid_t;
00049 #define UID_PRINTF_FORMAT "%d"
00050 #define STRTOUID(S) atoi((S))
00051 #endif
00052 
00053 
00054 class Handler;
00055 
00056 class Event {
00057 public:
00058         Event* next_;           
00059         Event* prev_;
00060         Handler* handler_;      
00061         double time_;           
00062         scheduler_uid_t uid_;   
00063         Event() : time_(0), uid_(0) {}
00064 };
00065 
00066 
00067 
00068 
00069 
00070 
00071 class Handler {
00072  public:
00073         virtual ~Handler () {}
00074         virtual void handle(Event* event) = 0;
00075 };
00076 
00077 #define SCHED_START     0.0     
00078 
00079 class Scheduler : public TclObject {
00080 public:
00081         static Scheduler& instance() {
00082                 return (*instance_);            
00083         }
00084         void schedule(Handler*, Event*, double delay);  
00085         virtual void run();                     
00086         virtual void cancel(Event*) = 0;        
00087         virtual void insert(Event*) = 0;        
00088         virtual Event* lookup(scheduler_uid_t uid) = 0; 
00089         virtual Event* deque() = 0;             
00090         virtual const Event* head() = 0;        
00091         double clock() const {                  
00092                 return (clock_);
00093         }
00094         virtual void sync() {};
00095         virtual double start() {                
00096                 return SCHED_START;
00097         }
00098         virtual void reset();
00099 protected:
00100         void dumpq();   
00101         void dispatch(Event*);  
00102         void dispatch(Event*, double);  
00103         Scheduler();
00104         virtual ~Scheduler();
00105         int command(int argc, const char*const* argv);
00106         double clock_;
00107         int halted_;
00108         static Scheduler* instance_;
00109         static scheduler_uid_t uid_;
00110 };
00111 
00112 class ListScheduler : public Scheduler {
00113 public:
00114         ListScheduler() : queue_(0) {}
00115         void cancel(Event*);
00116         void insert(Event*);
00117         Event* deque();
00118         const Event* head() { return queue_; }
00119         Event* lookup(scheduler_uid_t uid);
00120 
00121 protected:
00122         Event* queue_;
00123 };
00124 
00125 #include "heap.h"
00126 
00127 class HeapScheduler : public Scheduler {
00128 public:
00129         HeapScheduler() { hp_ = new Heap; } 
00130         void cancel(Event* e) {
00131                 if (e->uid_ <= 0)
00132                         return;
00133                 e->uid_ = - e->uid_;
00134                 hp_->heap_delete((void*) e);
00135         }
00136         void insert(Event* e) {
00137                 hp_->heap_insert(e->time_, (void*) e);
00138         }
00139         Event* lookup(scheduler_uid_t uid);
00140         Event* deque();
00141         const Event* head() { return (const Event *)hp_->heap_min(); }
00142 protected:
00143         Heap* hp_;
00144 };
00145 
00146 class CalendarScheduler : public Scheduler {
00147 public:
00148         CalendarScheduler();
00149         ~CalendarScheduler();
00150         void cancel(Event*);
00151         void insert(Event*);
00152         Event* lookup(scheduler_uid_t uid);
00153         Event* deque();
00154         const Event* head();
00155 
00156 protected:
00157         double min_bin_width_;          
00158         unsigned int adjust_new_width_interval_; 
00159         unsigned time_to_newwidth;      
00160         long unsigned head_search_;
00161         long unsigned insert_search_;
00162         int round_num_;
00163         long int gap_num_;              
00164         double last_time_;              
00165         double avg_gap_;                
00166 
00167         double width_;
00168         double diff0_, diff1_, diff2_; 
00169 
00170         int stat_qsize_;                
00171         int nbuckets_;
00172         int lastbucket_;
00173         int top_threshold_;
00174         int bot_threshold_;
00175 
00176         struct Bucket {
00177                 Event *list_;
00178                 int    count_;
00179         } *buckets_;
00180                 
00181         int qsize_;
00182 
00183         virtual void reinit(int nbuck, double bwidth, double start);
00184         virtual void resize(int newsize, double start);
00185         virtual double newwidth(int newsize);
00186 
00187 private:
00188         virtual void insert2(Event*);
00189         double cal_clock_;  
00190 
00191 };
00192 
00193 class SplayScheduler : public Scheduler 
00194 {
00195 public:
00196         SplayScheduler() : root_(0), qsize_(0) {}
00197         void insert(Event *);
00198         Event *deque();
00199         const Event *head();
00200         void cancel(Event *);
00201         Event *lookup(scheduler_uid_t);
00202 
00203         
00204     
00205 protected:
00206         
00207 
00208 
00209 
00210 
00211         Event *uid_lookup(Event *);
00212 
00213         Event                   *root_;
00214         scheduler_uid_t         lookup_uid_;
00215         int                     qsize_;
00216 private:
00217         int validate(Event *);
00218 };
00219 
00220 
00221 #endif