]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libthr/thread/thr_rtld.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libthr / thread / thr_rtld.c
1 /*
2  * Copyright (c) 2006, David Xu <davidxu@freebsd.org>
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 unmodified, this list of conditions, and the following
10  *    disclaimer.
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  *
28  */
29
30  /*
31   * A lockless rwlock for rtld.
32   */
33 #include <sys/cdefs.h>
34 #include <stdlib.h>
35
36 #include "rtld_lock.h"
37 #include "thr_private.h"
38
39 #undef errno
40 extern int errno;
41
42 static int      _thr_rtld_clr_flag(int);
43 static void     *_thr_rtld_lock_create(void);
44 static void     _thr_rtld_lock_destroy(void *);
45 static void     _thr_rtld_lock_release(void *);
46 static void     _thr_rtld_rlock_acquire(void *);
47 static int      _thr_rtld_set_flag(int);
48 static void     _thr_rtld_wlock_acquire(void *);
49
50 struct rtld_lock {
51         struct  urwlock lock;
52         char            _pad[CACHE_LINE_SIZE - sizeof(struct urwlock)];
53 };
54
55 static struct rtld_lock lock_place[MAX_RTLD_LOCKS] __aligned(CACHE_LINE_SIZE);
56 static int busy_places;
57
58 static void *
59 _thr_rtld_lock_create(void)
60 {
61         int locki;
62         struct rtld_lock *l;
63         static const char fail[] = "_thr_rtld_lock_create failed\n";
64
65         for (locki = 0; locki < MAX_RTLD_LOCKS; locki++) {
66                 if ((busy_places & (1 << locki)) == 0)
67                         break;
68         }
69         if (locki == MAX_RTLD_LOCKS) {
70                 write(2, fail, sizeof(fail) - 1);
71                 return (NULL);
72         }
73         busy_places |= (1 << locki);
74
75         l = &lock_place[locki];
76         l->lock.rw_flags = URWLOCK_PREFER_READER;
77         return (l);
78 }
79
80 static void
81 _thr_rtld_lock_destroy(void *lock)
82 {
83         int locki;
84
85         locki = (struct rtld_lock *)lock - &lock_place[0];
86         busy_places &= ~(1 << locki);
87 }
88
89 #define SAVE_ERRNO()    {                       \
90         if (curthread != _thr_initial)          \
91                 errsave = curthread->error;     \
92         else                                    \
93                 errsave = errno;                \
94 }
95
96 #define RESTORE_ERRNO() {                       \
97         if (curthread != _thr_initial)          \
98                 curthread->error = errsave;     \
99         else                                    \
100                 errno = errsave;                \
101 }
102
103 static void
104 _thr_rtld_rlock_acquire(void *lock)
105 {
106         struct pthread          *curthread;
107         struct rtld_lock        *l;
108         int                     errsave;
109
110         curthread = _get_curthread();
111         SAVE_ERRNO();
112         l = (struct rtld_lock *)lock;
113
114         THR_CRITICAL_ENTER(curthread);
115         while (_thr_rwlock_rdlock(&l->lock, 0, NULL) != 0)
116                 ;
117         curthread->rdlock_count++;
118         RESTORE_ERRNO();
119 }
120
121 static void
122 _thr_rtld_wlock_acquire(void *lock)
123 {
124         struct pthread          *curthread;
125         struct rtld_lock        *l;
126         int                     errsave;
127
128         curthread = _get_curthread();
129         SAVE_ERRNO();
130         l = (struct rtld_lock *)lock;
131
132         _thr_signal_block(curthread);
133         while (_thr_rwlock_wrlock(&l->lock, NULL) != 0)
134                 ;
135         RESTORE_ERRNO();
136 }
137
138 static void
139 _thr_rtld_lock_release(void *lock)
140 {
141         struct pthread          *curthread;
142         struct rtld_lock        *l;
143         int32_t                 state;
144         int                     errsave;
145
146         curthread = _get_curthread();
147         SAVE_ERRNO();
148         l = (struct rtld_lock *)lock;
149         
150         state = l->lock.rw_state;
151         if (_thr_rwlock_unlock(&l->lock) == 0) {
152                 curthread->rdlock_count--;
153                 if ((state & URWLOCK_WRITE_OWNER) == 0) {
154                         THR_CRITICAL_LEAVE(curthread);
155                 } else {
156                         _thr_signal_unblock(curthread);
157                 }
158         }
159         RESTORE_ERRNO();
160 }
161
162 static int
163 _thr_rtld_set_flag(int mask __unused)
164 {
165         /*
166          * The caller's code in rtld-elf is broken, it is not signal safe,
167          * just return zero to fool it.
168          */
169         return (0);
170 }
171
172 static int
173 _thr_rtld_clr_flag(int mask __unused)
174 {
175         return (0);
176 }
177
178 void
179 _thr_rtld_init(void)
180 {
181         struct RtldLockInfo     li;
182         struct pthread          *curthread;
183         long dummy = -1;
184
185         curthread = _get_curthread();
186
187         /* force to resolve _umtx_op PLT */
188         _umtx_op_err((struct umtx *)&dummy, UMTX_OP_WAKE, 1, 0, 0);
189         
190         /* force to resolve errno() PLT */
191         __error();
192
193         li.lock_create  = _thr_rtld_lock_create;
194         li.lock_destroy = _thr_rtld_lock_destroy;
195         li.rlock_acquire = _thr_rtld_rlock_acquire;
196         li.wlock_acquire = _thr_rtld_wlock_acquire;
197         li.lock_release  = _thr_rtld_lock_release;
198         li.thread_set_flag = _thr_rtld_set_flag;
199         li.thread_clr_flag = _thr_rtld_clr_flag;
200         li.at_fork = NULL;
201         
202         /* mask signals, also force to resolve __sys_sigprocmask PLT */
203         _thr_signal_block(curthread);
204         _rtld_thread_init(&li);
205         _thr_signal_unblock(curthread);
206 }
207
208 void
209 _thr_rtld_fini(void)
210 {
211         struct pthread  *curthread;
212
213         curthread = _get_curthread();
214         _thr_signal_block(curthread);
215         _rtld_thread_init(NULL);
216         _thr_signal_unblock(curthread);
217 }