source: rtems/testsuites/psxtests/psxkey08/init.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

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