source: rtems/cpukit/score/src/corerwlockobtainread.c @ 99fc1d1d

5
Last change on this file since 99fc1d1d was 93306058, checked in by Sebastian Huber <sebastian.huber@…>, on 05/27/16 at 12:43:19

score: _CORE_mutex_Check_dispatch_for_seize()

Move the safety check performed by
_CORE_mutex_Check_dispatch_for_seize() out of the performance critical
path and generalize it. Blocking on a thread queue with an unexpected
thread dispatch disabled level is illegal in all system states.

Add the expected thread dispatch disable level (which may be 1 or 2
depending on the operation) to Thread_queue_Context and use it in
_Thread_queue_Enqueue_critical().

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/**
2 * @file
3 *
4 * @brief Obtain RWLock for reading
5 * @ingroup ScoreRWLock
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2006.
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.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/score/corerwlockimpl.h>
22#include <rtems/score/threadimpl.h>
23#include <rtems/score/threadqimpl.h>
24#include <rtems/score/statesimpl.h>
25#include <rtems/score/watchdog.h>
26
27Status_Control _CORE_RWLock_Seize_for_reading(
28  CORE_RWLock_Control  *the_rwlock,
29  Thread_Control       *executing,
30  bool                  wait,
31  Watchdog_Interval     timeout,
32  Thread_queue_Context *queue_context
33)
34{
35  /*
36   *  If unlocked, then OK to read.
37   *  If locked for reading and no waiters, then OK to read.
38   *  If any thread is waiting, then we wait.
39   */
40
41  _CORE_RWLock_Acquire_critical( the_rwlock, queue_context );
42
43  switch ( the_rwlock->current_state ) {
44    case CORE_RWLOCK_UNLOCKED:
45      the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_READING;
46      the_rwlock->number_of_readers += 1;
47      _CORE_RWLock_Release( the_rwlock, queue_context );
48      return STATUS_SUCCESSFUL;
49
50    case CORE_RWLOCK_LOCKED_FOR_READING: {
51      Thread_Control *waiter;
52      waiter = _Thread_queue_First_locked(
53        &the_rwlock->Wait_queue,
54        CORE_RWLOCK_TQ_OPERATIONS
55      );
56      if ( !waiter ) {
57        the_rwlock->number_of_readers += 1;
58        _CORE_RWLock_Release( the_rwlock, queue_context );
59        return STATUS_SUCCESSFUL;
60      }
61      break;
62    }
63    case CORE_RWLOCK_LOCKED_FOR_WRITING:
64      break;
65  }
66
67  /*
68   *  If the thread is not willing to wait, then return immediately.
69   */
70
71  if ( !wait ) {
72    _CORE_RWLock_Release( the_rwlock, queue_context );
73    return STATUS_UNAVAILABLE;
74  }
75
76  /*
77   *  We need to wait to enter this critical section
78   */
79
80  executing->Wait.option = CORE_RWLOCK_THREAD_WAITING_FOR_READ;
81
82  _Thread_queue_Context_set_expected_level( queue_context, 1 );
83  _Thread_queue_Enqueue_critical(
84     &the_rwlock->Wait_queue.Queue,
85     CORE_RWLOCK_TQ_OPERATIONS,
86     executing,
87     STATES_WAITING_FOR_RWLOCK,
88     timeout,
89     queue_context
90  );
91  return _Thread_Wait_get_status( executing );
92}
Note: See TracBrowser for help on using the repository browser.