source: rtems/testsuites/psxtests/psxgetattrnp01/init.c @ d71542c

5
Last change on this file since d71542c was c4b8b147, checked in by Sebastian Huber <sebastian.huber@…>, on 11/03/17 at 07:35:38

tests: Use simple console driver

Update #3170.
Update #3199.

  • Property mode set to 100644
File size: 7.8 KB
Line 
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
21const char rtems_test_name[] = "PSXGETATTRNP 1";
22
23/* forward declarations to avoid warnings */
24void *POSIX_Init(void *argument);
25
26#if HAVE_DECL_PTHREAD_GETATTR_NP
27
28
29void *Thread_1(void *argument);
30
31pthread_t           Init_id;
32pthread_t           Thread_id;
33pthread_attr_t      Thread_attr;
34int                 max_priority;
35
36static int attribute_compare(
37  const pthread_attr_t *attr1,
38  const pthread_attr_t *attr2
39)
40{
41  if ( attr1->is_initialized  !=  attr2->is_initialized )
42    return 1;
43
44  if (
45    attr1->stackaddr != NULL &&
46      attr2->stackaddr != NULL &&
47      attr1->stackaddr != attr2->stackaddr )
48    return 1;
49
50  if (
51    attr1->stacksize != 0 &&
52      attr2->stacksize != 0 &&
53       attr1->stacksize != attr2->stacksize )
54    return 1;
55
56  if ( attr1->contentionscope != attr2->contentionscope )
57    return 1;
58
59  if ( attr1->inheritsched != attr2->inheritsched )
60    return 1;
61
62  if ( attr1->schedpolicy != attr2->schedpolicy )
63    return 1;
64
65  if ( attr1->schedparam.sched_priority != attr2->schedparam.sched_priority )
66    return 1;
67
68  #if HAVE_DECL_PTHREAD_ATTR_SETGUARDSIZE
69    if ( attr1->guardsize != attr2->guardsize )
70      return 1;
71  #endif
72
73  #if defined(_POSIX_THREAD_CPUTIME)
74    if ( attr1->cputime_clock_allowed != attr2->cputime_clock_allowed )
75      return 1;
76  #endif
77
78  if ( attr1->detachstate != attr2->detachstate )
79    return 1;
80
81  if ( attr1->affinitysetsize != attr2->affinitysetsize )
82    return 1;
83
84  if (!CPU_EQUAL_S(
85    attr1->affinitysetsize,
86    attr1->affinityset,
87    attr2->affinityset
88  ))
89    return 1;
90
91  if (!CPU_EQUAL_S(
92    attr1->affinitysetsize,
93    &attr1->affinitysetpreallocated,
94    &attr2->affinitysetpreallocated
95  ))
96    return 1;
97
98  return 0;
99}
100
101void *Thread_1(
102  void *argument
103)
104{
105  pthread_attr_t      attr;
106  struct sched_param  param;
107  int                 sc;
108  int                 value;
109  void               *stackaddr;
110  size_t              stacksize;
111  cpu_set_t           set;
112
113  puts("Thread - pthread_getattr_np - Verify value");
114  sc = pthread_getattr_np( Thread_id, &attr );
115  rtems_test_assert( sc == 0 );
116  rtems_test_assert( ! attribute_compare(&attr, &Thread_attr) );
117
118  param.sched_priority = max_priority;
119
120  puts( "Thread - pthread_setschedparam: Setting highest priority SCHED_FIFO" );
121  sc = pthread_setschedparam( Thread_id, SCHED_FIFO, &param );
122  rtems_test_assert( !sc );
123
124  puts("Thread - Detach");
125  sc = pthread_detach( Thread_id );
126  rtems_test_assert( !sc );
127
128  puts("Thread - pthread_getattr_np");
129  sc = pthread_getattr_np( Thread_id, &attr );
130  rtems_test_assert( !sc );
131
132  puts("Thread - Verify get stack");
133  stackaddr = NULL;
134  stacksize = 0;
135  sc = pthread_attr_getstack( &attr, &stackaddr, &stacksize );
136  rtems_test_assert( sc == 0 );
137  rtems_test_assert( stackaddr != NULL );
138  rtems_test_assert( stacksize != 0 );
139
140  puts("Thread - Verify contention scope");
141  sc = pthread_attr_getscope( &attr, &value );
142  rtems_test_assert( sc == 0 );
143  rtems_test_assert( value == PTHREAD_SCOPE_PROCESS );
144
145  puts("Thread - Verify explicit scheduler");
146  sc = pthread_attr_getinheritsched( &attr, &value );
147  rtems_test_assert( sc == 0 );
148  rtems_test_assert( value == PTHREAD_EXPLICIT_SCHED );
149
150  puts("Thread - Verify SCHED_FIFO policy");
151  sc = pthread_attr_getschedpolicy( &attr, &value );
152  rtems_test_assert( !sc );
153  rtems_test_assert( value == SCHED_FIFO );
154
155  puts("Thread - Verify max priority");
156  sc = pthread_attr_getschedparam( &attr, &param );
157  rtems_test_assert( !sc );
158  rtems_test_assert( param.sched_priority == max_priority );
159
160  puts("Thread - Verify detached");
161  sc = pthread_attr_getdetachstate( &attr, &value );
162  rtems_test_assert( sc == 0 );
163  rtems_test_assert( value == PTHREAD_CREATE_DETACHED );
164
165  puts("Thread - Verify affinity");
166  CPU_ZERO( &set );
167  sc = pthread_attr_getaffinity_np( &attr, sizeof( set ), &set );
168  rtems_test_assert( sc == 0 );
169  rtems_test_assert( CPU_ISSET( 0, &set ) );
170  rtems_test_assert( !CPU_ISSET( 1, &set ) );
171
172  puts("Thread - Destroy");
173  sc = pthread_attr_destroy( &attr );
174  rtems_test_assert( sc == 0 );
175
176  return NULL; /* just so the compiler thinks we returned something */
177}
178
179void *POSIX_Init(
180  void *ignored
181)
182{
183  int                 sc;
184  pthread_attr_t      attribute;
185  void               *stackaddr;
186  size_t              stacksize;
187  size_t              guardsize;
188  struct sched_param  param;
189  cpu_set_t           set;
190
191  TEST_BEGIN();
192
193  /* Initialize thread id */
194  Init_id = pthread_self();
195  max_priority = sched_get_priority_max( SCHED_FIFO );
196
197  puts( "Init - pthread_getattr_np - attr NULL - EINVAL" );
198  sc = pthread_getattr_np( Init_id, NULL );
199  rtems_test_assert( sc == EINVAL );
200
201  puts( "Init - pthread_getattr_np - invalid id - ESRCH" );
202  sc = pthread_getattr_np( 0xffff, &attribute );
203  rtems_test_assert( sc == ESRCH );
204
205
206  /* init task attributes */
207  puts("Init - pthread_attr_init");
208  sc = pthread_attr_init(&Thread_attr);
209  rtems_test_assert(!sc);
210
211  puts("Init - pthread_attr_setaffinity_np");
212  CPU_ZERO( &set );
213  CPU_SET( 0, &set );
214  sc = pthread_attr_setaffinity_np( &Thread_attr, sizeof( set ), &set );
215  rtems_test_assert(!sc);
216
217  puts("Init - pthread_attr_setinheritsched - PTHREAD_EXPLICIT_SCHED");
218  sc = pthread_attr_setinheritsched( &Thread_attr, PTHREAD_EXPLICIT_SCHED );
219  rtems_test_assert(!sc);
220  rtems_test_assert( Thread_attr.inheritsched == PTHREAD_EXPLICIT_SCHED );
221
222  puts("Init - pthread_attr_setschedpolicy to SCHED_RR");
223  sc = pthread_attr_setschedpolicy(&Thread_attr, SCHED_RR);
224  rtems_test_assert(!sc);
225
226  puts("Init - pthread_attr_setschedparam to minimum priority + 2");
227  param.sched_priority = sched_get_priority_min( SCHED_RR ) + 2;
228  sc = pthread_attr_setschedparam( &Thread_attr, &param );
229  rtems_test_assert(!sc);
230
231  puts("Init - pthread_attr_getstack");
232  sc = pthread_attr_getstack( &Thread_attr, &stackaddr, &stacksize );
233  rtems_test_assert(!sc);
234
235  stacksize *= 2;
236  puts("Init - pthread_attr_setstack double the stacksize");
237  sc = pthread_attr_setstacksize( &Thread_attr, stacksize );
238  rtems_test_assert(!sc);
239
240  puts("Init - pthread_attr_getguardsize");
241  sc = pthread_attr_getguardsize( &Thread_attr, &guardsize );
242  rtems_test_assert(!sc);
243
244  guardsize *= 2;
245  puts("Init - pthread_attr_setguardsize double the guardsize");
246  sc = pthread_attr_setguardsize( &Thread_attr, guardsize );
247  rtems_test_assert(!sc);
248
249  puts("Init - raise priority to max");
250  param.sched_priority = max_priority;
251  sc = pthread_setschedparam( Init_id, SCHED_RR, &param );
252  rtems_test_assert( !sc );
253
254 puts("Init - pthread_create");
255  sc = pthread_create( &Thread_id, &Thread_attr, Thread_1, NULL );
256  rtems_test_assert( !sc );
257
258  puts("Init - Lower priority");
259  fflush(stdout);
260  param.sched_priority = sched_get_priority_min( SCHED_RR );
261  sc = pthread_setschedparam( Init_id, SCHED_RR, &param );
262  rtems_test_assert(!sc);
263
264#if 0
265  sc = pthread_join( Thread_id, NULL );
266  rtems_test_assert( !sc );
267#endif
268
269  TEST_END();
270  rtems_test_exit(0);
271  return NULL; /* just so the compiler thinks we returned something */
272}
273#else
274void *POSIX_Init(
275  void *ignored
276)
277{
278  TEST_BEGIN();
279  puts( "  pthread_getattr_np NOT supported" );
280  TEST_END();
281  rtems_test_exit(0);
282  return NULL; /* just so the compiler thinks we returned something */
283}
284
285#endif
286/* configuration information */
287
288#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
289#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
290
291#define CONFIGURE_MAXIMUM_POSIX_THREADS  2
292
293#define CONFIGURE_POSIX_INIT_THREAD_TABLE
294
295#define CONFIGURE_INIT
296#include <rtems/confdefs.h>
297
298/* global variables */
Note: See TracBrowser for help on using the repository browser.