source: rtems/c/src/exec/posix/src/mutexinit.c @ 811fae1

4.104.114.84.95
Last change on this file since 811fae1 was 96c041c, checked in by Joel Sherrill <joel.sherrill@…>, on 11/02/99 at 17:19:23

Split mutex.c into multiple files.

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