OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [exec/] [posix/] [src/] [mutexinit.c] - Blame information for rev 532

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
2
 *  $Id: mutexinit.c,v 1.2 2001-09-27 11:59:17 chris Exp $
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
 
27
int 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
  if ( the_attr->recursive )
148
    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
149
  else
150
    the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_IS_ERROR;
151
  the_mutex_attr->only_owner_release = TRUE;
152
  the_mutex_attr->priority_ceiling =
153
    _POSIX_Priority_To_core( the_attr->prio_ceiling );
154
  the_mutex_attr->discipline = the_discipline;
155
 
156
  /*
157
   *  Must be initialized to unlocked.
158
   */
159
 
160
  _CORE_mutex_Initialize(
161
    &the_mutex->Mutex,
162
    OBJECTS_POSIX_MUTEXES,
163
    the_mutex_attr,
164
    CORE_MUTEX_UNLOCKED,
165
    NULL                      /* proxy_extract_callout */
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
 
179
    );
180
#endif
181
 
182
  _Thread_Enable_dispatch();
183
  return 0;
184
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.