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