Subversion Repositories shark

Rev

Details | 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
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
21
 CVS :        $Id: mutex.c,v 1.1.1.1 2002-03-29 14:12:52 pj Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1.1.1 $
25
 Last update: $Date: 2002-03-29 14:12:52 $
26
 ------------
27
 
28
This file contains the mutex and condition variables handling functions.
29
 
30
 
31
The mutex init function try to find a resource level with
32
rtype = MUTEX_RTYPE that can accept a mutex of the type of that
33
contained in mutexattr_t.
34
 
35
when found, it simply calls the level mutex init function.
36
 
37
Note that a mutex attribute must be specificated, whereas the
38
pthread_mutex_init accepts a pthread_mutexattr equal to NULL!!!
39
 
40
 
41
returns an integer of value:
42
 
43
EAGAIN  The system lacked the necessarty resources to initialize another mutex
44
ENOMEM  Insufficient memory exist to initialize the mutex
45
EPERM   The caller does not have the privilege to perform the operation
46
EBUSY   An init on a non-destroyed mutex is called
47
EINVAL  the value specified by attr parameter is invalid
48
        (no resource module found to handle the rtype...)
49
 
50
 
51
The others functions simply call the module-dependent functions...
52
 
53
**/
54
 
55
/*
56
 * Copyright (C) 2000 Paolo Gai
57
 *
58
 * This program is free software; you can redistribute it and/or modify
59
 * it under the terms of the GNU General Public License as published by
60
 * the Free Software Foundation; either version 2 of the License, or
61
 * (at your option) any later version.
62
 *
63
 * This program is distributed in the hope that it will be useful,
64
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
65
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
66
 * GNU General Public License for more details.
67
 *
68
 * You should have received a copy of the GNU General Public License
69
 * along with this program; if not, write to the Free Software
70
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
71
 *
72
 */
73
 
74
 
75
 
76
#include <kernel/const.h>
77
#include <sys/types.h>
78
#include <kernel/model.h>
79
#include <kernel/descr.h>
80
#include <kernel/var.h>
81
#include <kernel/func.h>
82
#include <errno.h>
83
 
84
/*---------------------------------------------------------------------*/
85
/* Mutexes                                                             */
86
/*---------------------------------------------------------------------*/
87
 
88
 
89
int mutex_init(mutex_t *mutex, const mutexattr_t *attr)
90
{
91
  RLEVEL l;
92
  int result = (EINVAL);
93
 
94
  kern_cli();
95
  mutex->mutexlevel = -1;
96
  mutex->use = 0;
97
 
98
  for (l=0; l<res_levels; l++) {
99
     if (resource_table[l]->rtype == MUTEX_RTYPE) {
100
       /* the cast to mutex_resource_des is legal */
101
       mutex_resource_des *m = (mutex_resource_des *)resource_table[l];
102
 
103
       /* can the mutex level manage the mutexattr_t ? */
104
       if (m->level_accept_mutexattr(l,attr) >=0) {
105
         result = m->init(l, mutex, attr);
106
       }
107
     }
108
  }
109
  kern_sti();
110
 
111
  return result;
112
}
113
 
114
 
115
int mutex_destroy(mutex_t *mutex)
116
{
117
  mutex_resource_des *m;
118
 
119
  if (mutex->mutexlevel == -1)
120
    return (EINVAL);
121
 
122
  if (mutex->use)
123
    return (EBUSY);
124
 
125
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
126
 
127
  return m->destroy(mutex->mutexlevel, mutex);
128
}
129
 
130
int mutex_lock(mutex_t *mutex)
131
{
132
  mutex_resource_des *m;
133
 
134
  if (mutex->mutexlevel == -1)
135
    return (EINVAL);
136
 
137
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
138
 
139
  return m->lock(mutex->mutexlevel, mutex);
140
}
141
 
142
int mutex_trylock(mutex_t *mutex)
143
{
144
  mutex_resource_des *m;
145
 
146
  if (mutex->mutexlevel == -1)
147
    return (EINVAL);
148
 
149
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
150
 
151
  return m->trylock(mutex->mutexlevel, mutex);
152
}
153
 
154
int mutex_unlock(mutex_t *mutex)
155
{
156
  mutex_resource_des *m;
157
 
158
  if (mutex->mutexlevel == -1)
159
    return (EINVAL);
160
 
161
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
162
 
163
  return m->unlock(mutex->mutexlevel, mutex);
164
}
165