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

Last change on this file since dafa5d88 was dafa5d88, checked in by Sebastian Huber <sebastian.huber@…>, on Sep 3, 2015 at 8:27:16 AM

score: Implement priority boosting

  • Property mode set to 100644
File size: 7.8 KB
Line 
1/**
2 * @file
3 *
4 * @brief Surrender the Mutex
5 * @ingroup ScoreMutex
6 */
7
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.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/system.h>
22#include <rtems/score/isr.h>
23#include <rtems/score/coremuteximpl.h>
24#include <rtems/score/thread.h>
25
26#ifdef __RTEMS_STRICT_ORDER_MUTEX__
27  static inline void _CORE_mutex_Push_priority(
28    CORE_mutex_Control *mutex,
29    Thread_Control *thread
30  )
31  {
32    _Chain_Prepend_unprotected(
33      &thread->lock_mutex,
34      &mutex->queue.lock_queue
35    );
36    mutex->queue.priority_before = thread->current_priority;
37  }
38
39  static inline CORE_mutex_Status _CORE_mutex_Pop_priority(
40    CORE_mutex_Control *mutex,
41    Thread_Control *holder
42  )
43  {
44    /*
45     *  Check whether the holder release the mutex in LIFO order if not return
46     *  error code.
47     */
48    if ( _Chain_First( &holder->lock_mutex ) != &mutex->queue.lock_queue ) {
49      mutex->nest_count++;
50
51      return CORE_MUTEX_RELEASE_NOT_ORDER;
52    }
53
54    /*
55     *  This pops the first node from the list.
56     */
57    _Chain_Get_first_unprotected( &holder->lock_mutex );
58
59    if ( mutex->queue.priority_before != holder->current_priority )
60      _Thread_Change_priority( holder, mutex->queue.priority_before, true );
61
62    return CORE_MUTEX_STATUS_SUCCESSFUL;
63  }
64#else
65  #define _CORE_mutex_Push_priority( mutex, thread ) ((void) 0)
66
67  #define _CORE_mutex_Pop_priority( mutex, thread ) \
68    CORE_MUTEX_STATUS_SUCCESSFUL
69#endif
70
71/*
72 *  _CORE_mutex_Surrender
73 *
74 *  This routine frees a unit to the mutex.  If a task was blocked waiting for
75 *  a unit from this mutex, then that task will be readied and the unit
76 *  given to that task.  Otherwise, the unit will be returned to the mutex.
77 *
78 *  Input parameters:
79 *    the_mutex            - the mutex to be flushed
80 *    id                   - id of parent mutex
81 *    api_mutex_mp_support - api dependent MP support actions
82 *
83 *  Output parameters:
84 *    CORE_MUTEX_STATUS_SUCCESSFUL - if successful
85 *    core error code              - if unsuccessful
86 */
87
88CORE_mutex_Status _CORE_mutex_Surrender(
89  CORE_mutex_Control                *the_mutex,
90#if defined(RTEMS_MULTIPROCESSING)
91  Objects_Id                         id,
92  CORE_mutex_API_mp_support_callout  api_mutex_mp_support,
93#else
94  Objects_Id                         id __attribute__((unused)),
95  CORE_mutex_API_mp_support_callout  api_mutex_mp_support __attribute__((unused)),
96#endif
97  ISR_lock_Context                  *lock_context
98)
99{
100  Thread_Control *the_thread;
101  Thread_Control *holder;
102
103  holder = the_mutex->holder;
104
105  /*
106   *  The following code allows a thread (or ISR) other than the thread
107   *  which acquired the mutex to release that mutex.  This is only
108   *  allowed when the mutex in quetion is FIFO or simple Priority
109   *  discipline.  But Priority Ceiling or Priority Inheritance mutexes
110   *  must be released by the thread which acquired them.
111   */
112
113  if ( the_mutex->Attributes.only_owner_release ) {
114    if ( !_Thread_Is_executing( holder ) ) {
115      _ISR_lock_ISR_enable( lock_context );
116      return CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE;
117    }
118  }
119
120  _Thread_queue_Acquire_critical( &the_mutex->Wait_queue, lock_context );
121
122  /* XXX already unlocked -- not right status */
123
124  if ( !the_mutex->nest_count ) {
125    _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
126    return CORE_MUTEX_STATUS_SUCCESSFUL;
127  }
128
129  the_mutex->nest_count--;
130
131  if ( the_mutex->nest_count != 0 ) {
132    /*
133     *  All error checking is on the locking side, so if the lock was
134     *  allowed to acquired multiple times, then we should just deal with
135     *  that.  The RTEMS_DEBUG is just a validation.
136     */
137    #if defined(RTEMS_DEBUG)
138      switch ( the_mutex->Attributes.lock_nesting_behavior ) {
139        case CORE_MUTEX_NESTING_ACQUIRES:
140          _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
141          return CORE_MUTEX_STATUS_SUCCESSFUL;
142        #if defined(RTEMS_POSIX_API)
143          case CORE_MUTEX_NESTING_IS_ERROR:
144            /* should never occur */
145            _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
146            return CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED;
147        #endif
148        case CORE_MUTEX_NESTING_BLOCKS:
149          /* Currently no API exercises this behavior. */
150          break;
151      }
152    #else
153      _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
154      /* must be CORE_MUTEX_NESTING_ACQUIRES or we wouldn't be here */
155      return CORE_MUTEX_STATUS_SUCCESSFUL;
156    #endif
157  }
158
159  /*
160   *  Formally release the mutex before possibly transferring it to a
161   *  blocked thread.
162   */
163  if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
164       _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
165    CORE_mutex_Status pop_status =
166      _CORE_mutex_Pop_priority( the_mutex, holder );
167
168    if ( pop_status != CORE_MUTEX_STATUS_SUCCESSFUL ) {
169      _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
170      return pop_status;
171    }
172
173    holder->resource_count--;
174  }
175  the_mutex->holder = NULL;
176
177  /*
178   *  Now we check if another thread was waiting for this mutex.  If so,
179   *  transfer the mutex to that thread.
180   */
181  if ( ( the_thread = _Thread_queue_First_locked( &the_mutex->Wait_queue ) ) ) {
182    bool unblock;
183
184    /*
185     * We must extract the thread now since this will restore its default
186     * thread lock.  This is necessary to avoid a deadlock in the
187     * _Thread_Change_priority() below due to a recursive thread queue lock
188     * acquire.
189     */
190    unblock = _Thread_queue_Extract_locked(
191      &the_mutex->Wait_queue.Queue,
192      the_mutex->Wait_queue.operations,
193      the_thread
194    );
195
196#if defined(RTEMS_MULTIPROCESSING)
197    _Thread_Dispatch_disable();
198
199    if ( _Objects_Is_local_id( the_thread->Object.id ) )
200#endif
201    {
202      the_mutex->holder     = the_thread;
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          _Thread_queue_Boost_priority( &the_mutex->Wait_queue.Queue, the_thread );
213          break;
214        case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
215          _CORE_mutex_Push_priority( the_mutex, the_thread );
216          the_thread->resource_count++;
217          _Thread_Raise_priority(
218            the_thread,
219            the_mutex->Attributes.priority_ceiling
220          );
221          break;
222      }
223    }
224
225    _Thread_queue_Unblock_critical(
226      unblock,
227      &the_mutex->Wait_queue.Queue,
228      the_thread,
229      lock_context
230    );
231
232#if defined(RTEMS_MULTIPROCESSING)
233    if ( !_Objects_Is_local_id( the_thread->Object.id ) ) {
234
235      the_mutex->holder     = NULL;
236      the_mutex->nest_count = 1;
237
238      ( *api_mutex_mp_support)( the_thread, id );
239
240    }
241
242    _Thread_Dispatch_enable( _Per_CPU_Get() );
243#endif
244  } else {
245    _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
246  }
247
248  /*
249   *  Whether or not someone is waiting for the mutex, an
250   *  inherited priority must be lowered if this is the last
251   *  mutex (i.e. resource) this task has.
252   */
253  if ( !_Thread_Owns_resources( holder ) ) {
254    /*
255     * Ensure that the holder resource count is visible to all other processors
256     * and that we read the latest priority restore hint.
257     */
258    _Atomic_Fence( ATOMIC_ORDER_ACQ_REL );
259
260    if ( holder->priority_restore_hint ) {
261      Per_CPU_Control *cpu_self;
262
263      cpu_self = _Thread_Dispatch_disable();
264      _Thread_Restore_priority( holder );
265      _Thread_Dispatch_enable( cpu_self );
266    }
267  }
268
269  return CORE_MUTEX_STATUS_SUCCESSFUL;
270}
Note: See TracBrowser for help on using the repository browser.