1 | /* |
---|
2 | * SuperCore RWLock Handler -- Obtain RWLock for reading |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2006. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #if HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <rtems/system.h> |
---|
19 | #include <rtems/score/corerwlock.h> |
---|
20 | #include <rtems/score/states.h> |
---|
21 | #include <rtems/score/thread.h> |
---|
22 | #include <rtems/score/watchdog.h> |
---|
23 | |
---|
24 | /*PAGE |
---|
25 | * |
---|
26 | * _CORE_rwlock_Obtain_for_reading |
---|
27 | * |
---|
28 | * This function waits for the rwlock to become available. Optionally, |
---|
29 | * a limit may be placed on the duration of the spin. |
---|
30 | * |
---|
31 | * Input parameters: |
---|
32 | * the_rwlock - the rwlock control block to initialize |
---|
33 | * timeout_allowed - true if timeout allowed |
---|
34 | * timeout - the maximum number of ticks to spin |
---|
35 | * |
---|
36 | * Output parameters: NONE |
---|
37 | */ |
---|
38 | |
---|
39 | void _CORE_RWLock_Obtain_for_reading( |
---|
40 | CORE_RWLock_Control *the_rwlock, |
---|
41 | Objects_Id id, |
---|
42 | bool wait, |
---|
43 | Watchdog_Interval timeout, |
---|
44 | CORE_RWLock_API_mp_support_callout api_rwlock_mp_support |
---|
45 | ) |
---|
46 | { |
---|
47 | ISR_Level level; |
---|
48 | Thread_Control *executing = _Thread_Executing; |
---|
49 | |
---|
50 | /* |
---|
51 | * If unlocked, then OK to read. |
---|
52 | * If locked for reading and no waiters, then OK to read. |
---|
53 | * If any thread is waiting, then we wait. |
---|
54 | */ |
---|
55 | |
---|
56 | _ISR_Disable( level ); |
---|
57 | switch ( the_rwlock->current_state ) { |
---|
58 | case CORE_RWLOCK_UNLOCKED: |
---|
59 | the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_READING; |
---|
60 | the_rwlock->number_of_readers += 1; |
---|
61 | _ISR_Enable( level ); |
---|
62 | executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; |
---|
63 | return; |
---|
64 | |
---|
65 | case CORE_RWLOCK_LOCKED_FOR_READING: { |
---|
66 | Thread_Control *waiter; |
---|
67 | waiter = _Thread_queue_First( &the_rwlock->Wait_queue ); |
---|
68 | if ( !waiter ) { |
---|
69 | the_rwlock->number_of_readers += 1; |
---|
70 | _ISR_Enable( level ); |
---|
71 | executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; |
---|
72 | return; |
---|
73 | } |
---|
74 | break; |
---|
75 | } |
---|
76 | case CORE_RWLOCK_LOCKED_FOR_WRITING: |
---|
77 | break; |
---|
78 | } |
---|
79 | |
---|
80 | /* |
---|
81 | * If the thread is not willing to wait, then return immediately. |
---|
82 | */ |
---|
83 | |
---|
84 | if ( !wait ) { |
---|
85 | _ISR_Enable( level ); |
---|
86 | executing->Wait.return_code = CORE_RWLOCK_UNAVAILABLE; |
---|
87 | return; |
---|
88 | } |
---|
89 | |
---|
90 | /* |
---|
91 | * We need to wait to enter this critical section |
---|
92 | */ |
---|
93 | |
---|
94 | _Thread_queue_Enter_critical_section( &the_rwlock->Wait_queue ); |
---|
95 | executing->Wait.queue = &the_rwlock->Wait_queue; |
---|
96 | executing->Wait.id = id; |
---|
97 | executing->Wait.option = CORE_RWLOCK_THREAD_WAITING_FOR_READ; |
---|
98 | executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; |
---|
99 | _ISR_Enable( level ); |
---|
100 | |
---|
101 | _Thread_queue_Enqueue_with_handler( |
---|
102 | &the_rwlock->Wait_queue, |
---|
103 | timeout, |
---|
104 | _CORE_RWLock_Timeout |
---|
105 | ); |
---|
106 | |
---|
107 | /* return to API level so it can dispatch and we block */ |
---|
108 | } |
---|