source: rtems/cpukit/posix/src/mutexinit.c @ 21789a21

5
Last change on this file since 21789a21 was 21789a21, checked in by Sebastian Huber <sebastian.huber@…>, on Jul 28, 2015 at 12:45:42 PM

score: Rename _POSIX_Absolute_timeout_to_ticks()

Rename _POSIX_Absolute_timeout_to_ticks() to
_TOD_Absolute_timeout_to_ticks() and move it to the score directory.
Delete empty <rtems/posix/time.h>.

  • Property mode set to 100644
File size: 4.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 <errno.h>
22#include <pthread.h>
23
24#include <rtems/system.h>
25#include <rtems/score/coremuteximpl.h>
26#include <rtems/score/watchdog.h>
27#include <rtems/posix/muteximpl.h>
28#include <rtems/posix/priorityimpl.h>
29
30/**
31 * 11.3.2 Initializing and Destroying a Mutex, P1003.1c/Draft 10, p. 87
32 *
33 * NOTE:  XXX Could be optimized so all the attribute error checking
34 *            is not performed when attr is NULL.
35 */
36
37int pthread_mutex_init(
38  pthread_mutex_t           *mutex,
39  const pthread_mutexattr_t *attr
40)
41{
42  POSIX_Mutex_Control          *the_mutex;
43  CORE_mutex_Attributes        *the_mutex_attr;
44  const pthread_mutexattr_t    *the_attr;
45  CORE_mutex_Disciplines        the_discipline;
46
47  if ( attr ) the_attr = attr;
48  else        the_attr = &_POSIX_Mutex_Default_attributes;
49
50  /* Check for NULL mutex */
51  if ( !mutex )
52    return EINVAL;
53
54  /*
55   *  This code should eventually be removed.
56   *
57   *  Although the POSIX specification says:
58   *
59   *  "Attempting to initialize an already initialized mutex results
60   *  in undefined behavior."
61   *
62   *  Trying to keep the caller from doing the create when *mutex
63   *  is actually a valid ID causes grief.  All it takes is the wrong
64   *  value in an uninitialized variable to make this fail.  As best
65   *  I can tell, RTEMS was the only pthread implementation to choose
66   *  this option for "undefined behavior" and doing so has created
67   *  portability problems.  In particular, Rosimildo DaSilva
68   *  <rdasilva@connecttel.com> saw seemingly random failures in the
69   *  RTEMS port of omniORB2 when this code was enabled.
70   *
71   *  Joel Sherrill <joel@OARcorp.com>     14 May 1999
72   *  NOTE: Be careful to avoid infinite recursion on call to this
73   *        routine in _POSIX_Mutex_Get.
74   */
75  #if 0
76  {
77    POSIX_Mutex_Control *mutex_in_use;
78    Objects_Locations    location;
79
80    if ( *mutex != PTHREAD_MUTEX_INITIALIZER ) {
81
82      /* EBUSY if *mutex is a valid id */
83
84      mutex_in_use = _POSIX_Mutex_Get( mutex, &location );
85      switch ( location ) {
86        case OBJECTS_LOCAL:
87          _Objects_Put( &mutex_in_use->Object );
88          return EBUSY;
89        #if defined(RTEMS_MULTIPROCESSING)
90          case OBJECTS_REMOTE:
91        #endif
92        case OBJECTS_ERROR:
93          break;
94      }
95    }
96  }
97  #endif
98
99  if ( !the_attr->is_initialized )
100    return EINVAL;
101
102  /*
103   *  We only support process private mutexes.
104   */
105  if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED )
106    return ENOSYS;
107
108  if ( the_attr->process_shared != PTHREAD_PROCESS_PRIVATE )
109    return EINVAL;
110
111  /*
112   *  Determine the discipline of the mutex
113   */
114  switch ( the_attr->protocol ) {
115    case PTHREAD_PRIO_NONE:
116      the_discipline = CORE_MUTEX_DISCIPLINES_FIFO;
117      break;
118    case PTHREAD_PRIO_INHERIT:
119      the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
120      break;
121    case PTHREAD_PRIO_PROTECT:
122      the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
123      break;
124    default:
125      return EINVAL;
126  }
127
128  /*
129   *  Validate the priority ceiling field -- should always be valid.
130   */
131  if ( !_POSIX_Priority_Is_valid( the_attr->prio_ceiling ) )
132    return EINVAL;
133
134#if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES)
135  /*
136   *  Validate the mutex type and set appropriate SuperCore mutex
137   *  attributes.
138   */
139  switch ( the_attr->type ) {
140    case PTHREAD_MUTEX_NORMAL:
141    case PTHREAD_MUTEX_RECURSIVE:
142    case PTHREAD_MUTEX_ERRORCHECK:
143    case PTHREAD_MUTEX_DEFAULT:
144      break;
145
146    default:
147      return EINVAL;
148  }
149#endif
150
151  the_mutex = _POSIX_Mutex_Allocate();
152
153  if ( !the_mutex ) {
154    _Objects_Allocator_unlock();
155    return EAGAIN;
156  }
157
158  the_mutex->process_shared = the_attr->process_shared;
159
160  the_mutex_attr = &the_mutex->Mutex.Attributes;
161
162  if ( the_attr->type == PTHREAD_MUTEX_RECURSIVE )
163    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
164  else
165    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_IS_ERROR;
166  the_mutex_attr->only_owner_release = true;
167  the_mutex_attr->priority_ceiling =
168    _POSIX_Priority_To_core( the_attr->prio_ceiling );
169  the_mutex_attr->discipline = the_discipline;
170
171  /*
172   *  Must be initialized to unlocked.
173   */
174  _CORE_mutex_Initialize( &the_mutex->Mutex, NULL, the_mutex_attr, false );
175
176  _Objects_Open_u32( &_POSIX_Mutex_Information, &the_mutex->Object, 0 );
177
178  *mutex = the_mutex->Object.id;
179
180  _Objects_Allocator_unlock();
181  return 0;
182}
Note: See TracBrowser for help on using the repository browser.