OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_1_x/] [or1ksim/] [support/] [sched.h] - Rev 1768

Go to most recent revision | Compare with Previous | Blame | View Log

/* sched.h -- Abstract entities header file handling job scheduler
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
 
This file is part of OpenRISC 1000 Architectural Simulator.
 
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
 
#ifndef _SCHED_H_
#define _SCHED_H_
 
/* Scheduler debug level */
#define SCHED_DEBUG     0
 
#define SCHED_HEAP_SIZE 128
#define SCHED_TIME_MAX  0x7fffffffffffffffLL
 
 
/* Structure for holding one job entry */
struct sched_entry {
  long long time;     /* Clock cycles before job starts */
  int  param;         /* Parameter to pass to the function */
  void (*func)(int);  /* Function to call when time reaches 0 */
};
 
/* Heap of jobs */
struct scheduler_struct {
  int size;
  struct sched_entry heap[SCHED_HEAP_SIZE];
};
 
extern struct scheduler_struct scheduler;
 
/* Dummy function, representing a guard, which protects heap from
   emptying */
extern void sched_guard (int i);
 
/* Init scheduler -- clear the heap */
#define SCHED_INIT() {\
        scheduler.heap[1].func = sched_guard;\
        scheduler.heap[1].time = SCHED_TIME_MAX;\
        scheduler.size = 2;\
}
 
#if SCHED_DEBUG > 1
#define SCHED_PRINT_JOBS() {\
        int i;\
        for (i = 1; i < scheduler.size; i++) \
          PRINTF ("\t%i: %x $%i @ %i\n", i, scheduler.heap[i].func, scheduler.heap[i].param, scheduler.heap[i].time);\
}
#else
#define SCHED_PRINT_JOBS()
#endif
 
/* Adds new job to the queue */
#define SCHED_ADD(job_func, job_param, job_time) {\
	int ___i;\
	if (SCHED_DEBUG > 0) PRINTF ("%s@%i:SCHED_ADD(func %p, param %i, time %i)\n", __FUNCTION__, runtime.sim.cycles, (job_func), (job_param), (job_time));\
	SCHED_PRINT_JOBS();\
	if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
	___i = scheduler.size++;\
	while (___i > 1 && scheduler.heap[___i / 2].time > (job_time)) scheduler.heap[___i] = scheduler.heap[___i /= 2];\
	scheduler.heap[___i].func = (job_func);\
	scheduler.heap[___i].param = (job_param);\
	scheduler.heap[___i].time = (job_time);\
	SCHED_PRINT_JOBS();\
}
 
/* Removes an item from the heap */
#define SCHED_REMOVE_ITEM(index) {\
	struct sched_entry *tmp;\
	int ___i = (index), ___j;\
        if (SCHED_DEBUG > 0) PRINTF ("%s@%i:SCHED_REMOVE%i(time %li)\n", __FUNCTION__, runtime.sim.cycles, (index), scheduler.heap[___i].time); \
	SCHED_PRINT_JOBS();\
	if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
	tmp = &scheduler.heap[--scheduler.size];\
	while (___i <= scheduler.size / 2) {\
	  ___j = 2 * ___i;\
	  if (___j < scheduler.size && scheduler.heap[___j].time > scheduler.heap[___j + 1].time) ___j++;\
          if (scheduler.heap[___j].time >= tmp->time) break;\
	  scheduler.heap[___i] = scheduler.heap[___j];\
	  ___i = ___j;\
	}\
	scheduler.heap[___i] = *tmp;\
	SCHED_PRINT_JOBS();\
}
 
/* Removes first item from the heap */
#define SCHED_REMOVE() SCHED_REMOVE_ITEM(1)
 
/* Returns item with lowest time in the heap */
#define SCHED_PEEK() scheduler.heap[1]
 
/* Returns a job with specified function and param, NULL if not found */
#define SCHED_FIND(f, p) ({\
        int i;\
        struct sched_entry *t = NULL;\
        for (i = 1; i < scheduler.size; i++)\
          if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
            t = &scheduler.heap[i]; break;\
          }\
        tmp;\
})
 
/* Returns a index of the job with specified function and param, 0 if not found */
#define SCHED_FIND_INDEX(f, p) ({\
        int i, found = 0;\
        for (i = 1; i < scheduler.size; i++)\
          if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
            found = i; break;\
          }\
        found;\
})
 
/* Deletes job with specified function and param */
#define SCHED_FIND_REMOVE(f, p) {\
        int index;\
        index = SCHED_FIND_INDEX(f, p);\
        if (index) SCHED_REMOVE_ITEM(index);\
}
 
#endif /* _SCHED_H_ */
 

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.