pthread_setspecific.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright (C) 2002, 2003, 2006 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. attribute_protected
  20. __pthread_setspecific (
  21. pthread_key_t key,
  22. const void *value)
  23. {
  24. struct pthread *self;
  25. unsigned int idx1st;
  26. unsigned int idx2nd;
  27. struct pthread_key_data *level2;
  28. unsigned int seq;
  29. self = THREAD_SELF;
  30. /* Special case access to the first 2nd-level block. This is the
  31. usual case. */
  32. if (__builtin_expect (key < PTHREAD_KEY_2NDLEVEL_SIZE, 1))
  33. {
  34. /* Verify the key is sane. */
  35. if (KEY_UNUSED ((seq = __pthread_keys[key].seq)))
  36. /* Not valid. */
  37. return EINVAL;
  38. level2 = &self->specific_1stblock[key];
  39. /* Remember that we stored at least one set of data. */
  40. if (value != NULL)
  41. THREAD_SETMEM (self, specific_used, true);
  42. }
  43. else
  44. {
  45. if (key >= PTHREAD_KEYS_MAX
  46. || KEY_UNUSED ((seq = __pthread_keys[key].seq)))
  47. /* Not valid. */
  48. return EINVAL;
  49. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  50. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  51. /* This is the second level array. Allocate it if necessary. */
  52. level2 = THREAD_GETMEM_NC (self, specific, idx1st);
  53. if (level2 == NULL)
  54. {
  55. if (value == NULL)
  56. /* We don't have to do anything. The value would in any case
  57. be NULL. We can save the memory allocation. */
  58. return 0;
  59. level2
  60. = (struct pthread_key_data *) calloc (PTHREAD_KEY_2NDLEVEL_SIZE,
  61. sizeof (*level2));
  62. if (level2 == NULL)
  63. return ENOMEM;
  64. THREAD_SETMEM_NC (self, specific, idx1st, level2);
  65. }
  66. /* Pointer to the right array element. */
  67. level2 = &level2[idx2nd];
  68. /* Remember that we stored at least one set of data. */
  69. THREAD_SETMEM (self, specific_used, true);
  70. }
  71. /* Store the data and the sequence number so that we can recognize
  72. stale data. */
  73. level2->seq = seq;
  74. level2->data = (void *) value;
  75. return 0;
  76. }
  77. strong_alias (__pthread_setspecific, pthread_setspecific)
  78. strong_alias (__pthread_setspecific, __pthread_setspecific_internal)