source: rtems/cpukit/score/src/coremutexseize.c @ 7cc8d6c

4.104.114.84.95
Last change on this file since 7cc8d6c was 7cc8d6c, checked in by Joel Sherrill <joel.sherrill@…>, on 11/02/99 at 21:48:15

Split core mutex and semaphore handlers into separate files.

  • Property mode set to 100644
File size: 4.5 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-1998.
10 *  On-Line Applications Research Corporation (OAR).
11 *  Copyright assigned to U.S. Government, 1994.
12 *
13 *  The license and distribution terms for this file may be
14 *  found in the file LICENSE in this distribution or at
15 *  http://www.OARcorp.com/rtems/license.html.
16 *
17 *  $Id$
18 */
19
20#include <rtems/system.h>
21#include <rtems/score/isr.h>
22#include <rtems/score/coremutex.h>
23#include <rtems/score/states.h>
24#include <rtems/score/thread.h>
25#include <rtems/score/threadq.h>
26
27/*PAGE
28 *
29 *  _CORE_mutex_Seize
30 *
31 *  This routine attempts to allocate a mutex to the calling thread.
32 *
33 *  Input parameters:
34 *    the_mutex - pointer to mutex control block
35 *    id        - id of object to wait on
36 *    wait      - TRUE if wait is allowed, FALSE otherwise
37 *    timeout   - number of ticks to wait (0 means forever)
38 *
39 *  Output parameters:  NONE
40 *
41 *  INTERRUPT LATENCY:
42 *    available
43 *    wait
44 */
45
46void _CORE_mutex_Seize(
47  CORE_mutex_Control  *the_mutex,
48  Objects_Id           id,
49  boolean              wait,
50  Watchdog_Interval    timeout
51)
52{
53  Thread_Control *executing;
54  ISR_Level       level;
55
56  executing = _Thread_Executing;
57  switch ( the_mutex->Attributes.discipline ) {
58    case CORE_MUTEX_DISCIPLINES_FIFO:
59    case CORE_MUTEX_DISCIPLINES_PRIORITY:
60    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
61      break;
62    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
63      if ( executing->current_priority <
64                              the_mutex->Attributes.priority_ceiling) {
65        executing->Wait.return_code = CORE_MUTEX_STATUS_CEILING_VIOLATED;
66        return;
67      }
68  }
69  executing->Wait.return_code = CORE_MUTEX_STATUS_SUCCESSFUL;
70  _ISR_Disable( level );
71  if ( ! _CORE_mutex_Is_locked( the_mutex ) ) {
72    the_mutex->lock       = CORE_MUTEX_LOCKED;
73    the_mutex->holder     = executing;
74    the_mutex->holder_id  = executing->Object.id;
75    the_mutex->nest_count = 1;
76    executing->resource_count++;
77    _ISR_Enable( level );
78    switch ( the_mutex->Attributes.discipline ) {
79      case CORE_MUTEX_DISCIPLINES_FIFO:
80      case CORE_MUTEX_DISCIPLINES_PRIORITY:
81      case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
82        /* already the highest priority */
83        break;
84      case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
85      if ( the_mutex->Attributes.priority_ceiling <
86                                           executing->current_priority ) {
87        _Thread_Change_priority(
88          the_mutex->holder,
89          the_mutex->Attributes.priority_ceiling,
90          FALSE
91        );
92      }
93    }
94    return;
95  }
96
97  if ( _Objects_Are_ids_equal(
98              _Thread_Executing->Object.id, the_mutex->holder_id ) ) {
99    if ( _CORE_mutex_Is_nesting_allowed( &the_mutex->Attributes ) )
100      the_mutex->nest_count++;
101    else
102      executing->Wait.return_code = CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED;
103
104    _ISR_Enable( level );
105    return;
106  }
107
108  if ( !wait ) {
109    _ISR_Enable( level );
110    executing->Wait.return_code = CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT;
111    return;
112  }
113
114  _Thread_queue_Enter_critical_section( &the_mutex->Wait_queue );
115  executing->Wait.queue = &the_mutex->Wait_queue;
116  executing->Wait.id    = id;
117  _ISR_Enable( level );
118
119  switch ( the_mutex->Attributes.discipline ) {
120    case CORE_MUTEX_DISCIPLINES_FIFO:
121    case CORE_MUTEX_DISCIPLINES_PRIORITY:
122    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
123      break;
124    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
125      if ( the_mutex->holder->current_priority > executing->current_priority ) {
126        _Thread_Change_priority(
127          the_mutex->holder,
128          executing->current_priority,
129          FALSE
130        );
131      }
132      break;
133  }
134
135  _Thread_queue_Enqueue( &the_mutex->Wait_queue, timeout );
136
137  if ( _Thread_Executing->Wait.return_code == CORE_MUTEX_STATUS_SUCCESSFUL ) {
138    switch ( the_mutex->Attributes.discipline ) {
139      case CORE_MUTEX_DISCIPLINES_FIFO:
140      case CORE_MUTEX_DISCIPLINES_PRIORITY:
141      case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
142        break;
143      case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
144        if ( the_mutex->Attributes.priority_ceiling <
145                                           executing->current_priority ) {
146          _Thread_Change_priority(
147            executing,
148            the_mutex->Attributes.priority_ceiling,
149            FALSE
150          );
151        };
152        break;
153    }
154  }
155}
156
Note: See TracBrowser for help on using the repository browser.