Subversion Repositories shark

Rev

Rev 3 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
12
 *   (see the web pages for full authors list)
13
 *
14
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
15
 *
16
 * http://www.sssup.it
17
 * http://retis.sssup.it
18
 * http://shark.sssup.it
19
 */
20
 
21
/*
22
 * Copyright (C) 2000 Massimiliano Giorgi
23
 *
24
 * This program is free software; you can redistribute it and/or modify
25
 * it under the terms of the GNU General Public License as published by
26
 * the Free Software Foundation; either version 2 of the License, or
27
 * (at your option) any later version.
28
 *
29
 * This program is distributed in the hope that it will be useful,
30
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32
 * GNU General Public License for more details.
33
 *
34
 * You should have received a copy of the GNU General Public License
35
 * along with this program; if not, write to the Free Software
36
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
37
 *
38
 */
39
 
40
#include "bqueue.h"
41
#include "glue.h"
42
 
43
/**/
44
 
45
#include <modules/bd_pscan.h>
46
 
47
static __inline__ int get_priority(void)
48
{
49
  int x;
50
  x=bd_pscan_getpriority();
51
  if (x<0||x>=NUMPRIORITY) return NUMPRIORITY-1;
52
  return x;
53
}
54
 
55
/**/
56
 
57
int pscan_init(pscan_queue_t *q)
58
{
59
  int i;
60
  __b_fastmutex_init(&q->mutex);
61
  q->disk=NULL;
62
  q->counter=0;
63
  q->actprior=0;
64
  for (i=0;i<NUMPRIORITY;i++) {
65
    q->pri[i].queue[0]=NULL;
66
    q->pri[i].queue[1]=NULL;
67
    q->pri[i].dir=0;
68
  }
69
  return 0;
70
}
71
 
72
int pscan_numelements(pscan_queue_t *q)
73
{
74
  return q->counter;
75
}
76
 
77
/* 0 -> no starvation (low performance) */
78
/* 1 -> possible starvation (high performance) */
79
#define STARVATION 1
80
 
81
int pscan_insertrequest(pscan_queue_t *q, request_prologue_t *e)
82
{
83
  request_prologue_t *p,*o;
84
  struct pscan_queues *ptr;
85
  int i;
86
 
87
  __b_fastmutex_lock(&q->mutex);
88
 
89
  i=get_priority();
90
  ptr=&q->pri[i];
91
 
92
  if (ptr->queue[ptr->dir]==NULL) ptr->dir^=1;
93
 
94
  if (ptr->queue[ptr->dir]==NULL) {
95
    ptr->queue[ptr->dir]=e;
96
    e->x.next=NULL;
97
  } else {
98
 
99
    if (e->cylinder==ptr->queue[ptr->dir]->cylinder) {
100
      if (ptr->dir==STARVATION) goto HI_INS;
101
      else                      goto LO_INS;
102
    }
103
 
104
    if (e->cylinder<ptr->queue[ptr->dir]->cylinder) {
105
    LO_INS:
106
      /* insert into low queue */
107
      o=NULL;
108
      p=ptr->queue[0];
109
      while (p!=NULL) {
110
        if (e->cylinder>p->cylinder) break;
111
        o=p;
112
        p=p->x.next;
113
      }
114
      if (o==NULL) ptr->queue[0]=e;
115
      else         o->x.next=e;
116
      e->x.next=p;
117
    } else {
118
    HI_INS:
119
      /* insert into high queue */
120
      o=NULL;
121
      p=ptr->queue[1];
122
      while (p!=NULL) {
123
        if (e->cylinder<p->cylinder) break;
124
        o=p;
125
        p=p->x.next;
126
      }
127
      if (o==NULL) ptr->queue[1]=e;
128
      else         o->x.next=e;
129
      e->x.next=p;
130
    }
131
 
132
  }
133
 
134
  q->counter++;
135
  __b_fastmutex_unlock(&q->mutex);  
136
  return 0;
137
}
138
 
139
request_prologue_t *pscan_getrequest(pscan_queue_t *q)
140
{
141
  request_prologue_t *ret;
142
  int i;
143
  __b_fastmutex_lock(&q->mutex);
144
  for (i=0;i<NUMPRIORITY;i++) {
145
    q->actprior=i;
146
    ret=q->pri[i].queue[q->pri[i].dir];
147
    if (ret!=NULL) break;
148
    else {
149
      q->pri[i].dir^=1;
150
      ret=q->pri[i].queue[q->pri[i].dir];
151
      if (ret!=NULL) break;
152
    }
153
  }
154
  //cprintf("{%i}",i);  
155
  __b_fastmutex_lock(&q->mutex);
156
  return ret;
157
}
158
 
159
int pscan_removerequest(pscan_queue_t *q)
160
{
161
  struct pscan_queues *ptr;
162
  __b_fastmutex_lock(&q->mutex);
163
  ptr=&q->pri[q->actprior];
164
  assertk(ptr->queue[ptr->dir]!=NULL);
165
  ptr->queue[ptr->dir]=ptr->queue[ptr->dir]->x.next;
166
  q->counter--;
167
  __b_fastmutex_unlock(&q->mutex);  
168
  return 0;
169
}