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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [support/] [sched.c] - Blame information for rev 97

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

Line No. Rev Author Line
1 19 jeremybenn
/* sched.c -- Abstract entities, handling job scheduling
2
 
3
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
4
   Copyright (C) 2008 Embecosm Limited
5
 
6
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
7
 
8
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
9
 
10
   This program is free software; you can redistribute it and/or modify it
11
   under the terms of the GNU General Public License as published by the Free
12
   Software Foundation; either version 3 of the License, or (at your option)
13
   any later version.
14
 
15
   This program is distributed in the hope that it will be useful, but WITHOUT
16
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18
   more details.
19
 
20
   You should have received a copy of the GNU General Public License along
21
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
 
23
/* This program is commented throughout in a fashion suitable for processing
24
   with Doxygen. */
25
 
26
 
27
/* Autoconf and/or portability configuration */
28
#include "config.h"
29
#include "port.h"
30
 
31
/* System includes */
32
#include <stdlib.h>
33
#include <stdio.h>
34
#include <limits.h>
35
 
36
/* Package includes */
37
#include "sched.h"
38
#include "debug.h"
39
#include "sim-config.h"
40
 
41
 
42
DECLARE_DEBUG_CHANNEL(sched_jobs);
43
 
44
#define SCHED_HEAP_SIZE 128
45
#define SCHED_TIME_MAX  INT32_MAX
46
 
47
/* FIXME: Scheduler should continue from previous cycles not current ones */
48
 
49
struct scheduler_struct scheduler;
50
 
51
/* Dummy function, representing a guard, which protects heap from
52
   emptying */
53
void sched_guard (void *dat)
54
{
55
  if(scheduler.job_queue)
56
    SCHED_ADD(sched_guard, dat, SCHED_TIME_MAX);
57
  else {
58
    scheduler.job_queue = scheduler.free_job_queue;
59
    scheduler.free_job_queue = scheduler.free_job_queue->next;
60
    scheduler.job_queue->next = NULL;
61
    scheduler.job_queue->func = sched_guard;
62
    scheduler.job_queue->time = SCHED_TIME_MAX;
63
  }
64
}
65
 
66
void sched_reset(void)
67
{
68
  struct sched_entry *cur, *next;
69
 
70
  for(cur = scheduler.job_queue; cur; cur = next) {
71
    next = cur->next;
72
    cur->next = scheduler.free_job_queue;
73
    scheduler.free_job_queue = cur;
74
  }
75
  scheduler.job_queue = NULL;
76
  sched_guard(NULL);
77
}
78
 
79
void sched_init(void)
80
{
81
  int i;
82
  struct sched_entry *new;
83
 
84
  scheduler.free_job_queue = NULL;
85
 
86
  for(i = 0; i < SCHED_HEAP_SIZE; i++) {
87
    if(!(new = malloc(sizeof(struct sched_entry)))) {
88
      fprintf(stderr, "Out-of-memory while allocateing scheduler queue\n");
89
      exit(1);
90
    }
91
    new->next = scheduler.free_job_queue;
92
    scheduler.free_job_queue = new;
93
  }
94
  scheduler.job_queue = NULL;
95
  sched_guard(NULL);
96
}
97
 
98
/* Executes jobs in time queue */
99
void do_scheduler(void)
100
{
101
  struct sched_entry *tmp;
102
 
103
  /* Execute all jobs till now */
104
  do {
105
    tmp = scheduler.job_queue;
106
    scheduler.job_queue = tmp->next;
107
    tmp->next = scheduler.free_job_queue;
108
    scheduler.free_job_queue = tmp;
109
 
110
    scheduler.job_queue->time += tmp->time;
111
 
112
    tmp->func (tmp->param);
113
  } while(scheduler.job_queue->time <= 0);
114
}
115
 
116
/* Adds new job to the queue */
117
void sched_add(void (*job_func)(void *), void *job_param, int32_t job_time,
118
               const char *func)
119
{
120
  struct sched_entry *cur, *prev, *new_job;
121
  int32_t alltime;
122
 
123
  cur = scheduler.job_queue;
124
  prev = NULL;
125
  alltime = cur->time;
126
  while(cur && (alltime < job_time)) {
127
    prev = cur;
128
    cur = cur->next;
129
    if(cur)
130
      alltime += cur->time;
131
  }
132
 
133
  new_job = scheduler.free_job_queue;
134
  scheduler.free_job_queue = new_job->next;
135
  new_job->next = cur;
136
 
137
  new_job->func = job_func;
138
  new_job->param = job_param;
139
 
140
  if(prev) {
141
    new_job->time = job_time - (alltime - (cur ? cur->time : 0));
142
    prev->next = new_job;
143
  } else {
144
    scheduler.job_queue = new_job;
145
    new_job->time = job_time >= 0 ? job_time : cur->time;
146
  }
147
 
148
  if(cur)
149
    cur->time -= new_job->time;
150
 
151
}
152
 
153
/* Returns a job with specified function and param, NULL if not found */
154
void sched_find_remove(void (*job_func)(void *), void *dat)
155
{
156
  struct sched_entry *cur;
157
  struct sched_entry *prev = NULL;
158
 
159
  for (cur = scheduler.job_queue; cur; prev = cur, cur = cur->next) {
160
    if ((cur->func == job_func) && (cur->param == dat)) {
161
      if(cur->next)
162
        cur->next->time += cur->time;
163
 
164
      if(prev)
165
        prev->next = cur->next;
166
      else
167
        scheduler.job_queue = cur->next;
168
      cur->next = scheduler.free_job_queue;
169
      scheduler.free_job_queue = cur;
170
      break;
171
    }
172
  }
173
}
174
 
175
/* Schedules the next job so that it will run after the next instruction */
176
void sched_next_insn(void (*func)(void *), void *dat)
177
{
178
  int32_t cycles = 1;
179
  struct sched_entry *cur = scheduler.job_queue;
180
 
181
  /* The cycles count of the jobs may go into negatives.  If this happens, func
182
   * will get called before the next instruction has executed. */
183
  while(cur && (cur->time < 0)) {
184
    cycles -= cur->time;
185
    cur = cur->next;
186
  }
187
 
188
  SCHED_ADD(func, dat, cycles);
189
}
190
 
191
 

powered by: WebSVN 2.1.0

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