pthread_setspecific.c 2.9 KB

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