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 3
0040 #define CONFIGURE_MAXIMUM_SEMAPHORES 2
0041 #define CONFIGURE_INIT
0042 #include <rtems/confdefs.h>
0043 
0044 const char rtems_test_name[] = "SPSEM 1";
0045 
0046 rtems_task Task01(rtems_task_argument ignored);
0047 rtems_task Task02(rtems_task_argument ignored);
0048 rtems_task Init(rtems_task_argument ignored);
0049 
0050 static int getprio(void)
0051 {
0052   rtems_status_code status;
0053   rtems_task_priority pri;
0054 
0055   status = rtems_task_set_priority(RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &pri);
0056   directive_failed( status, "rtems_task_set_priority");
0057   return (int)pri;
0058 }
0059 
0060 rtems_id   Task_id[2];
0061 rtems_name Task_name[2];
0062 
0063 rtems_id   sem_id[2];
0064 rtems_name sem_name[2];
0065 
0066 rtems_task Init(rtems_task_argument ignored)
0067 {
0068   rtems_status_code status;
0069   rtems_attribute sem_attr;
0070 
0071   TEST_BEGIN();
0072 
0073   sem_attr = RTEMS_INHERIT_PRIORITY | RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY;
0074 
0075   sem_name[0] = rtems_build_name( 'S','0',' ',' ');
0076   status = rtems_semaphore_create(
0077     sem_name[0],
0078     1,
0079     sem_attr,
0080     0,
0081     &sem_id[0]
0082   );
0083   directive_failed( status, "rtems_semaphore_create of S0");
0084   printf("init: S0 created\n");
0085 
0086   sem_name[1] = rtems_build_name( 'S','1',' ',' ');
0087   status = rtems_semaphore_create(
0088     sem_name[1],
0089     1,
0090     sem_attr,
0091     0,
0092     &sem_id[1]
0093   );
0094   directive_failed( status, "rtems_semaphore_create of S1");
0095   printf("init: S1 created\n");
0096 
0097   Task_name[0] = rtems_build_name( 'T','A','0','1');
0098   status = rtems_task_create(
0099     Task_name[0],
0100     36,
0101     RTEMS_MINIMUM_STACK_SIZE,
0102     RTEMS_DEFAULT_MODES,
0103     RTEMS_DEFAULT_ATTRIBUTES,
0104     &Task_id[0]
0105   );
0106   directive_failed( status, "rtems_task_create of TA01");
0107   printf("init: TA01 created with priority 36\n");
0108 
0109   Task_name[1] = rtems_build_name( 'T','A','0','2');
0110   status = rtems_task_create(
0111     Task_name[1],
0112     34,
0113     RTEMS_MINIMUM_STACK_SIZE,
0114     RTEMS_DEFAULT_MODES,
0115     RTEMS_DEFAULT_ATTRIBUTES,
0116     &Task_id[1]
0117   );
0118   directive_failed( status , "rtems_task_create of TA02\n");
0119   printf("init: TA02 created with priority 34\n");
0120 
0121   status = rtems_task_start( Task_id[0], Task01, 0);
0122   directive_failed( status, "rtems_task_start of TA01");
0123 
0124   rtems_task_exit();
0125 }
0126 
0127 /* Task01 starts with priority 36 */
0128 rtems_task Task01(rtems_task_argument ignored)
0129 {
0130   rtems_status_code status;
0131   printf("TA01: started with priority %d\n", getprio());
0132 
0133   status = rtems_semaphore_obtain( sem_id[0], RTEMS_WAIT, 0 );
0134   directive_failed( status, "rtems_semaphore_obtain of S0\n");
0135   printf("TA01: priority %d, holding S0\n", getprio());
0136 
0137   status = rtems_semaphore_obtain( sem_id[1], RTEMS_WAIT, 0 );
0138   directive_failed( status, "rtems_semaphore_obtain of S1");
0139   printf("TA01: priority %d, holding S0, S1\n", getprio());
0140 
0141   /* Start Task 2 (TA02) with priority 34. It will run immediately. */
0142   status = rtems_task_start( Task_id[1], Task02, 0);
0143   directive_failed( status, "rtems_task_start of TA02\n");
0144 
0145   status = rtems_semaphore_release(sem_id[1]);
0146   directive_failed( status, "rtems_semaphore_release of S1\n");
0147   printf("TA01: priority %d, holding S0\n", getprio());
0148 
0149   status = rtems_semaphore_release(sem_id[0]);
0150   directive_failed( status, "rtems_semaphore_release of S0\n");
0151   printf("TA01: priority %d\n", getprio());
0152 
0153   printf("TA01: exiting\n");
0154   TEST_END();
0155 
0156   rtems_test_exit(0);
0157 }
0158 
0159 /* TA02 starts at Task02 with priority 34 */
0160 rtems_task Task02(rtems_task_argument ignored)
0161 {
0162   rtems_status_code status;
0163 
0164   printf("TA02: started with priority %d\n", getprio());
0165 
0166   /* Obtain S1, which should be held by TA01 by now */
0167   status = rtems_semaphore_obtain( sem_id[1], RTEMS_WAIT, 0 );
0168   directive_failed( status, " rtems_semaphore_obtain S1");
0169   printf("TA02: priority %d, holding S1\n", getprio());
0170 
0171   printf("TA02: suspending\n");
0172   status = rtems_task_suspend( RTEMS_SELF);
0173   directive_failed( status, "rtems_task_suspend TA02");
0174 }
0175