source: rtems/cpukit/posix/src/mutexinit.c @ 8cdf733

4.104.115
Last change on this file since 8cdf733 was 8cdf733, checked in by Joel Sherrill <joel.sherrill@…>, on 07/06/09 at 14:46:36

2009-07-06 Joel Sherrill <joel.sherrill@…>

  • posix/Makefile.am, posix/src/mutex.c, posix/src/mutexinit.c: Add initial support for the pthread mutex type attribute added by UNIX98. It can be normal, recursive, errorcheck or default.
  • posix/src/mutexattrgettype.c, posix/src/mutexattrsettype.c: New files.
  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2007.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.com/license/LICENSE.
8 *
9 *  $Id$
10 */
11
12#if HAVE_CONFIG_H
13#include "config.h"
14#endif
15
16#include <errno.h>
17#include <pthread.h>
18
19#include <rtems/system.h>
20#include <rtems/score/coremutex.h>
21#include <rtems/score/watchdog.h>
22#include <rtems/posix/mutex.h>
23#include <rtems/posix/priority.h>
24#include <rtems/posix/time.h>
25
26/*PAGE
27 *
28 *  11.3.2 Initializing and Destroying a Mutex, P1003.1c/Draft 10, p. 87
29 *
30 *  NOTE:  XXX Could be optimized so all the attribute error checking
31 *             is not performed when attr is NULL.
32 */
33
34int pthread_mutex_init(
35  pthread_mutex_t           *mutex,
36  const pthread_mutexattr_t *attr
37)
38{
39  POSIX_Mutex_Control          *the_mutex;
40  CORE_mutex_Attributes        *the_mutex_attr;
41  const pthread_mutexattr_t    *the_attr;
42  CORE_mutex_Disciplines        the_discipline;
43
44  if ( attr ) the_attr = attr;
45  else        the_attr = &_POSIX_Mutex_Default_attributes;
46
47  /* Check for NULL mutex */
48  if ( !mutex )
49    return EINVAL;
50
51  /*
52   *  This code should eventually be removed.
53   *
54   *  Although the POSIX specification says:
55   *
56   *  "Attempting to initialize an already initialized mutex results
57   *  in undefined behavior."
58   *
59   *  Trying to keep the caller from doing the create when *mutex
60   *  is actually a valid ID causes grief.  All it takes is the wrong
61   *  value in an uninitialized variable to make this fail.  As best
62   *  I can tell, RTEMS was the only pthread implementation to choose
63   *  this option for "undefined behavior" and doing so has created
64   *  portability problems.  In particular, Rosimildo DaSilva
65   *  <rdasilva@connecttel.com> saw seemingly random failures in the
66   *  RTEMS port of omniORB2 when this code was enabled.
67   *
68   *  Joel Sherrill <joel@OARcorp.com>     14 May 1999
69   *  NOTE: Be careful to avoid infinite recursion on call to this
70   *        routine in _POSIX_Mutex_Get.
71   */
72  #if 0
73  {
74    POSIX_Mutex_Control *mutex_in_use;
75    Objects_Locations    location;
76
77    if ( *mutex != PTHREAD_MUTEX_INITIALIZER ) {
78
79      /* EBUSY if *mutex is a valid id */
80
81      mutex_in_use = _POSIX_Mutex_Get( mutex, &location );
82      switch ( location ) {
83        case OBJECTS_LOCAL:
84          _Thread_Enable_dispatch();
85          return EBUSY;
86        #if defined(RTEMS_MULTIPROCESSING)
87          case OBJECTS_REMOTE:
88        #endif
89        case OBJECTS_ERROR:
90          break;
91      }
92    }
93  }
94  #endif
95
96  if ( !the_attr->is_initialized )
97    return EINVAL;
98
99  /*
100   *  We only support process private mutexes.
101   */
102  if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED )
103    return ENOSYS;
104
105  if ( the_attr->process_shared != PTHREAD_PROCESS_PRIVATE )
106    return EINVAL;
107
108  /*
109   *  Determine the discipline of the mutex
110   */
111  switch ( the_attr->protocol ) {
112    case PTHREAD_PRIO_NONE:
113      the_discipline = CORE_MUTEX_DISCIPLINES_FIFO;
114      break;
115    case PTHREAD_PRIO_INHERIT:
116      the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
117      break;
118    case PTHREAD_PRIO_PROTECT:
119      the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
120      break;
121    default:
122      return EINVAL;
123  }
124
125  /*
126   *  Validate the priority ceiling field -- should always be valid.
127   */
128  if ( !_POSIX_Priority_Is_valid( the_attr->prio_ceiling ) )
129    return EINVAL;
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  /*
149   *  Enter a dispatching critical section and begin to do the real work.
150   */
151  _Thread_Disable_dispatch();
152
153  the_mutex = _POSIX_Mutex_Allocate();
154
155  if ( !the_mutex ) {
156    _Thread_Enable_dispatch();
157    return EAGAIN;
158  }
159
160  the_mutex->process_shared = the_attr->process_shared;
161
162  the_mutex_attr = &the_mutex->Mutex.Attributes;
163
164  if ( the_attr->recursive )
165    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
166  else
167    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_IS_ERROR;
168  the_mutex_attr->only_owner_release = true;
169  the_mutex_attr->priority_ceiling =
170    _POSIX_Priority_To_core( the_attr->prio_ceiling );
171  the_mutex_attr->discipline = the_discipline;
172
173  /*
174   *  Must be initialized to unlocked.
175   */
176  _CORE_mutex_Initialize(
177    &the_mutex->Mutex,
178    the_mutex_attr,
179    CORE_MUTEX_UNLOCKED
180  );
181
182  _Objects_Open_u32( &_POSIX_Mutex_Information, &the_mutex->Object, 0 );
183
184  *mutex = the_mutex->Object.id;
185
186  _Thread_Enable_dispatch();
187  return 0;
188}
Note: See TracBrowser for help on using the repository browser.