Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @brief RTEMS Monitor semaphore support
0007  */
0008 
0009 /*
0010  * COPYRIGHT (c) 1989-2022. On-Line Applications Research Corporation (OAR).
0011  *
0012  * Redistribution and use in source and binary forms, with or without
0013  * modification, are permitted provided that the following conditions
0014  * are met:
0015  * 1. Redistributions of source code must retain the above copyright
0016  *    notice, this list of conditions and the following disclaimer.
0017  * 2. Redistributions in binary form must reproduce the above copyright
0018  *    notice, this list of conditions and the following disclaimer in the
0019  *    documentation and/or other materials provided with the distribution.
0020  *
0021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0031  * POSSIBILITY OF SUCH DAMAGE.
0032  */
0033 
0034 #ifdef HAVE_CONFIG_H
0035 #include "config.h"
0036 #endif
0037 
0038 #include <rtems.h>
0039 #include <rtems/monitor.h>
0040 #include <rtems/rtems/semimpl.h>
0041 #include <stdio.h>
0042 #include <string.h>    /* memcpy() */
0043 
0044 void
0045 rtems_monitor_sema_canonical(
0046     rtems_monitor_sema_t  *canonical_sema,
0047     const void            *sema_void
0048 )
0049 {
0050     const Semaphore_Control *rtems_sema;
0051     uintptr_t flags;
0052     Thread_Control *owner;
0053 
0054     canonical_sema->attribute = 0;
0055     canonical_sema->priority_ceiling = 0;
0056     canonical_sema->max_count = 0;
0057     canonical_sema->cur_count = 0;
0058     canonical_sema->holder_id = 0;
0059 
0060     rtems_sema = (const Semaphore_Control *) sema_void;
0061     flags = _Semaphore_Get_flags( rtems_sema );
0062 
0063 #if defined(RTEMS_MULTIPROCESSING)
0064     if ( _Semaphore_Is_global( flags ) ) {
0065       canonical_sema->attribute |= RTEMS_GLOBAL;
0066     }
0067 #endif
0068 
0069     if ( _Semaphore_Get_discipline( flags ) == SEMAPHORE_DISCIPLINE_PRIORITY ) {
0070       canonical_sema->attribute |= RTEMS_PRIORITY;
0071     }
0072 
0073     switch ( _Semaphore_Get_variant( flags ) ) {
0074       case SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY:
0075         canonical_sema->attribute |= RTEMS_BINARY_SEMAPHORE
0076           | RTEMS_INHERIT_PRIORITY;
0077         break;
0078       case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING:
0079         canonical_sema->attribute |= RTEMS_BINARY_SEMAPHORE
0080           | RTEMS_PRIORITY_CEILING;
0081         break;
0082       case SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL:
0083         canonical_sema->attribute |= RTEMS_BINARY_SEMAPHORE;
0084         break;
0085 #if defined(RTEMS_SMP)
0086       case SEMAPHORE_VARIANT_MRSP:
0087         canonical_sema->attribute |= RTEMS_BINARY_SEMAPHORE
0088           | RTEMS_MULTIPROCESSOR_RESOURCE_SHARING;
0089         break;
0090 #endif
0091       case SEMAPHORE_VARIANT_SIMPLE_BINARY:
0092         canonical_sema->attribute |= RTEMS_SIMPLE_BINARY_SEMAPHORE;
0093         break;
0094       case SEMAPHORE_VARIANT_COUNTING:
0095         canonical_sema->attribute |= RTEMS_COUNTING_SEMAPHORE;
0096         break;
0097     }
0098 
0099     switch ( _Semaphore_Get_variant( flags ) ) {
0100       case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING:
0101         canonical_sema->priority_ceiling = _Scheduler_Unmap_priority(
0102           _CORE_ceiling_mutex_Get_scheduler( &rtems_sema->Core_control.Mutex ),
0103           _CORE_ceiling_mutex_Get_priority( &rtems_sema->Core_control.Mutex )
0104         );
0105         /* Fall through */
0106       case SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY:
0107       case SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL:
0108         owner = _CORE_mutex_Get_owner(
0109           &rtems_sema->Core_control.Mutex.Recursive.Mutex
0110         );
0111 
0112         if (owner != NULL) {
0113           canonical_sema->holder_id = owner->Object.id;
0114           canonical_sema->cur_count = 0;
0115         } else {
0116           canonical_sema->cur_count = 1;
0117         }
0118 
0119         canonical_sema->max_count = 1;
0120         break;
0121 #if defined(RTEMS_SMP)
0122       case SEMAPHORE_VARIANT_MRSP:
0123         canonical_sema->cur_count =
0124           _MRSP_Get_owner( &rtems_sema->Core_control.MRSP ) == NULL;
0125         canonical_sema->max_count = 1;
0126         break;
0127 #endif
0128       case SEMAPHORE_VARIANT_SIMPLE_BINARY:
0129         canonical_sema->cur_count = rtems_sema->Core_control.Semaphore.count;
0130         canonical_sema->max_count = 1;
0131         break;
0132       case SEMAPHORE_VARIANT_COUNTING:
0133         canonical_sema->cur_count = rtems_sema->Core_control.Semaphore.count;
0134         canonical_sema->max_count = UINT32_MAX;
0135         break;
0136     }
0137 }
0138 
0139 
0140 void
0141 rtems_monitor_sema_dump_header(
0142     bool verbose RTEMS_UNUSED
0143 )
0144 {
0145     printf("\
0146   ID       NAME   ATTR        PRICEIL CURR_CNT HOLDID \n");
0147 /*23456789 123456789 123456789 123456789 123456789 123456789 123456789 1234
0148           1         2         3         4         5         6         7    */
0149 
0150     rtems_monitor_separator();
0151 }
0152 
0153 /*
0154  */
0155 
0156 void
0157 rtems_monitor_sema_dump(
0158     rtems_monitor_sema_t *monitor_sema,
0159     bool  verbose RTEMS_UNUSED
0160 )
0161 {
0162     int length = 0;
0163 
0164     length += rtems_monitor_dump_id(monitor_sema->id);
0165     length += rtems_monitor_pad(11, length);
0166     length += rtems_monitor_dump_name(monitor_sema->id);
0167     length += rtems_monitor_pad(18, length);
0168     length += rtems_monitor_dump_attributes(monitor_sema->attribute);
0169     length += rtems_monitor_pad(30, length);
0170     length += rtems_monitor_dump_priority(monitor_sema->priority_ceiling);
0171     length += rtems_monitor_pad(38, length);
0172     length += rtems_monitor_dump_decimal(monitor_sema->cur_count);
0173     length += rtems_monitor_pad(47, length);
0174     length += rtems_monitor_dump_id(monitor_sema->holder_id);
0175     printf("\n");
0176 }