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.com/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/system.h> |
---|
22 | #include <rtems/score/corerwlockimpl.h> |
---|
23 | #include <rtems/score/states.h> |
---|
24 | #include <rtems/score/thread.h> |
---|
25 | #include <rtems/score/watchdog.h> |
---|
26 | |
---|
27 | void _CORE_RWLock_Obtain_for_reading( |
---|
28 | CORE_RWLock_Control *the_rwlock, |
---|
29 | Thread_Control *executing, |
---|
30 | Objects_Id id, |
---|
31 | bool wait, |
---|
32 | Watchdog_Interval timeout, |
---|
33 | CORE_RWLock_API_mp_support_callout api_rwlock_mp_support |
---|
34 | ) |
---|
35 | { |
---|
36 | ISR_Level level; |
---|
37 | |
---|
38 | /* |
---|
39 | * If unlocked, then OK to read. |
---|
40 | * If locked for reading and no waiters, then OK to read. |
---|
41 | * If any thread is waiting, then we wait. |
---|
42 | */ |
---|
43 | |
---|
44 | _ISR_Disable( level ); |
---|
45 | switch ( the_rwlock->current_state ) { |
---|
46 | case CORE_RWLOCK_UNLOCKED: |
---|
47 | the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_READING; |
---|
48 | the_rwlock->number_of_readers += 1; |
---|
49 | _ISR_Enable( level ); |
---|
50 | executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; |
---|
51 | return; |
---|
52 | |
---|
53 | case CORE_RWLOCK_LOCKED_FOR_READING: { |
---|
54 | Thread_Control *waiter; |
---|
55 | waiter = _Thread_queue_First( &the_rwlock->Wait_queue ); |
---|
56 | if ( !waiter ) { |
---|
57 | the_rwlock->number_of_readers += 1; |
---|
58 | _ISR_Enable( level ); |
---|
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 | _ISR_Enable( level ); |
---|
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 | _Thread_queue_Enter_critical_section( &the_rwlock->Wait_queue ); |
---|
83 | executing->Wait.queue = &the_rwlock->Wait_queue; |
---|
84 | executing->Wait.id = id; |
---|
85 | executing->Wait.option = CORE_RWLOCK_THREAD_WAITING_FOR_READ; |
---|
86 | executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; |
---|
87 | _ISR_Enable( level ); |
---|
88 | |
---|
89 | _Thread_queue_Enqueue_with_handler( |
---|
90 | &the_rwlock->Wait_queue, |
---|
91 | timeout, |
---|
92 | _CORE_RWLock_Timeout |
---|
93 | ); |
---|
94 | |
---|
95 | /* return to API level so it can dispatch and we block */ |
---|
96 | } |
---|