lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2002-2015 Free Software Foundation, Inc. |
| 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 |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <stdlib.h> |
| 21 | #include "pthreadP.h" |
| 22 | |
| 23 | |
| 24 | int |
| 25 | __pthread_setspecific (key, value) |
| 26 | pthread_key_t key; |
| 27 | const void *value; |
| 28 | { |
| 29 | struct pthread *self; |
| 30 | unsigned int idx1st; |
| 31 | unsigned int idx2nd; |
| 32 | struct pthread_key_data *level2; |
| 33 | unsigned int seq; |
| 34 | |
| 35 | self = THREAD_SELF; |
| 36 | |
| 37 | /* Special case access to the first 2nd-level block. This is the |
| 38 | usual case. */ |
| 39 | if (__glibc_likely (key < PTHREAD_KEY_2NDLEVEL_SIZE)) |
| 40 | { |
| 41 | /* Verify the key is sane. */ |
| 42 | if (KEY_UNUSED ((seq = __pthread_keys[key].seq))) |
| 43 | /* Not valid. */ |
| 44 | return EINVAL; |
| 45 | |
| 46 | level2 = &self->specific_1stblock[key]; |
| 47 | |
| 48 | /* Remember that we stored at least one set of data. */ |
| 49 | if (value != NULL) |
| 50 | THREAD_SETMEM (self, specific_used, true); |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | if (key >= PTHREAD_KEYS_MAX |
| 55 | || KEY_UNUSED ((seq = __pthread_keys[key].seq))) |
| 56 | /* Not valid. */ |
| 57 | return EINVAL; |
| 58 | |
| 59 | idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE; |
| 60 | idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE; |
| 61 | |
| 62 | /* This is the second level array. Allocate it if necessary. */ |
| 63 | level2 = THREAD_GETMEM_NC (self, specific, idx1st); |
| 64 | if (level2 == NULL) |
| 65 | { |
| 66 | if (value == NULL) |
| 67 | /* We don't have to do anything. The value would in any case |
| 68 | be NULL. We can save the memory allocation. */ |
| 69 | return 0; |
| 70 | |
| 71 | level2 |
| 72 | = (struct pthread_key_data *) calloc (PTHREAD_KEY_2NDLEVEL_SIZE, |
| 73 | sizeof (*level2)); |
| 74 | if (level2 == NULL) |
| 75 | return ENOMEM; |
| 76 | |
| 77 | THREAD_SETMEM_NC (self, specific, idx1st, level2); |
| 78 | } |
| 79 | |
| 80 | /* Pointer to the right array element. */ |
| 81 | level2 = &level2[idx2nd]; |
| 82 | |
| 83 | /* Remember that we stored at least one set of data. */ |
| 84 | THREAD_SETMEM (self, specific_used, true); |
| 85 | } |
| 86 | |
| 87 | /* Store the data and the sequence number so that we can recognize |
| 88 | stale data. */ |
| 89 | level2->seq = seq; |
| 90 | level2->data = (void *) value; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | strong_alias (__pthread_setspecific, pthread_setspecific) |
| 95 | hidden_def (__pthread_setspecific) |