summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psxgetattrnp01/init.c
blob: df3e33541d3764ae9b1b09bdbd53081c435b57cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 *  COPYRIGHT (c) 1989-2014.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define _GNU_SOURCE
#include <tmacros.h>
#include <errno.h>
#include <sched.h>
#include <pthread.h>
#include <rtems/posix/pthreadimpl.h>

const char rtems_test_name[] = "PSXGETATTRNP 1";

/* forward declarations to avoid warnings */
void *POSIX_Init(void *argument);

#if HAVE_DECL_PTHREAD_GETATTR_NP


void *Thread_1(void *argument);

pthread_t           Init_id;
pthread_t           Thread_id;
pthread_attr_t      Thread_attr;
int                 max_priority;

void *Thread_1(
  void *argument
)
{
  pthread_attr_t      attr;
  struct sched_param  param;
  int                 sc;
  int                 value;

  puts("Thread - pthread_getattr_np - Verify value");
  sc = pthread_getattr_np( Thread_id, &attr );
  rtems_test_assert( sc == 0 );
  rtems_test_assert( ! rtems_pthread_attribute_compare(&attr, &Thread_attr) );

  param.sched_priority = max_priority;

  puts( "Thread - pthread_setschedparam: Setting highest priority SCHED_FIFO" );
  sc = pthread_setschedparam( Thread_id, SCHED_FIFO, &param );
  rtems_test_assert( !sc );

  puts("Thread - Detach");
  sc = pthread_detach( Thread_id );
  rtems_test_assert( !sc );

  puts("Thread - pthread_getattr_np");
  sc = pthread_getattr_np( Thread_id, &attr );
  rtems_test_assert( !sc );

  puts("Thread - Verify SCHED_FIFO policy");
  sc = pthread_attr_getschedpolicy( &attr, &value );
  rtems_test_assert( !sc );
  rtems_test_assert( value == SCHED_FIFO );

  puts("Thread - Verify max priority");
  sc = pthread_attr_getschedparam( &attr, &param );
  rtems_test_assert( !sc );
  rtems_test_assert( param.sched_priority == max_priority );

  puts("Thread - Verify detached");
  sc = pthread_attr_getdetachstate( &attr, &value );
  rtems_test_assert( value == PTHREAD_CREATE_DETACHED );

  return NULL; /* just so the compiler thinks we returned something */
}

void *POSIX_Init(
  void *ignored
)
{
  int                 sc;
  pthread_attr_t      attribute;
  void               *stackaddr;
  size_t              stacksize;
  size_t              guardsize;
  struct sched_param  param;

  TEST_BEGIN();

  /* Initialize thread id */
  Init_id = pthread_self();
  max_priority = sched_get_priority_max( SCHED_FIFO );

  puts( "Init - pthread_getattr_np - attr NULL - EINVAL" );
  sc = pthread_getattr_np( Init_id, NULL );
  rtems_test_assert( sc == EINVAL );

  puts( "Init - pthread_getattr_np - invalid id - ESRCH" );
  sc = pthread_getattr_np( 0xffff, &attribute );
  rtems_test_assert( sc == ESRCH );


  /* init task attributes */
  puts("Init - pthread_attr_init");
  sc = pthread_attr_init(&Thread_attr);
  rtems_test_assert(!sc);

  puts("Init - pthread_attr_setinheritsched - PTHREAD_EXPLICIT_SCHED");
  sc = pthread_attr_setinheritsched( &Thread_attr, PTHREAD_EXPLICIT_SCHED );
  rtems_test_assert(!sc);
  rtems_test_assert( Thread_attr.inheritsched == PTHREAD_EXPLICIT_SCHED );

  puts("Init - pthread_attr_setschedpolicy to SCHED_RR");
  sc = pthread_attr_setschedpolicy(&Thread_attr, SCHED_RR);
  rtems_test_assert(!sc);

  puts("Init - pthread_attr_setschedparam to minimum priority + 2");
  param.sched_priority = sched_get_priority_min( SCHED_RR ) + 2;
  sc = pthread_attr_setschedparam( &Thread_attr, &param );
  rtems_test_assert(!sc);

  puts("Init - pthread_attr_getstack");
  sc = pthread_attr_getstack( &Thread_attr, &stackaddr, &stacksize );
  rtems_test_assert(!sc);

  stacksize *= 2;
  puts("Init - pthread_attr_setstack double the stacksize");
  sc = pthread_attr_setstacksize( &Thread_attr, stacksize );
  rtems_test_assert(!sc);

  puts("Init - pthread_attr_getguardsize");
  sc = pthread_attr_getguardsize( &Thread_attr, &guardsize );
  rtems_test_assert(!sc);

  guardsize *= 2;
  puts("Init - pthread_attr_setguardsize double the guardsize");
  sc = pthread_attr_setguardsize( &Thread_attr, guardsize );
  rtems_test_assert(!sc);

  puts("Init - raise priority to max");
  param.sched_priority = max_priority;
  sc = pthread_setschedparam( Init_id, SCHED_RR, &param );
  rtems_test_assert( !sc );

 puts("Init - pthread_create");
  sc = pthread_create( &Thread_id, &Thread_attr, Thread_1, NULL );
  rtems_test_assert( !sc );

  puts("Init - Lower priority");
  fflush(stdout);
  param.sched_priority = sched_get_priority_min( SCHED_RR );
  sc = pthread_setschedparam( Init_id, SCHED_RR, &param );
  rtems_test_assert(!sc);

#if 0
  sc = pthread_join( Thread_id, NULL );
  rtems_test_assert( !sc );
#endif

  TEST_END();
  rtems_test_exit(0);
  return NULL; /* just so the compiler thinks we returned something */
}
#else
void *POSIX_Init(
  void *ignored
)
{
  TEST_BEGIN();
  puts( "  pthread_getattr_np NOT supported" );
  TEST_END();
  rtems_test_exit(0);
  return NULL; /* just so the compiler thinks we returned something */
}

#endif
/* configuration information */

#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER

#define CONFIGURE_MAXIMUM_POSIX_THREADS  2

#define CONFIGURE_POSIX_INIT_THREAD_TABLE

#define CONFIGURE_INIT
#include <rtems/confdefs.h>

/* global variables */