📄 scheduler.cc
字号:
static char rcsid [] = "$Id: scheduler.cc,v 1.1 2001/08/07 22:10:38 suman Exp suman $";/* * $Log: scheduler.cc,v $ * Revision 1.1 2001/08/07 22:10:38 suman * Initial revision * *//* * File: scheduler.cc * Author: Suman Banerjee <suman@cs.umd.edu> * Date: July 31, 2001 * Terms: GPL * * myns simulator */#include <assert.h>#include <stdio.h>#include <stdlib.h>#include <limits.h>#include <timer.h>#include <scheduler.h>#undef LOG_JUNK2/* Precision of the simulation clock: default is usec precision */NamedConstant const_clock_precision_digits("clock_precision_digits",6);long int int_power (long int a, int b);int time_compare_fn (void *pd1, void *pd2);Heap * Scheduler::time_heap = NULL;double Scheduler::curr_time = -1.0;int Timer::timer_id_gen = 0;double ten_to_power_precision_digits;#ifdef LOG_JUNK2static int ___count = 0;#endif // LOG_JUNK2long int int_power (long int a, int b) { long int r = 1; for (int i = 0; i < b; i++) r *= a; return r;}void Scheduler::SchedulerClassInit (void){ time_heap = init_heap(); curr_time = -1.0; ten_to_power_precision_digits = (double)(int_power(10,CONST_CLOCK_PRECISION_DIGITS)); return;}void Scheduler::SchedulerClassTerminate (void){ delete_heap(time_heap); time_heap = NULL; curr_time = -1.0; return;}double Scheduler::Clock (void){ return curr_time;}void * Scheduler::AddAbsoluteEvent (double at_time, EventInfo *pevent){#ifdef LOG_JUNK2 ___count ++;#endif // LOG_JUNK2 if (at_time < curr_time) { fprintf (stderr, "TimeError : Move backwards \n"); exit(0); } double *pkey = new double; *pkey = at_time;#ifdef LOG_JUNK2 printf ("Heap ins %8.3f\n", *(double*)pkey);#endif // LOG_JUNK2 void *pos = heap_insert(time_heap,(void*)pkey,(void*)pevent,time_compare_fn); return pos;}void * Scheduler::AddRelativeEvent (double rel_time, EventInfo *pevent){ return AddAbsoluteEvent(curr_time+rel_time,pevent);}EventInfo * Scheduler::DeleteEvent (void *pos){ void *pkey; void *pevent;#ifdef LOG_JUNK2 ___count --;#endif // LOG_JUNK2 int found = heap_extract_data(time_heap,pos,pkey,pevent,time_compare_fn); assert (found != -1);#ifdef LOG_JUNK2 printf ("Heap extract %8.3f\n", *(double*)pkey);#endif // LOG_JUNK2 delete ((double*)pkey); return (EventInfo*)pevent;}EventInfo * Scheduler::GetNextEvent (void){ void *pkey; void *pevent;#ifdef LOG_JUNK2 printf ("Count : %d\n", ___count);#endif // LOG_JUNK2#ifdef LOG_JUNK2 printf ("Heap extract min\n") ;#endif // LOG_JUNK2 int found = heap_extract_min(time_heap,pkey,pevent,time_compare_fn); if (found == -1) return NULL;#ifdef LOG_JUNK2 ___count --;#endif // LOG_JUNK2 curr_time = *(double*)pkey; delete ((double*)pkey); return (EventInfo *)pevent;}int time_compare_fn (void *pd1, void *pd2){ double dd1 = *(double*)pd1; double dd2 = *(double*)pd2; long long int d1 = PRECISION_TIME(dd1); long long int d2 = PRECISION_TIME(dd2); if (d1 > d2) return 1; if (d1 == d2) return 0; return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -