1 /*
2  * Copyright (c) 2011 Aeroflex Gaisler
3  *
4  * BSD license:
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 
26 #include <sys/types.h>
27 #include <sys/lock.h>
28 #include <asm-leon/queue.h>
29 /*#include <sys/fsu_pthread_mutex.h>*/
30 
31 typedef int pthread_protocol_t;
32 typedef
33 TAILQ_HEAD (pthread_queue, pthread) *
34   pthread_queue_t;
35 
36 #define pthread_mutex_t_defined
37      typedef struct pthread_mutex
38      {
39        struct pthread_queue queue;
40        char lock;
41        struct pthread *owner;
42        int flags;
43        int count;
44        int prioceiling;
45        pthread_protocol_t protocol;
46        int prev_max_ceiling_prio;
47          TAILQ_ENTRY (pthread_mutex) dbglist;
48        char *dbgname;
49        int _fitothers[16];
50      } pthread_mutex_t;
51 
52      typedef struct
53      {
54        int flags;
55        int prioceiling;
56        pthread_protocol_t protocol;
57      } pthread_mutexattr_t;
58 
59 
60      int (*__lbst_pthread_mutex_init) (pthread_mutex_t * __mutex,
61 				       pthread_mutexattr_t * __mutex_attr) =
62   0;
63      int (*__lbst_pthread_mutex_destroy) (pthread_mutex_t * __mutex) = 0;
64      int (*__lbst_pthread_mutex_trylock) (pthread_mutex_t * __mutex) = 0;
65      int (*__lbst_pthread_mutex_lock) (pthread_mutex_t * __mutex) = 0;
66      int (*__lbst_pthread_mutex_unlock) (pthread_mutex_t * __mutex) = 0;
67      int (*__lbst_pthread_mutexattr_init) (pthread_mutexattr_t * __attr) = 0;
68      int (*__lbst_pthread_mutexattr_destroy) (pthread_mutexattr_t * __attr) =
69   0;
70      int (*__lbst_pthread_mutexattr_settype) (pthread_mutexattr_t * __attr,
71 					      int __kind) = 0;
72 
___st_pthread_mutex_init(mutex,attr)73      int ___st_pthread_mutex_init (mutex, attr)
74      pthread_mutex_t *mutex;
75      pthread_mutexattr_t *attr;
76 {
77   if (__lbst_pthread_mutex_init)
78     {
79       return __lbst_pthread_mutex_init (mutex, attr);
80     }
81   return 0;
82 }
83 
84 int
___st_pthread_mutex_destroy(mutex)85 ___st_pthread_mutex_destroy (mutex)
86      pthread_mutex_t *mutex;
87 {
88   if (__lbst_pthread_mutex_destroy)
89     {
90       return __lbst_pthread_mutex_destroy (mutex);
91     }
92   return 0;
93 }
94 
95 int
___st_pthread_mutex_lock(mutex)96 ___st_pthread_mutex_lock (mutex)
97      pthread_mutex_t *mutex;
98 {
99   if (__lbst_pthread_mutex_lock)
100     {
101       return __lbst_pthread_mutex_lock (mutex);
102     }
103   return 0;
104 }
105 
106 int
___st_pthread_mutex_trylock(mutex)107 ___st_pthread_mutex_trylock (mutex)
108      pthread_mutex_t *mutex;
109 {
110   if (__lbst_pthread_mutex_trylock)
111     {
112       return __lbst_pthread_mutex_trylock (mutex);
113     }
114   return 0;
115 }
116 
117 int
___st_pthread_mutex_unlock(mutex)118 ___st_pthread_mutex_unlock (mutex)
119      pthread_mutex_t *mutex;
120 {
121   if (__lbst_pthread_mutex_unlock)
122     {
123       return __lbst_pthread_mutex_unlock (mutex);
124     }
125   return 0;
126 }
127 
128 int
___st_pthread_mutexattr_init(attr)129 ___st_pthread_mutexattr_init (attr)
130      pthread_mutexattr_t *attr;
131 {
132   if (__lbst_pthread_mutexattr_init)
133     {
134       return __lbst_pthread_mutexattr_init (attr);
135     }
136   return (0);
137 }
138 
139 int
___st_pthread_mutexattr_destroy(attr)140 ___st_pthread_mutexattr_destroy (attr)
141      pthread_mutexattr_t *attr;
142 {
143   if (__lbst_pthread_mutexattr_destroy)
144     {
145       return __lbst_pthread_mutexattr_destroy (attr);
146     }
147   return 0;
148 }
149 
150 int
___st_pthread_mutexattr_settype(attr,kind)151 ___st_pthread_mutexattr_settype (attr, kind)
152      pthread_mutexattr_t *attr;
153      int kind;
154 {
155   if (__lbst_pthread_mutexattr_settype)
156     {
157       return __lbst_pthread_mutexattr_settype (attr, kind);
158     }
159   return 0;
160 }
161 
162 #include <asm-leon/elfmacro.h>
163 
164 weak_alias (___st_pthread_mutex_init, __st_pthread_mutex_init)
165 weak_alias (___st_pthread_mutex_destroy, __st_pthread_mutex_destroy)
166 weak_alias (___st_pthread_mutex_trylock, __st_pthread_mutex_trylock)
167 weak_alias (___st_pthread_mutex_lock, __st_pthread_mutex_lock)
168 weak_alias (___st_pthread_mutex_unlock, __st_pthread_mutex_unlock)
169 weak_alias (___st_pthread_mutexattr_init, __st_pthread_mutexattr_init)
170 weak_alias (___st_pthread_mutexattr_destroy, __st_pthread_mutexattr_destroy)
171 weak_alias (___st_pthread_mutexattr_settype, __st_pthread_mutexattr_settype)
172 /* /\* #ifndef weak_extern *\/ */
173 /* /\* #define weak_extern(symbol) _weak_extern (symbol) *\/ */
174 /* /\* #define _weak_extern(symbol) asm (".weak " #symbol); *\/ */
175 /* /\* #endif *\/ */
176 /* /\* weak_extern (__pthread_mutex_init) *\/ */
177 /* /\* weak_extern (__pthread_mutex_destroy) *\/ */
178 /* /\* weak_extern (__pthread_mutex_lock) *\/ */
179 /* /\* weak_extern (__pthread_mutex_trylock) *\/ */
180 /* /\* weak_extern (__pthread_mutex_unlock) *\/ */
181 /* /\* weak_extern (__pthread_mutexattr_init) *\/ */
182 /* /\* weak_extern (__pthread_mutexattr_destroy) *\/ */
183 /* /\* weak_extern (__pthread_mutexattr_settype) *\/ */
184 /* /\* weak_extern (__pthread_once) *\/ */
185 /* /\* weak_extern (__pthread_initialize) *\/ */
186 /* /\* Initialize the named lock variable, leaving it in a consistent, unlocked */
187 /*    state.  *\/ */
188 /* #define __libc_lock_init(NAME) \ */
189 /*   (__pthread_mutex_init != NULL ? __pthread_mutex_init (&(NAME), NULL) : 0); */
190 /* /\* Same as last but this time we initialize a recursive mutex.  *\/ */
191 /* #define __libc_lock_init_recursive(NAME) \ */
192 /*   do {									      \ */
193 /*     if (__pthread_mutex_init != NULL)					      \ */
194 /*       {									      \ */
195 /* 	pthread_mutexattr_t __attr;					      \ */
196 /* 	__pthread_mutexattr_init (&__attr);				      \ */
197 /* 	__pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \ */
198 /* 	__pthread_mutex_init (&(NAME), &__attr);			      \ */
199 /* 	__pthread_mutexattr_destroy (&__attr);				      \ */
200 /*       }									      \ */
201 /*   } while (0); */
202 /* /\* Finalize the named lock variable, which must be locked.  It cannot be */
203 /*    used again until __libc_lock_init is called again on it.  This must be */
204 /*    called on a lock variable before the containing storage is reused.  *\/ */
205 /* //#define __libc_lock_fini(NAME)              (__pthread_mutex_destroy != NULL ? __pthread_mutex_destroy (&(NAME)) : 0) */
206 /* #define __libc_lock_fini(NAME)              (__st_pthread_mutex_destroy (&(NAME))) */
207 /* /\* Finalize recursive named lock.  *\/ */
208 /* #define __libc_lock_fini_recursive(NAME)     __libc_lock_fini (NAME) */
209 /* /\* Lock the named lock variable.  *\/ */
210 /* //#define __libc_lock_lock(NAME)              (__pthread_mutex_lock != NULL ? __pthread_mutex_lock (&(NAME)) : 0) */
211 /* #define __libc_lock_lock(NAME)              (__st_pthread_mutex_lock (&(NAME))) */
212 /* /\* Lock the recursive named lock variable.  *\/ */
213 /* #define __libc_lock_lock_recursive(NAME)     __libc_lock_lock (NAME) */
214 /* /\* Try to lock the named lock variable.  *\/ */
215 /* //#define __libc_lock_trylock(NAME)           (__pthread_mutex_trylock != NULL ? __pthread_mutex_trylock (&(NAME)) : 0) */
216 /* #define __libc_lock_trylock(NAME)           (__st_pthread_mutex_trylock (&(NAME))) */
217 /* /\* Try to lock the recursive named lock variable.  *\/ */
218 /* #define __libc_lock_trylock_recursive(NAME)  __libc_lock_trylock (NAME) */
219 /* /\* Unlock the named lock variable.  *\/ */
220 /* //#define __libc_lock_unlock(NAME)            (__pthread_mutex_unlock != NULL ? __pthread_mutex_unlock (&(NAME)) : 0) */
221 /* #define __libc_lock_unlock(NAME)            (__st_pthread_mutex_unlock (&(NAME))) */
222 /* /\* Unlock the recursive named lock variable.  *\/ */
223 /* #define __libc_lock_unlock_recursive(NAME)   __libc_lock_unlock (NAME) */
224 /* extern int __st_pthread_mutex_init        (pthread_mutex_t *__mutex, pthread_mutexattr_t *__mutex_attr); */
225 /* extern int __st_pthread_mutex_destroy     (pthread_mutex_t *__mutex); */
226 /* extern int __st_pthread_mutex_trylock     (pthread_mutex_t *__mutex); */
227 /* extern int __st_pthread_mutex_lock        (pthread_mutex_t *__mutex); */
228 /* extern int __st_pthread_mutex_unlock      (pthread_mutex_t *__mutex); */
229 /* extern int __st_pthread_mutexattr_init    (pthread_mutexattr_t *__attr); */
230 /* extern int __st_pthread_mutexattr_destroy (pthread_mutexattr_t *__attr); */
231 /* extern int __st_pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind); */
232 /* /\* /\\* Functions that are used by this file and are internal to the GNU C library.  *\\/ *\/ */
233 /* /\* extern int __pthread_mutex_init        (pthread_mutex_t *__mutex, pthread_mutexattr_t *__mutex_attr); *\/ */
234 /* /\* extern int __pthread_mutex_destroy     (pthread_mutex_t *__mutex); *\/ */
235 /* /\* extern int __pthread_mutex_trylock     (pthread_mutex_t *__mutex); *\/ */
236 /* /\* extern int __pthread_mutex_lock        (pthread_mutex_t *__mutex); *\/ */
237 /* /\* extern int __pthread_mutex_unlock      (pthread_mutex_t *__mutex); *\/ */
238 /* /\* extern int __pthread_mutexattr_init    (pthread_mutexattr_t *__attr); *\/ */
239 /* /\* extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr); *\/ */
240 /* /\* extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind); *\/ */
241 /* /\* /\\* Make the pthread functions weak so that we can elide them from *\/ */
242 /* /\*    single-threaded processes.  *\\/ *\/ */
243 /* /\* #ifndef weak_extern *\/ */
244 /* /\* #define weak_extern(symbol) _weak_extern (symbol) *\/ */
245 /* /\* #define _weak_extern(symbol) asm (".weak " #symbol); *\/ */
246 /* /\* #endif *\/ */
247 /* /\* weak_extern (__pthread_mutex_init) *\/ */
248 /* /\* weak_extern (__pthread_mutex_destroy) *\/ */
249 /* /\* weak_extern (__pthread_mutex_lock) *\/ */
250 /* /\* weak_extern (__pthread_mutex_trylock) *\/ */
251 /* /\* weak_extern (__pthread_mutex_unlock) *\/ */
252 /* /\* weak_extern (__pthread_mutexattr_init) *\/ */
253 /* /\* weak_extern (__pthread_mutexattr_destroy) *\/ */
254 /* /\* weak_extern (__pthread_mutexattr_settype) *\/ */
255 /* /\* weak_extern (__pthread_once) *\/ */
256 /* /\* weak_extern (__pthread_initialize) *\/ */
257