]> git.saurik.com Git - apple/libc.git/blob - pthreads/pthread_tsd.c
Libc-320.tar.gz
[apple/libc.git] / pthreads / pthread_tsd.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
27 * All Rights Reserved
28 *
29 * Permission to use, copy, modify, and distribute this software and
30 * its documentation for any purpose and without fee is hereby granted,
31 * provided that the above copyright notice appears in all copies and
32 * that both the copyright notice and this permission notice appear in
33 * supporting documentation.
34 *
35 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
36 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
37 * FOR A PARTICULAR PURPOSE.
38 *
39 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
40 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
41 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
42 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
43 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 *
45 */
46 /*
47 * MkLinux
48 */
49
50 /*
51 * POSIX Pthread Library
52 * Thread Specific Data support
53 * NB: pthread_getspecific() is in a separate assembly file
54 */
55
56 #include "pthread_internals.h"
57
58 static struct
59 {
60 int created; /* Set TRUE if 'create_key' used this slot */
61 void (*destructor)(void *);
62 } _pthread_keys[_POSIX_THREAD_KEYS_MAX];
63 static pthread_lock_t tds_lock = LOCK_INITIALIZER;
64
65 /*
66 * Create a new key for thread specific data
67 */
68 int
69 pthread_key_create(pthread_key_t *key,
70 void (*destructor)(void *))
71 {
72 int res, i;
73 LOCK(tds_lock);
74 res = ENOMEM; /* No 'free' keys */
75 /* The first slot is reserved for pthread_self() */
76 for (i = 1; i < _POSIX_THREAD_KEYS_MAX; i++)
77 {
78 if (_pthread_keys[i].created == FALSE)
79 {
80 _pthread_keys[i].created = TRUE;
81 _pthread_keys[i].destructor = destructor;
82 *key = i;
83 res = ESUCCESS;
84 break;
85 }
86 }
87 UNLOCK(tds_lock);
88 return (res);
89 }
90
91 /*
92 * Destroy a thread specific data key
93 */
94 int
95 pthread_key_delete(pthread_key_t key)
96 {
97 int res;
98 LOCK(tds_lock);
99 /* The first slot is reserved for pthread_self() */
100 if ((key > 0) && (key < _POSIX_THREAD_KEYS_MAX))
101 {
102 if (_pthread_keys[key].created)
103 {
104 struct _pthread * p;
105
106 _pthread_keys[key].created = FALSE;
107 LOCK(_pthread_list_lock);
108 LIST_FOREACH(p, &__pthread_head, plist) {
109 /* It is an 32bit value no lock needed */
110 p->tsd[key] = 0;
111 }
112 UNLOCK(_pthread_list_lock);
113 res = ESUCCESS;
114 } else
115 {
116 res = EINVAL;
117 }
118 } else
119 { /* Invalid key */
120 res = EINVAL;
121 }
122 UNLOCK(tds_lock);
123 return (res);
124 }
125
126 /*
127 * Set the thread private value for a given key.
128 * We do not take the spinlock for this or pthread_getspecific.
129 * The assignment to self->tsd[] is thread-safe because we never
130 * refer to the tsd[] of a thread other than pthread_self().
131 * The reference to _pthread_keys[...].created could race with a
132 * pthread_key_delete() but in this case the behaviour is allowed
133 * to be undefined.
134 */
135 int
136 pthread_setspecific(pthread_key_t key,
137 const void *value)
138 {
139 int res;
140 pthread_t self;
141 /* The first slot is reserved for pthread_self() */
142 if ((key > 0) && (key < _POSIX_THREAD_KEYS_MAX))
143 {
144 if (_pthread_keys[key].created)
145 {
146 self = pthread_self();
147 self->tsd[key] = (void *) value;
148 res = ESUCCESS;
149 } else
150 {
151 res = EINVAL;
152 }
153 } else
154 { /* Invalid key */
155 res = EINVAL;
156 }
157 return (res);
158 }
159
160 /*
161 * Clean up thread specific data as thread 'dies'
162 */
163 void
164 _pthread_tsd_cleanup(pthread_t self)
165 {
166 int i, j;
167 void *param;
168 for (j = 0; j < PTHREAD_DESTRUCTOR_ITERATIONS; j++)
169 {
170 /* The first slot is reserved for pthread_self() */
171 for (i = 1; i < _POSIX_THREAD_KEYS_MAX; i++)
172 {
173 if (_pthread_keys[i].created && (param = self->tsd[i]))
174 {
175 self->tsd[i] = (void *)NULL;
176 if (_pthread_keys[i].destructor)
177 {
178 (_pthread_keys[i].destructor)(param);
179 }
180 }
181 }
182 }
183 }