Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.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. 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: src/lib/libc_r/uthread/uthread_spec.c,v 1.14.2.2 2002/10/22 14:44:03 fjoe Exp $
33  * $DragonFly: src/lib/libc_r/uthread/uthread_spec.c,v 1.2 2003/06/17 04:26:48 dillon Exp $
34  */
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <pthread.h>
40 #include "pthread_private.h"
41
42 /* Static variables: */
43 static  struct pthread_key key_table[PTHREAD_KEYS_MAX];
44
45 __weak_reference(_pthread_key_create, pthread_key_create);
46 __weak_reference(_pthread_key_delete, pthread_key_delete);
47 __weak_reference(_pthread_getspecific, pthread_getspecific);
48 __weak_reference(_pthread_setspecific, pthread_setspecific);
49
50
51 int
52 _pthread_key_create(pthread_key_t * key, void (*destructor) (void *))
53 {
54         for ((*key) = 0; (*key) < PTHREAD_KEYS_MAX; (*key)++) {
55                 /* Lock the key table entry: */
56                 _SPINLOCK(&key_table[*key].lock);
57
58                 if (key_table[(*key)].allocated == 0) {
59                         key_table[(*key)].allocated = 1;
60                         key_table[(*key)].destructor = destructor;
61
62                         /* Unlock the key table entry: */
63                         _SPINUNLOCK(&key_table[*key].lock);
64                         return (0);
65                 }
66
67                 /* Unlock the key table entry: */
68                 _SPINUNLOCK(&key_table[*key].lock);
69         }
70         return (EAGAIN);
71 }
72
73 int
74 _pthread_key_delete(pthread_key_t key)
75 {
76         int ret = 0;
77
78         if (key < PTHREAD_KEYS_MAX) {
79                 /* Lock the key table entry: */
80                 _SPINLOCK(&key_table[key].lock);
81
82                 if (key_table[key].allocated)
83                         key_table[key].allocated = 0;
84                 else
85                         ret = EINVAL;
86
87                 /* Unlock the key table entry: */
88                 _SPINUNLOCK(&key_table[key].lock);
89         } else
90                 ret = EINVAL;
91         return (ret);
92 }
93
94 void 
95 _thread_cleanupspecific(void)
96 {
97         struct pthread  *curthread = _get_curthread();
98         void           *data = NULL;
99         int             key;
100         int             itr;
101         void            (*destructor)( void *);
102
103         for (itr = 0; itr < PTHREAD_DESTRUCTOR_ITERATIONS; itr++) {
104                 for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
105                         if (curthread->specific_data_count) {
106                                 /* Lock the key table entry: */
107                                 _SPINLOCK(&key_table[key].lock);
108                                 destructor = NULL;
109
110                                 if (key_table[key].allocated) {
111                                         if (curthread->specific_data[key]) {
112                                                 data = (void *) curthread->specific_data[key];
113                                                 curthread->specific_data[key] = NULL;
114                                                 curthread->specific_data_count--;
115                                                 destructor = key_table[key].destructor;
116                                         }
117                                 }
118
119                                 /* Unlock the key table entry: */
120                                 _SPINUNLOCK(&key_table[key].lock);
121
122                                 /*
123                                  * If there is a destructore, call it
124                                  * with the key table entry unlocked:
125                                  */
126                                 if (destructor)
127                                         destructor(data);
128                         } else {
129                                 free(curthread->specific_data);
130                                 curthread->specific_data = NULL;
131                                 return;
132                         }
133                 }
134         }
135         free(curthread->specific_data);
136         curthread->specific_data = NULL;
137 }
138
139 static inline const void **
140 pthread_key_allocate_data(void)
141 {
142         const void    **new_data;
143         if ((new_data = (const void **) malloc(sizeof(void *) * PTHREAD_KEYS_MAX)) != NULL) {
144                 memset((void *) new_data, 0, sizeof(void *) * PTHREAD_KEYS_MAX);
145         }
146         return (new_data);
147 }
148
149 int 
150 _pthread_setspecific(pthread_key_t key, const void *value)
151 {
152         struct pthread  *pthread;
153         int             ret = 0;
154
155         /* Point to the running thread: */
156         pthread = _get_curthread();
157
158         if ((pthread->specific_data) ||
159             (pthread->specific_data = pthread_key_allocate_data())) {
160                 if (key < PTHREAD_KEYS_MAX) {
161                         if (key_table[key].allocated) {
162                                 if (pthread->specific_data[key] == NULL) {
163                                         if (value != NULL)
164                                                 pthread->specific_data_count++;
165                                 } else {
166                                         if (value == NULL)
167                                                 pthread->specific_data_count--;
168                                 }
169                                 pthread->specific_data[key] = value;
170                                 ret = 0;
171                         } else
172                                 ret = EINVAL;
173                 } else
174                         ret = EINVAL;
175         } else
176                 ret = ENOMEM;
177         return (ret);
178 }
179
180 void *
181 _pthread_getspecific(pthread_key_t key)
182 {
183         struct pthread  *pthread;
184         void            *data;
185
186         /* Point to the running thread: */
187         pthread = _get_curthread();
188
189         /* Check if there is specific data: */
190         if (pthread->specific_data != NULL && key < PTHREAD_KEYS_MAX) {
191                 /* Check if this key has been used before: */
192                 if (key_table[key].allocated) {
193                         /* Return the value: */
194                         data = (void *) pthread->specific_data[key];
195                 } else {
196                         /*
197                          * This key has not been used before, so return NULL
198                          * instead: 
199                          */
200                         data = NULL;
201                 }
202         } else
203                 /* No specific data has been created, so just return NULL: */
204                 data = NULL;
205         return (data);
206 }