OSDN Git Service

13b24dcfaf31c29ee1bc5fe18889ae67ff3655b5
[uclinux-h8/uClibc.git] / libpthread / nptl / pthread_setspecific.c
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
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <stdlib.h>
22 #include "pthreadP.h"
23
24
25 int
26 attribute_protected
27 __pthread_setspecific (
28      pthread_key_t key,
29      const void *value)
30 {
31   struct pthread *self;
32   unsigned int idx1st;
33   unsigned int idx2nd;
34   struct pthread_key_data *level2;
35   unsigned int seq;
36
37   self = THREAD_SELF;
38
39   /* Special case access to the first 2nd-level block.  This is the
40      usual case.  */
41   if (__builtin_expect (key < PTHREAD_KEY_2NDLEVEL_SIZE, 1))
42     {
43       /* Verify the key is sane.  */
44       if (KEY_UNUSED ((seq = __pthread_keys[key].seq)))
45         /* Not valid.  */
46         return EINVAL;
47
48       level2 = &self->specific_1stblock[key];
49
50       /* Remember that we stored at least one set of data.  */
51       if (value != NULL)
52         THREAD_SETMEM (self, specific_used, true);
53     }
54   else
55     {
56       if (key >= PTHREAD_KEYS_MAX
57           || KEY_UNUSED ((seq = __pthread_keys[key].seq)))
58         /* Not valid.  */
59         return EINVAL;
60
61       idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
62       idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
63
64       /* This is the second level array.  Allocate it if necessary.  */
65       level2 = THREAD_GETMEM_NC (self, specific, idx1st);
66       if (level2 == NULL)
67         {
68           if (value == NULL)
69             /* We don't have to do anything.  The value would in any case
70                be NULL.  We can save the memory allocation.  */
71             return 0;
72
73           level2
74             = (struct pthread_key_data *) calloc (PTHREAD_KEY_2NDLEVEL_SIZE,
75                                                   sizeof (*level2));
76           if (level2 == NULL)
77             return ENOMEM;
78
79           THREAD_SETMEM_NC (self, specific, idx1st, level2);
80         }
81
82       /* Pointer to the right array element.  */
83       level2 = &level2[idx2nd];
84
85       /* Remember that we stored at least one set of data.  */
86       THREAD_SETMEM (self, specific_used, true);
87     }
88
89   /* Store the data and the sequence number so that we can recognize
90      stale data.  */
91   level2->seq = seq;
92   level2->data = (void *) value;
93
94   return 0;
95 }
96 strong_alias (__pthread_setspecific, pthread_setspecific)
97 strong_alias (__pthread_setspecific, __pthread_setspecific_internal)