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 | |
---|
27 | Status_Control _CORE_RWLock_Seize_for_reading( |
---|
28 | CORE_RWLock_Control *the_rwlock, |
---|
29 | bool wait, |
---|
30 | Thread_queue_Context *queue_context |
---|
31 | ) |
---|
32 | { |
---|
33 | Thread_Control *executing; |
---|
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 | executing = _CORE_RWLock_Acquire( 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 | if ( _Thread_queue_Is_empty( &the_rwlock->Queue.Queue ) ) { |
---|
52 | the_rwlock->number_of_readers += 1; |
---|
53 | _CORE_RWLock_Release( the_rwlock, queue_context ); |
---|
54 | return STATUS_SUCCESSFUL; |
---|
55 | } |
---|
56 | break; |
---|
57 | case CORE_RWLOCK_LOCKED_FOR_WRITING: |
---|
58 | break; |
---|
59 | } |
---|
60 | |
---|
61 | /* |
---|
62 | * If the thread is not willing to wait, then return immediately. |
---|
63 | */ |
---|
64 | |
---|
65 | if ( !wait ) { |
---|
66 | _CORE_RWLock_Release( the_rwlock, queue_context ); |
---|
67 | return STATUS_UNAVAILABLE; |
---|
68 | } |
---|
69 | |
---|
70 | /* |
---|
71 | * We need to wait to enter this critical section |
---|
72 | */ |
---|
73 | |
---|
74 | executing->Wait.option = CORE_RWLOCK_THREAD_WAITING_FOR_READ; |
---|
75 | |
---|
76 | _Thread_queue_Context_set_thread_state( |
---|
77 | queue_context, |
---|
78 | STATES_WAITING_FOR_RWLOCK |
---|
79 | ); |
---|
80 | _Thread_queue_Context_set_enqueue_do_nothing_extra( queue_context ); |
---|
81 | _Thread_queue_Enqueue( |
---|
82 | &the_rwlock->Queue.Queue, |
---|
83 | CORE_RWLOCK_TQ_OPERATIONS, |
---|
84 | executing, |
---|
85 | queue_context |
---|
86 | ); |
---|
87 | return _Thread_Wait_get_status( executing ); |
---|
88 | } |
---|