]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/uclibc/lib/contrib/uclibc/libpthread/nptl/pthread_setspecific.c
Update
[l4.git] / l4 / pkg / l4re-core / uclibc / lib / contrib / uclibc / 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, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <stdlib.h>
21 #include "pthreadP.h"
22
23
24 int
25 attribute_protected
26 __pthread_setspecific (
27      pthread_key_t key,
28      const void *value)
29 {
30   struct pthread *self;
31   unsigned int idx1st;
32   unsigned int idx2nd;
33   struct pthread_key_data *level2;
34   unsigned int seq;
35
36   self = THREAD_SELF;
37
38   /* Special case access to the first 2nd-level block.  This is the
39      usual case.  */
40   if (__builtin_expect (key < PTHREAD_KEY_2NDLEVEL_SIZE, 1))
41     {
42       /* Verify the key is sane.  */
43       if (KEY_UNUSED ((seq = __pthread_keys[key].seq)))
44         /* Not valid.  */
45         return EINVAL;
46
47       level2 = &self->specific_1stblock[key];
48
49       /* Remember that we stored at least one set of data.  */
50       if (value != NULL)
51         THREAD_SETMEM (self, specific_used, true);
52     }
53   else
54     {
55       if (key >= PTHREAD_KEYS_MAX
56           || KEY_UNUSED ((seq = __pthread_keys[key].seq)))
57         /* Not valid.  */
58         return EINVAL;
59
60       idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
61       idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
62
63       /* This is the second level array.  Allocate it if necessary.  */
64       level2 = THREAD_GETMEM_NC (self, specific, idx1st);
65       if (level2 == NULL)
66         {
67           if (value == NULL)
68             /* We don't have to do anything.  The value would in any case
69                be NULL.  We can save the memory allocation.  */
70             return 0;
71
72           level2
73             = (struct pthread_key_data *) calloc (PTHREAD_KEY_2NDLEVEL_SIZE,
74                                                   sizeof (*level2));
75           if (level2 == NULL)
76             return ENOMEM;
77
78           THREAD_SETMEM_NC (self, specific, idx1st, level2);
79         }
80
81       /* Pointer to the right array element.  */
82       level2 = &level2[idx2nd];
83
84       /* Remember that we stored at least one set of data.  */
85       THREAD_SETMEM (self, specific_used, true);
86     }
87
88   /* Store the data and the sequence number so that we can recognize
89      stale data.  */
90   level2->seq = seq;
91   level2->data = (void *) value;
92
93   return 0;
94 }
95 strong_alias (__pthread_setspecific, pthread_setspecific)
96 strong_alias (__pthread_setspecific, __pthread_setspecific_internal)