]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (C) 2002-2018 Free Software Foundation, Inc. |
76a50749 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library 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 GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
76a50749 UD |
18 | |
19 | #include <errno.h> | |
20 | #include <stdlib.h> | |
21 | #include "pthreadP.h" | |
22 | ||
23 | ||
24 | int | |
9d46370c | 25 | __pthread_setspecific (pthread_key_t key, const void *value) |
76a50749 UD |
26 | { |
27 | struct pthread *self; | |
28 | unsigned int idx1st; | |
29 | unsigned int idx2nd; | |
30 | struct pthread_key_data *level2; | |
31 | unsigned int seq; | |
32 | ||
33 | self = THREAD_SELF; | |
34 | ||
35 | /* Special case access to the first 2nd-level block. This is the | |
36 | usual case. */ | |
a1ffb40e | 37 | if (__glibc_likely (key < PTHREAD_KEY_2NDLEVEL_SIZE)) |
76a50749 UD |
38 | { |
39 | /* Verify the key is sane. */ | |
40 | if (KEY_UNUSED ((seq = __pthread_keys[key].seq))) | |
41 | /* Not valid. */ | |
42 | return EINVAL; | |
43 | ||
44 | level2 = &self->specific_1stblock[key]; | |
6b4686a5 UD |
45 | |
46 | /* Remember that we stored at least one set of data. */ | |
47 | if (value != NULL) | |
48 | THREAD_SETMEM (self, specific_used, true); | |
76a50749 UD |
49 | } |
50 | else | |
51 | { | |
9c06eb66 UD |
52 | if (key >= PTHREAD_KEYS_MAX |
53 | || KEY_UNUSED ((seq = __pthread_keys[key].seq))) | |
76a50749 UD |
54 | /* Not valid. */ |
55 | return EINVAL; | |
56 | ||
57 | idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE; | |
58 | idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE; | |
59 | ||
60 | /* This is the second level array. Allocate it if necessary. */ | |
117c452c | 61 | level2 = THREAD_GETMEM_NC (self, specific, idx1st); |
76a50749 UD |
62 | if (level2 == NULL) |
63 | { | |
64 | if (value == NULL) | |
65 | /* We don't have to do anything. The value would in any case | |
66 | be NULL. We can save the memory allocation. */ | |
67 | return 0; | |
68 | ||
69 | level2 | |
70 | = (struct pthread_key_data *) calloc (PTHREAD_KEY_2NDLEVEL_SIZE, | |
71 | sizeof (*level2)); | |
72 | if (level2 == NULL) | |
73 | return ENOMEM; | |
74 | ||
117c452c | 75 | THREAD_SETMEM_NC (self, specific, idx1st, level2); |
76a50749 UD |
76 | } |
77 | ||
78 | /* Pointer to the right array element. */ | |
79 | level2 = &level2[idx2nd]; | |
6b4686a5 UD |
80 | |
81 | /* Remember that we stored at least one set of data. */ | |
82 | THREAD_SETMEM (self, specific_used, true); | |
76a50749 UD |
83 | } |
84 | ||
85 | /* Store the data and the sequence number so that we can recognize | |
86 | stale data. */ | |
87 | level2->seq = seq; | |
88 | level2->data = (void *) value; | |
89 | ||
76a50749 UD |
90 | return 0; |
91 | } | |
fa872e1b | 92 | weak_alias (__pthread_setspecific, pthread_setspecific) |
4d17e683 | 93 | hidden_def (__pthread_setspecific) |