1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
5 /* This program is free software; you can redistribute it and/or */
6 /* modify it under the terms of the GNU Library General Public License */
7 /* as published by the Free Software Foundation; either version 2 */
8 /* of the License, or (at your option) any later version. */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU Library General Public License for more details. */
15 /* Thread-specific data */
22 #include "internals.h"
28 static struct pthread_key_struct pthread_keys
[PTHREAD_KEYS_MAX
] =
31 /* For debugging purposes put the maximum number of keys in a variable. */
32 const int __linuxthreads_pthread_keys_max
= PTHREAD_KEYS_MAX
;
33 const int __linuxthreads_pthread_key_2ndlevel_size
= PTHREAD_KEY_2NDLEVEL_SIZE
;
35 /* Mutex to protect access to pthread_keys */
37 static pthread_mutex_t pthread_keys_mutex
= PTHREAD_MUTEX_INITIALIZER
;
39 /* Create a new key */
41 int pthread_key_create(pthread_key_t
* key
, destr_function destr
)
45 __pthread_mutex_lock(&pthread_keys_mutex
);
46 for (i
= 0; i
< PTHREAD_KEYS_MAX
; i
++) {
47 if (! pthread_keys
[i
].in_use
) {
49 pthread_keys
[i
].in_use
= 1;
50 pthread_keys
[i
].destr
= destr
;
51 __pthread_mutex_unlock(&pthread_keys_mutex
);
56 __pthread_mutex_unlock(&pthread_keys_mutex
);
61 int pthread_key_delete(pthread_key_t key
)
63 pthread_descr self
= thread_self();
65 __pthread_mutex_lock(&pthread_keys_mutex
);
66 if (key
>= PTHREAD_KEYS_MAX
|| !pthread_keys
[key
].in_use
) {
67 __pthread_mutex_unlock(&pthread_keys_mutex
);
70 pthread_keys
[key
].in_use
= 0;
71 pthread_keys
[key
].destr
= NULL
;
73 /* Set the value of the key to NULL in all running threads, so
74 that if the key is reallocated later by pthread_key_create, its
75 associated values will be NULL in all threads.
76 Do nothing if no threads have been created yet. */
77 if (__pthread_manager_request
!= -1)
80 unsigned int idx1st
, idx2nd
;
82 idx1st
= key
/ PTHREAD_KEY_2NDLEVEL_SIZE
;
83 idx2nd
= key
% PTHREAD_KEY_2NDLEVEL_SIZE
;
86 /* If the thread already is terminated don't modify the memory. */
87 if (!th
->p_terminated
&& th
->p_specific
[idx1st
] != NULL
)
88 th
->p_specific
[idx1st
][idx2nd
] = NULL
;
93 __pthread_mutex_unlock(&pthread_keys_mutex
);
97 /* Set the value of a key */
99 int pthread_setspecific(pthread_key_t key
, const void * pointer
)
101 pthread_descr self
= thread_self();
102 unsigned int idx1st
, idx2nd
;
104 if (key
>= PTHREAD_KEYS_MAX
|| !pthread_keys
[key
].in_use
)
106 idx1st
= key
/ PTHREAD_KEY_2NDLEVEL_SIZE
;
107 idx2nd
= key
% PTHREAD_KEY_2NDLEVEL_SIZE
;
108 if (THREAD_GETMEM_NC(self
, p_specific
[idx1st
]) == NULL
) {
109 void *newp
= calloc(PTHREAD_KEY_2NDLEVEL_SIZE
, sizeof (void *));
112 THREAD_SETMEM_NC(self
, p_specific
[idx1st
], newp
);
114 THREAD_GETMEM_NC(self
, p_specific
[idx1st
])[idx2nd
] = (void *) pointer
;
118 /* Get the value of a key */
120 void * pthread_getspecific(pthread_key_t key
)
122 pthread_descr self
= thread_self();
123 unsigned int idx1st
, idx2nd
;
125 if (key
>= PTHREAD_KEYS_MAX
)
127 idx1st
= key
/ PTHREAD_KEY_2NDLEVEL_SIZE
;
128 idx2nd
= key
% PTHREAD_KEY_2NDLEVEL_SIZE
;
129 if (THREAD_GETMEM_NC(self
, p_specific
[idx1st
]) == NULL
130 || !pthread_keys
[key
].in_use
)
132 return THREAD_GETMEM_NC(self
, p_specific
[idx1st
])[idx2nd
];
135 /* Call the destruction routines on all keys */
137 void __pthread_destroy_specifics(void)
139 pthread_descr self
= thread_self();
140 int i
, j
, round
, found_nonzero
;
141 destr_function destr
;
144 for (round
= 0, found_nonzero
= 1;
145 found_nonzero
&& round
< PTHREAD_DESTRUCTOR_ITERATIONS
;
148 for (i
= 0; i
< PTHREAD_KEY_1STLEVEL_SIZE
; i
++)
149 if (THREAD_GETMEM_NC(self
, p_specific
[i
]) != NULL
)
150 for (j
= 0; j
< PTHREAD_KEY_2NDLEVEL_SIZE
; j
++) {
151 destr
= pthread_keys
[i
* PTHREAD_KEY_2NDLEVEL_SIZE
+ j
].destr
;
152 data
= THREAD_GETMEM_NC(self
, p_specific
[i
])[j
];
153 if (destr
!= NULL
&& data
!= NULL
) {
154 THREAD_GETMEM_NC(self
, p_specific
[i
])[j
] = NULL
;
160 __pthread_lock(THREAD_GETMEM(self
, p_lock
), self
);
161 for (i
= 0; i
< PTHREAD_KEY_1STLEVEL_SIZE
; i
++) {
162 if (THREAD_GETMEM_NC(self
, p_specific
[i
]) != NULL
) {
163 free(THREAD_GETMEM_NC(self
, p_specific
[i
]));
164 THREAD_SETMEM_NC(self
, p_specific
[i
], NULL
);
167 __pthread_unlock(THREAD_GETMEM(self
, p_lock
));