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

5
Last change on this file since da9f5f1 was da9f5f1, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/17 at 13:34:26

posix: Remove rtems_pthread_attribute_compare()

Update #2514.
Close #3174.

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