pthread_getspecific.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright (C) 2002, 2003 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 <stdlib.h>
  17. #include "pthreadP.h"
  18. attribute_protected
  19. void *
  20. __pthread_getspecific (pthread_key_t key)
  21. {
  22. struct pthread_key_data *data;
  23. /* Special case access to the first 2nd-level block. This is the
  24. usual case. */
  25. if (__builtin_expect (key < PTHREAD_KEY_2NDLEVEL_SIZE, 1))
  26. data = &THREAD_SELF->specific_1stblock[key];
  27. else
  28. {
  29. /* Verify the key is sane. */
  30. if (key >= PTHREAD_KEYS_MAX)
  31. /* Not valid. */
  32. return NULL;
  33. unsigned int idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  34. unsigned int idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  35. /* If the sequence number doesn't match or the key cannot be defined
  36. for this thread since the second level array is not allocated
  37. return NULL, too. */
  38. struct pthread_key_data *level2 = THREAD_GETMEM_NC (THREAD_SELF,
  39. specific, idx1st);
  40. if (level2 == NULL)
  41. /* Not allocated, therefore no data. */
  42. return NULL;
  43. /* There is data. */
  44. data = &level2[idx2nd];
  45. }
  46. void *result = data->data;
  47. if (result != NULL)
  48. {
  49. uintptr_t seq = data->seq;
  50. if (__builtin_expect (seq != __pthread_keys[key].seq, 0))
  51. result = data->data = NULL;
  52. }
  53. return result;
  54. }
  55. strong_alias (__pthread_getspecific, pthread_getspecific)
  56. strong_alias (__pthread_getspecific, __pthread_getspecific_internal)