source: rtems/cpukit/score/src/coremutexsurrender.c @ 5472ad41

4.115
Last change on this file since 5472ad41 was 2610747, checked in by Joel Sherrill <joel.sherrill@…>, on 02/14/11 at 20:21:31

2011-02-14 Joel Sherrill <joel.sherrilL@…>

  • rtems/src/semtranslatereturncode.c, score/src/coremutexsurrender.c: Now all conditionals have the correct sense and all tests pass.
  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 *  Mutex Handler
3 *
4 *  DESCRIPTION:
5 *
6 *  This package is the implementation of the Mutex Handler.
7 *  This handler provides synchronization and mutual exclusion capabilities.
8 *
9 *  COPYRIGHT (c) 1989-2006.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.com/license/LICENSE.
15 *
16 *  $Id$
17 */
18
19#if HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <rtems/system.h>
24#include <rtems/score/isr.h>
25#include <rtems/score/coremutex.h>
26#include <rtems/score/states.h>
27#include <rtems/score/thread.h>
28#include <rtems/score/threadq.h>
29
30#ifdef __RTEMS_STRICT_ORDER_MUTEX__
31  static inline void _CORE_mutex_Push_priority(
32    CORE_mutex_Control *mutex,
33    Thread_Control *thread
34  )
35  {
36    _Chain_Prepend_unprotected(
37      &thread->lock_mutex,
38      &mutex->queue.lock_queue
39    );
40    mutex->queue.priority_before = thread->current_priority;
41  }
42
43  static inline CORE_mutex_Status _CORE_mutex_Pop_priority(
44    CORE_mutex_Control *mutex,
45    Thread_Control *holder
46  )
47  {
48    /*
49     *  Check whether the holder release the mutex in LIFO order if not return
50     *  error code.
51     */
52    if ( _Chain_First( holder->lock_mutex ) != &mutex->queue.lock_queue ) {
53      mutex->nest_count++;
54
55      return CORE_MUTEX_RELEASE_NOT_ORDER;
56    }
57
58    /*
59     *  This pops the first node from the list.
60     */
61    _Chain_Get_first_unprotected( &holder->lock_mutex );
62
63    if ( mutex->queue.priority_before != holder->current_priority )
64      _Thread_Change_priority( holder, mutex->queue.priority_before, true );
65
66    return CORE_MUTEX_STATUS_SUCCESSFUL;
67  }
68#else
69  #define _CORE_mutex_Push_priority( mutex, thread ) ((void) 0)
70
71  #define _CORE_mutex_Pop_priority( mutex, thread ) \
72    CORE_MUTEX_STATUS_SUCCESSFUL
73#endif
74
75/*
76 *  _CORE_mutex_Surrender
77 *
78 *  DESCRIPTION:
79 *
80 *  This routine frees a unit to the mutex.  If a task was blocked waiting for
81 *  a unit from this mutex, then that task will be readied and the unit
82 *  given to that task.  Otherwise, the unit will be returned to the mutex.
83 *
84 *  Input parameters:
85 *    the_mutex            - the mutex to be flushed
86 *    id                   - id of parent mutex
87 *    api_mutex_mp_support - api dependent MP support actions
88 *
89 *  Output parameters:
90 *    CORE_MUTEX_STATUS_SUCCESSFUL - if successful
91 *    core error code              - if unsuccessful
92 */
93
94CORE_mutex_Status _CORE_mutex_Surrender(
95  CORE_mutex_Control                *the_mutex,
96#if defined(RTEMS_MULTIPROCESSING)
97  Objects_Id                         id,
98  CORE_mutex_API_mp_support_callout  api_mutex_mp_support
99#else
100  Objects_Id                         id __attribute__((unused)),
101  CORE_mutex_API_mp_support_callout  api_mutex_mp_support __attribute__((unused))
102#endif
103)
104{
105  Thread_Control *the_thread;
106  Thread_Control *holder;
107
108  holder = the_mutex->holder;
109
110  /*
111   *  The following code allows a thread (or ISR) other than the thread
112   *  which acquired the mutex to release that mutex.  This is only
113   *  allowed when the mutex in quetion is FIFO or simple Priority
114   *  discipline.  But Priority Ceiling or Priority Inheritance mutexes
115   *  must be released by the thread which acquired them.
116   */
117
118  if ( the_mutex->Attributes.only_owner_release ) {
119    if ( !_Thread_Is_executing( holder ) )
120      return CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE;
121  }
122
123  /* XXX already unlocked -- not right status */
124
125  if ( !the_mutex->nest_count )
126    return CORE_MUTEX_STATUS_SUCCESSFUL;
127
128  the_mutex->nest_count--;
129
130  if ( the_mutex->nest_count != 0 ) {
131    /*
132     *  All error checking is on the locking side, so if the lock was
133     *  allowed to acquired multiple times, then we should just deal with
134     *  that.  The RTEMS_DEBUG is just a validation.
135     */
136    #if defined(RTEMS_DEBUG)
137      switch ( the_mutex->Attributes.lock_nesting_behavior ) {
138        case CORE_MUTEX_NESTING_ACQUIRES:
139          return CORE_MUTEX_STATUS_SUCCESSFUL;
140        #if defined(RTEMS_POSIX_API)
141          case CORE_MUTEX_NESTING_IS_ERROR:
142            /* should never occur */
143            return CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED;
144        #endif
145        case CORE_MUTEX_NESTING_BLOCKS:
146          /* Currently no API exercises this behavior. */
147          break;
148      }
149    #else
150      /* must be CORE_MUTEX_NESTING_ACQUIRES or we wouldn't be here */
151      return CORE_MUTEX_STATUS_SUCCESSFUL;
152    #endif
153  }
154
155  /*
156   *  Formally release the mutex before possibly transferring it to a
157   *  blocked thread.
158   */
159  if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
160       _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
161    CORE_mutex_Status pop_status =
162      _CORE_mutex_Pop_priority( the_mutex, holder );
163
164    if ( pop_status != CORE_MUTEX_STATUS_SUCCESSFUL )
165      return pop_status;
166
167    holder->resource_count--;
168
169    /*
170     *  Whether or not someone is waiting for the mutex, an
171     *  inherited priority must be lowered if this is the last
172     *  mutex (i.e. resource) this task has.
173     */
174    if ( holder->resource_count == 0 &&
175         holder->real_priority != holder->current_priority ) {
176      _Thread_Change_priority( holder, holder->real_priority, true );
177    }
178  }
179  the_mutex->holder    = NULL;
180  the_mutex->holder_id = 0;
181
182  /*
183   *  Now we check if another thread was waiting for this mutex.  If so,
184   *  transfer the mutex to that thread.
185   */
186  if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) {
187
188#if defined(RTEMS_MULTIPROCESSING)
189    if ( !_Objects_Is_local_id( the_thread->Object.id ) ) {
190
191      the_mutex->holder     = NULL;
192      the_mutex->holder_id  = the_thread->Object.id;
193      the_mutex->nest_count = 1;
194
195      ( *api_mutex_mp_support)( the_thread, id );
196
197    } else
198#endif
199    {
200
201      the_mutex->holder     = the_thread;
202      the_mutex->holder_id  = the_thread->Object.id;
203      the_mutex->nest_count = 1;
204
205      switch ( the_mutex->Attributes.discipline ) {
206        case CORE_MUTEX_DISCIPLINES_FIFO:
207        case CORE_MUTEX_DISCIPLINES_PRIORITY:
208          break;
209        case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
210          _CORE_mutex_Push_priority( the_mutex, the_thread );
211          the_thread->resource_count++;
212          break;
213        case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
214          _CORE_mutex_Push_priority( the_mutex, the_thread );
215          the_thread->resource_count++;
216          if (the_mutex->Attributes.priority_ceiling <
217              the_thread->current_priority){
218              _Thread_Change_priority(
219                the_thread,
220                the_mutex->Attributes.priority_ceiling,
221                false
222              );
223          }
224          break;
225      }
226    }
227  } else
228    the_mutex->lock = CORE_MUTEX_UNLOCKED;
229
230  return CORE_MUTEX_STATUS_SUCCESSFUL;
231}
Note: See TracBrowser for help on using the repository browser.