source: rtems/cpukit/score/src/coremutexsurrender.c @ bbe6a5fe

4.115
Last change on this file since bbe6a5fe was bbe6a5fe, checked in by Joel Sherrill <joel.sherrill@…>, on 07/15/10 at 15:44:40

2010-07-15 Sebastian Huber <Sebastian.Huber@…>

PR 1607/cpukit

  • score/src/coremutexsurrender.c: Clean up and split out helper routines.
  • Property mode set to 100644
File size: 6.6 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        case CORE_MUTEX_NESTING_IS_ERROR:
141          /* should never occur */
142          return CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED;
143        case CORE_MUTEX_NESTING_BLOCKS:
144          /* Currently no API exercises this behavior. */
145          break;
146      }
147    #else
148      /* must be CORE_MUTEX_NESTING_ACQUIRES or we wouldn't be here */
149      return CORE_MUTEX_STATUS_SUCCESSFUL;
150    #endif
151  }
152
153  /*
154   *  Formally release the mutex before possibly transferring it to a
155   *  blocked thread.
156   */
157  if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
158       _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
159    CORE_mutex_Status pop_status =
160      _CORE_mutex_Pop_priority( the_mutex, holder );
161
162    if ( pop_status != CORE_MUTEX_STATUS_SUCCESSFUL )
163      return pop_status;
164
165    holder->resource_count--;
166
167    /*
168     *  Whether or not someone is waiting for the mutex, an
169     *  inherited priority must be lowered if this is the last
170     *  mutex (i.e. resource) this task has.
171     */
172    if ( holder->resource_count == 0 &&
173         holder->real_priority != holder->current_priority ) {
174      _Thread_Change_priority( holder, holder->real_priority, true );
175    }
176  }
177  the_mutex->holder    = NULL;
178  the_mutex->holder_id = 0;
179
180  /*
181   *  Now we check if another thread was waiting for this mutex.  If so,
182   *  transfer the mutex to that thread.
183   */
184  if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) {
185
186#if defined(RTEMS_MULTIPROCESSING)
187    if ( !_Objects_Is_local_id( the_thread->Object.id ) ) {
188
189      the_mutex->holder     = NULL;
190      the_mutex->holder_id  = the_thread->Object.id;
191      the_mutex->nest_count = 1;
192
193      ( *api_mutex_mp_support)( the_thread, id );
194
195    } else
196#endif
197    {
198
199      the_mutex->holder     = the_thread;
200      the_mutex->holder_id  = the_thread->Object.id;
201      the_mutex->nest_count = 1;
202
203      switch ( the_mutex->Attributes.discipline ) {
204        case CORE_MUTEX_DISCIPLINES_FIFO:
205        case CORE_MUTEX_DISCIPLINES_PRIORITY:
206          break;
207        case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
208          _CORE_mutex_Push_priority( the_mutex, the_thread );
209          the_thread->resource_count++;
210          break;
211        case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
212          _CORE_mutex_Push_priority( the_mutex, the_thread );
213          the_thread->resource_count++;
214          if (the_mutex->Attributes.priority_ceiling <
215              the_thread->current_priority){
216              _Thread_Change_priority(
217                the_thread,
218                the_mutex->Attributes.priority_ceiling,
219                false
220              );
221          }
222          break;
223      }
224    }
225  } else
226    the_mutex->lock = CORE_MUTEX_UNLOCKED;
227
228  return CORE_MUTEX_STATUS_SUCCESSFUL;
229}
Note: See TracBrowser for help on using the repository browser.