Back to index

glibc  2.9
pthread_getspecific.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdlib.h>
00021 #include "pthreadP.h"
00022 
00023 
00024 void *
00025 __pthread_getspecific (key)
00026      pthread_key_t key;
00027 {
00028   struct pthread_key_data *data;
00029 
00030   /* Special case access to the first 2nd-level block.  This is the
00031      usual case.  */
00032   if (__builtin_expect (key < PTHREAD_KEY_2NDLEVEL_SIZE, 1))
00033     data = &THREAD_SELF->specific_1stblock[key];
00034   else
00035     {
00036       /* Verify the key is sane.  */
00037       if (key >= PTHREAD_KEYS_MAX)
00038        /* Not valid.  */
00039        return NULL;
00040 
00041       unsigned int idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
00042       unsigned int idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
00043 
00044       /* If the sequence number doesn't match or the key cannot be defined
00045         for this thread since the second level array is not allocated
00046         return NULL, too.  */
00047       struct pthread_key_data *level2 = THREAD_GETMEM_NC (THREAD_SELF,
00048                                                    specific, idx1st);
00049       if (level2 == NULL)
00050        /* Not allocated, therefore no data.  */
00051        return NULL;
00052 
00053       /* There is data.  */
00054       data = &level2[idx2nd];
00055     }
00056 
00057   void *result = data->data;
00058   if (result != NULL)
00059     {
00060       uintptr_t seq = data->seq;
00061 
00062       if (__builtin_expect (seq != __pthread_keys[key].seq, 0))
00063        result = data->data = NULL;
00064     }
00065 
00066   return result;
00067 }
00068 strong_alias (__pthread_getspecific, pthread_getspecific)
00069 strong_alias (__pthread_getspecific, __pthread_getspecific_internal)