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: nop.c,v 1.1 2005-02-25 10:40:58 pj Exp $
24
 
25
 File:        $File$
26
 Revision:    $Revision: 1.1 $
27
 Last update: $Date: 2005-02-25 10:40:58 $
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 <nop/nop/nop.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 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;
75
  IQUEUE blocked;
76
} NOP_mutex_t;
77
 
78
 
79
/* Wait status for this library */
80
#define NOP_WAIT LIB_STATUS_BASE
81
 
82
static int NOP_res_register(RLEVEL l, PID p, RES_MODEL *r)
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;
94
 
95
  if (a->mclass != NOP_MCLASS)
96
    return -1;
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;
107
  iq_init(&p->blocked, &freedesc, 0);
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
  SYS_FLAGS f;
120
 
121
  if ( ((NOP_mutex_t *)m->opt)->owner != NIL)
122
    return (EBUSY);
123
 
124
  f = kern_fsave();
125
  if (m->opt) {
126
    kern_free(m->opt,sizeof(NOP_mutex_t));
127
    m->opt = NULL;
128
  }
129
  kern_frestore(f);
130
 
131
  return 0;
132
}
133
 
134
static int NOP_lock(RLEVEL l, mutex_t *m)
135
{
136
  NOP_mutex_t *p;
137
  SYS_FLAGS f;
138
 
139
  f = kern_fsave();
140
 
141
  p = (NOP_mutex_t *)m->opt;
142
  if (!p) {
143
    /* if the mutex is not initialized, initialize it! */
144
    NOP_mutexattr_t a;
145
    NOP_mutexattr_default(a);
146
    NOP_init(l, m, &a);
147
  }
148
 
149
  if (p->owner == exec_shadow) {
150
    /* the task already owns the mutex */
151
    kern_frestore(f);
152
    return (EDEADLK);
153
  }
154
 
155
  if (p->owner != NIL)  {           /* We must block exec task   */
156
       LEVEL l;            /* for readableness only */
157
 
158
       proc_table[exec_shadow].context = kern_context_save();
159
       kern_epilogue_macro();
160
 
161
       l = proc_table[exec_shadow].task_level;
162
       level_table[l]->public_block(l,exec_shadow);
163
 
164
       /* we insert the task in the semaphore queue */
165
       proc_table[exec_shadow].status = NOP_WAIT;
166
       iq_insertlast(exec_shadow,&p->blocked);
167
 
168
       /* and finally we reschedule */
169
       exec = exec_shadow = -1;
170
       scheduler();
171
       kern_context_load(proc_table[exec_shadow].context);
172
  }
173
  else {
174
    /* the mutex is free, We can lock it! */
175
    p->owner = exec_shadow;
176
    kern_frestore(f);
177
  }
178
 
179
  return 0;
180
}
181
 
182
static int NOP_trylock(RLEVEL l, mutex_t *m)
183
{
184
  NOP_mutex_t *p;
185
  SYS_FLAGS f;
186
 
187
  f = kern_fsave();
188
 
189
  p = (NOP_mutex_t *)m->opt;
190
  if (!p) {
191
    /* if the mutex is not initialized, initialize it! */
192
    NOP_mutexattr_t a;
193
    NOP_mutexattr_default(a);
194
    NOP_init(l, m, &a);
195
  }
196
 
197
  if (p->owner != NIL)  {
198
    /* a task already owns the mutex */
199
    kern_frestore(f);
200
    return (EBUSY);
201
  }
202
  else {
203
    /* the mutex is free, We can lock it! */
204
    p->owner = exec_shadow;
205
    kern_frestore(f);
206
  }
207
 
208
  return 0;
209
}
210
 
211
static int NOP_unlock(RLEVEL l, mutex_t *m)
212
{
213
  NOP_mutex_t *p;
214
 
215
  p = (NOP_mutex_t *)m->opt;
216
  if (!p)
217
    return (EINVAL);
218
 
219
  if (p->owner != exec_shadow) {
220
    /* the mutex is owned by another task!!! */
221
    kern_sti();
222
    return (EPERM);
223
  }
224
 
225
  proc_table[exec_shadow].context = kern_context_save();
226
 
227
  /* the mutex is mine, pop the firsttask to extract */
228
  p->owner = iq_getfirst(&p->blocked);
229
  if (p->owner != NIL) {
230
    l = proc_table[p->owner].task_level;
231
    level_table[l]->public_unblock(l,p->owner);
232
  }
233
 
234
  scheduler();
235
  kern_context_load(proc_table[exec_shadow].context);
236
 
237
  return 0;
238
}
239
 
240
RLEVEL NOP_register_module(void)
241
{
242
  RLEVEL l;                  /* the level that we register */
243
  NOP_mutex_resource_des *m;  /* for readableness only */
244
 
245
  printk("NOP_register_module\n");
246
 
247
  /* request an entry in the level_table */
248
  l = resource_alloc_descriptor();
249
 
250
  /* alloc the space needed for the EDF_level_des */
251
  m = (NOP_mutex_resource_des *)kern_alloc(sizeof(NOP_mutex_resource_des));
252
 
253
  /* update the level_table with the new entry */
254
  resource_table[l] = (resource_des *)m;
255
 
256
  /* fill the resource_des descriptor */
257
  m->m.r.rtype                       = MUTEX_RTYPE;
258
  m->m.r.res_register                = NOP_res_register;
259
  m->m.r.res_detach                  = NOP_res_detach;
260
 
261
  /* fill the mutex_resource_des descriptor */
262
  m->m.init                          = NOP_init;
263
  m->m.destroy                       = NOP_destroy;
264
  m->m.lock                          = NOP_lock;
265
  m->m.trylock                       = NOP_trylock;
266
  m->m.unlock                        = NOP_unlock;
267
 
268
  return l;
269
}
270