source: rtems/cpukit/posix/src/mutexinit.c @ 0e16fa45

5
Last change on this file since 0e16fa45 was 1d39e96, checked in by Sebastian Huber <sebastian.huber@…>, on 10/05/18 at 06:11:09

score: Fix legacy RTEMS_STATIC_ASSERT()

In standard C pointer operands are not allowed in integer constant
expressions. Avoid a static assertion based on an array typedef since
this could lead to warnings ("variably modified 'x' at file scope" and
"typedef 'x' locally defined but not used");

This implementation requires unique messages.

  • Property mode set to 100644
File size: 4.5 KB
Line 
1/**
2 * @file
3 *
4 * @brief Initialize a Mutex
5 * @ingroup POSIXAPI
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2009.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/posix/muteximpl.h>
22#include <rtems/posix/posixapi.h>
23#include <rtems/posix/priorityimpl.h>
24#include <rtems/score/schedulerimpl.h>
25
26#include <limits.h>
27
28RTEMS_STATIC_ASSERT(
29  offsetof( POSIX_Mutex_Control, flags )
30    == offsetof( pthread_mutex_t, _flags ),
31  POSIX_MUTEX_CONTROL_FLAGS
32);
33
34RTEMS_STATIC_ASSERT(
35  offsetof( POSIX_Mutex_Control, Recursive )
36    == offsetof( pthread_mutex_t, _Recursive ),
37  POSIX_MUTEX_CONTROL_RECURSIVE
38);
39
40RTEMS_STATIC_ASSERT(
41  offsetof( POSIX_Mutex_Control, Priority_ceiling )
42    == offsetof( pthread_mutex_t, _Priority_ceiling ),
43  POSIX_MUTEX_CONTROL_PRIORITY_CEILING
44);
45
46RTEMS_STATIC_ASSERT(
47  offsetof( POSIX_Mutex_Control, scheduler )
48    == offsetof( pthread_mutex_t, _scheduler ),
49  POSIX_MUTEX_CONTROL_SCHEDULER
50);
51
52RTEMS_STATIC_ASSERT(
53  sizeof( POSIX_Mutex_Control ) == sizeof( pthread_mutex_t ),
54  POSIX_MUTEX_CONTROL_SIZE
55);
56
57const pthread_mutexattr_t _POSIX_Mutex_Default_attributes = {
58#if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES)
59  .type           = PTHREAD_MUTEX_DEFAULT,
60#endif
61  .is_initialized = true,
62  .process_shared = PTHREAD_PROCESS_PRIVATE,
63  .prio_ceiling   = INT_MAX,
64  .protocol       = PTHREAD_PRIO_NONE,
65  .recursive      = false
66};
67
68/**
69 * 11.3.2 Initializing and Destroying a Mutex, P1003.1c/Draft 10, p. 87
70 *
71 * NOTE:  XXX Could be optimized so all the attribute error checking
72 *            is not performed when attr is NULL.
73 */
74
75int pthread_mutex_init(
76  pthread_mutex_t           *mutex,
77  const pthread_mutexattr_t *attr
78)
79{
80  POSIX_Mutex_Control       *the_mutex;
81  const pthread_mutexattr_t *the_attr;
82  POSIX_Mutex_Protocol       protocol;
83  unsigned long              flags;
84  Priority_Control           priority;
85  const Scheduler_Control   *scheduler;
86
87  if ( attr ) the_attr = attr;
88  else        the_attr = &_POSIX_Mutex_Default_attributes;
89
90  /* Check for NULL mutex */
91  if ( !mutex )
92    return EINVAL;
93
94  /*
95   *  The POSIX specification says:
96   *
97   *  "Attempting to initialize an already initialized mutex results
98   *  in undefined behavior."
99   *
100   *  Trying to keep the caller from doing the create when *mutex
101   *  is actually a valid ID causes grief.  All it takes is the wrong
102   *  value in an uninitialized variable to make this fail.
103   *
104   *  Thus, we do not look at *mutex.
105   */
106
107  if ( !the_attr->is_initialized )
108    return EINVAL;
109
110  if ( !_POSIX_Is_valid_pshared( the_attr->process_shared ) ) {
111    return EINVAL;
112  }
113
114  /*
115   *  Determine the discipline of the mutex
116   */
117  switch ( the_attr->protocol ) {
118    case PTHREAD_PRIO_NONE:
119      protocol = POSIX_MUTEX_NO_PROTOCOL;
120      break;
121    case PTHREAD_PRIO_INHERIT:
122      protocol = POSIX_MUTEX_PRIORITY_INHERIT;
123      break;
124    case PTHREAD_PRIO_PROTECT:
125      protocol = POSIX_MUTEX_PRIORITY_CEILING;
126      break;
127    default:
128      return EINVAL;
129  }
130
131#if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES)
132  /*
133   *  Validate the mutex type and set appropriate SuperCore mutex
134   *  attributes.
135   */
136  switch ( the_attr->type ) {
137    case PTHREAD_MUTEX_NORMAL:
138    case PTHREAD_MUTEX_RECURSIVE:
139    case PTHREAD_MUTEX_ERRORCHECK:
140    case PTHREAD_MUTEX_DEFAULT:
141      break;
142
143    default:
144      return EINVAL;
145  }
146#endif
147
148  the_mutex = _POSIX_Mutex_Get( mutex );
149
150  flags = (uintptr_t) the_mutex ^ POSIX_MUTEX_MAGIC;
151  flags &= ~POSIX_MUTEX_FLAGS_MASK;
152  flags |= protocol;
153
154  if ( the_attr->type == PTHREAD_MUTEX_RECURSIVE ) {
155    flags |= POSIX_MUTEX_RECURSIVE;
156  }
157
158  the_mutex->flags = flags;
159
160  if ( protocol == POSIX_MUTEX_PRIORITY_CEILING ) {
161    int  prio_ceiling;
162    bool valid;
163
164    scheduler = _Thread_Scheduler_get_home( _Thread_Get_executing() );
165    prio_ceiling = the_attr->prio_ceiling;
166
167    if ( prio_ceiling == INT_MAX ) {
168      prio_ceiling = _POSIX_Priority_Get_maximum( scheduler );
169    }
170
171    priority = _POSIX_Priority_To_core( scheduler, prio_ceiling, &valid );
172    if ( !valid ) {
173      return EINVAL;
174    }
175  } else {
176    priority = 0;
177    scheduler = NULL;
178  }
179
180  _Thread_queue_Queue_initialize(
181    &the_mutex->Recursive.Mutex.Queue.Queue,
182    NULL
183  );
184  the_mutex->Recursive.nest_level = 0;
185  _Priority_Node_initialize( &the_mutex->Priority_ceiling, priority );
186  the_mutex->scheduler = scheduler;
187  return 0;
188}
Note: See TracBrowser for help on using the repository browser.