2 * Copyright (c) 2000-2003, 2007 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
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.
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
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.
49 * POSIX Pthread Library
50 * Thread Specific Data support
51 * NB: pthread_getspecific() is in a separate assembly file
54 #include "pthread_internals.h"
58 int created
; /* Set TRUE if 'create_key' used this slot */
59 void (*destructor
)(void *);
60 } _pthread_keys
[_INTERNAL_POSIX_THREAD_KEYS_END
];
61 static pthread_lock_t tds_lock
= LOCK_INITIALIZER
;
64 * Partition _pthread_keys in a lower part that dyld can use, and an upper
65 * part for libSystem. The libSystem part starts at __pthread_tsd_first = 4.
66 * dyld will set this value to 1.
67 * LEFT OVER TILL DYLD changes to using static (1-3) numbers
70 #ifdef DYLD_STATIC_LIBC_BUILD
71 __private_extern__
int __pthread_tsd_first
= 1;
72 __private_extern__
int __pthread_tsd_start
= 5;
73 __private_extern__
int __pthread_tsd_end
= 10;
74 __private_extern__
int __pthread_tsd_max
= 10;
76 __private_extern__
int __pthread_tsd_first
= 10;
77 __private_extern__
int __pthread_tsd_start
= _INTERNAL_POSIX_THREAD_KEYS_MAX
;
78 __private_extern__
int __pthread_tsd_end
= _INTERNAL_POSIX_THREAD_KEYS_END
;
79 __private_extern__
int __pthread_tsd_max
= 10;
82 * Create a new key for thread specific data
85 pthread_key_create(pthread_key_t
*key
,
86 void (*destructor
)(void *))
90 res
= EAGAIN
; /* No 'free' keys, return EAGAIN (as per standard) */
91 /* The first slot is reserved for pthread_self() */
92 for (i
= __pthread_tsd_start
; i
< __pthread_tsd_end
; i
++)
94 if (_pthread_keys
[i
].created
== FALSE
)
96 _pthread_keys
[i
].created
= TRUE
;
97 _pthread_keys
[i
].destructor
= destructor
;
108 * Destroy a thread specific data key
111 pthread_key_delete(pthread_key_t key
)
115 /* The first slot is reserved for pthread_self() */
116 if ((key
>= __pthread_tsd_start
) && (key
< __pthread_tsd_end
))
118 if (_pthread_keys
[key
].created
)
122 _pthread_keys
[key
].created
= FALSE
;
123 LOCK(_pthread_list_lock
);
124 TAILQ_FOREACH(p
, &__pthread_head
, plist
) {
125 /* It is an 32bit value no lock needed */
128 UNLOCK(_pthread_list_lock
);
143 * Set the thread private value for a given key.
144 * We do not take the spinlock for this or pthread_getspecific.
145 * The assignment to self->tsd[] is thread-safe because we never
146 * refer to the tsd[] of a thread other than pthread_self().
147 * The reference to _pthread_keys[...].created could race with a
148 * pthread_key_delete() but in this case the behaviour is allowed
152 pthread_setspecific(pthread_key_t key
,
157 /* The first slot is reserved for pthread_self() */
158 if ((key
>= __pthread_tsd_first
) && (key
< __pthread_tsd_end
))
160 if ((key
< __pthread_tsd_start
) || _pthread_keys
[key
].created
)
162 self
= pthread_self();
163 self
->tsd
[key
] = (void *) value
;
165 if ((key
< __pthread_tsd_start
) && (_pthread_keys
[key
].created
== FALSE
))
166 _pthread_keys
[key
].created
= TRUE
;
168 if (key
> self
->max_tsd_key
)
169 self
->max_tsd_key
= key
;
182 * Clean up thread specific data as thread 'dies'
185 _pthread_tsd_cleanup(pthread_t self
)
190 for (j
= 0; j
< PTHREAD_DESTRUCTOR_ITERATIONS
; j
++)
192 /* The first slot is reserved for pthread_self() */
194 for (i
= __pthread_tsd_first
; i
<= __pthread_tsd_max
; i
++)
196 if (_pthread_keys
[i
].created
&& (param
= self
->tsd
[i
]))
198 self
->tsd
[i
] = (void *)NULL
;
199 if (_pthread_keys
[i
].destructor
)
201 (_pthread_keys
[i
].destructor
)(param
);
205 for (i
= __pthread_tsd_start
; i
<= self
->max_tsd_key
; i
++)
207 if (_pthread_keys
[i
].created
&& (param
= self
->tsd
[i
]))
209 self
->tsd
[i
] = (void *)NULL
;
210 if (_pthread_keys
[i
].destructor
)
212 (_pthread_keys
[i
].destructor
)(param
);
217 self
->max_tsd_key
= 0;
221 pthread_key_init_np(int key
, void (*destructor
)(void *))
223 if ((key
>= __pthread_tsd_first
) && (key
< __pthread_tsd_start
)) {
225 _pthread_keys
[key
].created
= TRUE
;
226 _pthread_keys
[key
].destructor
= destructor
;
228 if (key
> __pthread_tsd_max
)
229 __pthread_tsd_max
= key
;
236 /* we need this till the switchover happens for the dyld. It get 1- 10 slot */
240 if (__pthread_tsd_first
== 1) {
241 __pthread_tsd_start
= 5;
242 __pthread_tsd_end
= 10;
243 __pthread_tsd_max
= 10;