source: rtems/cpukit/posix/src/mutexinit.c @ bbb3c5f

5
Last change on this file since bbb3c5f was bbb3c5f, checked in by Sebastian Huber <sebastian.huber@…>, on 05/27/16 at 09:48:14

posix: Delete POSIX_Mutex_Protocol::process_shared

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