1 | /* |
---|
2 | * SuperCore RWLock Handler -- Obtain RWLock for writing |
---|
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_writing |
---|
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_writing( |
---|
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 | * Otherwise, we have to block. |
---|
53 | * If locked for reading and no waiters, then OK to read. |
---|
54 | * If any thread is waiting, then we wait. |
---|
55 | */ |
---|
56 | |
---|
57 | _ISR_Disable( level ); |
---|
58 | switch ( the_rwlock->current_state ) { |
---|
59 | case CORE_RWLOCK_UNLOCKED: |
---|
60 | the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_WRITING; |
---|
61 | _ISR_Enable( level ); |
---|
62 | executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; |
---|
63 | return; |
---|
64 | |
---|
65 | case CORE_RWLOCK_LOCKED_FOR_READING: |
---|
66 | case CORE_RWLOCK_LOCKED_FOR_WRITING: |
---|
67 | break; |
---|
68 | } |
---|
69 | |
---|
70 | /* |
---|
71 | * If the thread is not willing to wait, then return immediately. |
---|
72 | */ |
---|
73 | |
---|
74 | if ( !wait ) { |
---|
75 | _ISR_Enable( level ); |
---|
76 | executing->Wait.return_code = CORE_RWLOCK_UNAVAILABLE; |
---|
77 | return; |
---|
78 | } |
---|
79 | |
---|
80 | /* |
---|
81 | * We need to wait to enter this critical section |
---|
82 | */ |
---|
83 | |
---|
84 | _Thread_queue_Enter_critical_section( &the_rwlock->Wait_queue ); |
---|
85 | executing->Wait.queue = &the_rwlock->Wait_queue; |
---|
86 | executing->Wait.id = id; |
---|
87 | executing->Wait.option = CORE_RWLOCK_THREAD_WAITING_FOR_WRITE; |
---|
88 | executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; |
---|
89 | _ISR_Enable( level ); |
---|
90 | |
---|
91 | _Thread_queue_Enqueue_with_handler( |
---|
92 | &the_rwlock->Wait_queue, |
---|
93 | timeout, |
---|
94 | _CORE_RWLock_Timeout |
---|
95 | ); |
---|
96 | |
---|
97 | |
---|
98 | /* return to API level so it can dispatch and we block */ |
---|
99 | } |
---|