]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkse/thread/thr_rtld.c
This commit was generated by cvs2svn to compensate for changes in r145557,
[FreeBSD/FreeBSD.git] / lib / libkse / thread / thr_rtld.c
1 /*
2  * Copyright (c) 2001 Alexander Kabaev
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 #include <sys/cdefs.h>
31 #include <stdlib.h>
32
33 #include "rtld_lock.h"
34 #include "thr_private.h"
35
36 static int      _thr_rtld_clr_flag(int);
37 static void     *_thr_rtld_lock_create(void);
38 static void     _thr_rtld_lock_destroy(void *);
39 static void     _thr_rtld_lock_release(void *);
40 static void     _thr_rtld_rlock_acquire(void *);
41 static int      _thr_rtld_set_flag(int);
42 static void     _thr_rtld_wlock_acquire(void *);
43
44 #ifdef NOTYET
45 static void *
46 _thr_rtld_lock_create(void)
47 {
48         pthread_rwlock_t prwlock;
49         if (_pthread_rwlock_init(&prwlock, NULL))
50                 return (NULL);
51         return (prwlock);
52 }
53
54 static void
55 _thr_rtld_lock_destroy(void *lock)
56 {
57         pthread_rwlock_t prwlock;
58
59         prwlock = (pthread_rwlock_t)lock;
60         if (prwlock != NULL)
61                 _pthread_rwlock_destroy(&prwlock);
62 }
63
64 static void
65 _thr_rtld_rlock_acquire(void *lock)
66 {
67         pthread_rwlock_t prwlock;
68
69         prwlock = (pthread_rwlock_t)lock;
70         _thr_rwlock_rdlock(&prwlock);
71 }
72
73 static void
74 _thr_rtld_wlock_acquire(void *lock)
75 {
76         pthread_rwlock_t prwlock;
77
78         prwlock = (pthread_rwlock_t)lock;
79         _thr_rwlock_wrlock(&prwlock);
80 }
81
82 static void
83 _thr_rtld_lock_release(void *lock)
84 {
85         pthread_rwlock_t prwlock;
86
87         prwlock = (pthread_rwlock_t)lock;
88         _thr_rwlock_unlock(&prwlock);
89 }
90
91
92 static int
93 _thr_rtld_set_flag(int mask)
94 {
95         struct pthread *curthread;
96         int bits;
97
98         curthread = _get_curthread();
99         if (curthread != NULL) {
100                 bits = curthread->rtld_bits;
101                 curthread->rtld_bits |= mask;
102         } else {
103                 bits = 0;
104                 PANIC("No current thread in rtld call");
105         }
106
107         return (bits);
108 }
109
110 static int
111 _thr_rtld_clr_flag(int mask)
112 {
113         struct pthread *curthread;
114         int bits;
115
116         curthread = _get_curthread();
117         if (curthread != NULL) {
118                 bits = curthread->rtld_bits;
119                 curthread->rtld_bits &= ~mask;
120         } else {
121                 bits = 0;
122                 PANIC("No current thread in rtld call");
123         }
124         return (bits);
125 }
126
127 void
128 _thr_rtld_init(void)
129 {
130         struct RtldLockInfo li;
131
132         li.lock_create  = _thr_rtld_lock_create;
133         li.lock_destroy = _thr_rtld_lock_destroy;
134         li.rlock_acquire = _thr_rtld_rlock_acquire;
135         li.wlock_acquire = _thr_rtld_wlock_acquire;
136         li.lock_release  = _thr_rtld_lock_release;
137         li.thread_set_flag = _thr_rtld_set_flag;
138         li.thread_clr_flag = _thr_rtld_clr_flag;
139         li.at_fork = NULL;
140         _rtld_thread_init(&li);
141 }
142
143 void
144 _thr_rtld_fini(void)
145 {
146         _rtld_thread_init(NULL);
147 }
148 #endif
149
150 struct rtld_kse_lock {
151         struct lock     lck;
152         struct kse      *owner;
153         kse_critical_t  crit;
154         int             count;
155         int             write;
156 };
157
158 static void *
159 _thr_rtld_lock_create(void)
160 {
161         struct rtld_kse_lock *l;
162
163         l = malloc(sizeof(struct rtld_kse_lock));
164         _lock_init(&l->lck, LCK_ADAPTIVE, _kse_lock_wait, _kse_lock_wakeup);
165         l->owner = NULL;
166         l->count = 0;
167         l->write = 0;
168         return (l);
169 }
170
171 static void
172 _thr_rtld_lock_destroy(void *lock)
173 {
174         /* XXX We really can not free memory after a fork() */
175 #if 0
176         struct rtld_kse_lock *l;
177
178         l = (struct rtld_kse_lock *)lock;
179         _lock_destroy(&l->lck);
180         free(l);
181 #endif
182         return;
183 }
184
185 static void
186 _thr_rtld_rlock_acquire(void *lock)
187 {
188         struct rtld_kse_lock *l;
189         kse_critical_t crit;
190         struct kse *curkse;
191
192         l  = (struct rtld_kse_lock *)lock;
193         crit = _kse_critical_enter();
194         curkse = _get_curkse();
195         if (l->owner == curkse) {
196                 l->count++;
197                 _kse_critical_leave(crit);      /* probably not necessary */
198         } else {
199                 KSE_LOCK_ACQUIRE(curkse, &l->lck);
200                 l->crit = crit;
201                 l->owner = curkse;
202                 l->count = 1;
203                 l->write = 0;
204         }
205 }
206
207 static void
208 _thr_rtld_wlock_acquire(void *lock)
209 {
210         struct rtld_kse_lock *l;
211         kse_critical_t crit;
212         struct kse *curkse;
213
214         l = (struct rtld_kse_lock *)lock;
215         crit = _kse_critical_enter();
216         curkse = _get_curkse();
217         if (l->owner == curkse) {
218                 _kse_critical_leave(crit);
219                 PANIC("Recursive write lock attempt on rtld lock");
220         } else {
221                 KSE_LOCK_ACQUIRE(curkse, &l->lck);
222                 l->crit = crit;
223                 l->owner = curkse;
224                 l->count = 1;
225                 l->write = 1;
226         }
227 }
228
229 static void
230 _thr_rtld_lock_release(void *lock)
231 {
232         struct rtld_kse_lock *l;
233         kse_critical_t crit;
234         struct kse *curkse;
235
236         l = (struct rtld_kse_lock *)lock;
237         crit = _kse_critical_enter();
238         curkse = _get_curkse();
239         if (l->owner != curkse) {
240                 /*
241                  * We might want to forcibly unlock the rtld lock
242                  * and/or disable threaded mode so there is better
243                  * chance that the panic will work.  Otherwise,
244                  * we could end up trying to take the rtld lock
245                  * again.
246                  */
247                 _kse_critical_leave(crit);
248                 PANIC("Attempt to unlock rtld lock when not owner.");
249         } else {
250                 l->count--;
251                 if (l->count == 0) {
252                         /*
253                          * If there ever is a count associated with
254                          * _kse_critical_leave(), we'll need to add
255                          * another call to it here with the crit
256                          * value from above.
257                          */
258                         crit  = l->crit;
259                         l->owner = NULL;
260                         l->write = 0;
261                         KSE_LOCK_RELEASE(curkse, &l->lck);
262                 }
263                 _kse_critical_leave(crit);
264         }
265 }
266
267
268 static int
269 _thr_rtld_set_flag(int mask)
270 {
271         return (0);
272 }
273
274 static int
275 _thr_rtld_clr_flag(int mask)
276 {
277         return (0);
278 }
279
280 void
281 _thr_rtld_init(void)
282 {
283         struct RtldLockInfo li;
284
285         li.lock_create  = _thr_rtld_lock_create;
286         li.lock_destroy = _thr_rtld_lock_destroy;
287         li.rlock_acquire = _thr_rtld_rlock_acquire;
288         li.wlock_acquire = _thr_rtld_wlock_acquire;
289         li.lock_release  = _thr_rtld_lock_release;
290         li.thread_set_flag = _thr_rtld_set_flag;
291         li.thread_clr_flag = _thr_rtld_clr_flag;
292         li.at_fork = NULL;
293         _rtld_thread_init(&li);
294 }
295
296 void
297 _thr_rtld_fini(void)
298 {
299         _rtld_thread_init(NULL);
300 }