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

4.115
Last change on this file since a0e6c73 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2009.
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
10#if HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <errno.h>
15#include <pthread.h>
16
17#include <rtems/system.h>
18#include <rtems/score/coremutex.h>
19#include <rtems/score/watchdog.h>
20#include <rtems/posix/mutex.h>
21#include <rtems/posix/priority.h>
22#include <rtems/posix/time.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   *  This code should eventually be removed.
50   *
51   *  Although 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.  As best
59   *  I can tell, RTEMS was the only pthread implementation to choose
60   *  this option for "undefined behavior" and doing so has created
61   *  portability problems.  In particular, Rosimildo DaSilva
62   *  <rdasilva@connecttel.com> saw seemingly random failures in the
63   *  RTEMS port of omniORB2 when this code was enabled.
64   *
65   *  Joel Sherrill <joel@OARcorp.com>     14 May 1999
66   *  NOTE: Be careful to avoid infinite recursion on call to this
67   *        routine in _POSIX_Mutex_Get.
68   */
69  #if 0
70  {
71    POSIX_Mutex_Control *mutex_in_use;
72    Objects_Locations    location;
73
74    if ( *mutex != PTHREAD_MUTEX_INITIALIZER ) {
75
76      /* EBUSY if *mutex is a valid id */
77
78      mutex_in_use = _POSIX_Mutex_Get( mutex, &location );
79      switch ( location ) {
80        case OBJECTS_LOCAL:
81          _Thread_Enable_dispatch();
82          return EBUSY;
83        #if defined(RTEMS_MULTIPROCESSING)
84          case OBJECTS_REMOTE:
85        #endif
86        case OBJECTS_ERROR:
87          break;
88      }
89    }
90  }
91  #endif
92
93  if ( !the_attr->is_initialized )
94    return EINVAL;
95
96  /*
97   *  We only support process private mutexes.
98   */
99  if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED )
100    return ENOSYS;
101
102  if ( the_attr->process_shared != PTHREAD_PROCESS_PRIVATE )
103    return EINVAL;
104
105  /*
106   *  Determine the discipline of the mutex
107   */
108  switch ( the_attr->protocol ) {
109    case PTHREAD_PRIO_NONE:
110      the_discipline = CORE_MUTEX_DISCIPLINES_FIFO;
111      break;
112    case PTHREAD_PRIO_INHERIT:
113      the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
114      break;
115    case PTHREAD_PRIO_PROTECT:
116      the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
117      break;
118    default:
119      return EINVAL;
120  }
121
122  /*
123   *  Validate the priority ceiling field -- should always be valid.
124   */
125  if ( !_POSIX_Priority_Is_valid( the_attr->prio_ceiling ) )
126    return EINVAL;
127
128#if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES)
129  /*
130   *  Validate the mutex type and set appropriate SuperCore mutex
131   *  attributes.
132   */
133  switch ( the_attr->type ) {
134    case PTHREAD_MUTEX_NORMAL:
135    case PTHREAD_MUTEX_RECURSIVE:
136    case PTHREAD_MUTEX_ERRORCHECK:
137    case PTHREAD_MUTEX_DEFAULT:
138      break;
139
140    default:
141      return EINVAL;
142  }
143#endif
144
145  /*
146   *  Enter a dispatching critical section and begin to do the real work.
147   */
148  _Thread_Disable_dispatch();
149
150  the_mutex = _POSIX_Mutex_Allocate();
151
152  if ( !the_mutex ) {
153    _Thread_Enable_dispatch();
154    return EAGAIN;
155  }
156
157  the_mutex->process_shared = the_attr->process_shared;
158
159  the_mutex_attr = &the_mutex->Mutex.Attributes;
160
161  if ( the_attr->recursive )
162    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
163  else
164    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_IS_ERROR;
165  the_mutex_attr->only_owner_release = true;
166  the_mutex_attr->priority_ceiling =
167    _POSIX_Priority_To_core( the_attr->prio_ceiling );
168  the_mutex_attr->discipline = the_discipline;
169
170  /*
171   *  Must be initialized to unlocked.
172   */
173  _CORE_mutex_Initialize(
174    &the_mutex->Mutex,
175    the_mutex_attr,
176    CORE_MUTEX_UNLOCKED
177  );
178
179  _Objects_Open_u32( &_POSIX_Mutex_Information, &the_mutex->Object, 0 );
180
181  *mutex = the_mutex->Object.id;
182
183  _Thread_Enable_dispatch();
184  return 0;
185}
Note: See TracBrowser for help on using the repository browser.