summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/coremutexseize.c
blob: 94c480d0540c1514938df794907fe9b168b6cdde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
 *  @file
 *
 *  @brief Seize Mutex with Blocking
 *  @ingroup ScoreMutex
 */

/*
 *  COPYRIGHT (c) 1989-2006.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/system.h>
#include <rtems/score/isr.h>
#include <rtems/score/coremuteximpl.h>
#include <rtems/score/schedulerimpl.h>
#include <rtems/score/statesimpl.h>
#include <rtems/score/thread.h>

#if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
void _CORE_mutex_Seize(
  CORE_mutex_Control  *_the_mutex,
  Thread_Control      *_executing,
  Objects_Id           _id,
  bool                 _wait,
  Watchdog_Interval    _timeout,
  ISR_Level            _level
)
{
  _CORE_mutex_Seize_body(
    _the_mutex,
    _executing,
    _id,
    _wait,
    _timeout,
    _level
  );
}
#endif

void _CORE_mutex_Seize_interrupt_blocking(
  CORE_mutex_Control  *the_mutex,
  Thread_Control      *executing,
  Watchdog_Interval    timeout,
  ISR_lock_Context    *lock_context
)
{
#if !defined(RTEMS_SMP)
  /*
   * We must disable thread dispatching here since we enable the interrupts for
   * priority inheritance mutexes.
   */
  _Thread_Dispatch_disable();
#endif

  if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ) {
    Thread_Control *holder = the_mutex->holder;

#if !defined(RTEMS_SMP)
    /*
     * To enable interrupts here works only since exactly one executing thread
     * exists and only threads are allowed to seize and surrender mutexes with
     * the priority inheritance protocol.  On SMP configurations more than one
     * executing thread may exist, so here we must not release the lock, since
     * otherwise the current holder may be no longer the holder of the mutex
     * once we released the lock.
     */
    _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
#endif

    _Scheduler_Change_priority_if_higher(
      _Scheduler_Get( holder ),
      holder,
      executing->current_priority,
      false
    );

#if !defined(RTEMS_SMP)
    _Thread_queue_Acquire( &the_mutex->Wait_queue, lock_context );
#endif
  }

  _Thread_queue_Enqueue_critical(
    &the_mutex->Wait_queue,
    executing,
    STATES_WAITING_FOR_MUTEX,
    timeout,
    CORE_MUTEX_TIMEOUT,
    lock_context
  );

#if !defined(RTEMS_SMP)
  _Thread_Dispatch_enable( _Per_CPU_Get() );
#endif
}