source: rtems/cpukit/score/src/corerwlockobtainread.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 2.5 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/watchdog.h>
24
25void _CORE_RWLock_Obtain_for_reading(
26  CORE_RWLock_Control                 *the_rwlock,
27  Thread_Control                      *executing,
28  Objects_Id                           id,
29  bool                                 wait,
30  Watchdog_Interval                    timeout,
31  CORE_RWLock_API_mp_support_callout   api_rwlock_mp_support
32)
33{
34  ISR_Level       level;
35
36  /*
37   *  If unlocked, then OK to read.
38   *  If locked for reading and no waiters, then OK to read.
39   *  If any thread is waiting, then we wait.
40   */
41
42  _ISR_Disable( level );
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        _ISR_Enable( level );
48        executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL;
49        return;
50
51      case CORE_RWLOCK_LOCKED_FOR_READING: {
52        Thread_Control *waiter;
53        waiter = _Thread_queue_First( &the_rwlock->Wait_queue );
54        if ( !waiter ) {
55          the_rwlock->number_of_readers += 1;
56          _ISR_Enable( level );
57          executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL;
58          return;
59        }
60        break;
61      }
62      case CORE_RWLOCK_LOCKED_FOR_WRITING:
63        break;
64    }
65
66    /*
67     *  If the thread is not willing to wait, then return immediately.
68     */
69
70    if ( !wait ) {
71      _ISR_Enable( level );
72      executing->Wait.return_code = CORE_RWLOCK_UNAVAILABLE;
73      return;
74    }
75
76    /*
77     *  We need to wait to enter this critical section
78     */
79
80    _Thread_queue_Enter_critical_section( &the_rwlock->Wait_queue );
81    executing->Wait.queue       = &the_rwlock->Wait_queue;
82    executing->Wait.id          = id;
83    executing->Wait.option      = CORE_RWLOCK_THREAD_WAITING_FOR_READ;
84    executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL;
85    _ISR_Enable( level );
86
87    _Thread_queue_Enqueue_with_handler(
88       &the_rwlock->Wait_queue,
89       executing,
90       timeout,
91       _CORE_RWLock_Timeout
92    );
93
94    /* return to API level so it can dispatch and we block */
95}
Note: See TracBrowser for help on using the repository browser.