Subversion Repositories shark

Rev

Rev 29 | 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: nopm.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
 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 <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 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;
29 pj 75
  IQUEUE blocked;
2 pj 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;
29 pj 110
    if (!iq_isempty(&ptr->blocked)) {
2 pj 111
      kern_printf("%i blocks on 0x%p: ",ptr->owner,table[i]);
29 pj 112
      j=iq_query_first(&ptr->blocked);
2 pj 113
      while (j!=NIL) {
114
        kern_printf("%i ",(int)j);
29 pj 115
        j=iq_query_next(j, &ptr->blocked);
2 pj 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
 
38 pj 141
static int NOPM_res_register(RLEVEL l, PID p, RES_MODEL *r)
2 pj 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
 
38 pj 154
  if (a->mclass != NOPM_MCLASS)
155
    return -1;
156
 
2 pj 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;
29 pj 166
  iq_init(&p->blocked, &freedesc, 0);
2 pj 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
 
183
  if ( ((NOPM_mutex_t *)m->opt)->owner != NIL)
184
    return (EBUSY);
185
 
186
  kern_cli();
187
  if (m->opt) {
188
    kern_free(m->opt,sizeof(NOPM_mutex_t));
189
    m->opt = NULL;
190
  }
191
  kern_sti();
192
 
193
  return 0;
194
}
195
 
196
static int NOPM_lock(RLEVEL l, mutex_t *m)
197
{
198
  NOPM_mutex_t *p;
199
 
200
  kern_cli();
201
 
202
  p = (NOPM_mutex_t *)m->opt;
203
  if (!p) {
204
    /* if the mutex is not initialized, initialize it! */
205
    NOPM_mutexattr_t a;
206
    NOPM_mutexattr_default(a);
207
    NOPM_init(l, m, &a);
208
  }
209
 
210
  if (p->owner == exec_shadow) {
211
    /* the task already owns the mutex */
212
    p->counter++;
213
    kern_sti();
214
    return 0;
215
  }
216
 
217
  if (p->owner != NIL)  {           /* We must block exec task   */
218
       LEVEL l;            /* for readableness only */
219
 
220
       proc_table[exec_shadow].context = kern_context_save();
38 pj 221
       kern_epilogue_macro();
2 pj 222
 
223
       l = proc_table[exec_shadow].task_level;
38 pj 224
       level_table[l]->public_block(l,exec_shadow);
2 pj 225
 
226
       /* we insert the task in the semaphore queue */
227
       proc_table[exec_shadow].status = NOPM_WAIT;
29 pj 228
       iq_insertlast(exec_shadow,&p->blocked);
2 pj 229
 
230
       /* and finally we reschedule */
231
       exec = exec_shadow = -1;
232
       scheduler();
233
       kern_context_load(proc_table[exec_shadow].context);            
234
  }
235
  else {
236
    /* the mutex is free, We can lock it! */
237
    p->owner = exec_shadow;
238
    p->counter++;
239
    kern_sti();
240
  }
241
 
242
  return 0;
243
}
244
 
245
static int NOPM_trylock(RLEVEL l, mutex_t *m)
246
{
247
  NOPM_mutex_t *p;
248
 
249
  kern_cli();
250
 
251
  p = (NOPM_mutex_t *)m->opt;
252
  if (!p) {
253
    /* if the mutex is not initialized, initialize it! */
254
    NOPM_mutexattr_t a;
255
    NOPM_mutexattr_default(a);
256
    NOPM_init(l, m, &a);
257
  }
258
 
259
  if (p->owner != NIL)  {
260
    /* a task already owns the mutex */
261
    kern_sti();
262
    return (EBUSY);
263
  }
264
  else {
265
    /* the mutex is free, We can lock it! */
266
    p->owner = exec_shadow;
267
    p->counter++;
268
    kern_sti();
269
  }
270
 
271
  return 0;
272
}
273
 
274
static int NOPM_unlock(RLEVEL l, mutex_t *m)
275
{
276
  NOPM_mutex_t *p;
277
  PID e;
278
 
279
  p = (NOPM_mutex_t *)m->opt;
280
  if (!p)
281
    return (EINVAL);
282
 
283
  if (p->owner != exec_shadow) {
284
    /* the mutex is owned by another task!!! */
285
    kern_printf("wrongunlock<owner=%i,unlocker=%i>",p->owner,exec_shadow);
286
    kern_sti();
287
    return (EPERM);
288
  }
289
 
290
  p->counter--;
291
  if (p->counter!=0) {
292
    /* we have multiple lock on this mutex */
293
    kern_sti();
294
    return 0;
295
  }
296
 
297
  proc_table[exec_shadow].context = kern_context_save();
298
 
299
  /* the mutex is mine, pop the firsttask to extract */
300
  for (;;) {
29 pj 301
    e = iq_getfirst(&p->blocked);
2 pj 302
    if (e == NIL) {
303
      p->owner = NIL;
304
      break;
305
    } else if (proc_table[e].status == NOPM_WAIT) {
306
      l = proc_table[e].task_level;
38 pj 307
      level_table[l]->public_unblock(l,e);
2 pj 308
      p->counter++;
309
      break;
310
    }
311
  }
312
 
313
  /* MG!!! */
314
  p->owner = e;
315
 
316
  scheduler();
317
  kern_context_load(proc_table[exec_shadow].context);
318
 
319
  return 0;
320
}
321
 
38 pj 322
RLEVEL NOPM_register_module(void)
2 pj 323
{
324
  RLEVEL l;                  /* the level that we register */
325
  NOPM_mutex_resource_des *m;  /* for readableness only */
326
 
327
  printk("NOPM_register_module\n");
328
 
329
  /* request an entry in the level_table */
330
  l = resource_alloc_descriptor();
331
 
332
  /* alloc the space needed for the EDF_level_des */
333
  m = (NOPM_mutex_resource_des *)kern_alloc(sizeof(NOPM_mutex_resource_des));
334
 
335
  /* update the level_table with the new entry */
336
  resource_table[l] = (resource_des *)m;
337
 
338
  /* fill the resource_des descriptor */
339
  m->m.r.rtype                       = MUTEX_RTYPE;
340
  m->m.r.res_register                = NOPM_res_register;
341
  m->m.r.res_detach                  = NOPM_res_detach;
342
 
343
  /* fill the mutex_resource_des descriptor */
344
  m->m.init                          = NOPM_init;
345
  m->m.destroy                       = NOPM_destroy;
346
  m->m.lock                          = NOPM_lock;
347
  m->m.trylock                       = NOPM_trylock;
348
  m->m.unlock                        = NOPM_unlock;
349
 
38 pj 350
  return l;
2 pj 351
}
352