Subversion Repositories shark

Rev

Rev 29 | Go to most recent revision | Details | Compare with Previous | 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
 ------------
38 pj 21
 CVS :        $Id: int_sem.c,v 1.3 2003-01-07 17:07:49 pj Exp $
2 pj 22
 
23
 File:        $File$
38 pj 24
 Revision:    $Revision: 1.3 $
25
 Last update: $Date: 2003-01-07 17:07:49 $
2 pj 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;
29 pj 71
  iq_init(&s->blocked,&freedesc,0);
2 pj 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
 
90
    proc_table[exec_shadow].context = kern_context_save();
38 pj 91
 
92
    kern_epilogue_macro();
2 pj 93
 
94
    l = proc_table[exec_shadow].task_level;
38 pj 95
    level_table[l]->public_block(l,exec_shadow);
2 pj 96
 
97
    /* we insert the task in the semaphore queue */
98
    proc_table[exec_shadow].status = INTERNAL_SEM_WAIT;
29 pj 99
    iq_insertlast(exec_shadow,&s->blocked);
2 pj 100
 
101
    /* and finally we reschedule */
102
    exec = exec_shadow = -1;
103
    scheduler();
104
 
105
    ll_context_to(proc_table[exec_shadow].context);
106
    kern_after_dispatch();
107
    kern_frestore(f);
108
  }
109
 
110
}
111
 
112
/* return 0 if the counter is decremented, -1 if not */
113
int internal_sem_trywait(internal_sem_t *s)
114
{
115
  SYS_FLAGS f;
116
 
117
  //kern_cli();
118
  f = kern_fsave();
119
 
120
  if (s->count) {
121
    s->count--;
122
    //kern_sti();
123
    kern_frestore(f);
124
    return 0;
125
  }
126
 
127
  //kern_sti();
128
  kern_frestore(f);
129
  return -1;
130
}
131
 
132
 
133
void internal_sem_post(internal_sem_t *s)
134
{
135
  proc_table[exec_shadow].context = kern_context_save();
136
 
137
  if (s->blocked.first != -1) {
138
    register PID p;
139
    register LEVEL l;
140
 
29 pj 141
    p = iq_getfirst(&s->blocked);
2 pj 142
    l = proc_table[p].task_level;
38 pj 143
    level_table[l]->public_unblock(l,p);
2 pj 144
 
145
    scheduler();
146
  }
147
  else
148
    s->count++;
149
 
150
  kern_context_load(proc_table[exec_shadow].context);
151
}
152
 
153
int internal_sem_getvalue(internal_sem_t *s)
154
{
155
  register int returnvalue;
156
  SYS_FLAGS f;
157
 
158
  //kern_cli();
159
  f = kern_fsave();
160
  if (s->blocked.first == -1)
161
    returnvalue = s->count;
162
  else
163
    returnvalue = -1;
164
 
165
  //kern_sti();
166
  kern_frestore(f);
167
  return returnvalue;
168
}