source: rtems/cpukit/posix/src/mutexinit.c @ 860c34e

4.104.114.9
Last change on this file since 860c34e was 860c34e, checked in by Glenn Humphrey <glenn.humphrey@…>, on Nov 30, 2007 at 8:34:13 PM

2007-11-30 Glenn Humphrey <glenn.humphrey@…>

  • posix/include/rtems/posix/mutex.h, posix/include/rtems/posix/semaphore.h, posix/src/cancel.c, posix/src/conddestroy.c, posix/src/condsignalsupp.c, posix/src/condwaitsupp.c, posix/src/keydelete.c, posix/src/keygetspecific.c, posix/src/keysetspecific.c, posix/src/mqueueclose.c, posix/src/mqueuegetattr.c, posix/src/mqueuenotify.c, posix/src/mqueuerecvsupp.c, posix/src/mqueuesendsupp.c, posix/src/mqueuesetattr.c, posix/src/mqueuetranslatereturncode.c, posix/src/mutexdestroy.c, posix/src/mutexgetprioceiling.c, posix/src/mutexinit.c, posix/src/mutexlocksupp.c, posix/src/mutexsetprioceiling.c, posix/src/mutexunlock.c, posix/src/pbarrierdestroy.c, posix/src/pbarriertranslatereturncode.c, posix/src/pbarrierwait.c, posix/src/prwlockdestroy.c, posix/src/prwlockrdlock.c, posix/src/prwlocktimedrdlock.c, posix/src/prwlocktimedwrlock.c, posix/src/prwlocktranslatereturncode.c, posix/src/prwlocktryrdlock.c, posix/src/prwlocktrywrlock.c, posix/src/prwlockunlock.c, posix/src/prwlockwrlock.c, posix/src/pspindestroy.c, posix/src/pspinlock.c, posix/src/pspinlocktranslatereturncode.c, posix/src/pspintrylock.c, posix/src/pspinunlock.c, posix/src/pthreaddetach.c, posix/src/pthreadequal.c, posix/src/pthreadgetschedparam.c, posix/src/pthreadjoin.c, posix/src/pthreadkill.c, posix/src/pthreadsetschedparam.c, posix/src/ptimer1.c, posix/src/semaphorewaitsupp.c, posix/src/semclose.c, posix/src/semdestroy.c, posix/src/semgetvalue.c, posix/src/sempost.c, posix/src/types.c, rtems/src/msgqtranslatereturncode.c, rtems/src/semobtain.c, rtems/src/timerfireafter.c, score/include/rtems/system.h, score/include/rtems/score/corebarrier.h, score/include/rtems/score/coremsg.h, score/include/rtems/score/coremutex.h, score/include/rtems/score/coresem.h: Restructed to move the OBJECTS_LOCAL case to the top of the switch statement and eliminate the fall-through return of POSIX_BOTTOM_REACHED. These changes produced simplier assembly code and allowed for complete test coverage. Also applied some consistency to the functions that translate the core status codes to POSIX status codes.
  • posix/src/mutextranslatereturncode.c, posix/src/semaphoretranslatereturncode.c: New files.
  • posix/src/mutexfromcorestatus.c: Removed.
  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 *  $Id$
3 */
4
5#if HAVE_CONFIG_H
6#include "config.h"
7#endif
8
9#include <assert.h>
10#include <errno.h>
11#include <pthread.h>
12
13#include <rtems/system.h>
14#include <rtems/score/coremutex.h>
15#include <rtems/score/watchdog.h>
16#if defined(RTEMS_MULTIPROCESSING)
17#include <rtems/score/mpci.h>
18#endif
19#include <rtems/posix/mutex.h>
20#include <rtems/posix/priority.h>
21#include <rtems/posix/time.h>
22
23/*PAGE
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#if 0
41  register POSIX_Mutex_Control *mutex_in_use;
42  Objects_Locations             location;
43#endif
44
45  if ( attr ) the_attr = attr;
46  else        the_attr = &_POSIX_Mutex_Default_attributes;
47
48  /* Check for NULL mutex */
49
50  if ( !mutex )
51    return EINVAL;
52
53  /*
54   *  This code should eventually be removed.
55   *
56   *  Although the POSIX specification says:
57   *
58   *  "Attempting to initialize an already initialized mutex results
59   *  in undefined behavior."
60   *
61   *  Trying to keep the caller from doing the create when *mutex
62   *  is actually a valid ID causes grief.  All it takes is the wrong
63   *  value in an uninitialized variable to make this fail.  As best
64   *  I can tell, RTEMS was the only pthread implementation to choose
65   *  this option for "undefined behavior" and doing so has created
66   *  portability problems.  In particular, Rosimildo DaSilva
67   *  <rdasilva@connecttel.com> saw seemingly random failures in the
68   *  RTEMS port of omniORB2 when this code was enabled.
69   *
70   *  Joel Sherrill <joel@OARcorp.com>     14 May 1999
71   */
72#if 0
73  /* avoid infinite recursion on call to this routine in _POSIX_Mutex_Get */
74
75  if ( *mutex != PTHREAD_MUTEX_INITIALIZER ) {
76
77    /* EBUSY if *mutex is a valid id */
78
79    mutex_in_use = _POSIX_Mutex_Get( mutex, &location );
80    switch ( location ) {
81      case OBJECTS_LOCAL:
82        _Thread_Enable_dispatch();
83        return EBUSY;
84#if defined(RTEMS_MULTIPROCESSING)
85      case OBJECTS_REMOTE:
86#endif
87      case OBJECTS_ERROR:
88        break;
89    }
90  }
91#endif
92
93  if ( !the_attr->is_initialized )
94    return EINVAL;
95
96  /*
97   *  XXX: Be careful about attributes when global!!!
98   */
99
100  assert( the_attr->process_shared == PTHREAD_PROCESS_PRIVATE );
101
102  if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED )
103    return ENOSYS;
104
105  /*
106   *  Determine the discipline of the mutex
107   */
108
109  switch ( the_attr->protocol ) {
110    case PTHREAD_PRIO_NONE:
111      the_discipline = CORE_MUTEX_DISCIPLINES_FIFO;
112      break;
113    case PTHREAD_PRIO_INHERIT:
114      the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
115      break;
116    case PTHREAD_PRIO_PROTECT:
117      the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
118      break;
119    default:
120      return EINVAL;
121  }
122
123  if ( !_POSIX_Priority_Is_valid( the_attr->prio_ceiling ) )
124    return EINVAL;
125
126  _Thread_Disable_dispatch();
127
128  the_mutex = _POSIX_Mutex_Allocate();
129
130  if ( !the_mutex ) {
131    _Thread_Enable_dispatch();
132    return EAGAIN;
133  }
134
135#if defined(RTEMS_MULTIPROCESSING)
136  if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED &&
137       !( _Objects_MP_Allocate_and_open( &_POSIX_Mutex_Information, 0,
138                            the_mutex->Object.id, FALSE ) ) ) {
139    _POSIX_Mutex_Free( the_mutex );
140    _Thread_Enable_dispatch();
141    return EAGAIN;
142  }
143#endif
144
145  the_mutex->process_shared = the_attr->process_shared;
146
147  the_mutex_attr = &the_mutex->Mutex.Attributes;
148
149  if ( the_attr->recursive )
150    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
151  else
152    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_IS_ERROR;
153  the_mutex_attr->only_owner_release = TRUE;
154  the_mutex_attr->priority_ceiling =
155    _POSIX_Priority_To_core( the_attr->prio_ceiling );
156  the_mutex_attr->discipline = the_discipline;
157
158  /*
159   *  Must be initialized to unlocked.
160   */
161
162  _CORE_mutex_Initialize(
163    &the_mutex->Mutex,
164    the_mutex_attr,
165    CORE_MUTEX_UNLOCKED
166  );
167
168  _Objects_Open( &_POSIX_Mutex_Information, &the_mutex->Object, 0 );
169
170  *mutex = the_mutex->Object.id;
171
172#if defined(RTEMS_MULTIPROCESSING)
173  if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED )
174    _POSIX_Mutex_MP_Send_process_packet(
175      POSIX_MUTEX_MP_ANNOUNCE_CREATE,
176      the_mutex->Object.id,
177      0,                         /* Name not used */
178      0                          /* Not used */
179    );
180#endif
181
182  _Thread_Enable_dispatch();
183  return 0;
184}
Note: See TracBrowser for help on using the repository browser.