1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Set Specific Key |
---|
5 | * @ingroup POSIXAPI |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * Copyright (c) 2012 Zhongwei Yao. |
---|
10 | * COPYRIGHT (c) 1989-2014. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.com/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #if HAVE_CONFIG_H |
---|
19 | #include "config.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <rtems/posix/keyimpl.h> |
---|
23 | #include <rtems/score/thread.h> |
---|
24 | #include <rtems/score/chainimpl.h> |
---|
25 | |
---|
26 | #include <errno.h> |
---|
27 | |
---|
28 | /* |
---|
29 | * 17.1.2 Thread-Specific Data Management, P1003.1c/Draft 10, p. 165 |
---|
30 | */ |
---|
31 | |
---|
32 | int pthread_setspecific( |
---|
33 | pthread_key_t key, |
---|
34 | const void *value |
---|
35 | ) |
---|
36 | { |
---|
37 | POSIX_Keys_Control *the_key; |
---|
38 | Objects_Locations location; |
---|
39 | POSIX_Keys_Key_value_pair *value_pair_ptr; |
---|
40 | |
---|
41 | the_key = _POSIX_Keys_Get( key, &location ); |
---|
42 | switch ( location ) { |
---|
43 | |
---|
44 | case OBJECTS_LOCAL: |
---|
45 | value_pair_ptr = _POSIX_Keys_Key_value_pair_allocate(); |
---|
46 | |
---|
47 | if ( !value_pair_ptr ) { |
---|
48 | _Objects_Put( &the_key->Object ); |
---|
49 | |
---|
50 | return ENOMEM; |
---|
51 | } |
---|
52 | |
---|
53 | value_pair_ptr->key = key; |
---|
54 | value_pair_ptr->thread_id = _Thread_Executing->Object.id; |
---|
55 | value_pair_ptr->value = value; |
---|
56 | if ( _RBTree_Insert( &_POSIX_Keys_Key_value_lookup_tree, |
---|
57 | &(value_pair_ptr->Key_value_lookup_node) ) ) { |
---|
58 | _Freechain_Put( (Freechain_Control *)&_POSIX_Keys_Keypool, |
---|
59 | (void *) value_pair_ptr ); |
---|
60 | _Objects_Put( &the_key->Object ); |
---|
61 | |
---|
62 | return EAGAIN; |
---|
63 | } |
---|
64 | |
---|
65 | /** append rb_node to the thread API extension's chain */ |
---|
66 | _Chain_Append_unprotected( |
---|
67 | &_Thread_Executing->Key_Chain, |
---|
68 | &value_pair_ptr->Key_values_per_thread_node |
---|
69 | ); |
---|
70 | |
---|
71 | _Objects_Put( &the_key->Object ); |
---|
72 | |
---|
73 | return 0; |
---|
74 | |
---|
75 | #if defined(RTEMS_MULTIPROCESSING) |
---|
76 | case OBJECTS_REMOTE: /* should never happen */ |
---|
77 | #endif |
---|
78 | case OBJECTS_ERROR: |
---|
79 | break; |
---|
80 | } |
---|
81 | |
---|
82 | return EINVAL; |
---|
83 | } |
---|