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 | * 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 | ------------ |
||
38 | pj | 23 | CVS : $Id: nop.c,v 1.3 2003-01-07 17:07:50 pj Exp $ |
2 | pj | 24 | |
25 | File: $File$ |
||
38 | pj | 26 | Revision: $Revision: 1.3 $ |
27 | Last update: $Date: 2003-01-07 17:07:50 $ |
||
2 | pj | 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 <kernel/descr.h> |
||
62 | #include <kernel/var.h> |
||
63 | #include <kernel/func.h> |
||
64 | |||
65 | /* The NOP resource level descriptor */ |
||
66 | typedef struct { |
||
67 | mutex_resource_des m; /*+ the mutex interface +*/ |
||
68 | } NOP_mutex_resource_des; |
||
69 | |||
70 | |||
71 | /* this is the structure normally pointed by the opt field in the |
||
72 | mutex_t structure */ |
||
73 | typedef struct { |
||
74 | PID owner; |
||
29 | pj | 75 | IQUEUE blocked; |
2 | pj | 76 | } NOP_mutex_t; |
77 | |||
78 | |||
79 | /* Wait status for this library */ |
||
80 | #define NOP_WAIT LIB_STATUS_BASE |
||
81 | |||
38 | pj | 82 | static int NOP_res_register(RLEVEL l, PID p, RES_MODEL *r) |
2 | pj | 83 | { |
84 | return -1; |
||
85 | } |
||
86 | |||
87 | static void NOP_res_detach(RLEVEL l, PID p) |
||
88 | { |
||
89 | } |
||
90 | |||
91 | static int NOP_init(RLEVEL l, mutex_t *m, const mutexattr_t *a) |
||
92 | { |
||
93 | NOP_mutex_t *p; |
||
38 | pj | 94 | |
95 | if (a->mclass != NOP_MCLASS) |
||
96 | return -1; |
||
2 | pj | 97 | |
98 | p = (NOP_mutex_t *) kern_alloc(sizeof(NOP_mutex_t)); |
||
99 | |||
100 | /* control if there is enough memory; no control on init on a |
||
101 | non- destroyed mutex */ |
||
102 | |||
103 | if (!p) |
||
104 | return (ENOMEM); |
||
105 | |||
106 | p->owner = NIL; |
||
29 | pj | 107 | iq_init(&p->blocked, &freedesc, 0); |
2 | pj | 108 | |
109 | m->mutexlevel = l; |
||
110 | m->opt = (void *)p; |
||
111 | |||
112 | return 0; |
||
113 | } |
||
114 | |||
115 | |||
116 | static int NOP_destroy(RLEVEL l, mutex_t *m) |
||
117 | { |
||
118 | // NOP_mutex_resource_des *lev = (NOP_mutex_resource_des *)(resource_table[l]); |
||
119 | |||
120 | if ( ((NOP_mutex_t *)m->opt)->owner != NIL) |
||
121 | return (EBUSY); |
||
122 | |||
123 | kern_cli(); |
||
124 | if (m->opt) { |
||
125 | kern_free(m->opt,sizeof(NOP_mutex_t)); |
||
126 | m->opt = NULL; |
||
127 | } |
||
128 | kern_sti(); |
||
129 | |||
130 | return 0; |
||
131 | } |
||
132 | |||
133 | static int NOP_lock(RLEVEL l, mutex_t *m) |
||
134 | { |
||
135 | NOP_mutex_t *p; |
||
136 | |||
137 | kern_cli(); |
||
138 | |||
139 | p = (NOP_mutex_t *)m->opt; |
||
140 | if (!p) { |
||
141 | /* if the mutex is not initialized, initialize it! */ |
||
142 | NOP_mutexattr_t a; |
||
143 | NOP_mutexattr_default(a); |
||
144 | NOP_init(l, m, &a); |
||
145 | } |
||
146 | |||
147 | if (p->owner == exec_shadow) { |
||
148 | /* the task already owns the mutex */ |
||
149 | kern_sti(); |
||
150 | return (EDEADLK); |
||
151 | } |
||
152 | |||
153 | if (p->owner != NIL) { /* We must block exec task */ |
||
154 | LEVEL l; /* for readableness only */ |
||
155 | |||
156 | proc_table[exec_shadow].context = kern_context_save(); |
||
38 | pj | 157 | kern_epilogue_macro(); |
2 | pj | 158 | |
159 | l = proc_table[exec_shadow].task_level; |
||
38 | pj | 160 | level_table[l]->public_block(l,exec_shadow); |
2 | pj | 161 | |
162 | /* we insert the task in the semaphore queue */ |
||
163 | proc_table[exec_shadow].status = NOP_WAIT; |
||
29 | pj | 164 | iq_insertlast(exec_shadow,&p->blocked); |
2 | pj | 165 | |
166 | /* and finally we reschedule */ |
||
167 | exec = exec_shadow = -1; |
||
168 | scheduler(); |
||
169 | kern_context_load(proc_table[exec_shadow].context); |
||
170 | } |
||
171 | else { |
||
172 | /* the mutex is free, We can lock it! */ |
||
173 | p->owner = exec_shadow; |
||
174 | kern_sti(); |
||
175 | } |
||
176 | |||
177 | return 0; |
||
178 | } |
||
179 | |||
180 | static int NOP_trylock(RLEVEL l, mutex_t *m) |
||
181 | { |
||
182 | NOP_mutex_t *p; |
||
183 | |||
184 | kern_cli(); |
||
185 | |||
186 | p = (NOP_mutex_t *)m->opt; |
||
187 | if (!p) { |
||
188 | /* if the mutex is not initialized, initialize it! */ |
||
189 | NOP_mutexattr_t a; |
||
190 | NOP_mutexattr_default(a); |
||
191 | NOP_init(l, m, &a); |
||
192 | } |
||
193 | |||
194 | if (p->owner != NIL) { |
||
195 | /* a task already owns the mutex */ |
||
196 | kern_sti(); |
||
197 | return (EBUSY); |
||
198 | } |
||
199 | else { |
||
200 | /* the mutex is free, We can lock it! */ |
||
201 | p->owner = exec_shadow; |
||
202 | kern_sti(); |
||
203 | } |
||
204 | |||
205 | return 0; |
||
206 | } |
||
207 | |||
208 | static int NOP_unlock(RLEVEL l, mutex_t *m) |
||
209 | { |
||
210 | NOP_mutex_t *p; |
||
211 | |||
212 | p = (NOP_mutex_t *)m->opt; |
||
213 | if (!p) |
||
214 | return (EINVAL); |
||
215 | |||
216 | if (p->owner != exec_shadow) { |
||
217 | /* the mutex is owned by another task!!! */ |
||
218 | kern_sti(); |
||
219 | return (EPERM); |
||
220 | } |
||
221 | |||
222 | proc_table[exec_shadow].context = kern_context_save(); |
||
223 | |||
224 | /* the mutex is mine, pop the firsttask to extract */ |
||
29 | pj | 225 | p->owner = iq_getfirst(&p->blocked); |
2 | pj | 226 | if (p->owner != NIL) { |
227 | l = proc_table[p->owner].task_level; |
||
38 | pj | 228 | level_table[l]->public_unblock(l,p->owner); |
2 | pj | 229 | } |
230 | |||
231 | scheduler(); |
||
232 | kern_context_load(proc_table[exec_shadow].context); |
||
233 | |||
234 | return 0; |
||
235 | } |
||
236 | |||
38 | pj | 237 | RLEVEL NOP_register_module(void) |
2 | pj | 238 | { |
239 | RLEVEL l; /* the level that we register */ |
||
240 | NOP_mutex_resource_des *m; /* for readableness only */ |
||
241 | |||
242 | printk("NOP_register_module\n"); |
||
243 | |||
244 | /* request an entry in the level_table */ |
||
245 | l = resource_alloc_descriptor(); |
||
246 | |||
247 | /* alloc the space needed for the EDF_level_des */ |
||
248 | m = (NOP_mutex_resource_des *)kern_alloc(sizeof(NOP_mutex_resource_des)); |
||
249 | |||
250 | /* update the level_table with the new entry */ |
||
251 | resource_table[l] = (resource_des *)m; |
||
252 | |||
253 | /* fill the resource_des descriptor */ |
||
254 | m->m.r.rtype = MUTEX_RTYPE; |
||
255 | m->m.r.res_register = NOP_res_register; |
||
256 | m->m.r.res_detach = NOP_res_detach; |
||
257 | |||
258 | /* fill the mutex_resource_des descriptor */ |
||
259 | m->m.init = NOP_init; |
||
260 | m->m.destroy = NOP_destroy; |
||
261 | m->m.lock = NOP_lock; |
||
262 | m->m.trylock = NOP_trylock; |
||
263 | m->m.unlock = NOP_unlock; |
||
264 | |||
38 | pj | 265 | return l; |
2 | pj | 266 | } |
267 |