summaryrefslogtreecommitdiffstats
path: root/testsuites/samples/unlimited/test2.c
blob: ede41b7347b1b0d50381c31ac0e2d5fd00c6041c (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*  Init
 *
 *  This routine is the initialization task for this test program.
 *  It is called from init_exec and has the responsibility for creating
 *  and starting the tasks that make up the test.  If the time of day
 *  clock is required for the test, it should also be set to a known
 *  value by this function.
 *
 *  Input parameters:  NONE
 *
 *  Output parameters:  NONE
 *
 *  COPYRIGHT (c) 1989-1997.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may in
 *  the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

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

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

#include "system.h"
#include "tmacros.h"

void test2()
{
  rtems_status_code   result;
  uint32_t      remove_task;
  uint32_t      task;
  uint32_t      block;
  uint32_t      task_count = 0;
  rtems_id      removed_ids[TASK_ALLOCATION_SIZE * 2];

  char               c1 = 'a';
  char               c2 = 'a';
  char               c3 = '0';
  char               c4 = '0';

  printf( "\n TEST2 : re-allocate of index numbers, and a block free'ed and one inactive\n" );

  /*
   *  Allocate enought tasks so the Inactive list is empty. Remember
   *  to count the Init task, ie ... - 1.
   */

  while (task_count < ((TASK_ALLOCATION_SIZE * 5) - TASK_INDEX_OFFSET))
  {
    rtems_name name;

    printf(" TEST2 : creating task '%c%c%c%c', ", c1, c2, c3, c4);

    name = rtems_build_name(c1, c2, c3, c4);

    result = rtems_task_create(name,
                               10,
                               RTEMS_MINIMUM_STACK_SIZE,
                               RTEMS_DEFAULT_ATTRIBUTES,
                               RTEMS_LOCAL,
                               &task_id[task_count]);

    if (status_code_bad(result))
      break;

    printf("number = %3" PRIi32 ", id = %08" PRIxrtems_id ", starting, ", task_count, task_id[task_count]);
    fflush(stdout);

    result = rtems_task_start(task_id[task_count],
                              test_task,
                              (rtems_task_argument) task_count);

    if (status_code_bad(result))
      break;

    /*
     *  Update the name.
     */

    NEXT_TASK_NAME(c1, c2, c3, c4);

    task_count++;
  }

  /*
   *  Take out the second and fourth allocation size block of tasks
   */

  if (task_count != ((TASK_ALLOCATION_SIZE * 5) - TASK_INDEX_OFFSET)) {
    printf( " FAIL2 : not enough tasks created -\n"
            "         task created = %" PRIi32 ", required number = %i\n",
            task_count, (TASK_ALLOCATION_SIZE * 5) - TASK_INDEX_OFFSET);
    destory_all_tasks("TEST2");
    exit( 1 );
  }

  task = 0;

  for (block = 1; block < 4; block += 2)
  {
    for (remove_task = (block * TASK_ALLOCATION_SIZE) - TASK_INDEX_OFFSET;
         remove_task < (((block + 1) * TASK_ALLOCATION_SIZE) - TASK_INDEX_OFFSET);
         remove_task++)
    {
      if (!task_id[remove_task])
      {
        printf( " FAIL2 : remove task has a 0 id -\n"
                "         task number = %" PRIi32 "\n",
                remove_task);
        destory_all_tasks("TEST2");
        exit( 1 );
      }

      /*
       * Save the id's to match them against the reallocated ids
       */

      removed_ids[task++] = task_id[remove_task];

      printf(" TEST2 : block %" PRIi32 " remove, signal task %08" PRIxrtems_id ", ", block, task_id[remove_task]);
      rtems_event_send(task_id[remove_task], 1);
      task_id[remove_task] = 0;
    }
  }

  for (task = 0; task < (TASK_ALLOCATION_SIZE * 2); task++)
  {
    rtems_name       name;
    uint32_t   id_slot;

    /*
     *  Find a free slot in the task id table.
     */

    for (id_slot = 0; id_slot < MAX_TASKS; id_slot++)
      if (!task_id[id_slot])
        break;

    if (id_slot == MAX_TASKS)
    {
      printf( " FAIL2 : no free task id slot.\n");
      destory_all_tasks("TEST2");
      exit( 1 );
    }

    printf(" TEST2 : creating task '%c%c%c%c', ", c1, c2, c3, c4);

    name = rtems_build_name(c1, c2, c3, c4);

    result = rtems_task_create(name,
                               10,
                               RTEMS_MINIMUM_STACK_SIZE,
                               RTEMS_DEFAULT_ATTRIBUTES,
                               RTEMS_LOCAL,
                               &task_id[id_slot]);

    if (status_code_bad(result))
    {
      printf( " FAIL2 : re-creating a task -\n"
              "         task number = %" PRIi32 "\n",
              id_slot);
      destory_all_tasks("TEST2");
      exit( 1 );
    }

    printf("number = %3" PRIi32 ", id = %08" PRIxrtems_id ", starting, ", task_count, task_id[id_slot]);

    result = rtems_task_start(task_id[id_slot],
                              test_task,
                              (rtems_task_argument) task_count);

    if (status_code_bad(result))
    {
      printf( " FAIL : re-starting a task -\n"
              "        task number = %" PRIi32 "\n",
              id_slot);
      destory_all_tasks("TEST2");
      exit( 1 );
    }

    /*
     *  Update the name.
     */

    NEXT_TASK_NAME(c1, c2, c3, c4);

    /*
     *  Search the removed ids to see if it existed, clear the removed id when found
     */

    for (remove_task = 0; remove_task < (TASK_ALLOCATION_SIZE * 2); remove_task++)
      if (removed_ids[remove_task] == task_id[id_slot])
      {
        removed_ids[remove_task] = 0;
        break;
      }

    /*
     *  If not located in the removed id table, check and make sure it is not
     *  already allocated
     */

    if (remove_task == (TASK_ALLOCATION_SIZE * 2))
    {
      uint32_t   allocated_id;

      for (allocated_id = 0; allocated_id < MAX_TASKS; allocated_id++)
        if ((task_id[id_slot] == task_id[allocated_id]) && (id_slot != allocated_id))
        {
          printf( " FAIL2 : the new id is the same as an id already allocated -\n"
                  "         task id = %08" PRIxrtems_id "\n",
                  task_id[id_slot]);
          exit( 1 );
        }

      printf( " FAIL2 : could not find the task id in the removed table -\n"
              "         task id = %08" PRIxrtems_id "\n",
              task_id[id_slot]);
      exit( 1 );
    }

    task_count++;
  }

  destory_all_tasks("TEST2");

  printf( " TEST2 : completed\n" );
}