Subversion Repositories shark

Rev

Rev 1618 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
961 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: nopm.c,v 1.1 2005-02-25 10:55:09 pj Exp $
24
 
25
 File:        $File$
26
 Revision:    $Revision: 1.1 $
27
 Last update: $Date: 2005-02-25 10:55:09 $
28
 ------------
29
 
30
 See modules/nopm.h.
31
 This code is a copy of nop.c with minor modifications.
32
**/
33
 
34
/*
35
 * Copyright (C) 2000 Massimiliano Giorgi
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 <nopm/nopm/nopm.h>
55
 
56
#include <ll/ll.h>
1621 fabio 57
#include <arch/stdio.h>
58
#include <arch/string.h>
961 pj 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 NOPM resource level descriptor */
66
typedef struct {
67
  mutex_resource_des m;   /*+ the mutex interface +*/
68
} NOPM_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;
75
  IQUEUE blocked;
76
  int counter;
77
} NOPM_mutex_t;
78
 
79
 
80
 
81
 
82
 
83
 
84
 
85
 
86
 
87
#define MAXTABLE 4096
88
static mutex_t *table[MAXTABLE];
89
static int index=0;
90
 
91
static int register_nopm(mutex_t *p)
92
{
93
  if (index>=MAXTABLE) return -1;
94
  table[index++]=p;
95
  return 0;
96
}
97
 
98
void dump_nopm_table(void)
99
{
100
  NOPM_mutex_t *ptr;
101
  SYS_FLAGS f;
102
  PID j;
103
  int i;
104
 
105
  f=kern_fsave();
106
  kern_printf("nopm_mutex module TABLE\n");
107
  kern_printf("----------------------\n");
108
  for(i=0;i<index;i++) {
109
    ptr=table[i]->opt;
110
    if (!iq_isempty(&ptr->blocked)) {
111
      kern_printf("%i blocks on 0x%p: ",ptr->owner,table[i]);
112
      j=iq_query_first(&ptr->blocked);
113
      while (j!=NIL) {
114
        kern_printf("%i ",(int)j);
115
        j=iq_query_next(j, &ptr->blocked);
116
      }
117
      kern_printf("\n");
118
    } else {
119
      //kern_printf("0x%p no block\n",table[i]);
120
    }      
121
  }
122
  kern_frestore(f);
123
 
124
}
125
 
126
 
127
 
128
 
129
 
130
 
131
 
132
 
133
 
134
 
135
 
136
/* Wait status for this library */
137
#define NOPM_WAIT LIB_STATUS_BASE
138
 
139
 
140
 
141
static int NOPM_res_register(RLEVEL l, PID p, RES_MODEL *r)
142
{
143
  return -1;
144
}
145
 
146
static void NOPM_res_detach(RLEVEL l, PID p)
147
{
148
}
149
 
150
static int NOPM_init(RLEVEL l, mutex_t *m, const mutexattr_t *a)
151
{
152
  NOPM_mutex_t *p;
153
 
154
  if (a->mclass != NOPM_MCLASS)
155
    return -1;
156
 
157
  p = (NOPM_mutex_t *) kern_alloc(sizeof(NOPM_mutex_t));
158
 
159
  /* control if there is enough memory; no control on init on a
160
     non- destroyed mutex */
161
 
162
  if (!p)
163
    return (ENOMEM);
164
 
165
  p->owner = NIL;
166
  iq_init(&p->blocked, &freedesc, 0);
167
  p->counter=0;
168
 
169
  m->mutexlevel = l;
170
  m->opt = (void *)p;
171
 
172
  /* MG */
173
  register_nopm(m);
174
 
175
  return 0;
176
}
177
 
178
 
179
static int NOPM_destroy(RLEVEL l, mutex_t *m)
180
{
181
//  NOPM_mutex_resource_des *lev = (NOPM_mutex_resource_des *)(resource_table[l]);
182
  SYS_FLAGS f;
183
 
184
  if ( ((NOPM_mutex_t *)m->opt)->owner != NIL)
185
    return (EBUSY);
186
 
187
  f = kern_fsave();
188
  if (m->opt) {
189
    kern_free(m->opt,sizeof(NOPM_mutex_t));
190
    m->opt = NULL;
191
  }
192
  kern_frestore(f);
193
 
194
  return 0;
195
}
196
 
197
static int NOPM_lock(RLEVEL l, mutex_t *m)
198
{
199
  NOPM_mutex_t *p;
200
  SYS_FLAGS f;
201
 
202
  f = kern_fsave();
203
 
204
  p = (NOPM_mutex_t *)m->opt;
205
  if (!p) {
206
    /* if the mutex is not initialized, initialize it! */
207
    NOPM_mutexattr_t a;
208
    NOPM_mutexattr_default(a);
209
    NOPM_init(l, m, &a);
210
  }
211
 
212
  if (p->owner == exec_shadow) {
213
    /* the task already owns the mutex */
214
    p->counter++;
215
    kern_frestore(f);
216
    return 0;
217
  }
218
 
219
  if (p->owner != NIL)  {           /* We must block exec task   */
220
       LEVEL l;            /* for readableness only */
221
 
222
       proc_table[exec_shadow].context = kern_context_save();
223
       kern_epilogue_macro();
224
 
225
       l = proc_table[exec_shadow].task_level;
226
       level_table[l]->public_block(l,exec_shadow);
227
 
228
       /* we insert the task in the semaphore queue */
229
       proc_table[exec_shadow].status = NOPM_WAIT;
230
       iq_insertlast(exec_shadow,&p->blocked);
231
 
232
       /* and finally we reschedule */
233
       exec = exec_shadow = -1;
234
       scheduler();
235
       kern_context_load(proc_table[exec_shadow].context);            
236
  }
237
  else {
238
    /* the mutex is free, We can lock it! */
239
    p->owner = exec_shadow;
240
    p->counter++;
241
    kern_frestore(f);
242
  }
243
 
244
  return 0;
245
}
246
 
247
static int NOPM_trylock(RLEVEL l, mutex_t *m)
248
{
249
  NOPM_mutex_t *p;
250
  SYS_FLAGS f;
251
 
252
  f = kern_fsave();
253
 
254
  p = (NOPM_mutex_t *)m->opt;
255
  if (!p) {
256
    /* if the mutex is not initialized, initialize it! */
257
    NOPM_mutexattr_t a;
258
    NOPM_mutexattr_default(a);
259
    NOPM_init(l, m, &a);
260
  }
261
 
262
  if (p->owner != NIL)  {
263
    /* a task already owns the mutex */
264
    kern_frestore(f);
265
    return (EBUSY);
266
  }
267
  else {
268
    /* the mutex is free, We can lock it! */
269
    p->owner = exec_shadow;
270
    p->counter++;
271
    kern_frestore(f);
272
  }
273
 
274
  return 0;
275
}
276
 
277
static int NOPM_unlock(RLEVEL l, mutex_t *m)
278
{
279
  NOPM_mutex_t *p;
280
  PID e;
281
 
282
  p = (NOPM_mutex_t *)m->opt;
283
  if (!p)
284
    return (EINVAL);
285
 
286
  if (p->owner != exec_shadow) {
287
    /* the mutex is owned by another task!!! */
288
    kern_printf("wrongunlock<owner=%i,unlocker=%i>",p->owner,exec_shadow);
289
    kern_sti();
290
    return (EPERM);
291
  }
292
 
293
  p->counter--;
294
  if (p->counter!=0) {
295
    /* we have multiple lock on this mutex */
296
    kern_sti();
297
    return 0;
298
  }
299
 
300
  proc_table[exec_shadow].context = kern_context_save();
301
 
302
  /* the mutex is mine, pop the firsttask to extract */
303
  for (;;) {
304
    e = iq_getfirst(&p->blocked);
305
    if (e == NIL) {
306
      p->owner = NIL;
307
      break;
308
    } else if (proc_table[e].status == NOPM_WAIT) {
309
      l = proc_table[e].task_level;
310
      level_table[l]->public_unblock(l,e);
311
      p->counter++;
312
      break;
313
    }
314
  }
315
 
316
  /* MG!!! */
317
  p->owner = e;
318
 
319
  scheduler();
320
  kern_context_load(proc_table[exec_shadow].context);
321
 
322
  return 0;
323
}
324
 
325
RLEVEL NOPM_register_module(void)
326
{
327
  RLEVEL l;                  /* the level that we register */
328
  NOPM_mutex_resource_des *m;  /* for readableness only */
329
 
330
  printk("NOPM_register_module\n");
331
 
332
  /* request an entry in the level_table */
333
  l = resource_alloc_descriptor();
334
 
335
  /* alloc the space needed for the EDF_level_des */
336
  m = (NOPM_mutex_resource_des *)kern_alloc(sizeof(NOPM_mutex_resource_des));
337
 
338
  /* update the level_table with the new entry */
339
  resource_table[l] = (resource_des *)m;
340
 
341
  /* fill the resource_des descriptor */
342
  m->m.r.rtype                       = MUTEX_RTYPE;
343
  m->m.r.res_register                = NOPM_res_register;
344
  m->m.r.res_detach                  = NOPM_res_detach;
345
 
346
  /* fill the mutex_resource_des descriptor */
347
  m->m.init                          = NOPM_init;
348
  m->m.destroy                       = NOPM_destroy;
349
  m->m.lock                          = NOPM_lock;
350
  m->m.trylock                       = NOPM_trylock;
351
  m->m.unlock                        = NOPM_unlock;
352
 
353
  return l;
354
}
355