summaryrefslogtreecommitdiffstats
path: root/cpukit/libmisc/monitor/mon-sema.c
blob: 7334611465ccb23187f226107819fc44227f945e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * RTEMS Monitor semaphore support
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems.h>
#include "monitor.h"
#include <rtems/rtems/semimpl.h>
#include <stdio.h>
#include <string.h>    /* memcpy() */

void
rtems_monitor_sema_canonical(
    rtems_monitor_sema_t  *canonical_sema,
    const void            *sema_void
)
{
    const Semaphore_Control *rtems_sema = (const Semaphore_Control *) sema_void;
    Thread_Control *owner;

    memset(canonical_sema, 0, sizeof(*canonical_sema));

#if defined(RTEMS_MULTIPROCESSING)
    if (rtems_sema->is_global) {
      canonical_sema->attribute |= RTEMS_GLOBAL;
    }
#endif

    if (rtems_sema->discipline == SEMAPHORE_DISCIPLINE_PRIORITY) {
      canonical_sema->attribute |= RTEMS_PRIORITY;
    }

    switch ( rtems_sema->variant ) {
      case SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY:
        canonical_sema->attribute |= RTEMS_BINARY_SEMAPHORE
          | RTEMS_INHERIT_PRIORITY;
        break;
      case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING:
        canonical_sema->attribute |= RTEMS_BINARY_SEMAPHORE
          | RTEMS_PRIORITY_CEILING;
        break;
      case SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL:
        canonical_sema->attribute |= RTEMS_BINARY_SEMAPHORE;
        break;
#if defined(RTEMS_SMP)
      case SEMAPHORE_VARIANT_MRSP:
        canonical_sema->attribute |= RTEMS_BINARY_SEMAPHORE
          | RTEMS_MULTIPROCESSOR_RESOURCE_SHARING;
        break;
#endif
      case SEMAPHORE_VARIANT_SIMPLE_BINARY:
        canonical_sema->attribute |= RTEMS_SIMPLE_BINARY_SEMAPHORE;
        break;
      case SEMAPHORE_VARIANT_COUNTING:
        canonical_sema->attribute |= RTEMS_COUNTING_SEMAPHORE;
        break;
    }

    switch ( rtems_sema->variant ) {
      case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING:
        canonical_sema->priority_ceiling = _Scheduler_Unmap_priority(
          _CORE_ceiling_mutex_Get_scheduler( &rtems_sema->Core_control.Mutex ),
          _CORE_ceiling_mutex_Get_priority( &rtems_sema->Core_control.Mutex )
        );
        /* Fall through */
      case SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY:
      case SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL:
        owner = _CORE_mutex_Get_owner(
          &rtems_sema->Core_control.Mutex.Recursive.Mutex
        );

        if (owner != NULL) {
          canonical_sema->holder_id = owner->Object.id;
          canonical_sema->cur_count = 0;
        } else {
          canonical_sema->cur_count = 1;
        }

        canonical_sema->max_count = 1;
        break;
#if defined(RTEMS_SMP)
      case SEMAPHORE_VARIANT_MRSP:
        canonical_sema->cur_count =
          rtems_sema->Core_control.MRSP.Resource.owner == NULL;
        canonical_sema->max_count = 1;
        break;
#endif
      case SEMAPHORE_VARIANT_SIMPLE_BINARY:
        canonical_sema->cur_count = rtems_sema->Core_control.Semaphore.count;
        canonical_sema->max_count = 1;
        break;
      case SEMAPHORE_VARIANT_COUNTING:
        canonical_sema->cur_count = rtems_sema->Core_control.Semaphore.count;
        canonical_sema->max_count = UINT32_MAX;
        break;
    }
}


void
rtems_monitor_sema_dump_header(
    bool verbose RTEMS_UNUSED
)
{
    printf("\
  ID       NAME   ATTR        PRICEIL CURR_CNT HOLDID \n");
/*23456789 123456789 123456789 123456789 123456789 123456789 123456789 1234
          1         2         3         4         5         6         7    */

    rtems_monitor_separator();
}

/*
 */

void
rtems_monitor_sema_dump(
    rtems_monitor_sema_t *monitor_sema,
    bool  verbose RTEMS_UNUSED
)
{
    int length = 0;

    length += rtems_monitor_dump_id(monitor_sema->id);
    length += rtems_monitor_pad(11, length);
    length += rtems_monitor_dump_name(monitor_sema->id);
    length += rtems_monitor_pad(18, length);
    length += rtems_monitor_dump_attributes(monitor_sema->attribute);
    length += rtems_monitor_pad(30, length);
    length += rtems_monitor_dump_priority(monitor_sema->priority_ceiling);
    length += rtems_monitor_pad(38, length);
    length += rtems_monitor_dump_decimal(monitor_sema->cur_count);
    length += rtems_monitor_pad(47, length);
    length += rtems_monitor_dump_id(monitor_sema->holder_id);
    printf("\n");
}