source: rtems/cpukit/posix/src/mutexinit.c @ 5a32c48

5
Last change on this file since 5a32c48 was 5a32c48, checked in by Sebastian Huber <sebastian.huber@…>, on 06/14/16 at 13:57:54

posix: Make POSIX API aware of scheduler instances

  • Property mode set to 100644
File size: 3.7 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/priorityimpl.h>
23#include <rtems/score/schedulerimpl.h>
24
25/**
26 * 11.3.2 Initializing and Destroying a Mutex, P1003.1c/Draft 10, p. 87
27 *
28 * NOTE:  XXX Could be optimized so all the attribute error checking
29 *            is not performed when attr is NULL.
30 */
31
32int pthread_mutex_init(
33  pthread_mutex_t           *mutex,
34  const pthread_mutexattr_t *attr
35)
36{
37  POSIX_Mutex_Control       *the_mutex;
38  const pthread_mutexattr_t *the_attr;
39  POSIX_Mutex_Protocol       protocol;
40  const Scheduler_Control   *scheduler;
41  Priority_Control           priority;
42
43  if ( attr ) the_attr = attr;
44  else        the_attr = &_POSIX_Mutex_Default_attributes;
45
46  /* Check for NULL mutex */
47  if ( !mutex )
48    return EINVAL;
49
50  /*
51   *  The POSIX specification says:
52   *
53   *  "Attempting to initialize an already initialized mutex results
54   *  in undefined behavior."
55   *
56   *  Trying to keep the caller from doing the create when *mutex
57   *  is actually a valid ID causes grief.  All it takes is the wrong
58   *  value in an uninitialized variable to make this fail.
59   *
60   *  Thus, we do not look at *mutex.
61   */
62
63  if ( !the_attr->is_initialized )
64    return EINVAL;
65
66  /*
67   *  We only support process private mutexes.
68   */
69  if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED )
70    return ENOSYS;
71
72  if ( the_attr->process_shared != PTHREAD_PROCESS_PRIVATE )
73    return EINVAL;
74
75  /*
76   *  Determine the discipline of the mutex
77   */
78  switch ( the_attr->protocol ) {
79    case PTHREAD_PRIO_NONE:
80      protocol = POSIX_MUTEX_NO_PROTOCOL;
81      break;
82    case PTHREAD_PRIO_INHERIT:
83      protocol = POSIX_MUTEX_PRIORITY_INHERIT;
84      break;
85    case PTHREAD_PRIO_PROTECT:
86      protocol = POSIX_MUTEX_PRIORITY_CEILING;
87      break;
88    default:
89      return EINVAL;
90  }
91
92#if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES)
93  /*
94   *  Validate the mutex type and set appropriate SuperCore mutex
95   *  attributes.
96   */
97  switch ( the_attr->type ) {
98    case PTHREAD_MUTEX_NORMAL:
99    case PTHREAD_MUTEX_RECURSIVE:
100    case PTHREAD_MUTEX_ERRORCHECK:
101    case PTHREAD_MUTEX_DEFAULT:
102      break;
103
104    default:
105      return EINVAL;
106  }
107#endif
108
109  if ( protocol == POSIX_MUTEX_PRIORITY_CEILING ) {
110    int prio_ceiling;
111
112    scheduler = _Scheduler_Get_own( _Thread_Get_executing() );
113    prio_ceiling = the_attr->prio_ceiling;
114
115    if ( prio_ceiling == INT_MAX ) {
116      prio_ceiling = _POSIX_Priority_Get_maximum( scheduler );
117    }
118
119    if ( !_POSIX_Priority_Is_valid( scheduler, prio_ceiling ) ) {
120      return EINVAL;
121    }
122
123    priority = _POSIX_Priority_To_core( scheduler, prio_ceiling );
124  }
125
126  the_mutex = _POSIX_Mutex_Allocate();
127
128  if ( !the_mutex ) {
129    _Objects_Allocator_unlock();
130    return EAGAIN;
131  }
132
133  the_mutex->protocol = protocol;
134  the_mutex->is_recursive = ( the_attr->type == PTHREAD_MUTEX_RECURSIVE );
135
136  switch ( protocol ) {
137    case POSIX_MUTEX_PRIORITY_CEILING:
138      _CORE_ceiling_mutex_Initialize(
139        &the_mutex->Mutex,
140        priority
141      );
142      break;
143    default:
144      _Assert(
145        the_mutex->protocol == POSIX_MUTEX_NO_PROTOCOL
146          || the_mutex->protocol == POSIX_MUTEX_PRIORITY_INHERIT
147      );
148      _CORE_recursive_mutex_Initialize(
149        &the_mutex->Mutex.Recursive
150      );
151      break;
152  }
153
154  _Objects_Open_u32( &_POSIX_Mutex_Information, &the_mutex->Object, 0 );
155
156  *mutex = the_mutex->Object.id;
157
158  _Objects_Allocator_unlock();
159  return 0;
160}
Note: See TracBrowser for help on using the repository browser.