source: rtems/cpukit/posix/src/mutexinit.c @ 976162a6

4.104.114.9
Last change on this file since 976162a6 was 976162a6, checked in by Joel Sherrill <joel.sherrill@…>, on Dec 3, 2007 at 10:23:13 PM

2007-12-03 Joel Sherrill <joel.sherrill@…>

  • libcsupport/src/malloc.c, libmisc/monitor/mon-command.c, posix/preinstall.am, posix/include/rtems/posix/cond.h, posix/include/rtems/posix/mqueue.h, posix/include/rtems/posix/mutex.h, posix/include/rtems/posix/pthread.h, posix/include/rtems/posix/semaphore.h, posix/src/conddestroy.c, posix/src/mutexdestroy.c, posix/src/mutexinit.c, posix/src/mutexsetprioceiling.c, posix/src/mutexunlock.c, sapi/include/confdefs.h, sapi/include/rtems/config.h, sapi/include/rtems/init.h, sapi/include/rtems/sptables.h, sapi/src/exinit.c, score/include/rtems/system.h, score/include/rtems/score/mpci.h, score/src/mpci.c, score/src/thread.c, score/src/threadcreateidle.c, score/src/threadstackallocate.c, score/src/threadstackfree.c, score/src/wkspace.c: Moved most of the remaining CPU Table fields to the Configuration Table. This included pretasking_hook, predriver_hook, postdriver_hook, idle_task, do_zero_of_workspace, extra_mpci_receive_server_stack, stack_allocate_hook, and stack_free_hook. As a side-effect of this effort some multiprocessing code was made conditional and some style clean up occurred.
  • Property mode set to 100644
File size: 3.9 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#include <rtems/posix/mutex.h>
17#include <rtems/posix/priority.h>
18#include <rtems/posix/time.h>
19
20/*PAGE
21 *
22 *  11.3.2 Initializing and Destroying a Mutex, P1003.1c/Draft 10, p. 87
23 *
24 *  NOTE:  XXX Could be optimized so all the attribute error checking
25 *             is not performed when attr is NULL.
26 */
27
28int pthread_mutex_init(
29  pthread_mutex_t           *mutex,
30  const pthread_mutexattr_t *attr
31)
32{
33  POSIX_Mutex_Control          *the_mutex;
34  CORE_mutex_Attributes        *the_mutex_attr;
35  const pthread_mutexattr_t    *the_attr;
36  CORE_mutex_Disciplines        the_discipline;
37#if 0
38  register POSIX_Mutex_Control *mutex_in_use;
39  Objects_Locations             location;
40#endif
41
42  if ( attr ) the_attr = attr;
43  else        the_attr = &_POSIX_Mutex_Default_attributes;
44
45  /* Check for NULL mutex */
46
47  if ( !mutex )
48    return EINVAL;
49
50  /*
51   *  This code should eventually be removed.
52   *
53   *  Although the POSIX specification says:
54   *
55   *  "Attempting to initialize an already initialized mutex results
56   *  in undefined behavior."
57   *
58   *  Trying to keep the caller from doing the create when *mutex
59   *  is actually a valid ID causes grief.  All it takes is the wrong
60   *  value in an uninitialized variable to make this fail.  As best
61   *  I can tell, RTEMS was the only pthread implementation to choose
62   *  this option for "undefined behavior" and doing so has created
63   *  portability problems.  In particular, Rosimildo DaSilva
64   *  <rdasilva@connecttel.com> saw seemingly random failures in the
65   *  RTEMS port of omniORB2 when this code was enabled.
66   *
67   *  Joel Sherrill <joel@OARcorp.com>     14 May 1999
68   */
69#if 0
70  /* avoid infinite recursion on call to this routine in _POSIX_Mutex_Get */
71
72  if ( *mutex != PTHREAD_MUTEX_INITIALIZER ) {
73
74    /* EBUSY if *mutex is a valid id */
75
76    mutex_in_use = _POSIX_Mutex_Get( mutex, &location );
77    switch ( location ) {
78      case OBJECTS_LOCAL:
79        _Thread_Enable_dispatch();
80        return EBUSY;
81#if defined(RTEMS_MULTIPROCESSING)
82      case OBJECTS_REMOTE:
83#endif
84      case OBJECTS_ERROR:
85        break;
86    }
87  }
88#endif
89
90  if ( !the_attr->is_initialized )
91    return EINVAL;
92
93  /*
94   *  XXX: Be careful about attributes when global!!!
95   */
96
97  assert( the_attr->process_shared == PTHREAD_PROCESS_PRIVATE );
98
99  if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED )
100    return ENOSYS;
101
102  /*
103   *  Determine the discipline of the mutex
104   */
105
106  switch ( the_attr->protocol ) {
107    case PTHREAD_PRIO_NONE:
108      the_discipline = CORE_MUTEX_DISCIPLINES_FIFO;
109      break;
110    case PTHREAD_PRIO_INHERIT:
111      the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
112      break;
113    case PTHREAD_PRIO_PROTECT:
114      the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
115      break;
116    default:
117      return EINVAL;
118  }
119
120  if ( !_POSIX_Priority_Is_valid( the_attr->prio_ceiling ) )
121    return EINVAL;
122
123  _Thread_Disable_dispatch();
124
125  the_mutex = _POSIX_Mutex_Allocate();
126
127  if ( !the_mutex ) {
128    _Thread_Enable_dispatch();
129    return EAGAIN;
130  }
131
132  the_mutex->process_shared = the_attr->process_shared;
133
134  the_mutex_attr = &the_mutex->Mutex.Attributes;
135
136  if ( the_attr->recursive )
137    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
138  else
139    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_IS_ERROR;
140  the_mutex_attr->only_owner_release = TRUE;
141  the_mutex_attr->priority_ceiling =
142    _POSIX_Priority_To_core( the_attr->prio_ceiling );
143  the_mutex_attr->discipline = the_discipline;
144
145  /*
146   *  Must be initialized to unlocked.
147   */
148
149  _CORE_mutex_Initialize(
150    &the_mutex->Mutex,
151    the_mutex_attr,
152    CORE_MUTEX_UNLOCKED
153  );
154
155  _Objects_Open( &_POSIX_Mutex_Information, &the_mutex->Object, 0 );
156
157  *mutex = the_mutex->Object.id;
158
159  _Thread_Enable_dispatch();
160  return 0;
161}
Note: See TracBrowser for help on using the repository browser.