]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libc_r/uthread/uthread_spec.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libc_r / uthread / uthread_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. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include <signal.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <pthread.h>
36 #include "pthread_private.h"
37
38 struct pthread_key {
39         spinlock_t      lock;
40         volatile int    allocated;
41         volatile int    count;
42         int             seqno;
43         void            (*destructor) ();
44 };
45
46 /* Static variables: */
47 static  struct pthread_key key_table[PTHREAD_KEYS_MAX];
48
49 __weak_reference(_pthread_key_create, pthread_key_create);
50 __weak_reference(_pthread_key_delete, pthread_key_delete);
51 __weak_reference(_pthread_getspecific, pthread_getspecific);
52 __weak_reference(_pthread_setspecific, pthread_setspecific);
53
54
55 int
56 _pthread_key_create(pthread_key_t * key, void (*destructor) (void *))
57 {
58         for ((*key) = 0; (*key) < PTHREAD_KEYS_MAX; (*key)++) {
59                 /* Lock the key table entry: */
60                 _SPINLOCK(&key_table[*key].lock);
61
62                 if (key_table[(*key)].allocated == 0) {
63                         key_table[(*key)].allocated = 1;
64                         key_table[(*key)].destructor = destructor;
65                         key_table[(*key)].seqno++;
66
67                         /* Unlock the key table entry: */
68                         _SPINUNLOCK(&key_table[*key].lock);
69                         return (0);
70                 }
71
72                 /* Unlock the key table entry: */
73                 _SPINUNLOCK(&key_table[*key].lock);
74         }
75         return (EAGAIN);
76 }
77
78 int
79 _pthread_key_delete(pthread_key_t key)
80 {
81         int ret = 0;
82
83         if (key < PTHREAD_KEYS_MAX) {
84                 /* Lock the key table entry: */
85                 _SPINLOCK(&key_table[key].lock);
86
87                 if (key_table[key].allocated)
88                         key_table[key].allocated = 0;
89                 else
90                         ret = EINVAL;
91
92                 /* Unlock the key table entry: */
93                 _SPINUNLOCK(&key_table[key].lock);
94         } else
95                 ret = EINVAL;
96         return (ret);
97 }
98
99 void 
100 _thread_cleanupspecific(void)
101 {
102         struct pthread  *curthread = _get_curthread();
103         void            *data = NULL;
104         int             key;
105         int             itr;
106         void            (*destructor)( void *);
107
108         for (itr = 0; itr < PTHREAD_DESTRUCTOR_ITERATIONS; itr++) {
109                 for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
110                         if (curthread->specific_data_count > 0) {
111                                 /* Lock the key table entry: */
112                                 _SPINLOCK(&key_table[key].lock);
113                                 destructor = NULL;
114
115                                 if (key_table[key].allocated &&
116                                     (curthread->specific[key].data != NULL)) {
117                                         if (curthread->specific[key].seqno ==
118                                             key_table[key].seqno) {
119                                                 data = (void *) curthread->specific[key].data;
120                                                 destructor = key_table[key].destructor;
121                                         }
122                                         curthread->specific[key].data = NULL;
123                                         curthread->specific_data_count--;
124                                 }
125
126                                 /* Unlock the key table entry: */
127                                 _SPINUNLOCK(&key_table[key].lock);
128
129                                 /*
130                                  * If there is a destructore, call it
131                                  * with the key table entry unlocked:
132                                  */
133                                 if (destructor)
134                                         destructor(data);
135                         } else {
136                                 free(curthread->specific);
137                                 curthread->specific = NULL;
138                                 return;
139                         }
140                 }
141         }
142         if (curthread->specific != NULL) {
143                 free(curthread->specific);
144                 curthread->specific = NULL;
145         }
146 }
147
148 static inline struct pthread_specific_elem *
149 pthread_key_allocate_data(void)
150 {
151         struct pthread_specific_elem *new_data;
152
153         new_data = (struct pthread_specific_elem *)
154             malloc(sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
155         if (new_data != NULL) {
156                 memset((void *) new_data, 0,
157                     sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
158         }
159         return (new_data);
160 }
161
162 int 
163 _pthread_setspecific(pthread_key_t key, const void *value)
164 {
165         struct pthread  *pthread;
166         int             ret = 0;
167
168         /* Point to the running thread: */
169         pthread = _get_curthread();
170
171         if ((pthread->specific) ||
172             (pthread->specific = pthread_key_allocate_data())) {
173                 if (key < PTHREAD_KEYS_MAX) {
174                         if (key_table[key].allocated) {
175                                 if (pthread->specific[key].data == NULL) {
176                                         if (value != NULL)
177                                                 pthread->specific_data_count++;
178                                 } else {
179                                         if (value == NULL)
180                                                 pthread->specific_data_count--;
181                                 }
182                                 pthread->specific[key].data = value;
183                                 pthread->specific[key].seqno =
184                                     key_table[key].seqno;
185                                 ret = 0;
186                         } else
187                                 ret = EINVAL;
188                 } else
189                         ret = EINVAL;
190         } else
191                 ret = ENOMEM;
192         return (ret);
193 }
194
195 void *
196 _pthread_getspecific(pthread_key_t key)
197 {
198         struct pthread  *pthread;
199         void            *data;
200
201         /* Point to the running thread: */
202         pthread = _get_curthread();
203
204         /* Check if there is specific data: */
205         if (pthread->specific != NULL && key < PTHREAD_KEYS_MAX) {
206                 /* Check if this key has been used before: */
207                 if (key_table[key].allocated &&
208                     (pthread->specific[key].seqno == key_table[key].seqno)) {
209                         /* Return the value: */
210                         data = (void *) pthread->specific[key].data;
211                 } else {
212                         /*
213                          * This key has not been used before, so return NULL
214                          * instead: 
215                          */
216                         data = NULL;
217                 }
218         } else
219                 /* No specific data has been created, so just return NULL: */
220                 data = NULL;
221         return (data);
222 }