Subversion Repositories shark

Rev

Rev 385 | 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
 ------------
502 giacomo 21
 CVS :        $Id: mutex.c,v 1.6 2004-03-10 14:51:42 giacomo Exp $
2 pj 22
 
23
 File:        $File$
502 giacomo 24
 Revision:    $Revision: 1.6 $
25
 Last update: $Date: 2004-03-10 14:51:42 $
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
 
385 giacomo 84
#include <tracer.h>
85
 
2 pj 86
/*---------------------------------------------------------------------*/
87
/* Mutexes                                                             */
88
/*---------------------------------------------------------------------*/
89
 
90
 
91
int mutex_init(mutex_t *mutex, const mutexattr_t *attr)
92
{
93
  RLEVEL l;
318 giacomo 94
  SYS_FLAGS f;
38 pj 95
  int result;
2 pj 96
 
318 giacomo 97
  f = kern_fsave();
2 pj 98
  mutex->mutexlevel = -1;
99
  mutex->use = 0;
100
 
101
  for (l=0; l<res_levels; l++) {
102
     if (resource_table[l]->rtype == MUTEX_RTYPE) {
103
       /* the cast to mutex_resource_des is legal */
104
       mutex_resource_des *m = (mutex_resource_des *)resource_table[l];
105
 
106
       /* can the mutex level manage the mutexattr_t ? */
228 giacomo 107
       if ((result = m->init(l, mutex, attr)) >=0) {
318 giacomo 108
         kern_frestore(f);
38 pj 109
         return result;
228 giacomo 110
       }
2 pj 111
     }
112
  }
318 giacomo 113
  kern_frestore(f);
2 pj 114
 
38 pj 115
  return EINVAL;
2 pj 116
}
117
 
118
 
119
int mutex_destroy(mutex_t *mutex)
120
{
121
  mutex_resource_des *m;
122
 
123
  if (mutex->mutexlevel == -1)
124
    return (EINVAL);
125
 
126
  if (mutex->use)
127
    return (EBUSY);
128
 
129
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
130
 
131
  return m->destroy(mutex->mutexlevel, mutex);
132
}
133
 
134
int mutex_lock(mutex_t *mutex)
135
{
136
  mutex_resource_des *m;
137
 
138
  if (mutex->mutexlevel == -1)
139
    return (EINVAL);
140
 
502 giacomo 141
  TRACER_LOGEVENT(FTrace_EVT_set_mutex_lock,(unsigned short int)proc_table[exec_shadow].context,(unsigned int)(mutex));
385 giacomo 142
 
2 pj 143
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
144
 
145
  return m->lock(mutex->mutexlevel, mutex);
146
}
147
 
148
int mutex_trylock(mutex_t *mutex)
149
{
150
  mutex_resource_des *m;
151
 
152
  if (mutex->mutexlevel == -1)
153
    return (EINVAL);
154
 
155
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
156
 
157
  return m->trylock(mutex->mutexlevel, mutex);
158
}
159
 
160
int mutex_unlock(mutex_t *mutex)
161
{
162
  mutex_resource_des *m;
163
 
164
  if (mutex->mutexlevel == -1)
165
    return (EINVAL);
166
 
502 giacomo 167
  TRACER_LOGEVENT(FTrace_EVT_set_mutex_unlock,(unsigned short int)proc_table[exec_shadow].context,(unsigned int)(mutex));
385 giacomo 168
 
2 pj 169
  m = (mutex_resource_des *)resource_table[mutex->mutexlevel];
170
 
171
  return m->unlock(mutex->mutexlevel, mutex);
172
}
173