Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:32

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*  Test1
0004  *
0005  *  This test uses creates a number of tasks so the capture engine
0006  *  can show a trace.
0007  *
0008  *  Input parameters:  NONE
0009  *
0010  *  Output parameters:  NONE
0011  *
0012  *  COPYRIGHT (c) 1989-1997.
0013  *  On-Line Applications Research Corporation (OAR).
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifdef HAVE_CONFIG_H
0038 #include "config.h"
0039 #endif
0040 
0041 #include "system.h"
0042 #include <stdio.h>
0043 #include <stdlib.h>
0044 
0045 #include <rtems.h>
0046 
0047 static volatile int capture_CT1a_deleted;
0048 static volatile int capture_CT1b_deleted;
0049 static volatile int capture_CT1c_deleted;
0050 
0051 static void
0052 capture_wait (uint32_t period)
0053 {
0054   rtems_task_wake_after (RTEMS_MICROSECONDS_TO_TICKS (period * 1000));
0055 }
0056 
0057 /*
0058  * CT1a: Claim the mutex and then wait a while then wake
0059  *       up and release the mutex. While this task waits with
0060  *       the mutex another higher priority task is started that
0061  *       just loops using all the processing time. It is not until
0062  *       another even higher priority thread blocks on the mutex
0063  *       does this task get raised to that priority and so
0064  *       releases the mutex. This will allow us to capture the
0065  *       action of priority inversion.
0066  */
0067 static void
0068 capture_CT1a (rtems_task_argument arg)
0069 {
0070   rtems_id mutex = (rtems_id) arg;
0071   rtems_status_code sc;
0072 
0073   sc = rtems_semaphore_obtain (mutex, RTEMS_WAIT, 0);
0074 
0075   if (sc != RTEMS_SUCCESSFUL)
0076     printf ("error: CT1a: mutex obtain: %s\n", rtems_status_text (sc));
0077 
0078   capture_wait (2500);
0079 
0080   sc = rtems_semaphore_release (mutex);
0081 
0082   if (sc != RTEMS_SUCCESSFUL)
0083     printf ("error: CT1a: mutex release: %s\n", rtems_status_text (sc));
0084 
0085   capture_CT1a_deleted = 1;
0086 
0087   rtems_task_exit();
0088 }
0089 
0090 static void
0091 capture_CT1b (rtems_task_argument arg)
0092 {
0093   volatile int i;
0094 
0095   while (!capture_CT1c_deleted)
0096     i++;
0097 
0098   capture_CT1b_deleted = 1;
0099 
0100   rtems_task_exit();
0101 }
0102 
0103 static void
0104 capture_CT1c (rtems_task_argument arg)
0105 {
0106   rtems_id          mutex = (rtems_id) arg;
0107   rtems_status_code sc;
0108 
0109   sc = rtems_semaphore_obtain (mutex, RTEMS_WAIT, 0);
0110 
0111   if (sc != RTEMS_SUCCESSFUL)
0112     printf ("error: CT1c: mutex obtain: %s\n", rtems_status_text (sc));
0113 
0114   capture_wait (500);
0115 
0116   sc = rtems_semaphore_release (mutex);
0117 
0118   if (sc != RTEMS_SUCCESSFUL)
0119     printf ("error: CT1c: mutex release: %s\n", rtems_status_text (sc));
0120 
0121   capture_CT1c_deleted = 1;
0122 
0123   rtems_task_exit();
0124 }
0125 
0126 void capture_test_1 ()
0127 {
0128   rtems_status_code sc;
0129   rtems_name        name;
0130   rtems_id          id[3];
0131   rtems_id          mutex;
0132   int               loops;
0133 
0134   capture_CT1a_deleted = 0;
0135   capture_CT1b_deleted = 0;
0136   capture_CT1c_deleted = 0;
0137 
0138   name = rtems_build_name('C', 'T', 'm', '1');
0139 
0140   sc = rtems_semaphore_create (name, 1,
0141                                RTEMS_PRIORITY | RTEMS_BINARY_SEMAPHORE |
0142                                RTEMS_INHERIT_PRIORITY,
0143                                0, &mutex);
0144 
0145   if (sc != RTEMS_SUCCESSFUL)
0146   {
0147     printf ("error: Test 1: cannot mutex: %s\n", rtems_status_text (sc));
0148     return;
0149   }
0150 
0151   name = rtems_build_name('C', 'T', '1', 'a');
0152 
0153   sc = rtems_task_create (name, 102, 2 * 1024,
0154                           RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
0155                           RTEMS_PREEMPT | RTEMS_TIMESLICE | RTEMS_NO_ASR,
0156                           &id[0]);
0157 
0158   if (sc != RTEMS_SUCCESSFUL)
0159   {
0160     printf ("error: Test 1: cannot create CT1a: %s\n", rtems_status_text (sc));
0161     rtems_semaphore_delete (mutex);
0162     return;
0163   }
0164 
0165   sc = rtems_task_start (id[0], capture_CT1a, (rtems_task_argument) mutex);
0166 
0167   if (sc != RTEMS_SUCCESSFUL)
0168   {
0169     printf ("error: Test 1: cannot start CT1a: %s\n", rtems_status_text (sc));
0170     rtems_task_exit();
0171     rtems_semaphore_delete (mutex);
0172     return;
0173   }
0174 
0175   capture_wait (1000);
0176 
0177   name = rtems_build_name('C', 'T', '1', 'b');
0178 
0179   sc = rtems_task_create (name, 101, 2 * 1024,
0180                           RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
0181                           RTEMS_PREEMPT | RTEMS_TIMESLICE | RTEMS_NO_ASR,
0182                           &id[1]);
0183 
0184   if (sc != RTEMS_SUCCESSFUL)
0185   {
0186     printf ("error: Test 1: cannot create CT1b: %s\n", rtems_status_text (sc));
0187     rtems_task_exit();
0188     rtems_semaphore_delete (mutex);
0189     return;
0190   }
0191 
0192   sc = rtems_task_start (id[1], capture_CT1b, 0);
0193 
0194   if (sc != RTEMS_SUCCESSFUL)
0195   {
0196     printf ("error: Test 1: cannot start CT1b: %s\n", rtems_status_text (sc));
0197     rtems_task_exit();
0198     rtems_task_exit();
0199     rtems_semaphore_delete (mutex);
0200     return;
0201   }
0202 
0203   capture_wait (1000);
0204 
0205   name = rtems_build_name('C', 'T', '1', 'c');
0206 
0207   sc = rtems_task_create (name, 100, 2 * 1024,
0208                           RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
0209                           RTEMS_PREEMPT | RTEMS_TIMESLICE | RTEMS_NO_ASR,
0210                           &id[2]);
0211 
0212   if (sc != RTEMS_SUCCESSFUL)
0213   {
0214     printf ("error: Test 1: cannot create CT1c: %s\n", rtems_status_text (sc));
0215     rtems_task_exit();
0216     rtems_task_exit();
0217     rtems_semaphore_delete (mutex);
0218     return;
0219   }
0220 
0221   sc = rtems_task_start (id[2], capture_CT1c, (rtems_task_argument) mutex);
0222 
0223   if (sc != RTEMS_SUCCESSFUL)
0224   {
0225     printf ("error: Test 1: cannot start CT1c: %s\n", rtems_status_text (sc));
0226     rtems_task_exit();
0227     rtems_task_exit();
0228     rtems_task_exit();
0229     rtems_semaphore_delete (mutex);
0230     return;
0231   }
0232 
0233   loops = 15;
0234 
0235   while (!(capture_CT1a_deleted || capture_CT1b_deleted ||
0236            capture_CT1c_deleted) && loops)
0237   {
0238     loops--;
0239     capture_wait (1000);
0240   }
0241 
0242   if (!loops)
0243   {
0244     printf ("error: Test 1: test tasks did not delete\n");
0245     rtems_task_exit();
0246     rtems_task_exit();
0247     rtems_task_exit();
0248   }
0249 
0250   sc = rtems_semaphore_delete (mutex);
0251   if (sc != RTEMS_SUCCESSFUL)
0252     printf ("error: Test 1: deleting the mutex: %s\n", rtems_status_text (sc));
0253 
0254 }