Rev 38 | 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 | ------------ |
||
318 | giacomo | 21 | CVS : $Id: int_sem.c,v 1.4 2003-11-05 15:05:11 giacomo Exp $ |
2 | pj | 22 | |
23 | File: $File$ |
||
318 | giacomo | 24 | Revision: $Revision: 1.4 $ |
25 | Last update: $Date: 2003-11-05 15:05:11 $ |
||
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 | f = kern_fsave(); |
||
79 | |||
80 | if (s->count) { |
||
81 | s->count--; |
||
82 | kern_frestore(f); |
||
83 | return; |
||
84 | } |
||
85 | else { /* We must block exec task */ |
||
86 | LEVEL l; /* for readableness only */ |
||
87 | |||
88 | proc_table[exec_shadow].context = kern_context_save(); |
||
38 | pj | 89 | |
90 | kern_epilogue_macro(); |
||
2 | pj | 91 | |
92 | l = proc_table[exec_shadow].task_level; |
||
38 | pj | 93 | level_table[l]->public_block(l,exec_shadow); |
2 | pj | 94 | |
95 | /* we insert the task in the semaphore queue */ |
||
96 | proc_table[exec_shadow].status = INTERNAL_SEM_WAIT; |
||
29 | pj | 97 | iq_insertlast(exec_shadow,&s->blocked); |
2 | pj | 98 | |
99 | /* and finally we reschedule */ |
||
100 | exec = exec_shadow = -1; |
||
101 | scheduler(); |
||
102 | |||
103 | ll_context_to(proc_table[exec_shadow].context); |
||
104 | kern_after_dispatch(); |
||
105 | kern_frestore(f); |
||
106 | } |
||
107 | |||
108 | } |
||
109 | |||
110 | /* return 0 if the counter is decremented, -1 if not */ |
||
111 | int internal_sem_trywait(internal_sem_t *s) |
||
112 | { |
||
113 | SYS_FLAGS f; |
||
114 | |||
115 | f = kern_fsave(); |
||
116 | |||
117 | if (s->count) { |
||
118 | s->count--; |
||
119 | kern_frestore(f); |
||
120 | return 0; |
||
121 | } |
||
122 | |||
123 | return -1; |
||
124 | } |
||
125 | |||
126 | |||
127 | void internal_sem_post(internal_sem_t *s) |
||
128 | { |
||
129 | proc_table[exec_shadow].context = kern_context_save(); |
||
130 | |||
131 | if (s->blocked.first != -1) { |
||
132 | register PID p; |
||
133 | register LEVEL l; |
||
134 | |||
29 | pj | 135 | p = iq_getfirst(&s->blocked); |
2 | pj | 136 | l = proc_table[p].task_level; |
38 | pj | 137 | level_table[l]->public_unblock(l,p); |
2 | pj | 138 | |
139 | scheduler(); |
||
140 | } |
||
141 | else |
||
142 | s->count++; |
||
143 | |||
144 | kern_context_load(proc_table[exec_shadow].context); |
||
145 | } |
||
146 | |||
147 | int internal_sem_getvalue(internal_sem_t *s) |
||
148 | { |
||
149 | register int returnvalue; |
||
150 | SYS_FLAGS f; |
||
151 | |||
152 | f = kern_fsave(); |
||
153 | if (s->blocked.first == -1) |
||
154 | returnvalue = s->count; |
||
155 | else |
||
156 | returnvalue = -1; |
||
157 | |||
158 | kern_frestore(f); |
||
159 | return returnvalue; |
||
160 | } |