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: npp.c,v 1.1 2005-02-25 10:45:36 pj Exp $
24
 
25
 File:        $File$
26
 Revision:    $Revision: 1.1 $
27
 Last update: $Date: 2005-02-25 10:45:36 $
28
 ------------
29
 
30
 Non Preemptive Protocol. see npp.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 <npp/npp/npp.h>
55
 
56
#include <ll/ll.h>
1621 fabio 57
#include <arch/string.h>
58
#include <arch/stdio.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 NPP resource level descriptor */
66
typedef struct {
67
  mutex_resource_des m;   /*+ the mutex interface +*/
68
 
69
  int nlocked;  /*+ how many mutex a task currently locks +*/
70
} NPP_mutex_resource_des;
71
 
72
 
73
#if 0
74
/*+ print resource protocol statistics...+*/
75
static void NPP_resource_status(RLEVEL r)
76
{
77
  NPP_mutex_resource_des *m = (NPP_mutex_resource_des *)(resource_table[r]);
78
 
79
  kern_printf("%d Resources owned by the tasks %d\n", m->nlocked, exec_shadow);
80
}
81
#endif
82
 
83
static int NPP_res_register(RLEVEL l, PID p, RES_MODEL *r)
84
{
85
  /* NPP works with all tasks without Resource parameters */
86
  return -1;
87
}
88
 
89
static void NPP_res_detach(RLEVEL l, PID p)
90
{
91
  NPP_mutex_resource_des *m = (NPP_mutex_resource_des *)(resource_table[l]);
92
 
93
  if (m->nlocked)
94
    kern_raise(XMUTEX_OWNER_KILLED, p);
95
}
96
 
97
static int NPP_init(RLEVEL l, mutex_t *m, const mutexattr_t *a)
98
{
99
  if (a->mclass != NPP_MCLASS)
100
    return -1;
101
 
102
  m->mutexlevel = l;
103
  m->opt = (void *)NIL;
104
 
105
  return 0;
106
}
107
 
108
 
109
static int NPP_destroy(RLEVEL l, mutex_t *m)
110
{
111
//  NPP_mutex_resource_des *lev = (NPP_mutex_resource_des *)(resource_table[l]);
112
 
113
  if ( ((PID) m->opt) != NIL)
114
    return (EBUSY);
115
 
116
  return 0;
117
}
118
 
119
static int NPP_lock(RLEVEL l, mutex_t *m)
120
{
121
  NPP_mutex_resource_des *lev;
122
  SYS_FLAGS f;
123
 
124
  f = kern_fsave();
125
 
126
  if (((PID)m->opt) == exec_shadow) {
127
    /* the task already owns the mutex */
128
    kern_frestore(f);
129
    return (EDEADLK);
130
  }
131
 
132
  /* p->opt == NIL (It can't be the case of p->opt != NIL and != exec_shadow
133
     because when a task lock a mutex it become unpreemptable */
134
 
135
  /* the mutex is free, We can lock it! */
136
  lev = (NPP_mutex_resource_des *)(resource_table[l]);
137
 
138
  if (!lev->nlocked) task_nopreempt();
139
  lev->nlocked++;
140
 
141
  m->opt = (void *)exec_shadow;
142
 
143
  kern_frestore(f);
144
 
145
  return 0;
146
}
147
 
148
// static int NPP_trylock(RLEVEL l, mutex_t *m) is a non-sense!
149
 
150
static int NPP_unlock(RLEVEL l, mutex_t *m)
151
{
152
  NPP_mutex_resource_des *lev;
153
 
154
  /* the mutex is mine */
155
  lev = (NPP_mutex_resource_des *)(resource_table[l]);
156
  lev->nlocked--;
157
 
158
  m->opt = (void *)NIL;
159
 
160
  if (!lev->nlocked) task_preempt();
161
 
162
  return 0;
163
}
164
 
165
void NPP_register_module(void)
166
{
167
  RLEVEL l;                  /* the level that we register */
168
  NPP_mutex_resource_des *m;  /* for readableness only */
169
 
170
  printk("NPP_register_module\n");
171
 
172
  /* request an entry in the level_table */
173
  l = resource_alloc_descriptor();
174
 
175
  /* alloc the space needed for the EDF_level_des */
176
  m = (NPP_mutex_resource_des *)kern_alloc(sizeof(NPP_mutex_resource_des));
177
 
178
  /* update the level_table with the new entry */
179
  resource_table[l] = (resource_des *)m;
180
 
181
  /* fill the resource_des descriptor */
182
  m->m.r.rtype                       = MUTEX_RTYPE;
183
  m->m.r.res_register                = NPP_res_register;
184
  m->m.r.res_detach                  = NPP_res_detach;
185
 
186
  /* fill the mutex_resource_des descriptor */
187
  m->m.init                          = NPP_init;
188
  m->m.destroy                       = NPP_destroy;
189
  m->m.lock                          = NPP_lock;
190
  m->m.trylock                       = NPP_lock;   // !!!!!!!!!!!!
191
  m->m.unlock                        = NPP_unlock;
192
 
193
  /* fill the NPP_mutex_resource_des descriptor */
194
  m->nlocked = 0;
195
}
196