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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_52/] [or1ksim/] [support/] [sched.h] - Blame information for rev 1365

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

Line No. Rev Author Line
1 807 markom
/* sched.h -- Abstract entities header file handling job scheduler
2
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
#ifndef _SCHED_H_
21
#define _SCHED_H_
22
 
23
/* Scheduler debug level */
24
#define SCHED_DEBUG     0
25
 
26
#define SCHED_HEAP_SIZE 128
27 1319 phoenix
#define SCHED_TIME_MAX  0x7fffffffffffffffLL
28 807 markom
 
29 1319 phoenix
 
30 807 markom
/* Structure for holding one job entry */
31
struct sched_entry {
32 1319 phoenix
  long long time;     /* Clock cycles before job starts */
33 1365 nogj
  void *param;        /* Parameter to pass to the function */
34
  void (*func)(void *);  /* Function to call when time reaches 0 */
35 807 markom
};
36
 
37
/* Heap of jobs */
38
struct scheduler_struct {
39
  int size;
40
  struct sched_entry heap[SCHED_HEAP_SIZE];
41
};
42
 
43
extern struct scheduler_struct scheduler;
44
 
45
/* Dummy function, representing a guard, which protects heap from
46
   emptying */
47 1365 nogj
extern void sched_guard (void *);
48 807 markom
 
49
/* Init scheduler -- clear the heap */
50
#define SCHED_INIT() {\
51
        scheduler.heap[1].func = sched_guard;\
52 1319 phoenix
        scheduler.heap[1].time = SCHED_TIME_MAX;\
53 807 markom
        scheduler.size = 2;\
54
}
55
 
56
#if SCHED_DEBUG > 1
57
#define SCHED_PRINT_JOBS() {\
58
        int i;\
59
        for (i = 1; i < scheduler.size; i++) \
60 997 markom
          PRINTF ("\t%i: %x $%i @ %i\n", i, scheduler.heap[i].func, scheduler.heap[i].param, scheduler.heap[i].time);\
61 807 markom
}
62
#else
63
#define SCHED_PRINT_JOBS()
64
#endif
65
 
66
/* Adds new job to the queue */
67 1365 nogj
static inline void sched_add(void (*job_func)(void *), void *job_param,
68
                             long long job_time, const char *func)
69
{
70
  int i;
71
 
72
  if (SCHED_DEBUG > 0)
73
    PRINTF ("%s@%lli:SCHED_ADD(func %p, param %p, time %lli)\n", func,
74
            runtime.sim.cycles, job_func, job_param, job_time);
75
  SCHED_PRINT_JOBS();
76
 
77
  if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
78
 
79
  i = scheduler.size++;
80
  while (i > 1 && scheduler.heap[i / 2].time > job_time) {
81
    scheduler.heap[i] = scheduler.heap[i / 2];
82
    i /= 2;
83
  }
84
 
85
  scheduler.heap[i].func = job_func;
86
  scheduler.heap[i].param = job_param;
87
  scheduler.heap[i].time = job_time;
88
  SCHED_PRINT_JOBS();
89 807 markom
}
90
 
91 1365 nogj
#define SCHED_ADD(job_func, job_param, job_time) sched_add(job_func, job_param, job_time, __FUNCTION__)
92
 
93 807 markom
/* Removes an item from the heap */
94
#define SCHED_REMOVE_ITEM(index) {\
95
        struct sched_entry *tmp;\
96 821 markom
        int ___i = (index), ___j;\
97 1308 phoenix
        if (SCHED_DEBUG > 0) PRINTF ("%s@%i:SCHED_REMOVE%i(time %li)\n", __FUNCTION__, runtime.sim.cycles, (index), scheduler.heap[___i].time); \
98 807 markom
        SCHED_PRINT_JOBS();\
99 997 markom
        if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
100 807 markom
        tmp = &scheduler.heap[--scheduler.size];\
101 821 markom
        while (___i <= scheduler.size / 2) {\
102
          ___j = 2 * ___i;\
103
          if (___j < scheduler.size && scheduler.heap[___j].time > scheduler.heap[___j + 1].time) ___j++;\
104
          if (scheduler.heap[___j].time >= tmp->time) break;\
105
          scheduler.heap[___i] = scheduler.heap[___j];\
106
          ___i = ___j;\
107 807 markom
        }\
108 821 markom
        scheduler.heap[___i] = *tmp;\
109 807 markom
        SCHED_PRINT_JOBS();\
110
}
111
 
112
/* Removes first item from the heap */
113
#define SCHED_REMOVE() SCHED_REMOVE_ITEM(1)
114
 
115
/* Returns item with lowest time in the heap */
116
#define SCHED_PEEK() scheduler.heap[1]
117
 
118
/* Returns a job with specified function and param, NULL if not found */
119
#define SCHED_FIND(f, p) ({\
120
        int i;\
121
        struct sched_entry *t = NULL;\
122
        for (i = 1; i < scheduler.size; i++)\
123
          if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
124
            t = &scheduler.heap[i]; break;\
125
          }\
126
        tmp;\
127
})
128
 
129
/* Returns a index of the job with specified function and param, 0 if not found */
130
#define SCHED_FIND_INDEX(f, p) ({\
131
        int i, found = 0;\
132
        for (i = 1; i < scheduler.size; i++)\
133
          if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
134
            found = i; break;\
135
          }\
136
        found;\
137
})
138
 
139
/* Deletes job with specified function and param */
140
#define SCHED_FIND_REMOVE(f, p) {\
141
        int index;\
142
        index = SCHED_FIND_INDEX(f, p);\
143
        if (index) SCHED_REMOVE_ITEM(index);\
144
}
145
 
146
#endif /* _SCHED_H_ */

powered by: WebSVN 2.1.0

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