Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (c) 2013 Gedare Bloom.
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #include <rtems.h>
0029 
0030 #include <stdio.h>
0031 #include "tmacros.h"
0032 
0033 /* configuration information */
0034 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0035 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0036 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0037 
0038 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0039 #define CONFIGURE_MAXIMUM_TASKS 4
0040 #define CONFIGURE_MAXIMUM_SEMAPHORES 2
0041 #define CONFIGURE_INIT
0042 #include <rtems/confdefs.h>
0043 
0044 const char rtems_test_name[] = "SPSEM 2";
0045 
0046 rtems_task Task01(rtems_task_argument ignored);
0047 rtems_task Task02(rtems_task_argument ignored);
0048 rtems_task Task03(rtems_task_argument ignored);
0049 rtems_task Init(rtems_task_argument ignored);
0050 
0051 static int getprio(void)
0052 {
0053   rtems_status_code status;
0054   rtems_task_priority pri;
0055 
0056   status = rtems_task_set_priority(RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &pri);
0057   directive_failed( status, "rtems_task_set_priority");
0058   return (int)pri;
0059 }
0060 
0061 rtems_id   Task_id[3];
0062 rtems_name Task_name[3];
0063 
0064 rtems_id   sem_id[2];
0065 rtems_name sem_name[2];
0066 
0067 rtems_task Init(rtems_task_argument ignored)
0068 {
0069   rtems_status_code status;
0070   rtems_attribute sem_attr;
0071 
0072   TEST_BEGIN();
0073 
0074   sem_attr = RTEMS_INHERIT_PRIORITY | RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY;
0075 
0076   sem_name[0] = rtems_build_name( 'S','0',' ',' ');
0077   status = rtems_semaphore_create(
0078     sem_name[0],
0079     1,
0080     sem_attr,
0081     0,
0082     &sem_id[0]
0083   );
0084   directive_failed( status, "rtems_semaphore_create of S0");
0085   printf("init: S0 created\n");
0086 
0087   sem_name[1] = rtems_build_name( 'S','1',' ',' ');
0088   status = rtems_semaphore_create(
0089     sem_name[1],
0090     1,
0091     sem_attr,
0092     0,
0093     &sem_id[1]
0094   );
0095   directive_failed( status, "rtems_semaphore_create of S1");
0096   printf("init: S1 created\n");
0097 
0098   Task_name[0] = rtems_build_name( 'T','A','0','1');
0099   status = rtems_task_create(
0100     Task_name[0],
0101     36,
0102     RTEMS_MINIMUM_STACK_SIZE,
0103     RTEMS_DEFAULT_MODES,
0104     RTEMS_DEFAULT_ATTRIBUTES,
0105     &Task_id[0]
0106   );
0107   directive_failed( status, "rtems_task_create of TA01");
0108   printf("init: TA01 created with priority 36\n");
0109 
0110   Task_name[1] = rtems_build_name( 'T','A','0','2');
0111   status = rtems_task_create(
0112     Task_name[1],
0113     34,
0114     RTEMS_MINIMUM_STACK_SIZE,
0115     RTEMS_DEFAULT_MODES,
0116     RTEMS_DEFAULT_ATTRIBUTES,
0117     &Task_id[1]
0118   );
0119   directive_failed( status , "rtems_task_create of TA02\n");
0120   printf("init: TA02 created with priority 34\n");
0121 
0122   Task_name[2] = rtems_build_name( 'T','A','0','3');
0123   status = rtems_task_create(
0124     Task_name[2],
0125     32,
0126     RTEMS_MINIMUM_STACK_SIZE,
0127     RTEMS_DEFAULT_MODES,
0128     RTEMS_DEFAULT_ATTRIBUTES,
0129     &Task_id[2]
0130   );
0131   directive_failed( status , "rtems_task_create of TA03\n");
0132   printf("init: TA03 created with priority 32\n");
0133 
0134   status = rtems_task_start( Task_id[0], Task01, 0);
0135   directive_failed( status, "rtems_task_start of TA01");
0136 
0137   rtems_task_exit();
0138 }
0139 
0140 /* Task01 starts with priority 36 */
0141 rtems_task Task01(rtems_task_argument ignored)
0142 {
0143   rtems_status_code status;
0144   printf("TA01: started with priority %d\n", getprio());
0145 
0146   status = rtems_semaphore_obtain( sem_id[0], RTEMS_WAIT, 0 );
0147   directive_failed( status, "rtems_semaphore_obtain of S0\n");
0148   printf("TA01: priority %d, holding S0\n", getprio());
0149 
0150   status = rtems_semaphore_obtain( sem_id[1], RTEMS_WAIT, 0 );
0151   directive_failed( status, "rtems_semaphore_obtain of S1");
0152   printf("TA01: priority %d, holding S0, S1\n", getprio());
0153 
0154   /* Start Task 2 (TA02) with priority 34. It will run immediately. */
0155   status = rtems_task_start( Task_id[1], Task02, 0);
0156   directive_failed( status, "rtems_task_start of TA02\n");
0157 
0158   /* Start Task 3 (TA03) with priority 32. It will run immediately. */
0159   status = rtems_task_start( Task_id[2], Task03, 0);
0160   directive_failed( status, "rtems_task_start of TA03\n");
0161   printf("TA01: priority %d, holding S0, S1\n", getprio());
0162 
0163   status = rtems_semaphore_release(sem_id[1]);
0164   directive_failed( status, "rtems_semaphore_release of S1\n");
0165   printf("TA01: priority %d, holding S0\n", getprio());
0166 
0167   status = rtems_semaphore_release(sem_id[0]);
0168   directive_failed( status, "rtems_semaphore_release of S0\n");
0169   printf("TA01: priority %d\n", getprio());
0170 
0171   printf("TA01: exiting\n");
0172   TEST_END();
0173 
0174   rtems_test_exit(0);
0175 }
0176 
0177 /* TA02 starts at Task02 with priority 34 */
0178 rtems_task Task02(rtems_task_argument ignored)
0179 {
0180   rtems_status_code status;
0181 
0182   printf("TA02: started with priority %d\n", getprio());
0183 
0184   /* Obtain S1, which should be held by TA01 by now */
0185   status = rtems_semaphore_obtain( sem_id[1], RTEMS_WAIT, 0 );
0186   directive_failed( status, " rtems_semaphore_obtain S1");
0187   printf("TA02: priority %d, holding S1\n", getprio());
0188 
0189   printf("TA02: suspending\n");
0190   status = rtems_task_suspend( RTEMS_SELF);
0191   directive_failed( status, "rtems_task_suspend TA02");
0192 }
0193 
0194 /* Task03 starts with priority 32 */
0195 rtems_task Task03(rtems_task_argument ignored)
0196 {
0197   rtems_status_code status;
0198   printf("TA03: started with priority %d\n", getprio());
0199 
0200   status = rtems_semaphore_obtain( sem_id[0], RTEMS_WAIT, 0 );
0201   directive_failed( status, "rtems_semaphore_obtain of S0\n");
0202   printf("TA03: priority %d, holding S0\n", getprio());
0203 
0204   status = rtems_semaphore_release(sem_id[0]);
0205   directive_failed( status, "rtems_semaphore_release of S0\n");
0206   printf("TA03: priority %d\n", getprio());
0207 
0208   printf("TA03: exiting\n");
0209   rtems_task_exit();
0210 }