Subversion Repositories shark

Rev

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
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
21
 CVS :        $Id: int_sem.c,v 1.1.1.1 2002-03-29 14:12:51 pj Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1.1.1 $
25
 Last update: $Date: 2002-03-29 14:12:51 $
26
 ------------
27
 
28
 Internal semaphores.
29
 
30
 They are different from the Posix semaphores and the mutexes because:
31
 - internal_sem_wait is not a cancellation point
32
 - there are no limits on the semaphores that can be created
33
   (they works like a mutex_t...)
34
 - the queuing policy is FIFO
35
 - Be Careful!
36
   they are made to be fast... so not so many controls are done!!!
37
 
38
**/
39
 
40
/*
41
 * Copyright (C) 2000 Paolo Gai
42
 *
43
 * This program is free software; you can redistribute it and/or modify
44
 * it under the terms of the GNU General Public License as published by
45
 * the Free Software Foundation; either version 2 of the License, or
46
 * (at your option) any later version.
47
 *
48
 * This program is distributed in the hope that it will be useful,
49
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51
 * GNU General Public License for more details.
52
 *
53
 * You should have received a copy of the GNU General Public License
54
 * along with this program; if not, write to the Free Software
55
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
56
 *
57
 */
58
 
59
 
60
#include <kernel/int_sem.h>
61
#include <kernel/var.h>
62
#include <kernel/func.h>
63
 
64
/* Wait status for this library */
65
#define INTERNAL_SEM_WAIT LIB_STATUS_BASE
66
 
67
 
68
void internal_sem_init(internal_sem_t *s, int value)
69
{
70
  s->count = value;
71
  qq_init(&s->blocked);
72
}
73
 
74
void internal_sem_wait(internal_sem_t *s)
75
{
76
  SYS_FLAGS f;
77
 
78
  //kern_cli();
79
  f = kern_fsave();
80
 
81
  if (s->count) {
82
    s->count--;
83
    //kern_sti();
84
    kern_frestore(f);
85
    return;
86
  }
87
  else {                /* We must block exec task   */
88
    LEVEL l;            /* for readableness only */
89
    TIME tx;            /* a dummy TIME for timespec operations */
90
    struct timespec ty; /* a dummy timespec for timespec operations */
91
 
92
    proc_table[exec_shadow].context = kern_context_save();
93
    /* SAME AS SCHEDULER... manage the capacity event and the load_info */
94
    ll_gettime(TIME_EXACT, &schedule_time);
95
    SUBTIMESPEC(&schedule_time, &cap_lasttime, &ty);
96
    tx = TIMESPEC2USEC(&ty);
97
    proc_table[exec_shadow].avail_time -= tx;
98
    jet_update_slice(tx);
99
    if (cap_timer != NIL) {
100
      event_delete(cap_timer);
101
      cap_timer = NIL;
102
    }
103
 
104
    l = proc_table[exec_shadow].task_level;
105
    level_table[l]->task_extract(l,exec_shadow);
106
 
107
    /* we insert the task in the semaphore queue */
108
    proc_table[exec_shadow].status = INTERNAL_SEM_WAIT;
109
    qq_insertlast(exec_shadow,&s->blocked);
110
 
111
    /* and finally we reschedule */
112
    exec = exec_shadow = -1;
113
    scheduler();
114
 
115
    ll_context_to(proc_table[exec_shadow].context);
116
    kern_after_dispatch();
117
    kern_frestore(f);
118
  }
119
 
120
}
121
 
122
/* return 0 if the counter is decremented, -1 if not */
123
int internal_sem_trywait(internal_sem_t *s)
124
{
125
  SYS_FLAGS f;
126
 
127
  //kern_cli();
128
  f = kern_fsave();
129
 
130
  if (s->count) {
131
    s->count--;
132
    //kern_sti();
133
    kern_frestore(f);
134
    return 0;
135
  }
136
 
137
  //kern_sti();
138
  kern_frestore(f);
139
  return -1;
140
}
141
 
142
 
143
void internal_sem_post(internal_sem_t *s)
144
{
145
  proc_table[exec_shadow].context = kern_context_save();
146
 
147
  if (s->blocked.first != -1) {
148
    register PID p;
149
    register LEVEL l;
150
 
151
    p = qq_getfirst(&s->blocked);
152
    l = proc_table[p].task_level;
153
    level_table[l]->task_insert(l,p);
154
 
155
    scheduler();
156
  }
157
  else
158
    s->count++;
159
 
160
  kern_context_load(proc_table[exec_shadow].context);
161
}
162
 
163
int internal_sem_getvalue(internal_sem_t *s)
164
{
165
  register int returnvalue;
166
  SYS_FLAGS f;
167
 
168
  //kern_cli();
169
  f = kern_fsave();
170
  if (s->blocked.first == -1)
171
    returnvalue = s->count;
172
  else
173
    returnvalue = -1;
174
 
175
  //kern_sti();
176
  kern_frestore(f);
177
  return returnvalue;
178
}