Subversion Repositories shark

Rev

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