pthread_getspecific.c 2.2 KB

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