Rev 228 | Rev 385 | 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 | ------------ |
||
318 | giacomo | 21 | CVS : $Id: mutex.c,v 1.4 2003-11-05 15:05:12 giacomo Exp $ |
2 | pj | 22 | |
23 | File: $File$ |
||
318 | giacomo | 24 | Revision: $Revision: 1.4 $ |
25 | Last update: $Date: 2003-11-05 15:05:12 $ |
||
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; |
||
318 | giacomo | 92 | SYS_FLAGS f; |
38 | pj | 93 | int result; |
2 | pj | 94 | |
318 | giacomo | 95 | f = kern_fsave(); |
2 | pj | 96 | mutex->mutexlevel = -1; |
97 | mutex->use = 0; |
||
98 | |||
99 | for (l=0; l<res_levels; l++) { |
||
100 | if (resource_table[l]->rtype == MUTEX_RTYPE) { |
||
101 | /* the cast to mutex_resource_des is legal */ |
||
102 | mutex_resource_des *m = (mutex_resource_des *)resource_table[l]; |
||
103 | |||
104 | /* can the mutex level manage the mutexattr_t ? */ |
||
228 | giacomo | 105 | if ((result = m->init(l, mutex, attr)) >=0) { |
318 | giacomo | 106 | kern_frestore(f); |
38 | pj | 107 | return result; |
228 | giacomo | 108 | } |
2 | pj | 109 | } |
110 | } |
||
318 | giacomo | 111 | kern_frestore(f); |
2 | pj | 112 | |
38 | pj | 113 | return EINVAL; |
2 | pj | 114 | } |
115 | |||
116 | |||
117 | int mutex_destroy(mutex_t *mutex) |
||
118 | { |
||
119 | mutex_resource_des *m; |
||
120 | |||
121 | if (mutex->mutexlevel == -1) |
||
122 | return (EINVAL); |
||
123 | |||
124 | if (mutex->use) |
||
125 | return (EBUSY); |
||
126 | |||
127 | m = (mutex_resource_des *)resource_table[mutex->mutexlevel]; |
||
128 | |||
129 | return m->destroy(mutex->mutexlevel, mutex); |
||
130 | } |
||
131 | |||
132 | int mutex_lock(mutex_t *mutex) |
||
133 | { |
||
134 | mutex_resource_des *m; |
||
135 | |||
136 | if (mutex->mutexlevel == -1) |
||
137 | return (EINVAL); |
||
138 | |||
139 | m = (mutex_resource_des *)resource_table[mutex->mutexlevel]; |
||
140 | |||
141 | return m->lock(mutex->mutexlevel, mutex); |
||
142 | } |
||
143 | |||
144 | int mutex_trylock(mutex_t *mutex) |
||
145 | { |
||
146 | mutex_resource_des *m; |
||
147 | |||
148 | if (mutex->mutexlevel == -1) |
||
149 | return (EINVAL); |
||
150 | |||
151 | m = (mutex_resource_des *)resource_table[mutex->mutexlevel]; |
||
152 | |||
153 | return m->trylock(mutex->mutexlevel, mutex); |
||
154 | } |
||
155 | |||
156 | int mutex_unlock(mutex_t *mutex) |
||
157 | { |
||
158 | mutex_resource_des *m; |
||
159 | |||
160 | if (mutex->mutexlevel == -1) |
||
161 | return (EINVAL); |
||
162 | |||
163 | m = (mutex_resource_des *)resource_table[mutex->mutexlevel]; |
||
164 | |||
165 | return m->unlock(mutex->mutexlevel, mutex); |
||
166 | } |
||
167 |