source: rtems/cpukit/score/src/corerwlockobtainread.c @ 469dc47

5
Last change on this file since 469dc47 was 84a53988, checked in by Sebastian Huber <sebastian.huber@…>, on 04/21/16 at 04:32:16

score: Avoid Giant lock for CORE rwlock

Update #2555.

  • 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/threadqimpl.h>
23#include <rtems/score/statesimpl.h>
24#include <rtems/score/watchdog.h>
25
26void _CORE_RWLock_Seize_for_reading(
27  CORE_RWLock_Control *the_rwlock,
28  Thread_Control      *executing,
29  bool                 wait,
30  Watchdog_Interval    timeout,
31  ISR_lock_Context    *lock_context
32)
33{
34  /*
35   *  If unlocked, then OK to read.
36   *  If locked for reading and no waiters, then OK to read.
37   *  If any thread is waiting, then we wait.
38   */
39
40  _CORE_RWLock_Acquire_critical( the_rwlock, lock_context );
41
42  switch ( the_rwlock->current_state ) {
43    case CORE_RWLOCK_UNLOCKED:
44      the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_READING;
45      the_rwlock->number_of_readers += 1;
46      _CORE_RWLock_Release( the_rwlock, lock_context );
47      executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL;
48      return;
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, lock_context );
59        executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL;
60        return;
61      }
62      break;
63    }
64    case CORE_RWLOCK_LOCKED_FOR_WRITING:
65      break;
66  }
67
68  /*
69   *  If the thread is not willing to wait, then return immediately.
70   */
71
72  if ( !wait ) {
73    _CORE_RWLock_Release( the_rwlock, lock_context );
74    executing->Wait.return_code = CORE_RWLOCK_UNAVAILABLE;
75    return;
76  }
77
78  /*
79   *  We need to wait to enter this critical section
80   */
81
82  executing->Wait.option      = CORE_RWLOCK_THREAD_WAITING_FOR_READ;
83  executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL;
84
85  _Thread_queue_Enqueue_critical(
86     &the_rwlock->Wait_queue.Queue,
87     CORE_RWLOCK_TQ_OPERATIONS,
88     executing,
89     STATES_WAITING_FOR_RWLOCK,
90     timeout,
91     CORE_RWLOCK_TIMEOUT,
92     lock_context
93  );
94}
Note: See TracBrowser for help on using the repository browser.