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 | ------------ |
||
23 | CVS : $Id: nop.c,v 1.1.1.1 2002-03-29 14:12:52 pj Exp $ |
||
24 | |||
25 | File: $File$ |
||
26 | Revision: $Revision: 1.1.1.1 $ |
||
27 | Last update: $Date: 2002-03-29 14:12:52 $ |
||
28 | ------------ |
||
29 | |||
30 | Binary Semaphores. see nop.h for more details... |
||
31 | |||
32 | **/ |
||
33 | |||
34 | /* |
||
35 | * Copyright (C) 2000 Paolo Gai |
||
36 | * |
||
37 | * This program is free software; you can redistribute it and/or modify |
||
38 | * it under the terms of the GNU General Public License as published by |
||
39 | * the Free Software Foundation; either version 2 of the License, or |
||
40 | * (at your option) any later version. |
||
41 | * |
||
42 | * This program is distributed in the hope that it will be useful, |
||
43 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
44 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
45 | * GNU General Public License for more details. |
||
46 | * |
||
47 | * You should have received a copy of the GNU General Public License |
||
48 | * along with this program; if not, write to the Free Software |
||
49 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
50 | * |
||
51 | */ |
||
52 | |||
53 | |||
54 | #include <modules/nop.h> |
||
55 | |||
56 | #include <ll/ll.h> |
||
57 | #include <ll/stdio.h> |
||
58 | #include <ll/string.h> |
||
59 | #include <kernel/const.h> |
||
60 | #include <sys/types.h> |
||
61 | #include <modules/codes.h> |
||
62 | #include <kernel/descr.h> |
||
63 | #include <kernel/var.h> |
||
64 | #include <kernel/func.h> |
||
65 | |||
66 | /* The NOP resource level descriptor */ |
||
67 | typedef struct { |
||
68 | mutex_resource_des m; /*+ the mutex interface +*/ |
||
69 | } NOP_mutex_resource_des; |
||
70 | |||
71 | |||
72 | /* this is the structure normally pointed by the opt field in the |
||
73 | mutex_t structure */ |
||
74 | typedef struct { |
||
75 | PID owner; |
||
76 | QQUEUE blocked; |
||
77 | } NOP_mutex_t; |
||
78 | |||
79 | |||
80 | /* Wait status for this library */ |
||
81 | #define NOP_WAIT LIB_STATUS_BASE |
||
82 | |||
83 | |||
84 | /*+ print resource protocol statistics...+*/ |
||
85 | static void NOP_resource_status(RLEVEL r) |
||
86 | { |
||
87 | kern_printf("No status for NOP module\n"); |
||
88 | } |
||
89 | |||
90 | |||
91 | static int NOP_level_accept_resource_model(RLEVEL l, RES_MODEL *r) |
||
92 | { |
||
93 | /* priority inheritance works with all tasks without Resource parameters */ |
||
94 | return -1; |
||
95 | } |
||
96 | |||
97 | static void NOP_res_register(RLEVEL l, PID p, RES_MODEL *r) |
||
98 | { |
||
99 | /* never called!!! */ |
||
100 | } |
||
101 | |||
102 | static void NOP_res_detach(RLEVEL l, PID p) |
||
103 | { |
||
104 | } |
||
105 | |||
106 | static int NOP_level_accept_mutexattr(RLEVEL l, const mutexattr_t *a) |
||
107 | { |
||
108 | if (a->mclass == NOP_MCLASS || a->mclass == (NOP_MCLASS | l) ) |
||
109 | return 0; |
||
110 | else |
||
111 | return -1; |
||
112 | } |
||
113 | |||
114 | static int NOP_init(RLEVEL l, mutex_t *m, const mutexattr_t *a) |
||
115 | { |
||
116 | NOP_mutex_t *p; |
||
117 | |||
118 | p = (NOP_mutex_t *) kern_alloc(sizeof(NOP_mutex_t)); |
||
119 | |||
120 | /* control if there is enough memory; no control on init on a |
||
121 | non- destroyed mutex */ |
||
122 | |||
123 | if (!p) |
||
124 | return (ENOMEM); |
||
125 | |||
126 | p->owner = NIL; |
||
127 | qq_init(&p->blocked); |
||
128 | |||
129 | m->mutexlevel = l; |
||
130 | m->opt = (void *)p; |
||
131 | |||
132 | return 0; |
||
133 | } |
||
134 | |||
135 | |||
136 | static int NOP_destroy(RLEVEL l, mutex_t *m) |
||
137 | { |
||
138 | // NOP_mutex_resource_des *lev = (NOP_mutex_resource_des *)(resource_table[l]); |
||
139 | |||
140 | if ( ((NOP_mutex_t *)m->opt)->owner != NIL) |
||
141 | return (EBUSY); |
||
142 | |||
143 | kern_cli(); |
||
144 | if (m->opt) { |
||
145 | kern_free(m->opt,sizeof(NOP_mutex_t)); |
||
146 | m->opt = NULL; |
||
147 | } |
||
148 | kern_sti(); |
||
149 | |||
150 | return 0; |
||
151 | } |
||
152 | |||
153 | static int NOP_lock(RLEVEL l, mutex_t *m) |
||
154 | { |
||
155 | NOP_mutex_t *p; |
||
156 | |||
157 | kern_cli(); |
||
158 | |||
159 | p = (NOP_mutex_t *)m->opt; |
||
160 | if (!p) { |
||
161 | /* if the mutex is not initialized, initialize it! */ |
||
162 | NOP_mutexattr_t a; |
||
163 | NOP_mutexattr_default(a); |
||
164 | NOP_init(l, m, &a); |
||
165 | } |
||
166 | |||
167 | if (p->owner == exec_shadow) { |
||
168 | /* the task already owns the mutex */ |
||
169 | kern_sti(); |
||
170 | return (EDEADLK); |
||
171 | } |
||
172 | |||
173 | if (p->owner != NIL) { /* We must block exec task */ |
||
174 | LEVEL l; /* for readableness only */ |
||
175 | TIME tx; /* a dummy TIME for timespec operations */ |
||
176 | struct timespec ty; /* a dummy timespec for timespec operations */ |
||
177 | |||
178 | proc_table[exec_shadow].context = kern_context_save(); |
||
179 | /* SAME AS SCHEDULER... manage the capacity event and the load_info */ |
||
180 | ll_gettime(TIME_EXACT, &schedule_time); |
||
181 | SUBTIMESPEC(&schedule_time, &cap_lasttime, &ty); |
||
182 | tx = TIMESPEC2USEC(&ty); |
||
183 | proc_table[exec_shadow].avail_time -= tx; |
||
184 | jet_update_slice(tx); |
||
185 | if (cap_timer != NIL) { |
||
186 | event_delete(cap_timer); |
||
187 | cap_timer = NIL; |
||
188 | } |
||
189 | |||
190 | l = proc_table[exec_shadow].task_level; |
||
191 | level_table[l]->task_extract(l,exec_shadow); |
||
192 | |||
193 | /* we insert the task in the semaphore queue */ |
||
194 | proc_table[exec_shadow].status = NOP_WAIT; |
||
195 | qq_insertlast(exec_shadow,&p->blocked); |
||
196 | |||
197 | /* and finally we reschedule */ |
||
198 | exec = exec_shadow = -1; |
||
199 | scheduler(); |
||
200 | kern_context_load(proc_table[exec_shadow].context); |
||
201 | } |
||
202 | else { |
||
203 | /* the mutex is free, We can lock it! */ |
||
204 | p->owner = exec_shadow; |
||
205 | kern_sti(); |
||
206 | } |
||
207 | |||
208 | return 0; |
||
209 | } |
||
210 | |||
211 | static int NOP_trylock(RLEVEL l, mutex_t *m) |
||
212 | { |
||
213 | NOP_mutex_t *p; |
||
214 | |||
215 | kern_cli(); |
||
216 | |||
217 | p = (NOP_mutex_t *)m->opt; |
||
218 | if (!p) { |
||
219 | /* if the mutex is not initialized, initialize it! */ |
||
220 | NOP_mutexattr_t a; |
||
221 | NOP_mutexattr_default(a); |
||
222 | NOP_init(l, m, &a); |
||
223 | } |
||
224 | |||
225 | if (p->owner != NIL) { |
||
226 | /* a task already owns the mutex */ |
||
227 | kern_sti(); |
||
228 | return (EBUSY); |
||
229 | } |
||
230 | else { |
||
231 | /* the mutex is free, We can lock it! */ |
||
232 | p->owner = exec_shadow; |
||
233 | kern_sti(); |
||
234 | } |
||
235 | |||
236 | return 0; |
||
237 | } |
||
238 | |||
239 | static int NOP_unlock(RLEVEL l, mutex_t *m) |
||
240 | { |
||
241 | NOP_mutex_t *p; |
||
242 | |||
243 | p = (NOP_mutex_t *)m->opt; |
||
244 | if (!p) |
||
245 | return (EINVAL); |
||
246 | |||
247 | if (p->owner != exec_shadow) { |
||
248 | /* the mutex is owned by another task!!! */ |
||
249 | kern_sti(); |
||
250 | return (EPERM); |
||
251 | } |
||
252 | |||
253 | proc_table[exec_shadow].context = kern_context_save(); |
||
254 | |||
255 | /* the mutex is mine, pop the firsttask to extract */ |
||
256 | p->owner = qq_getfirst(&p->blocked); |
||
257 | if (p->owner != NIL) { |
||
258 | l = proc_table[p->owner].task_level; |
||
259 | level_table[l]->task_insert(l,p->owner); |
||
260 | } |
||
261 | |||
262 | scheduler(); |
||
263 | kern_context_load(proc_table[exec_shadow].context); |
||
264 | |||
265 | return 0; |
||
266 | } |
||
267 | |||
268 | void NOP_register_module(void) |
||
269 | { |
||
270 | RLEVEL l; /* the level that we register */ |
||
271 | NOP_mutex_resource_des *m; /* for readableness only */ |
||
272 | |||
273 | printk("NOP_register_module\n"); |
||
274 | |||
275 | /* request an entry in the level_table */ |
||
276 | l = resource_alloc_descriptor(); |
||
277 | |||
278 | /* alloc the space needed for the EDF_level_des */ |
||
279 | m = (NOP_mutex_resource_des *)kern_alloc(sizeof(NOP_mutex_resource_des)); |
||
280 | |||
281 | /* update the level_table with the new entry */ |
||
282 | resource_table[l] = (resource_des *)m; |
||
283 | |||
284 | /* fill the resource_des descriptor */ |
||
285 | strncpy(m->m.r.res_name, NOP_MODULENAME, MAX_MODULENAME); |
||
286 | m->m.r.res_code = NOP_MODULE_CODE; |
||
287 | m->m.r.res_version = NOP_MODULE_VERSION; |
||
288 | |||
289 | m->m.r.rtype = MUTEX_RTYPE; |
||
290 | |||
291 | m->m.r.resource_status = NOP_resource_status; |
||
292 | m->m.r.level_accept_resource_model = NOP_level_accept_resource_model; |
||
293 | m->m.r.res_register = NOP_res_register; |
||
294 | |||
295 | m->m.r.res_detach = NOP_res_detach; |
||
296 | |||
297 | /* fill the mutex_resource_des descriptor */ |
||
298 | m->m.level_accept_mutexattr = NOP_level_accept_mutexattr; |
||
299 | m->m.init = NOP_init; |
||
300 | m->m.destroy = NOP_destroy; |
||
301 | m->m.lock = NOP_lock; |
||
302 | m->m.trylock = NOP_trylock; |
||
303 | m->m.unlock = NOP_unlock; |
||
304 | |||
305 | } |
||
306 |