source: rtems/testsuites/libtests/capture01/test1.c

Last change on this file was 33078a2, checked in by Joel Sherrill <joel@…>, on 04/07/22 at 13:44:05

testsuites/libtests/[a-l]*: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 6.8 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*  Test1
4 *
5 *  This test uses creates a number of tasks so the capture engine
6 *  can show a trace.
7 *
8 *  Input parameters:  NONE
9 *
10 *  Output parameters:  NONE
11 *
12 *  COPYRIGHT (c) 1989-1997.
13 *  On-Line Applications Research Corporation (OAR).
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#ifdef HAVE_CONFIG_H
38#include "config.h"
39#endif
40
41#include "system.h"
42#include <stdio.h>
43#include <stdlib.h>
44
45#include <rtems.h>
46
47static volatile int capture_CT1a_deleted;
48static volatile int capture_CT1b_deleted;
49static volatile int capture_CT1c_deleted;
50
51static void
52capture_wait (uint32_t period)
53{
54  rtems_task_wake_after (RTEMS_MICROSECONDS_TO_TICKS (period * 1000));
55}
56
57/*
58 * CT1a: Claim the mutex and then wait a while then wake
59 *       up and release the mutex. While this task waits with
60 *       the mutex another higher priority task is started that
61 *       just loops using all the processing time. It is not until
62 *       another even higher priority thread blocks on the mutex
63 *       does this task get raised to that priority and so
64 *       releases the mutex. This will allow us to capture the
65 *       action of priority inversion.
66 */
67static void
68capture_CT1a (rtems_task_argument arg)
69{
70  rtems_id mutex = (rtems_id) arg;
71  rtems_status_code sc;
72
73  sc = rtems_semaphore_obtain (mutex, RTEMS_WAIT, 0);
74
75  if (sc != RTEMS_SUCCESSFUL)
76    printf ("error: CT1a: mutex obtain: %s\n", rtems_status_text (sc));
77
78  capture_wait (2500);
79
80  sc = rtems_semaphore_release (mutex);
81
82  if (sc != RTEMS_SUCCESSFUL)
83    printf ("error: CT1a: mutex release: %s\n", rtems_status_text (sc));
84
85  capture_CT1a_deleted = 1;
86
87  rtems_task_exit();
88}
89
90static void
91capture_CT1b (rtems_task_argument arg)
92{
93  volatile int i;
94
95  while (!capture_CT1c_deleted)
96    i++;
97
98  capture_CT1b_deleted = 1;
99
100  rtems_task_exit();
101}
102
103static void
104capture_CT1c (rtems_task_argument arg)
105{
106  rtems_id          mutex = (rtems_id) arg;
107  rtems_status_code sc;
108
109  sc = rtems_semaphore_obtain (mutex, RTEMS_WAIT, 0);
110
111  if (sc != RTEMS_SUCCESSFUL)
112    printf ("error: CT1c: mutex obtain: %s\n", rtems_status_text (sc));
113
114  capture_wait (500);
115
116  sc = rtems_semaphore_release (mutex);
117
118  if (sc != RTEMS_SUCCESSFUL)
119    printf ("error: CT1c: mutex release: %s\n", rtems_status_text (sc));
120
121  capture_CT1c_deleted = 1;
122
123  rtems_task_exit();
124}
125
126void capture_test_1 ()
127{
128  rtems_status_code sc;
129  rtems_name        name;
130  rtems_id          id[3];
131  rtems_id          mutex;
132  int               loops;
133
134  capture_CT1a_deleted = 0;
135  capture_CT1b_deleted = 0;
136  capture_CT1c_deleted = 0;
137
138  name = rtems_build_name('C', 'T', 'm', '1');
139
140  sc = rtems_semaphore_create (name, 1,
141                               RTEMS_PRIORITY | RTEMS_BINARY_SEMAPHORE |
142                               RTEMS_INHERIT_PRIORITY,
143                               0, &mutex);
144
145  if (sc != RTEMS_SUCCESSFUL)
146  {
147    printf ("error: Test 1: cannot mutex: %s\n", rtems_status_text (sc));
148    return;
149  }
150
151  name = rtems_build_name('C', 'T', '1', 'a');
152
153  sc = rtems_task_create (name, 102, 2 * 1024,
154                          RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
155                          RTEMS_PREEMPT | RTEMS_TIMESLICE | RTEMS_NO_ASR,
156                          &id[0]);
157
158  if (sc != RTEMS_SUCCESSFUL)
159  {
160    printf ("error: Test 1: cannot create CT1a: %s\n", rtems_status_text (sc));
161    rtems_semaphore_delete (mutex);
162    return;
163  }
164
165  sc = rtems_task_start (id[0], capture_CT1a, (rtems_task_argument) mutex);
166
167  if (sc != RTEMS_SUCCESSFUL)
168  {
169    printf ("error: Test 1: cannot start CT1a: %s\n", rtems_status_text (sc));
170    rtems_task_exit();
171    rtems_semaphore_delete (mutex);
172    return;
173  }
174
175  capture_wait (1000);
176
177  name = rtems_build_name('C', 'T', '1', 'b');
178
179  sc = rtems_task_create (name, 101, 2 * 1024,
180                          RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
181                          RTEMS_PREEMPT | RTEMS_TIMESLICE | RTEMS_NO_ASR,
182                          &id[1]);
183
184  if (sc != RTEMS_SUCCESSFUL)
185  {
186    printf ("error: Test 1: cannot create CT1b: %s\n", rtems_status_text (sc));
187    rtems_task_exit();
188    rtems_semaphore_delete (mutex);
189    return;
190  }
191
192  sc = rtems_task_start (id[1], capture_CT1b, 0);
193
194  if (sc != RTEMS_SUCCESSFUL)
195  {
196    printf ("error: Test 1: cannot start CT1b: %s\n", rtems_status_text (sc));
197    rtems_task_exit();
198    rtems_task_exit();
199    rtems_semaphore_delete (mutex);
200    return;
201  }
202
203  capture_wait (1000);
204
205  name = rtems_build_name('C', 'T', '1', 'c');
206
207  sc = rtems_task_create (name, 100, 2 * 1024,
208                          RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
209                          RTEMS_PREEMPT | RTEMS_TIMESLICE | RTEMS_NO_ASR,
210                          &id[2]);
211
212  if (sc != RTEMS_SUCCESSFUL)
213  {
214    printf ("error: Test 1: cannot create CT1c: %s\n", rtems_status_text (sc));
215    rtems_task_exit();
216    rtems_task_exit();
217    rtems_semaphore_delete (mutex);
218    return;
219  }
220
221  sc = rtems_task_start (id[2], capture_CT1c, (rtems_task_argument) mutex);
222
223  if (sc != RTEMS_SUCCESSFUL)
224  {
225    printf ("error: Test 1: cannot start CT1c: %s\n", rtems_status_text (sc));
226    rtems_task_exit();
227    rtems_task_exit();
228    rtems_task_exit();
229    rtems_semaphore_delete (mutex);
230    return;
231  }
232
233  loops = 15;
234
235  while (!(capture_CT1a_deleted || capture_CT1b_deleted ||
236           capture_CT1c_deleted) && loops)
237  {
238    loops--;
239    capture_wait (1000);
240  }
241
242  if (!loops)
243  {
244    printf ("error: Test 1: test tasks did not delete\n");
245    rtems_task_exit();
246    rtems_task_exit();
247    rtems_task_exit();
248  }
249
250  sc = rtems_semaphore_delete (mutex);
251  if (sc != RTEMS_SUCCESSFUL)
252    printf ("error: Test 1: deleting the mutex: %s\n", rtems_status_text (sc));
253
254}
Note: See TracBrowser for help on using the repository browser.