source: rtems/c/src/exec/score/src/coremutexseize.c @ 5870ac55

4.104.114.84.95
Last change on this file since 5870ac55 was 5870ac55, checked in by Joel Sherrill <joel.sherrill@…>, on Jan 5, 2000 at 10:19:21 PM

Added support for simple binary semaphores in addition to the high
power binary/mutex style semaphores already supported by RTEMS. This
was done at the request of Eric Norum <eric@…> in support
of his effort to port EPICS to RTEMS. This change consisted of
changing the nesting_allowed boolean into a lock_nesting_behavior
enumerated value as well as allowing the core mutex object to optionally
support ensuring that the holder of a binary semaphore released it.
Finally, a more subtle enhancement was to allow the non-holder to release
a priority inheritance/ceiling mutex and still allow the holding task
to return to its original priority.

  • Property mode set to 100644
File size: 4.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-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/*PAGE
27 *
28 *  _CORE_mutex_Seize
29 *
30 *  This routine attempts to allocate a mutex to the calling thread.
31 *
32 *  Input parameters:
33 *    the_mutex - pointer to mutex control block
34 *    id        - id of object to wait on
35 *    wait      - TRUE if wait is allowed, FALSE otherwise
36 *    timeout   - number of ticks to wait (0 means forever)
37 *
38 *  Output parameters:  NONE
39 *
40 *  INTERRUPT LATENCY:
41 *    available
42 *    wait
43 */
44
45void _CORE_mutex_Seize(
46  CORE_mutex_Control  *the_mutex,
47  Objects_Id           id,
48  boolean              wait,
49  Watchdog_Interval    timeout
50)
51{
52  Thread_Control *executing;
53  ISR_Level       level;
54
55  executing = _Thread_Executing;
56  switch ( the_mutex->Attributes.discipline ) {
57    case CORE_MUTEX_DISCIPLINES_FIFO:
58    case CORE_MUTEX_DISCIPLINES_PRIORITY:
59    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
60      break;
61    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
62      if ( executing->current_priority <
63                              the_mutex->Attributes.priority_ceiling) {
64        executing->Wait.return_code = CORE_MUTEX_STATUS_CEILING_VIOLATED;
65        return;
66      }
67  }
68  executing->Wait.return_code = CORE_MUTEX_STATUS_SUCCESSFUL;
69  _ISR_Disable( level );
70  if ( ! _CORE_mutex_Is_locked( the_mutex ) ) {
71    the_mutex->lock       = CORE_MUTEX_LOCKED;
72    the_mutex->holder     = executing;
73    the_mutex->holder_id  = executing->Object.id;
74    the_mutex->nest_count = 1;
75    executing->resource_count++;
76    _ISR_Enable( level );
77    switch ( the_mutex->Attributes.discipline ) {
78      case CORE_MUTEX_DISCIPLINES_FIFO:
79      case CORE_MUTEX_DISCIPLINES_PRIORITY:
80      case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
81        /* already the highest priority */
82        break;
83      case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
84      if ( the_mutex->Attributes.priority_ceiling <
85                                           executing->current_priority ) {
86        _Thread_Change_priority(
87          the_mutex->holder,
88          the_mutex->Attributes.priority_ceiling,
89          FALSE
90        );
91      }
92    }
93    executing->Wait.return_code = CORE_MUTEX_STATUS_SUCCESSFUL;
94    return;
95  }
96
97  if ( _Thread_Is_executing( the_mutex->holder ) ) {
98    switch ( the_mutex->Attributes.lock_nesting_behavior ) {
99      case CORE_MUTEX_NESTING_ACQUIRES:
100        the_mutex->nest_count++;
101        _ISR_Enable( level );
102        return;
103      case CORE_MUTEX_NESTING_IS_ERROR:
104        executing->Wait.return_code = CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED;
105        _ISR_Enable( level );
106        return;
107      case CORE_MUTEX_NESTING_BLOCKS:
108        break;
109    }
110  }
111
112  if ( !wait ) {
113    _ISR_Enable( level );
114    executing->Wait.return_code = CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT;
115    return;
116  }
117
118  _Thread_queue_Enter_critical_section( &the_mutex->Wait_queue );
119  executing->Wait.queue = &the_mutex->Wait_queue;
120  executing->Wait.id    = id;
121  _ISR_Enable( level );
122
123  switch ( the_mutex->Attributes.discipline ) {
124    case CORE_MUTEX_DISCIPLINES_FIFO:
125    case CORE_MUTEX_DISCIPLINES_PRIORITY:
126    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
127      break;
128    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
129      if ( the_mutex->holder->current_priority > executing->current_priority ) {
130        _Thread_Change_priority(
131          the_mutex->holder,
132          executing->current_priority,
133          FALSE
134        );
135      }
136      break;
137  }
138
139  _Thread_queue_Enqueue( &the_mutex->Wait_queue, timeout );
140
141  if ( _Thread_Executing->Wait.return_code == CORE_MUTEX_STATUS_SUCCESSFUL ) {
142    switch ( the_mutex->Attributes.discipline ) {
143      case CORE_MUTEX_DISCIPLINES_FIFO:
144      case CORE_MUTEX_DISCIPLINES_PRIORITY:
145      case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
146        break;
147      case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
148        if ( the_mutex->Attributes.priority_ceiling <
149                                           executing->current_priority ) {
150          _Thread_Change_priority(
151            executing,
152            the_mutex->Attributes.priority_ceiling,
153            FALSE
154          );
155        };
156        break;
157    }
158  }
159}
160
Note: See TracBrowser for help on using the repository browser.