Back to index

glibc  2.9
pthread_setspecific.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2006 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 <errno.h>
00021 #include <stdlib.h>
00022 #include "pthreadP.h"
00023 
00024 
00025 int
00026 __pthread_setspecific (key, value)
00027      pthread_key_t key;
00028      const void *value;
00029 {
00030   struct pthread *self;
00031   unsigned int idx1st;
00032   unsigned int idx2nd;
00033   struct pthread_key_data *level2;
00034   unsigned int seq;
00035 
00036   self = THREAD_SELF;
00037 
00038   /* Special case access to the first 2nd-level block.  This is the
00039      usual case.  */
00040   if (__builtin_expect (key < PTHREAD_KEY_2NDLEVEL_SIZE, 1))
00041     {
00042       /* Verify the key is sane.  */
00043       if (KEY_UNUSED ((seq = __pthread_keys[key].seq)))
00044        /* Not valid.  */
00045        return EINVAL;
00046 
00047       level2 = &self->specific_1stblock[key];
00048 
00049       /* Remember that we stored at least one set of data.  */
00050       if (value != NULL)
00051        THREAD_SETMEM (self, specific_used, true);
00052     }
00053   else
00054     {
00055       if (key >= PTHREAD_KEYS_MAX
00056          || KEY_UNUSED ((seq = __pthread_keys[key].seq)))
00057        /* Not valid.  */
00058        return EINVAL;
00059 
00060       idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
00061       idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
00062 
00063       /* This is the second level array.  Allocate it if necessary.  */
00064       level2 = THREAD_GETMEM_NC (self, specific, idx1st);
00065       if (level2 == NULL)
00066        {
00067          if (value == NULL)
00068            /* We don't have to do anything.  The value would in any case
00069               be NULL.  We can save the memory allocation.  */
00070            return 0;
00071 
00072          level2
00073            = (struct pthread_key_data *) calloc (PTHREAD_KEY_2NDLEVEL_SIZE,
00074                                             sizeof (*level2));
00075          if (level2 == NULL)
00076            return ENOMEM;
00077 
00078          THREAD_SETMEM_NC (self, specific, idx1st, level2);
00079        }
00080 
00081       /* Pointer to the right array element.  */
00082       level2 = &level2[idx2nd];
00083 
00084       /* Remember that we stored at least one set of data.  */
00085       THREAD_SETMEM (self, specific_used, true);
00086     }
00087 
00088   /* Store the data and the sequence number so that we can recognize
00089      stale data.  */
00090   level2->seq = seq;
00091   level2->data = (void *) value;
00092 
00093   return 0;
00094 }
00095 strong_alias (__pthread_setspecific, pthread_setspecific)
00096 strong_alias (__pthread_setspecific, __pthread_setspecific_internal)