source: rtems/testsuites/psxtests/psxkey08/init.c @ 9a4eca5

5
Last change on this file since 9a4eca5 was 698c2e50, checked in by Sebastian Huber <sebastian.huber@…>, on 03/25/14 at 07:06:16

tests/psxtests: Use <rtems/test.h>

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 *  Copyright (c) 2012 Zhongwei Yao.
3 *  COPYRIGHT (c) 1989-2014.
4 *  On-Line Applications Research Corporation (OAR).
5 *
6 *  The license and distribution terms for this file may be
7 *  found in the file LICENSE in this distribution or at
8 *  http://www.rtems.org/license/LICENSE.
9 */
10
11#ifdef HAVE_CONFIG_H
12#include "config.h"
13#endif
14
15#include "tmacros.h"
16
17#include <stdio.h>
18#include <rtems.h>
19#include <pthread.h>
20#include <unistd.h>
21#include <errno.h>
22
23const char rtems_test_name[] = "PSXKEY 8";
24
25pthread_key_t Key;
26int created_task_count, setted_task_count, got_task_count;
27int all_thread_created;
28rtems_id sema1, sema2;
29rtems_name name1, name2;
30
31/* forward declarations to avoid warnings */
32rtems_task Init(rtems_task_argument arg);
33rtems_task test_task(rtems_task_argument arg);
34
35rtems_task test_task(rtems_task_argument arg)
36{
37  int sc;
38  int *value_p, *value_p2;
39
40  value_p = malloc( sizeof( int ) );
41  sc = pthread_setspecific( Key, value_p );
42  rtems_test_assert( !sc );
43  ++setted_task_count;
44  sc = rtems_semaphore_release( sema1 );
45
46  /**
47   * blocked untill all tasks have been created.
48   */
49  rtems_semaphore_obtain( sema2 , RTEMS_WAIT, 0 );
50
51  value_p2 = pthread_getspecific( Key );
52  rtems_test_assert( value_p == value_p2 );
53  ++got_task_count;
54
55  rtems_task_delete( RTEMS_SELF );
56}
57
58rtems_task Init(rtems_task_argument arg)
59{
60  rtems_status_code  status;
61  int                sc;
62  uintptr_t          max_free_size = 13 * RTEMS_MINIMUM_STACK_SIZE;
63  void              *greedy;
64
65  all_thread_created = 0;
66
67  TEST_BEGIN();
68
69  puts( "Init - Semaphore 1 create - OK" );
70  name1 = rtems_build_name('S', 'E', 'M', '1');
71  sc = rtems_semaphore_create(
72    name1, 0,
73    RTEMS_SIMPLE_BINARY_SEMAPHORE | RTEMS_FIFO,
74    0,
75    &sema1
76  );
77  rtems_test_assert( sc == RTEMS_SUCCESSFUL );
78
79  puts( "Init - Semaphore 2 create - OK" );
80  name2 = rtems_build_name('S', 'E', 'M', '2');
81  sc = rtems_semaphore_create(
82    name2,
83    0,
84    RTEMS_SIMPLE_BINARY_SEMAPHORE | RTEMS_FIFO,
85    0,
86    &sema2
87  );
88  rtems_test_assert( sc == RTEMS_SUCCESSFUL );
89
90  puts( "Init - pthread Key create - OK" );
91  sc = pthread_key_create( &Key, NULL );
92  rtems_test_assert( !sc );
93
94  /* Reduce workspace size if necessary to shorten test time */
95  greedy = rtems_workspace_greedy_allocate( &max_free_size, 1 );
96
97  for ( ; ; ) {
98    rtems_id task_id;
99
100    sc = rtems_task_create(
101      rtems_build_name('T','A',created_task_count, ' '),
102      1,
103      RTEMS_MINIMUM_STACK_SIZE,
104      RTEMS_DEFAULT_MODES,
105      RTEMS_DEFAULT_ATTRIBUTES,
106      &task_id
107    );
108    rtems_test_assert(
109      (sc == RTEMS_UNSATISFIED) ||
110      (sc == RTEMS_TOO_MANY) ||
111      (sc == RTEMS_SUCCESSFUL)
112    );
113
114    /**
115     * when return is RTEMS_TOO_MANY or RTEMS_UNSATISFIED, there is not
116     * enough source to create task.
117     */
118    if ( (sc == RTEMS_TOO_MANY) || (sc == RTEMS_UNSATISFIED) ) {
119      break;
120    }
121    ++created_task_count;
122
123    sc = rtems_task_start( task_id,  test_task, 0 );
124    rtems_test_assert( sc == RTEMS_SUCCESSFUL );
125
126    sc = rtems_semaphore_obtain( sema1, RTEMS_WAIT, 0 );
127    rtems_test_assert( sc == RTEMS_SUCCESSFUL );
128  }
129
130  rtems_workspace_greedy_free( greedy );
131
132  printf(
133    "Init - %d tasks have been created - OK\n"
134    "Init - %d tasks have been setted key data - OK\n",
135    setted_task_count,
136    created_task_count
137  );
138  rtems_test_assert( created_task_count == setted_task_count );
139
140  /* unblock all created tasks to let them set key data.*/
141  puts( "Init - flush semaphore 2 - OK" );
142  sc = rtems_semaphore_flush( sema2 );
143  rtems_test_assert( sc == RTEMS_SUCCESSFUL );
144
145  puts( "Init - sleep to yield processor - OK" );
146  status = rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
147  directive_failed( status, "rtems_task_wake_after" );
148
149  printf( "Init - %d Tasks have been got key data - OK\n", got_task_count );
150  rtems_test_assert( created_task_count == got_task_count );
151  puts( "Init - pthread Key delete - OK" );
152  sc = pthread_key_delete( Key );
153  rtems_test_assert( sc == 0 );
154
155  puts( "Init - semaphore 1 delete - OK" );
156  sc = rtems_semaphore_delete( sema1 );
157  rtems_test_assert( !sc );
158
159  puts( "Init - semaphore 2 delete - OK" );
160  sc = rtems_semaphore_delete( sema2 );
161  rtems_test_assert( !sc );
162
163  TEST_END();
164  exit(0);
165}
166
167/* configuration information */
168#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
169#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
170
171#define CONFIGURE_MAXIMUM_TASKS rtems_resource_unlimited(5)
172#define CONFIGURE_MAXIMUM_SEMAPHORES 2
173#define CONFIGURE_MAXIMUM_POSIX_KEYS 1
174
175#define CONFIGURE_INIT_TASK_INITIAL_MODES \
176  (RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_ASR | RTEMS_INTERRUPT_LEVEL(0))
177
178#define CONFIGURE_INIT_TASK_PRIORITY 4
179#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
180
181#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
182
183#define CONFIGURE_UNIFIED_WORK_AREAS
184
185#define CONFIGURE_INIT
186#include <rtems/confdefs.h>
187/* end of file */
Note: See TracBrowser for help on using the repository browser.