Rev 3 | Go to most recent revision | 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 | * Massimiliano Giorgi <massy@gandalf.sssup.it> |
||
11 | * Luca Abeni <luca@gandalf.sssup.it> |
||
12 | * (see the web pages for full authors list) |
||
13 | * |
||
14 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
15 | * |
||
16 | * http://www.sssup.it |
||
17 | * http://retis.sssup.it |
||
18 | * http://shark.sssup.it |
||
19 | */ |
||
20 | |||
21 | /*************************************** |
||
22 | |||
23 | Mutexs for the kernel |
||
24 | |||
25 | ***************************************/ |
||
26 | |||
27 | /* |
||
28 | * CVS : $Id: mutex.h,v 1.1.1.1 2002-03-29 14:12:51 pj Exp $ |
||
29 | * |
||
30 | * File: $File$ |
||
31 | * Revision: $Revision: 1.1.1.1 $ |
||
32 | * Last update: $Date: 2002-03-29 14:12:51 $ |
||
33 | */ |
||
34 | |||
35 | /* |
||
36 | * Copyright (C) 1999,2000 Massimiliano Giorgi |
||
37 | * |
||
38 | * This program is free software; you can redistribute it and/or modify |
||
39 | * it under the terms of the GNU General Public License as published by |
||
40 | * the Free Software Foundation; either version 2 of the License, or |
||
41 | * (at your option) any later version. |
||
42 | * |
||
43 | * This program is distributed in the hope that it will be useful, |
||
44 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
45 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
46 | * GNU General Public License for more details. |
||
47 | * |
||
48 | * You should have received a copy of the GNU General Public License |
||
49 | * along with this program; if not, write to the Free Software |
||
50 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
51 | * |
||
52 | */ |
||
53 | |||
54 | #ifndef _FS_MUTEX_H |
||
55 | #define _FS_MUTEX_H |
||
56 | |||
57 | /* |
||
58 | * standard mutex |
||
59 | */ |
||
60 | |||
61 | #include <kernel/int_sem.h> |
||
62 | |||
63 | /*+ a semaphore object +*/ |
||
64 | typedef internal_sem_t __mutex_t; |
||
65 | |||
66 | /* |
||
67 | The following macros can be used to synchronize events; all |
||
68 | require a pointer to a semaphore object and return nothing; |
||
69 | a semaphore can have "val" resource free. |
||
70 | */ |
||
71 | |||
72 | /*+ Initialize a semaphore (to integer val) +*/ |
||
73 | #define __mutex_init(ptr,attr) internal_sem_init((ptr),1) |
||
74 | |||
75 | /*+ Wait for a semaphore +*/ |
||
76 | #define __mutex_lock(ptr) (internal_sem_wait(ptr)) |
||
77 | |||
78 | /*+ Try to wait for a semaphore (return 0 on success) +*/ |
||
79 | #define __mutex_trylock(ptr) (internal_sem_wait(ptr)) |
||
80 | |||
81 | /*+ Signal a semaphore +*/ |
||
82 | #define __mutex_unlock(ptr) (internal_sem_post(ptr)) |
||
83 | |||
84 | #if 0 |
||
85 | /* start old implementation */ |
||
86 | |||
87 | #include <kernel/model.h> |
||
88 | #include <kernel/func.h> |
||
89 | #include <fs/assert.h> |
||
90 | |||
91 | /*+ a mutex object +*/ |
||
92 | typedef mutex_t __mutex_t; |
||
93 | |||
94 | /* |
||
95 | The following macros require a pointer to a mutex object and |
||
96 | return nothing. |
||
97 | */ |
||
98 | |||
99 | /* Initialize a mutex object +*/ |
||
100 | #define __mutex_init(ptr,attr) _assert(mutex_init(ptr,attr)==0) |
||
101 | /*+ Lock a mutex object +*/ |
||
102 | #define __mutex_lock(ptr) _assert(mutex_lock(ptr)==0) |
||
103 | /*+ Try to lock a mutex (return 0 on success locking) +*/ |
||
104 | #define __mutex_trylock(ptr) (mutex_trylock((ptr))) |
||
105 | /*+ Unlock a mutex +*/ |
||
106 | #define __mutex_unlock(ptr) _assert(mutex_unlock(ptr)==0) |
||
107 | |||
108 | /* end old implementation */ |
||
109 | #endif |
||
110 | |||
111 | /* |
||
112 | * fast mutex |
||
113 | */ |
||
114 | |||
115 | #include <kernel/func.h> |
||
116 | |||
117 | typedef SYS_FLAGS __fastmutex_t; |
||
118 | |||
119 | #define __fastmutex_init(ptr) |
||
120 | #define __fastmutex_lock(ptr) *ptr=kern_fsave() |
||
121 | #define __fastmutex_unlock(ptr) kern_frestore(*ptr) |
||
122 | |||
123 | #if 0 |
||
124 | |||
125 | /* Hartik 3.x implemantation */ |
||
126 | |||
127 | /* |
||
128 | #include <hartik/const.h> |
||
129 | |||
130 | typedef struct { |
||
131 | QUEUE queue; |
||
132 | int value; |
||
133 | } __mutex_t; |
||
134 | |||
135 | void __mutex_init(__mytex_t *); |
||
136 | void __mutex_lock(__mytex_t *); |
||
137 | int __mutex_trylock(__mytex_t *); |
||
138 | void __mutex_unlock(__mytex_t *); |
||
139 | */ |
||
140 | |||
141 | /* |
||
142 | * temporary hack |
||
143 | */ |
||
144 | |||
145 | #include <h/sys/kern.h> |
||
146 | #include <h/sys/const.h> |
||
147 | |||
148 | /*+ a mutex object +*/ |
||
149 | typedef SEM __mutex_t; |
||
150 | |||
151 | /* |
||
152 | The following macros require a pointer to a mutex object and |
||
153 | return nothing. |
||
154 | */ |
||
155 | |||
156 | #ifdef FSCHECKMUTEX |
||
157 | #include <fs/assert.h> |
||
158 | #define checkmutex(x) assert((x)>=0&&(x)<MAX_SEM) |
||
159 | #else |
||
160 | #define checkmutex(x) |
||
161 | #endif |
||
162 | |||
163 | /* Initialize a mutex object +*/ |
||
164 | #define __mutex_init(ptr) { *(ptr)=sem_create(1); checkmutex(*ptr); } |
||
165 | |||
166 | /*+ Lock a mutex object +*/ |
||
167 | #define __mutex_lock(ptr) { checkmutex(*ptr); sem_wait(*(ptr),BLOCK); } |
||
168 | |||
169 | /*+ Try to lock a mutex (return 0 on success locking) +*/ |
||
170 | #define __mutex_trylock(ptr) { checkmutex(*ptr); sem_wait(*(ptr),NON_BLOCK); } |
||
171 | |||
172 | /*+ Unlock a mutex +*/ |
||
173 | #define __mutex_unlock(ptr) { checkmutex(*ptr); sem_signal(*(ptr)); } |
||
174 | |||
175 | #endif |
||
176 | |||
177 | |||
178 | |||
179 | |||
180 | |||
181 | |||
182 | #endif |
||
183 |