source: rtems/c/src/exec/score/src/coremutexsurrender.c @ 5283cc82

4.104.114.84.95
Last change on this file since 5283cc82 was db6ec79e, checked in by Joel Sherrill <joel.sherrill@…>, on 01/05/00 at 18:30:09

Fixed recursive release for mutexes with nesting allowed.

  • Property mode set to 100644
File size: 4.2 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-1999.
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.OARcorp.com/rtems/license.html.
15 *
16 *  $Id$
17 */
18
19#include <rtems/system.h>
20#include <rtems/score/isr.h>
21#include <rtems/score/coremutex.h>
22#include <rtems/score/states.h>
23#include <rtems/score/thread.h>
24#include <rtems/score/threadq.h>
25
26/*
27 *  _CORE_mutex_Surrender
28 *
29 *  DESCRIPTION:
30 *
31 *  This routine frees a unit to the mutex.  If a task was blocked waiting for
32 *  a unit from this mutex, then that task will be readied and the unit
33 *  given to that task.  Otherwise, the unit will be returned to the mutex.
34 *
35 *  Input parameters:
36 *    the_mutex            - the mutex to be flushed
37 *    id                   - id of parent mutex
38 *    api_mutex_mp_support - api dependent MP support actions
39 *
40 *  Output parameters:
41 *    CORE_MUTEX_STATUS_SUCCESSFUL - if successful
42 *    core error code              - if unsuccessful
43 */
44
45CORE_mutex_Status _CORE_mutex_Surrender(
46  CORE_mutex_Control                *the_mutex,
47  Objects_Id                         id,
48  CORE_mutex_API_mp_support_callout  api_mutex_mp_support
49)
50{
51  Thread_Control *the_thread;
52  Thread_Control *executing;
53
54  executing = _Thread_Executing;
55
56  /*
57   *  The following code allows a thread (or ISR) other than the thread
58   *  which acquired the mutex to release that mutex.  This is only
59   *  allowed when the mutex in quetion is FIFO or simple Priority
60   *  discipline.  But Priority Ceiling or Priority Inheritance mutexes
61   *  must be released by the thread which acquired them.
62   */
63
64  if ( the_mutex->Attributes.allow_nesting ) {
65    if ( !_Objects_Are_ids_equal(
66             _Thread_Executing->Object.id, the_mutex->holder_id ) ) {
67 
68      switch ( the_mutex->Attributes.discipline ) {
69        case CORE_MUTEX_DISCIPLINES_FIFO:
70        case CORE_MUTEX_DISCIPLINES_PRIORITY:
71          break;
72        case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
73        case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
74          return( CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE );
75          break;
76      }
77    }
78  }
79
80  /* XXX already unlocked -- not right status */
81
82  if ( !the_mutex->nest_count )
83    return( CORE_MUTEX_STATUS_SUCCESSFUL );
84
85  the_mutex->nest_count--;
86
87  if ( the_mutex->nest_count != 0 ) {
88    if ( the_mutex->Attributes.allow_nesting )
89      return( CORE_MUTEX_STATUS_SUCCESSFUL );
90    return( CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED );
91  }
92
93  _Thread_Executing->resource_count--;
94  the_mutex->holder    = NULL;
95  the_mutex->holder_id = 0;
96
97  /*
98   *  Whether or not someone is waiting for the mutex, an
99   *  inherited priority must be lowered if this is the last
100   *  mutex (i.e. resource) this task has.
101   */
102
103  switch ( the_mutex->Attributes.discipline ) {
104    case CORE_MUTEX_DISCIPLINES_FIFO:
105    case CORE_MUTEX_DISCIPLINES_PRIORITY:
106      break;
107    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
108    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
109      if ( executing->resource_count == 0 &&
110           executing->real_priority != executing->current_priority ) {
111         _Thread_Change_priority( executing, executing->real_priority, TRUE );
112      }
113      break;
114  }
115
116
117  if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) {
118
119#if defined(RTEMS_MULTIPROCESSING)
120    if ( !_Objects_Is_local_id( the_thread->Object.id ) ) {
121     
122      the_mutex->holder     = NULL;
123      the_mutex->holder_id  = the_thread->Object.id;
124      the_mutex->nest_count = 1;
125
126      ( *api_mutex_mp_support)( the_thread, id );
127
128    } else
129#endif
130    {
131
132      the_mutex->holder     = the_thread;
133      the_mutex->holder_id  = the_thread->Object.id;
134      the_thread->resource_count++;
135      the_mutex->nest_count = 1;
136
137     /*
138      *  No special action for priority inheritance or priority ceiling
139      *  because the_thread is guaranteed to be the highest priority
140      *  thread waiting for the mutex.
141      */
142    }
143  } else
144    the_mutex->lock = CORE_MUTEX_UNLOCKED;
145
146  return( CORE_MUTEX_STATUS_SUCCESSFUL );
147}
148
Note: See TracBrowser for help on using the repository browser.