]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/lib/libthr/thread/thr_spec.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / lib / libthr / thread / thr_spec.c
1 /*
2  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
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  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by John Birrell.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <pthread.h>
40
41 #include "thr_private.h"
42
43 /* Static variables: */
44 struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
45
46 __weak_reference(_pthread_key_create, pthread_key_create);
47 __weak_reference(_pthread_key_delete, pthread_key_delete);
48 __weak_reference(_pthread_getspecific, pthread_getspecific);
49 __weak_reference(_pthread_setspecific, pthread_setspecific);
50
51
52 int
53 _pthread_key_create(pthread_key_t *key, void (*destructor) (void *))
54 {
55         struct pthread *curthread;
56         int i;
57
58         _thr_check_init();
59
60         curthread = _get_curthread();
61
62         /* Lock the key table: */
63         THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
64         for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
65
66                 if (_thread_keytable[i].allocated == 0) {
67                         _thread_keytable[i].allocated = 1;
68                         _thread_keytable[i].destructor = destructor;
69                         _thread_keytable[i].seqno++;
70
71                         /* Unlock the key table: */
72                         THR_LOCK_RELEASE(curthread, &_keytable_lock);
73                         *key = i;
74                         return (0);
75                 }
76
77         }
78         /* Unlock the key table: */
79         THR_LOCK_RELEASE(curthread, &_keytable_lock);
80         return (EAGAIN);
81 }
82
83 int
84 _pthread_key_delete(pthread_key_t key)
85 {
86         struct pthread *curthread = _get_curthread();
87         int ret = 0;
88
89         if ((unsigned int)key < PTHREAD_KEYS_MAX) {
90                 /* Lock the key table: */
91                 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
92
93                 if (_thread_keytable[key].allocated)
94                         _thread_keytable[key].allocated = 0;
95                 else
96                         ret = EINVAL;
97
98                 /* Unlock the key table: */
99                 THR_LOCK_RELEASE(curthread, &_keytable_lock);
100         } else
101                 ret = EINVAL;
102         return (ret);
103 }
104
105 void 
106 _thread_cleanupspecific(void)
107 {
108         struct pthread  *curthread = _get_curthread();
109         void            (*destructor)( void *);
110         void            *data = NULL;
111         int             key;
112         int             i;
113
114         if (curthread->specific == NULL)
115                 return;
116
117         /* Lock the key table: */
118         THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
119         for (i = 0; (i < PTHREAD_DESTRUCTOR_ITERATIONS) &&
120             (curthread->specific_data_count > 0); i++) {
121                 for (key = 0; (key < PTHREAD_KEYS_MAX) &&
122                     (curthread->specific_data_count > 0); key++) {
123                         destructor = NULL;
124
125                         if (_thread_keytable[key].allocated &&
126                             (curthread->specific[key].data != NULL)) {
127                                 if (curthread->specific[key].seqno ==
128                                     _thread_keytable[key].seqno) {
129                                         data = (void *)
130                                             curthread->specific[key].data;
131                                         destructor = _thread_keytable[key].destructor;
132                                 }
133                                 curthread->specific[key].data = NULL;
134                                 curthread->specific_data_count--;
135                         }
136
137                         /*
138                          * If there is a destructore, call it
139                          * with the key table entry unlocked:
140                          */
141                         if (destructor != NULL) {
142                                 /*
143                                  * Don't hold the lock while calling the
144                                  * destructor:
145                                  */
146                                 THR_LOCK_RELEASE(curthread, &_keytable_lock);
147                                 destructor(data);
148                                 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
149                         }
150                 }
151         }
152         THR_LOCK_RELEASE(curthread, &_keytable_lock);
153         free(curthread->specific);
154         curthread->specific = NULL;
155         if (curthread->specific_data_count > 0)
156                 stderr_debug("Thread %p has exited with leftover "
157                     "thread-specific data after %d destructor iterations\n",
158                     curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
159 }
160
161 static inline struct pthread_specific_elem *
162 pthread_key_allocate_data(void)
163 {
164         struct pthread_specific_elem *new_data;
165
166         new_data = (struct pthread_specific_elem *)
167             malloc(sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
168         if (new_data != NULL) {
169                 memset((void *) new_data, 0,
170                     sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
171         }
172         return (new_data);
173 }
174
175 int 
176 _pthread_setspecific(pthread_key_t key, const void *value)
177 {
178         struct pthread  *pthread;
179         int             ret = 0;
180
181         /* Point to the running thread: */
182         pthread = _get_curthread();
183
184         if ((pthread->specific) ||
185             (pthread->specific = pthread_key_allocate_data())) {
186                 if ((unsigned int)key < PTHREAD_KEYS_MAX) {
187                         if (_thread_keytable[key].allocated) {
188                                 if (pthread->specific[key].data == NULL) {
189                                         if (value != NULL)
190                                                 pthread->specific_data_count++;
191                                 } else if (value == NULL)
192                                         pthread->specific_data_count--;
193                                 pthread->specific[key].data = value;
194                                 pthread->specific[key].seqno =
195                                     _thread_keytable[key].seqno;
196                                 ret = 0;
197                         } else
198                                 ret = EINVAL;
199                 } else
200                         ret = EINVAL;
201         } else
202                 ret = ENOMEM;
203         return (ret);
204 }
205
206 void *
207 _pthread_getspecific(pthread_key_t key)
208 {
209         struct pthread  *pthread;
210         void            *data;
211
212         /* Point to the running thread: */
213         pthread = _get_curthread();
214
215         /* Check if there is specific data: */
216         if (pthread->specific != NULL && (unsigned int)key < PTHREAD_KEYS_MAX) {
217                 /* Check if this key has been used before: */
218                 if (_thread_keytable[key].allocated &&
219                     (pthread->specific[key].seqno == _thread_keytable[key].seqno)) {
220                         /* Return the value: */
221                         data = (void *) pthread->specific[key].data;
222                 } else {
223                         /*
224                          * This key has not been used before, so return NULL
225                          * instead: 
226                          */
227                         data = NULL;
228                 }
229         } else
230                 /* No specific data has been created, so just return NULL: */
231                 data = NULL;
232         return (data);
233 }