winlin

reserach st: refine key.c

@@ -56,51 +56,52 @@ static int key_max = 0; @@ -56,51 +56,52 @@ static int key_max = 0;
56 */ 56 */
57 int st_key_create(int *keyp, _st_destructor_t destructor) 57 int st_key_create(int *keyp, _st_destructor_t destructor)
58 { 58 {
59 - if (key_max >= ST_KEYS_MAX) {  
60 - errno = EAGAIN;  
61 - return -1;  
62 - }  
63 -  
64 - *keyp = key_max++;  
65 - _st_destructors[*keyp] = destructor;  
66 -  
67 - return 0; 59 + if (key_max >= ST_KEYS_MAX) {
  60 + errno = EAGAIN;
  61 + return -1;
  62 + }
  63 +
  64 + *keyp = key_max++;
  65 + _st_destructors[*keyp] = destructor;
  66 +
  67 + return 0;
68 } 68 }
69 69
70 70
71 int st_key_getlimit(void) 71 int st_key_getlimit(void)
72 { 72 {
73 - return ST_KEYS_MAX; 73 + return ST_KEYS_MAX;
74 } 74 }
75 75
76 76
77 int st_thread_setspecific(int key, void *value) 77 int st_thread_setspecific(int key, void *value)
78 { 78 {
79 - _st_thread_t *me = _ST_CURRENT_THREAD();  
80 -  
81 - if (key < 0 || key >= key_max) {  
82 - errno = EINVAL;  
83 - return -1;  
84 - }  
85 -  
86 - if (value != me->private_data[key]) {  
87 - /* free up previously set non-NULL data value */  
88 - if (me->private_data[key] && _st_destructors[key]) {  
89 - (*_st_destructors[key])(me->private_data[key]); 79 + _st_thread_t *me = _ST_CURRENT_THREAD();
  80 +
  81 + if (key < 0 || key >= key_max) {
  82 + errno = EINVAL;
  83 + return -1;
90 } 84 }
91 - me->private_data[key] = value;  
92 - }  
93 -  
94 - return 0; 85 +
  86 + if (value != me->private_data[key]) {
  87 + /* free up previously set non-NULL data value */
  88 + if (me->private_data[key] && _st_destructors[key]) {
  89 + (*_st_destructors[key])(me->private_data[key]);
  90 + }
  91 + me->private_data[key] = value;
  92 + }
  93 +
  94 + return 0;
95 } 95 }
96 96
97 97
98 void *st_thread_getspecific(int key) 98 void *st_thread_getspecific(int key)
99 { 99 {
100 - if (key < 0 || key >= key_max)  
101 - return NULL;  
102 -  
103 - return ((_ST_CURRENT_THREAD())->private_data[key]); 100 + if (key < 0 || key >= key_max) {
  101 + return NULL;
  102 + }
  103 +
  104 + return ((_ST_CURRENT_THREAD())->private_data[key]);
104 } 105 }
105 106
106 107
@@ -109,13 +110,13 @@ void *st_thread_getspecific(int key) @@ -109,13 +110,13 @@ void *st_thread_getspecific(int key)
109 */ 110 */
110 void _st_thread_cleanup(_st_thread_t *thread) 111 void _st_thread_cleanup(_st_thread_t *thread)
111 { 112 {
112 - int key;  
113 -  
114 - for (key = 0; key < key_max; key++) {  
115 - if (thread->private_data[key] && _st_destructors[key]) {  
116 - (*_st_destructors[key])(thread->private_data[key]);  
117 - thread->private_data[key] = NULL; 113 + int key;
  114 +
  115 + for (key = 0; key < key_max; key++) {
  116 + if (thread->private_data[key] && _st_destructors[key]) {
  117 + (*_st_destructors[key])(thread->private_data[key]);
  118 + thread->private_data[key] = NULL;
  119 + }
118 } 120 }
119 - }  
120 } 121 }
121 122