summaryrefslogtreecommitdiffstats
path: root/posix_api/psx_mutex_report/mutex_attr_report.c
blob: 444a4d08c523385e5806c6078386709f3f0831b1 (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
/*
 *  Program to print default POSIX mutex attributes
 */

/*
 * Copyright 2018 Joel Sherrill (joel@rtems.org)
 *
 * This file's license is 2-clause BSD as in this distribution's LICENSE.2 file.
 */

#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <pthread.h>
#include <assert.h>

static void print_shared(pthread_mutexattr_t *attr)
{
  int   rc;
  int   shared;
  char *s;

  rc = pthread_mutexattr_getpshared( attr, &shared );
  assert( rc == 0 );

  switch ( shared ) {
    case PTHREAD_PROCESS_PRIVATE: s = "PTHREAD_PROCESS_PRIVATE"; break;
    case PTHREAD_PROCESS_SHARED:  s = "PTHREAD_PROCESS_SHARED"; break;
    default:                      s = "UNKNOWN"; break;
  }

  printf( "Process shared: %s\n", s );
}

static void print_type(pthread_mutexattr_t *attr)
{
  int   rc;
  int   type;
  char *s;

#ifndef PTHREAD_MUTEX_ROBUST
  puts( "PTHREAD_MUTEX_ROBUST is not supported" );
#endif
#ifndef PTHREAD_MUTEX_STALLED
  puts( "PTHREAD_MUTEX_STALLED is not supported" );
#endif
#if (PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL)
  puts( "PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL" );
#endif

  /*
   * NOTE: The FACE Technical Standard defines multiple POSIX profiles.
   *       Some of the profiles do not include pthread_mutexattr_gettype().
   */
#ifdef OS_DOES_NOT_SUPPORT_PTHREAD_MUTEXATTR_GETTYPE
  s = "pthread_mutexattr_gettype() is not supported";
#else
  rc = pthread_mutexattr_gettype( attr, &type );
  assert( rc == 0 );
  switch ( type ) {
#if (PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_NORMAL)
    case PTHREAD_MUTEX_DEFAULT:    s = "PTHREAD_MUTEX_DEFAULT"; break;
#endif
    case PTHREAD_MUTEX_ERRORCHECK: s = "PTHREAD_MUTEX_ERRORCHECK"; break;
    case PTHREAD_MUTEX_NORMAL:     s = "PTHREAD_MUTEX_NORMAL"; break;
    case PTHREAD_MUTEX_RECURSIVE:  s = "PTHREAD_MUTEX_RECURSIVE"; break;
#ifdef PTHREAD_MUTEX_ROBUST
    case PTHREAD_MUTEX_ROBUST:     s = "PTHREAD_MUTEX_ROBUST"; break;
#endif
#ifdef PTHREAD_MUTEX_STALLED
    case PTHREAD_MUTEX_STALLED:    s = "PTHREAD_MUTEX_STALLED"; break;
#endif
    default:                       s = "UNKNOWN"; break;
  }
#endif

  printf( "Type: %s\n", s );
}

static void print_protocol(pthread_mutexattr_t *attr)
{
#if defined(__CYGWIN__)
    /* Cygwin does not support the protocols */
   puts( "Mutex protocols not supported" ); 
#else
  int   rc;
  int   protocol;
  char *s;

  rc = pthread_mutexattr_getprotocol( attr, &protocol );
  assert( rc == 0 );
  switch ( protocol ) {
    /* XXX something is not right on CentOS. The constants are not available.*/
    /* XXX Figure it out */
#ifdef __linux__
    case 0: s = "PTHREAD_PRIO_NONE"; break;
    case 1: s = "PTHREAD_PRIO_INHERIT"; break;
    case 2: s = "PTHREAD_PRIO_PROTECT"; break;
#else
    case PTHREAD_PRIO_NONE:    s = "PTHREAD_PRIO_NONE"; break;
    case PTHREAD_PRIO_INHERIT: s = "PTHREAD_PRIO_INHERIT"; break;
    case PTHREAD_PRIO_PROTECT: s = "PTHREAD_PRIO_PROTECT"; break;
#endif
    default:                   s = "UNKNOWN"; break;
  }

  printf( "Protocol: %s\n", s );
  /*
   * Assuming that PTHREAD_PRIO_CEILING is not used as default, so
   * no need to print ceiling.
   */
#endif
}

int main()
{
  pthread_mutexattr_t  attr;
  int                  rc;
  
  puts( "*** POSIX Mutex Default Attributes Report ***" );

  rc = pthread_mutexattr_init( &attr );
  assert( rc == 0 );

  print_type( &attr );
  print_shared( &attr );
  print_protocol( &attr );

  puts( "*** END OF POSIX Mutex Default Attributes Report ***" );
  exit( 0 );
}