Subversion Repositories shark

Rev

Rev 38 | Rev 318 | Go to most recent revision | 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
 *   (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
 ------------
228 giacomo 21
 CVS :        $Id: mutex.c,v 1.3 2003-09-12 10:10:41 giacomo Exp $
2 pj 22
 
23
 File:        $File$
228 giacomo 24
 Revision:    $Revision: 1.3 $
25
 Last update: $Date: 2003-09-12 10:10:41 $
2 pj 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;
38 pj 92
  int result;
2 pj 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 ? */
228 giacomo 104
       if ((result = m->init(l, mutex, attr)) >=0) {
105
         kern_sti();
38 pj 106
         return result;
228 giacomo 107
       }
2 pj 108
     }
109
  }
110
  kern_sti();
111
 
38 pj 112
  return EINVAL;
2 pj 113
}
114
 
115
 
116
int mutex_destroy(mutex_t *mutex)
117
{
118
  mutex_resource_des *m;
119
 
120
  if (mutex->mutexlevel == -1)
121
    return (EINVAL);
122
 
123
  if (mutex->use)
124
    return (EBUSY);
125
 
126
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
127
 
128
  return m->destroy(mutex->mutexlevel, mutex);
129
}
130
 
131
int mutex_lock(mutex_t *mutex)
132
{
133
  mutex_resource_des *m;
134
 
135
  if (mutex->mutexlevel == -1)
136
    return (EINVAL);
137
 
138
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
139
 
140
  return m->lock(mutex->mutexlevel, mutex);
141
}
142
 
143
int mutex_trylock(mutex_t *mutex)
144
{
145
  mutex_resource_des *m;
146
 
147
  if (mutex->mutexlevel == -1)
148
    return (EINVAL);
149
 
150
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
151
 
152
  return m->trylock(mutex->mutexlevel, mutex);
153
}
154
 
155
int mutex_unlock(mutex_t *mutex)
156
{
157
  mutex_resource_des *m;
158
 
159
  if (mutex->mutexlevel == -1)
160
    return (EINVAL);
161
 
162
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
163
 
164
  return m->unlock(mutex->mutexlevel, mutex);
165
}
166