URL
https://opencores.org/ocsvn/or1k/or1k/trunk
Subversion Repositories or1k
[/] [or1k/] [tags/] [tn_m001/] [or1ksim/] [support/] [sched.h] - Rev 807
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 /* Structure for holding one job entry */ struct sched_entry { 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 = INT_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 %x, param %i, time %i)\n", __FUNCTION__, 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 %i)\n", __FUNCTION__, 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