summaryrefslogtreecommitdiff
path: root/libpthread/linuxthreads/specific.c
blob: d8b5bb0b362ed24209077a0898aac954d020d69d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Linuxthreads - a simple clone()-based implementation of Posix        */
/* threads for Linux.                                                   */
/* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
/*                                                                      */
/* This program is free software; you can redistribute it and/or        */
/* modify it under the terms of the GNU Library General Public License  */
/* as published by the Free Software Foundation; either version 2       */
/* of the License, or (at your option) any later version.               */
/*                                                                      */
/* This program is distributed in the hope that it will be useful,      */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
/* GNU Library General Public License for more details.                 */

/* Thread-specific data */

#include <features.h>
#define __USE_GNU
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include "pthread.h"
#include "internals.h"
#include "spinlock.h"
#include "restart.h"


/* Table of keys. */

static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] =
  { { 0, NULL } };

/* For debugging purposes put the maximum number of keys in a variable.  */
const int __linuxthreads_pthread_keys_max = PTHREAD_KEYS_MAX;
const int __linuxthreads_pthread_key_2ndlevel_size = PTHREAD_KEY_2NDLEVEL_SIZE;

/* Mutex to protect access to pthread_keys */

static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER;

/* Create a new key */

int pthread_key_create(pthread_key_t * key, destr_function destr)
{
    int i;

    pthread_mutex_lock(&pthread_keys_mutex);
    for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
	if (! pthread_keys[i].in_use) {
	    /* Mark key in use */
	    pthread_keys[i].in_use = 1;
	    pthread_keys[i].destr = destr;
	    pthread_mutex_unlock(&pthread_keys_mutex);
	    *key = i;
	    return 0;
	}
    }
    pthread_mutex_unlock(&pthread_keys_mutex);
    return EAGAIN;
}

/* Delete a key */
int pthread_key_delete(pthread_key_t key)
{
    pthread_descr self = thread_self();

    pthread_mutex_lock(&pthread_keys_mutex);
    if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
	pthread_mutex_unlock(&pthread_keys_mutex);
	return EINVAL;
    }
    pthread_keys[key].in_use = 0;
    pthread_keys[key].destr = NULL;

    /* Set the value of the key to NULL in all running threads, so
       that if the key is reallocated later by pthread_key_create, its
       associated values will be NULL in all threads.
       Do nothing if no threads have been created yet.  */
    if (__pthread_manager_request != -1)
    {
	pthread_descr th;
	unsigned int idx1st, idx2nd;

	idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
	idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
	th = self;
	do {
	    /* If the thread already is terminated don't modify the memory.  */
	    if (!th->p_terminated && th->p_specific[idx1st] != NULL)
		th->p_specific[idx1st][idx2nd] = NULL;
	    th = th->p_nextlive;
	} while (th != self);
    }

    pthread_mutex_unlock(&pthread_keys_mutex);
    return 0;
}

/* Set the value of a key */

int pthread_setspecific(pthread_key_t key, const void * pointer)
{
    pthread_descr self = thread_self();
    unsigned int idx1st, idx2nd;

    if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use)
	return EINVAL;
    idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
    idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
    if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL) {
	void *newp = calloc(PTHREAD_KEY_2NDLEVEL_SIZE, sizeof (void *));
	if (newp == NULL)
	    return ENOMEM;
	THREAD_SETMEM_NC(self, p_specific[idx1st], newp);
    }
    THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd] = (void *) pointer;
    return 0;
}

/* Get the value of a key */

void * pthread_getspecific(pthread_key_t key)
{
    pthread_descr self = thread_self();
    unsigned int idx1st, idx2nd;

    if (key >= PTHREAD_KEYS_MAX)
	return NULL;
    idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
    idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
    if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL
	    || !pthread_keys[key].in_use)
	return NULL;
    return THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd];
}

/* Call the destruction routines on all keys */

void __pthread_destroy_specifics()
{
    pthread_descr self = thread_self();
    int i, j, round, found_nonzero;
    destr_function destr;
    void * data;

    for (round = 0, found_nonzero = 1;
	    found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
	    round++) {
	found_nonzero = 0;
	for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
	    if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
		for (j = 0; j < PTHREAD_KEY_2NDLEVEL_SIZE; j++) {
		    destr = pthread_keys[i * PTHREAD_KEY_2NDLEVEL_SIZE + j].destr;
		    data = THREAD_GETMEM_NC(self, p_specific[i])[j];
		    if (destr != NULL && data != NULL) {
			THREAD_GETMEM_NC(self, p_specific[i])[j] = NULL;
			destr(data);
			found_nonzero = 1;
		    }
		}
    }
    __pthread_lock(THREAD_GETMEM(self, p_lock), self);
    for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
	if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL) {
	    free(THREAD_GETMEM_NC(self, p_specific[i]));
	    THREAD_SETMEM_NC(self, p_specific[i], NULL);
	}
    }
    __pthread_unlock(THREAD_GETMEM(self, p_lock));
}


/* Thread-specific data for libc. */
#if !(USE_TLS && HAVE___THREAD)
static int
libc_internal_tsd_set(enum __libc_tsd_key_t key, const void * pointer)
{
    pthread_descr self = thread_self();

    THREAD_SETMEM_NC(self, p_libc_specific[key], (void *) pointer);
    return 0;
}
int (*__libc_internal_tsd_set)(enum __libc_tsd_key_t key, const void * pointer)
     = libc_internal_tsd_set;

static void *
libc_internal_tsd_get(enum __libc_tsd_key_t key)
{
    pthread_descr self = thread_self();

    return THREAD_GETMEM_NC(self, p_libc_specific[key]);
}
void * (*__libc_internal_tsd_get)(enum __libc_tsd_key_t key)
     = libc_internal_tsd_get;

static void ** __attribute__ ((__const__))
libc_internal_tsd_address (enum __libc_tsd_key_t key)
{
    pthread_descr self = thread_self();
    return &self->p_libc_specific[key];
}
void **(*const __libc_internal_tsd_address) (enum __libc_tsd_key_t key)
     __THROW __attribute__ ((__const__)) = libc_internal_tsd_address;
#endif