summaryrefslogtreecommitdiff
path: root/cpukit/score/src/coremutexseize.c
blob: ed5eb0aeec1efa3c0fe6c7164dfe3f9c6184436b (plain)
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
105
106
/**
 *  @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/score/coremuteximpl.h>
#include <rtems/score/statesimpl.h>
#include <rtems/score/thread.h>

Status_Control _CORE_mutex_Seize_interrupt_blocking(
  CORE_mutex_Control   *the_mutex,
  Thread_Control       *executing,
  Watchdog_Interval     timeout,
  Thread_queue_Context *queue_context
)
{
  Thread_Control *holder;

#if !defined(RTEMS_SMP)
  /*
   * We must disable thread dispatching here since we enable the interrupts for
   * priority inheritance mutexes.
   */
  _Thread_Dispatch_disable();
#endif

  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.
   */
  _CORE_mutex_Release( the_mutex, queue_context );
#endif

  _Thread_Inherit_priority( holder, executing );

#if defined(RTEMS_SMP)
  _Thread_queue_Context_set_expected_level( queue_context, 1 );
#else
  _ISR_lock_ISR_disable( &queue_context->Lock_context );
  _CORE_mutex_Acquire_critical( the_mutex, queue_context );
  _Thread_queue_Context_set_expected_level( queue_context, 2 );
#endif

  _Thread_queue_Enqueue_critical(
    &the_mutex->Wait_queue.Queue,
    CORE_MUTEX_TQ_OPERATIONS,
    executing,
    STATES_WAITING_FOR_MUTEX,
    timeout,
    queue_context
  );

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

  return _Thread_Wait_get_status( executing );
}

Status_Control _CORE_mutex_Seize_no_protocol_slow(
  CORE_mutex_Control            *the_mutex,
  const Thread_queue_Operations *operations,
  Thread_Control                *executing,
  bool                           wait,
  Watchdog_Interval              timeout,
  Thread_queue_Context          *queue_context
)
{
  if ( wait ) {
    _Thread_queue_Context_set_expected_level( queue_context, 1 );
    _Thread_queue_Enqueue_critical(
      &the_mutex->Wait_queue.Queue,
      operations,
      executing,
      STATES_WAITING_FOR_MUTEX,
      timeout,
      queue_context
    );
    return _Thread_Wait_get_status( executing );
  } else {
    _CORE_mutex_Release( the_mutex, queue_context );
    return STATUS_UNAVAILABLE;
  }
}