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
int sstf_init(sstf_queue_t *q)
44
{
45
  __b_fastmutex_init(&q->mutex);
46
  q->disk=NULL;
47
  q->lqueue=NULL;
48
  q->hqueue=NULL;
49
  q->actual=NULL;
50
  q->counter=0;
51
  return 0;
52
}
53
 
54
int sstf_numelements(sstf_queue_t *q)
55
{
56
  return q->counter;
57
}
58
 
59
int sstf_insertrequest(sstf_queue_t *q, request_prologue_t *e)
60
{
61
  request_prologue_t *p,*o;
62
 
63
  __b_fastmutex_lock(&q->mutex);
64
  if (q->actual==NULL)
65
    q->actual=e;
66
  else {
67
    if (e->cylinder<=q->actual->cylinder) {
68
      /* insert into low queue */
69
      o=NULL;
70
      p=q->lqueue;
71
      while (p!=NULL) {
72
        if (e->cylinder>p->cylinder) break;
73
        o=p;
74
        p=p->x.next;
75
      }
76
      if (o==NULL) q->lqueue=e;
77
      else         o->x.next=e;
78
      e->x.next=p;
79
    } else {
80
      /* insert into high queue */
81
      o=NULL;
82
      p=q->hqueue;
83
      while (p!=NULL) {
84
        if (e->cylinder<p->cylinder) break;
85
        o=p;
86
        p=p->x.next;
87
      }
88
      if (o==NULL) q->hqueue=e;
89
      else         o->x.next=e;
90
      e->x.next=p;
91
    }
92
  }
93
  q->counter++;
94
  __b_fastmutex_unlock(&q->mutex);  
95
  return 0;
96
}
97
 
98
request_prologue_t *sstf_getrequest(sstf_queue_t *q)
99
{
100
  request_prologue_t *ret;
101
  __b_fastmutex_lock(&q->mutex);
102
  ret=q->actual;
103
  __b_fastmutex_lock(&q->mutex);
104
  return ret;
105
}
106
 
107
int sstf_removerequest(sstf_queue_t *q)
108
{
109
  __b_fastmutex_lock(&q->mutex);
110
  assertk(q->actual!=NULL);
111
  if (q->lqueue==NULL) {
112
    if (q->hqueue==NULL) {
113
 
114
      /* lqueue==NULL && hqueue==NULL */
115
 
116
      q->actual=NULL;
117
 
118
    } else {
119
 
120
      /* lqueue==NULL && hqueue!=NULL */
121
 
122
      q->actual=q->hqueue;
123
      q->hqueue=q->hqueue->x.next;
124
 
125
    }
126
  } else {
127
    if (q->hqueue==NULL) {
128
 
129
      /* lqueue!=NULL && hqueue==NULL */
130
 
131
      q->actual=q->lqueue;
132
      q->lqueue=q->lqueue->x.next;
133
 
134
    } else {
135
 
136
      /* lqueue!=NULL && hqueue!=NULL */
137
 
138
      int dl,dh;
139
      dh=q->hqueue->cylinder-q->actual->cylinder;
140
      dl=q->actual->cylinder-q->lqueue->cylinder;
141
      if (dh<dl) {
142
        q->actual=q->hqueue;
143
        q->hqueue=q->hqueue->x.next;
144
      } else {
145
        q->actual=q->lqueue;
146
        q->lqueue=q->lqueue->x.next;    
147
      }
148
 
149
    }
150
  }
151
 
152
  q->counter--;
153
  __b_fastmutex_unlock(&q->mutex);  
154
  return 0;
155
}