pthread_setspecific.c 2.7 KB

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