1 | /* |
---|
2 | * Copyright (c) 2012 Zhongwei Yao. |
---|
3 | * COPYRIGHT (c) 1989-2014. |
---|
4 | * On-Line Applications Research Corporation (OAR). |
---|
5 | * |
---|
6 | * The license and distribution terms for this file may be |
---|
7 | * found in the file LICENSE in this distribution or at |
---|
8 | * http://www.rtems.org/license/LICENSE. |
---|
9 | */ |
---|
10 | |
---|
11 | #ifdef HAVE_CONFIG_H |
---|
12 | #include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <pthread.h> |
---|
16 | #include <sched.h> |
---|
17 | #include <errno.h> |
---|
18 | #include "tmacros.h" |
---|
19 | #include "pmacros.h" |
---|
20 | |
---|
21 | const char rtems_test_name[] = "PSXKEY 7"; |
---|
22 | |
---|
23 | /* forward declarations to avoid warnings */ |
---|
24 | rtems_task Init(rtems_task_argument argument); |
---|
25 | rtems_task Test_Thread(rtems_task_argument argument); |
---|
26 | |
---|
27 | pthread_key_t Key; |
---|
28 | int created_thread_count, setted_thread_count, got_thread_count; |
---|
29 | int all_thread_created; |
---|
30 | pthread_mutex_t mutex1, mutex2; |
---|
31 | pthread_cond_t create_condition_var, set_condition_var; |
---|
32 | |
---|
33 | rtems_task Test_Thread(rtems_task_argument argument) |
---|
34 | { |
---|
35 | int sc; |
---|
36 | int *value_p, *value_p2; |
---|
37 | |
---|
38 | value_p = malloc( sizeof( int ) ); |
---|
39 | sc = pthread_setspecific( Key, value_p ); |
---|
40 | rtems_test_assert( !sc ); |
---|
41 | |
---|
42 | pthread_mutex_lock( &mutex1 ); |
---|
43 | ++setted_thread_count; |
---|
44 | pthread_cond_signal( &set_condition_var ); |
---|
45 | pthread_mutex_unlock( &mutex1 ); |
---|
46 | |
---|
47 | /** |
---|
48 | * blocked untill all threads have been created. |
---|
49 | */ |
---|
50 | pthread_mutex_lock( &mutex2 ); |
---|
51 | while( !all_thread_created ) |
---|
52 | pthread_cond_wait( &create_condition_var, &mutex2 ); |
---|
53 | pthread_mutex_unlock( &mutex2 ); |
---|
54 | |
---|
55 | value_p2 = pthread_getspecific( Key ); |
---|
56 | rtems_test_assert( value_p == value_p2 ); |
---|
57 | ++got_thread_count; |
---|
58 | |
---|
59 | rtems_task_delete( RTEMS_SELF ); |
---|
60 | } |
---|
61 | |
---|
62 | rtems_task Init(rtems_task_argument argument) |
---|
63 | { |
---|
64 | rtems_status_code rc; |
---|
65 | int sc; |
---|
66 | struct timespec delay_request; |
---|
67 | uintptr_t max_free_size = 13 * RTEMS_MINIMUM_STACK_SIZE; |
---|
68 | void *greedy; |
---|
69 | |
---|
70 | all_thread_created = 0; |
---|
71 | |
---|
72 | TEST_BEGIN(); |
---|
73 | |
---|
74 | puts( "Init - Mutex 1 create - OK" ); |
---|
75 | sc = pthread_mutex_init( &mutex1, NULL ); |
---|
76 | rtems_test_assert( !sc ); |
---|
77 | |
---|
78 | puts( "Init - Mutex 2 create - OK" ); |
---|
79 | sc = pthread_mutex_init( &mutex2, NULL ); |
---|
80 | rtems_test_assert( !sc ); |
---|
81 | |
---|
82 | puts( "Init - Condition variable 1 create - OK" ); |
---|
83 | sc = pthread_cond_init( &create_condition_var, NULL ); |
---|
84 | rtems_test_assert( !sc ); |
---|
85 | |
---|
86 | puts( "Init - Condition variable 2 create - OK" ); |
---|
87 | sc = pthread_cond_init( &set_condition_var, NULL ); |
---|
88 | rtems_test_assert( !sc ); |
---|
89 | |
---|
90 | puts( "Init - pthread Key create - OK" ); |
---|
91 | sc = pthread_key_create( &Key, NULL ); |
---|
92 | rtems_test_assert( !sc ); |
---|
93 | |
---|
94 | /* Reduce workspace size if necessary to shorten test time */ |
---|
95 | greedy = rtems_workspace_greedy_allocate( &max_free_size, 1 ); |
---|
96 | |
---|
97 | for ( ; ; ) { |
---|
98 | rtems_id task_id; |
---|
99 | |
---|
100 | pthread_mutex_lock( &mutex1 ); |
---|
101 | |
---|
102 | rc = rtems_task_create( |
---|
103 | rtems_build_name( 'T', 'E', 'S', 'T' ), |
---|
104 | 1, |
---|
105 | RTEMS_MINIMUM_STACK_SIZE, |
---|
106 | RTEMS_DEFAULT_MODES, |
---|
107 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
108 | &task_id |
---|
109 | ); |
---|
110 | rtems_test_assert( |
---|
111 | ( rc == RTEMS_SUCCESSFUL ) || ( rc == RTEMS_UNSATISFIED ) |
---|
112 | || ( rc == RTEMS_TOO_MANY ) |
---|
113 | ); |
---|
114 | |
---|
115 | if ( rc == RTEMS_SUCCESSFUL ) { |
---|
116 | rc = rtems_task_start( task_id, Test_Thread, 0 ); |
---|
117 | rtems_test_assert( rc == RTEMS_SUCCESSFUL ); |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * check if return is not successful, it means RTEMS Workspace RAM |
---|
122 | * have been exhausted. |
---|
123 | */ |
---|
124 | if ( rc != RTEMS_SUCCESSFUL ) { |
---|
125 | pthread_mutex_unlock( &mutex1 ); |
---|
126 | break; |
---|
127 | } |
---|
128 | ++created_thread_count; |
---|
129 | |
---|
130 | /** |
---|
131 | * wait for test thread set key, the while loop here is used to |
---|
132 | * avoid suprious wakeup. |
---|
133 | */ |
---|
134 | while( created_thread_count > setted_thread_count ) |
---|
135 | pthread_cond_wait( &set_condition_var, &mutex1 ); |
---|
136 | pthread_mutex_unlock( &mutex1 ); |
---|
137 | } |
---|
138 | |
---|
139 | rtems_workspace_greedy_free( greedy ); |
---|
140 | |
---|
141 | printf( |
---|
142 | "Init - %d pthreads have been created - OK\n" |
---|
143 | "Init - %d pthreads have been setted key data - OK\n", |
---|
144 | created_thread_count, |
---|
145 | setted_thread_count |
---|
146 | ); |
---|
147 | rtems_test_assert( created_thread_count == setted_thread_count ); |
---|
148 | |
---|
149 | /* unblock all created pthread to let them set key data.*/ |
---|
150 | pthread_mutex_lock( &mutex2 ); |
---|
151 | all_thread_created = 1; |
---|
152 | pthread_cond_broadcast( &create_condition_var ); |
---|
153 | pthread_mutex_unlock( &mutex2 ); |
---|
154 | |
---|
155 | puts( "Init - sleep - let threads run - OK" ); |
---|
156 | delay_request.tv_sec = 0; |
---|
157 | delay_request.tv_nsec = 8 * 100000000; |
---|
158 | sc = nanosleep( &delay_request, NULL ); |
---|
159 | rtems_test_assert( !sc ); |
---|
160 | |
---|
161 | printf( |
---|
162 | "Init - %d pthreads have been got key data - OK\n", |
---|
163 | got_thread_count |
---|
164 | ); |
---|
165 | rtems_test_assert( created_thread_count == got_thread_count ); |
---|
166 | |
---|
167 | puts( "Init - pthread Key delete - OK" ); |
---|
168 | sc = pthread_key_delete( Key ); |
---|
169 | rtems_test_assert( sc == 0 ); |
---|
170 | |
---|
171 | puts( "Init - Mutex1 delete - OK" ); |
---|
172 | sc = pthread_mutex_destroy( &mutex1 ); |
---|
173 | rtems_test_assert( !sc ); |
---|
174 | |
---|
175 | puts( "Init - Mutex2 delete - OK" ); |
---|
176 | sc = pthread_mutex_destroy( &mutex2 ); |
---|
177 | rtems_test_assert( !sc ); |
---|
178 | |
---|
179 | puts( "Init - Condition variable 1 delete - OK" ); |
---|
180 | sc = pthread_cond_destroy( &create_condition_var ); |
---|
181 | rtems_test_assert( !sc ); |
---|
182 | |
---|
183 | puts( "Init - Condition variable 2 delete - OK" ); |
---|
184 | sc = pthread_cond_destroy( &set_condition_var ); |
---|
185 | rtems_test_assert( !sc ); |
---|
186 | |
---|
187 | TEST_END(); |
---|
188 | rtems_test_exit(0); |
---|
189 | } |
---|
190 | |
---|
191 | /* configuration information */ |
---|
192 | |
---|
193 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
194 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
195 | |
---|
196 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
197 | |
---|
198 | #define CONFIGURE_MAXIMUM_TASKS rtems_resource_unlimited(10) |
---|
199 | #define CONFIGURE_MAXIMUM_POSIX_MUTEXES 2 |
---|
200 | #define CONFIGURE_MAXIMUM_POSIX_KEYS 1 |
---|
201 | #define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 2 |
---|
202 | #define CONFIGURE_UNIFIED_WORK_AREAS |
---|
203 | |
---|
204 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
205 | |
---|
206 | #define CONFIGURE_INIT |
---|
207 | #include <rtems/confdefs.h> |
---|
208 | |
---|
209 | /* global variables */ |
---|