1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2014. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #define _GNU_SOURCE |
---|
15 | #include <tmacros.h> |
---|
16 | #include <errno.h> |
---|
17 | #include <sched.h> |
---|
18 | #include <pthread.h> |
---|
19 | #include <rtems/posix/pthreadimpl.h> |
---|
20 | |
---|
21 | const char rtems_test_name[] = "PSXGETATTRNP 1"; |
---|
22 | |
---|
23 | /* forward declarations to avoid warnings */ |
---|
24 | void *POSIX_Init(void *argument); |
---|
25 | |
---|
26 | #if HAVE_DECL_PTHREAD_GETATTR_NP |
---|
27 | |
---|
28 | |
---|
29 | void *Thread_1(void *argument); |
---|
30 | |
---|
31 | pthread_t Init_id; |
---|
32 | pthread_t Thread_id; |
---|
33 | pthread_attr_t Thread_attr; |
---|
34 | int max_priority; |
---|
35 | |
---|
36 | void *Thread_1( |
---|
37 | void *argument |
---|
38 | ) |
---|
39 | { |
---|
40 | pthread_attr_t attr; |
---|
41 | struct sched_param param; |
---|
42 | int sc; |
---|
43 | int value; |
---|
44 | void *stackaddr; |
---|
45 | size_t stacksize; |
---|
46 | cpu_set_t set; |
---|
47 | |
---|
48 | puts("Thread - pthread_getattr_np - Verify value"); |
---|
49 | sc = pthread_getattr_np( Thread_id, &attr ); |
---|
50 | rtems_test_assert( sc == 0 ); |
---|
51 | rtems_test_assert( ! rtems_pthread_attribute_compare(&attr, &Thread_attr) ); |
---|
52 | |
---|
53 | param.sched_priority = max_priority; |
---|
54 | |
---|
55 | puts( "Thread - pthread_setschedparam: Setting highest priority SCHED_FIFO" ); |
---|
56 | sc = pthread_setschedparam( Thread_id, SCHED_FIFO, ¶m ); |
---|
57 | rtems_test_assert( !sc ); |
---|
58 | |
---|
59 | puts("Thread - Detach"); |
---|
60 | sc = pthread_detach( Thread_id ); |
---|
61 | rtems_test_assert( !sc ); |
---|
62 | |
---|
63 | puts("Thread - pthread_getattr_np"); |
---|
64 | sc = pthread_getattr_np( Thread_id, &attr ); |
---|
65 | rtems_test_assert( !sc ); |
---|
66 | |
---|
67 | puts("Thread - Verify get stack"); |
---|
68 | stackaddr = NULL; |
---|
69 | stacksize = 0; |
---|
70 | sc = pthread_attr_getstack( &attr, &stackaddr, &stacksize ); |
---|
71 | rtems_test_assert( sc == 0 ); |
---|
72 | rtems_test_assert( stackaddr != NULL ); |
---|
73 | rtems_test_assert( stacksize != 0 ); |
---|
74 | |
---|
75 | puts("Thread - Verify contention scope"); |
---|
76 | sc = pthread_attr_getscope( &attr, &value ); |
---|
77 | rtems_test_assert( sc == 0 ); |
---|
78 | rtems_test_assert( value == PTHREAD_SCOPE_PROCESS ); |
---|
79 | |
---|
80 | puts("Thread - Verify explicit scheduler"); |
---|
81 | sc = pthread_attr_getinheritsched( &attr, &value ); |
---|
82 | rtems_test_assert( sc == 0 ); |
---|
83 | rtems_test_assert( value == PTHREAD_EXPLICIT_SCHED ); |
---|
84 | |
---|
85 | puts("Thread - Verify SCHED_FIFO policy"); |
---|
86 | sc = pthread_attr_getschedpolicy( &attr, &value ); |
---|
87 | rtems_test_assert( !sc ); |
---|
88 | rtems_test_assert( value == SCHED_FIFO ); |
---|
89 | |
---|
90 | puts("Thread - Verify max priority"); |
---|
91 | sc = pthread_attr_getschedparam( &attr, ¶m ); |
---|
92 | rtems_test_assert( !sc ); |
---|
93 | rtems_test_assert( param.sched_priority == max_priority ); |
---|
94 | |
---|
95 | puts("Thread - Verify detached"); |
---|
96 | sc = pthread_attr_getdetachstate( &attr, &value ); |
---|
97 | rtems_test_assert( sc == 0 ); |
---|
98 | rtems_test_assert( value == PTHREAD_CREATE_DETACHED ); |
---|
99 | |
---|
100 | puts("Thread - Verify affinity"); |
---|
101 | CPU_ZERO( &set ); |
---|
102 | sc = pthread_attr_getaffinity_np( &attr, sizeof( set ), &set ); |
---|
103 | rtems_test_assert( sc == 0 ); |
---|
104 | rtems_test_assert( CPU_ISSET( 0, &set ) ); |
---|
105 | rtems_test_assert( !CPU_ISSET( 1, &set ) ); |
---|
106 | |
---|
107 | puts("Thread - Destroy"); |
---|
108 | sc = pthread_attr_destroy( &attr ); |
---|
109 | rtems_test_assert( sc == 0 ); |
---|
110 | |
---|
111 | return NULL; /* just so the compiler thinks we returned something */ |
---|
112 | } |
---|
113 | |
---|
114 | void *POSIX_Init( |
---|
115 | void *ignored |
---|
116 | ) |
---|
117 | { |
---|
118 | int sc; |
---|
119 | pthread_attr_t attribute; |
---|
120 | void *stackaddr; |
---|
121 | size_t stacksize; |
---|
122 | size_t guardsize; |
---|
123 | struct sched_param param; |
---|
124 | |
---|
125 | TEST_BEGIN(); |
---|
126 | |
---|
127 | /* Initialize thread id */ |
---|
128 | Init_id = pthread_self(); |
---|
129 | max_priority = sched_get_priority_max( SCHED_FIFO ); |
---|
130 | |
---|
131 | puts( "Init - pthread_getattr_np - attr NULL - EINVAL" ); |
---|
132 | sc = pthread_getattr_np( Init_id, NULL ); |
---|
133 | rtems_test_assert( sc == EINVAL ); |
---|
134 | |
---|
135 | puts( "Init - pthread_getattr_np - invalid id - ESRCH" ); |
---|
136 | sc = pthread_getattr_np( 0xffff, &attribute ); |
---|
137 | rtems_test_assert( sc == ESRCH ); |
---|
138 | |
---|
139 | |
---|
140 | /* init task attributes */ |
---|
141 | puts("Init - pthread_attr_init"); |
---|
142 | sc = pthread_attr_init(&Thread_attr); |
---|
143 | rtems_test_assert(!sc); |
---|
144 | |
---|
145 | puts("Init - pthread_attr_setinheritsched - PTHREAD_EXPLICIT_SCHED"); |
---|
146 | sc = pthread_attr_setinheritsched( &Thread_attr, PTHREAD_EXPLICIT_SCHED ); |
---|
147 | rtems_test_assert(!sc); |
---|
148 | rtems_test_assert( Thread_attr.inheritsched == PTHREAD_EXPLICIT_SCHED ); |
---|
149 | |
---|
150 | puts("Init - pthread_attr_setschedpolicy to SCHED_RR"); |
---|
151 | sc = pthread_attr_setschedpolicy(&Thread_attr, SCHED_RR); |
---|
152 | rtems_test_assert(!sc); |
---|
153 | |
---|
154 | puts("Init - pthread_attr_setschedparam to minimum priority + 2"); |
---|
155 | param.sched_priority = sched_get_priority_min( SCHED_RR ) + 2; |
---|
156 | sc = pthread_attr_setschedparam( &Thread_attr, ¶m ); |
---|
157 | rtems_test_assert(!sc); |
---|
158 | |
---|
159 | puts("Init - pthread_attr_getstack"); |
---|
160 | sc = pthread_attr_getstack( &Thread_attr, &stackaddr, &stacksize ); |
---|
161 | rtems_test_assert(!sc); |
---|
162 | |
---|
163 | stacksize *= 2; |
---|
164 | puts("Init - pthread_attr_setstack double the stacksize"); |
---|
165 | sc = pthread_attr_setstacksize( &Thread_attr, stacksize ); |
---|
166 | rtems_test_assert(!sc); |
---|
167 | |
---|
168 | puts("Init - pthread_attr_getguardsize"); |
---|
169 | sc = pthread_attr_getguardsize( &Thread_attr, &guardsize ); |
---|
170 | rtems_test_assert(!sc); |
---|
171 | |
---|
172 | guardsize *= 2; |
---|
173 | puts("Init - pthread_attr_setguardsize double the guardsize"); |
---|
174 | sc = pthread_attr_setguardsize( &Thread_attr, guardsize ); |
---|
175 | rtems_test_assert(!sc); |
---|
176 | |
---|
177 | puts("Init - raise priority to max"); |
---|
178 | param.sched_priority = max_priority; |
---|
179 | sc = pthread_setschedparam( Init_id, SCHED_RR, ¶m ); |
---|
180 | rtems_test_assert( !sc ); |
---|
181 | |
---|
182 | puts("Init - pthread_create"); |
---|
183 | sc = pthread_create( &Thread_id, &Thread_attr, Thread_1, NULL ); |
---|
184 | rtems_test_assert( !sc ); |
---|
185 | |
---|
186 | puts("Init - Lower priority"); |
---|
187 | fflush(stdout); |
---|
188 | param.sched_priority = sched_get_priority_min( SCHED_RR ); |
---|
189 | sc = pthread_setschedparam( Init_id, SCHED_RR, ¶m ); |
---|
190 | rtems_test_assert(!sc); |
---|
191 | |
---|
192 | #if 0 |
---|
193 | sc = pthread_join( Thread_id, NULL ); |
---|
194 | rtems_test_assert( !sc ); |
---|
195 | #endif |
---|
196 | |
---|
197 | TEST_END(); |
---|
198 | rtems_test_exit(0); |
---|
199 | return NULL; /* just so the compiler thinks we returned something */ |
---|
200 | } |
---|
201 | #else |
---|
202 | void *POSIX_Init( |
---|
203 | void *ignored |
---|
204 | ) |
---|
205 | { |
---|
206 | TEST_BEGIN(); |
---|
207 | puts( " pthread_getattr_np NOT supported" ); |
---|
208 | TEST_END(); |
---|
209 | rtems_test_exit(0); |
---|
210 | return NULL; /* just so the compiler thinks we returned something */ |
---|
211 | } |
---|
212 | |
---|
213 | #endif |
---|
214 | /* configuration information */ |
---|
215 | |
---|
216 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
217 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
218 | |
---|
219 | #define CONFIGURE_MAXIMUM_POSIX_THREADS 2 |
---|
220 | |
---|
221 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
222 | |
---|
223 | #define CONFIGURE_INIT |
---|
224 | #include <rtems/confdefs.h> |
---|
225 | |
---|
226 | /* global variables */ |
---|