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

Last change on this file was 9e07bcc, checked in by Joel Sherrill <joel@…>, on 04/07/22 at 15:04:59

testsuites/psxtests/psx[g-m1]*: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 8.7 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 *  COPYRIGHT (c) 1989-2014.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#define _GNU_SOURCE
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include <tmacros.h>
36#include <errno.h>
37#include <sched.h>
38#include <pthread.h>
39#include <rtems/posix/pthreadimpl.h>
40
41const char rtems_test_name[] = "PSXGETATTRNP 1";
42
43/* forward declarations to avoid warnings */
44void *POSIX_Init(void *argument);
45
46void *Thread_1(void *argument);
47
48pthread_t           Init_id;
49pthread_t           Thread_id;
50pthread_attr_t      Thread_attr;
51int                 max_priority;
52
53static int attribute_compare(
54  const pthread_attr_t *attr1,
55  const pthread_attr_t *attr2
56)
57{
58  if ( attr1->is_initialized  !=  attr2->is_initialized )
59    return 1;
60
61  if (
62    attr1->stackaddr != NULL &&
63      attr2->stackaddr != NULL &&
64      attr1->stackaddr != attr2->stackaddr )
65    return 1;
66
67  if (
68    attr1->stacksize != 0 &&
69      attr2->stacksize != 0 &&
70       attr1->stacksize != attr2->stacksize )
71    return 1;
72
73  if ( attr1->contentionscope != attr2->contentionscope )
74    return 1;
75
76  if ( attr1->inheritsched != attr2->inheritsched )
77    return 1;
78
79  if ( attr1->schedpolicy != attr2->schedpolicy )
80    return 1;
81
82  if ( attr1->schedparam.sched_priority != attr2->schedparam.sched_priority )
83    return 1;
84
85  if ( attr1->guardsize != attr2->guardsize )
86    return 1;
87
88  #if defined(_POSIX_THREAD_CPUTIME)
89    if ( attr1->cputime_clock_allowed != attr2->cputime_clock_allowed )
90      return 1;
91  #endif
92
93  if ( attr1->detachstate != attr2->detachstate )
94    return 1;
95
96  if ( attr1->affinitysetsize != attr2->affinitysetsize )
97    return 1;
98
99  if (!CPU_EQUAL_S(
100    attr1->affinitysetsize,
101    attr1->affinityset,
102    attr2->affinityset
103  ))
104    return 1;
105
106  if (!CPU_EQUAL_S(
107    attr1->affinitysetsize,
108    &attr1->affinitysetpreallocated,
109    &attr2->affinitysetpreallocated
110  ))
111    return 1;
112
113  return 0;
114}
115
116void *Thread_1(
117  void *argument
118)
119{
120  pthread_attr_t      attr;
121  struct sched_param  param;
122  int                 sc;
123  int                 value;
124  void               *stackaddr;
125  size_t              stacksize;
126  cpu_set_t           set;
127
128  puts("Thread - pthread_getattr_np - Verify value");
129  sc = pthread_getattr_np( Thread_id, &attr );
130  rtems_test_assert( sc == 0 );
131  rtems_test_assert( ! attribute_compare(&attr, &Thread_attr) );
132
133  param.sched_priority = max_priority;
134
135  puts( "Thread - pthread_setschedparam: Setting highest priority SCHED_FIFO" );
136  sc = pthread_setschedparam( Thread_id, SCHED_FIFO, &param );
137  rtems_test_assert( !sc );
138
139  puts("Thread - Detach");
140  sc = pthread_detach( Thread_id );
141  rtems_test_assert( !sc );
142
143  puts("Thread - pthread_getattr_np");
144  sc = pthread_getattr_np( Thread_id, &attr );
145  rtems_test_assert( !sc );
146
147  puts("Thread - Verify get stack");
148  stackaddr = NULL;
149  stacksize = 0;
150  sc = pthread_attr_getstack( &attr, &stackaddr, &stacksize );
151  rtems_test_assert( sc == 0 );
152  rtems_test_assert( stackaddr != NULL );
153  rtems_test_assert( stacksize != 0 );
154
155  puts("Thread - Verify contention scope");
156  sc = pthread_attr_getscope( &attr, &value );
157  rtems_test_assert( sc == 0 );
158  rtems_test_assert( value == PTHREAD_SCOPE_PROCESS );
159
160  puts("Thread - Verify explicit scheduler");
161  sc = pthread_attr_getinheritsched( &attr, &value );
162  rtems_test_assert( sc == 0 );
163  rtems_test_assert( value == PTHREAD_EXPLICIT_SCHED );
164
165  puts("Thread - Verify SCHED_FIFO policy");
166  sc = pthread_attr_getschedpolicy( &attr, &value );
167  rtems_test_assert( !sc );
168  rtems_test_assert( value == SCHED_FIFO );
169
170  puts("Thread - Verify max priority");
171  sc = pthread_attr_getschedparam( &attr, &param );
172  rtems_test_assert( !sc );
173  rtems_test_assert( param.sched_priority == max_priority );
174
175  puts("Thread - Verify detached");
176  sc = pthread_attr_getdetachstate( &attr, &value );
177  rtems_test_assert( sc == 0 );
178  rtems_test_assert( value == PTHREAD_CREATE_DETACHED );
179
180  puts("Thread - Verify affinity");
181  CPU_ZERO( &set );
182  sc = pthread_attr_getaffinity_np( &attr, sizeof( set ), &set );
183  rtems_test_assert( sc == 0 );
184  rtems_test_assert( CPU_ISSET( 0, &set ) );
185  rtems_test_assert( !CPU_ISSET( 1, &set ) );
186
187  puts("Thread - Destroy");
188  sc = pthread_attr_destroy( &attr );
189  rtems_test_assert( sc == 0 );
190
191  return NULL; /* just so the compiler thinks we returned something */
192}
193
194void *POSIX_Init(
195  void *ignored
196)
197{
198  int                 sc;
199  pthread_attr_t      attribute;
200  void               *stackaddr;
201  size_t              stacksize;
202  size_t              guardsize;
203  struct sched_param  param;
204  cpu_set_t           set;
205
206  TEST_BEGIN();
207
208  /* Initialize thread id */
209  Init_id = pthread_self();
210  max_priority = sched_get_priority_max( SCHED_FIFO );
211
212  puts( "Init - pthread_getattr_np - attr NULL - EINVAL" );
213  sc = pthread_getattr_np( Init_id, NULL );
214  rtems_test_assert( sc == EINVAL );
215
216  puts( "Init - pthread_getattr_np - invalid id - ESRCH" );
217  sc = pthread_getattr_np( 0xffff, &attribute );
218  rtems_test_assert( sc == ESRCH );
219
220
221  /* init task attributes */
222  puts("Init - pthread_attr_init");
223  sc = pthread_attr_init(&Thread_attr);
224  rtems_test_assert(!sc);
225
226  puts("Init - pthread_attr_setaffinity_np");
227  CPU_ZERO( &set );
228  CPU_SET( 0, &set );
229  sc = pthread_attr_setaffinity_np( &Thread_attr, sizeof( set ), &set );
230  rtems_test_assert(!sc);
231
232  puts("Init - pthread_attr_setinheritsched - PTHREAD_EXPLICIT_SCHED");
233  sc = pthread_attr_setinheritsched( &Thread_attr, PTHREAD_EXPLICIT_SCHED );
234  rtems_test_assert(!sc);
235  rtems_test_assert( Thread_attr.inheritsched == PTHREAD_EXPLICIT_SCHED );
236
237  puts("Init - pthread_attr_setschedpolicy to SCHED_RR");
238  sc = pthread_attr_setschedpolicy(&Thread_attr, SCHED_RR);
239  rtems_test_assert(!sc);
240
241  puts("Init - pthread_attr_setschedparam to minimum priority + 2");
242  param.sched_priority = sched_get_priority_min( SCHED_RR ) + 2;
243  sc = pthread_attr_setschedparam( &Thread_attr, &param );
244  rtems_test_assert(!sc);
245
246  puts("Init - pthread_attr_getstack");
247  sc = pthread_attr_getstack( &Thread_attr, &stackaddr, &stacksize );
248  rtems_test_assert(!sc);
249
250  stacksize *= 2;
251  puts("Init - pthread_attr_setstack double the stacksize");
252  sc = pthread_attr_setstacksize( &Thread_attr, stacksize );
253  rtems_test_assert(!sc);
254
255  puts("Init - pthread_attr_getguardsize");
256  sc = pthread_attr_getguardsize( &Thread_attr, &guardsize );
257  rtems_test_assert(!sc);
258
259  guardsize *= 2;
260  puts("Init - pthread_attr_setguardsize double the guardsize");
261  sc = pthread_attr_setguardsize( &Thread_attr, guardsize );
262  rtems_test_assert(!sc);
263
264  puts("Init - raise priority to max");
265  param.sched_priority = max_priority;
266  sc = pthread_setschedparam( Init_id, SCHED_RR, &param );
267  rtems_test_assert( !sc );
268
269 puts("Init - pthread_create");
270  sc = pthread_create( &Thread_id, &Thread_attr, Thread_1, NULL );
271  rtems_test_assert( !sc );
272
273  puts("Init - Lower priority");
274  fflush(stdout);
275  param.sched_priority = sched_get_priority_min( SCHED_RR );
276  sc = pthread_setschedparam( Init_id, SCHED_RR, &param );
277  rtems_test_assert(!sc);
278
279#if 0
280  sc = pthread_join( Thread_id, NULL );
281  rtems_test_assert( !sc );
282#endif
283
284  TEST_END();
285  rtems_test_exit(0);
286  return NULL; /* just so the compiler thinks we returned something */
287}
288
289/* configuration information */
290
291#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
292#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
293
294#define CONFIGURE_MAXIMUM_POSIX_THREADS  2
295
296#define CONFIGURE_POSIX_INIT_THREAD_TABLE
297
298#define CONFIGURE_INIT
299#include <rtems/confdefs.h>
300
301/* global variables */
Note: See TracBrowser for help on using the repository browser.