1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief RWLock Obtain for Writing |
---|
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/score/corerwlockimpl.h> |
---|
22 | #include <rtems/score/threadqimpl.h> |
---|
23 | #include <rtems/score/watchdog.h> |
---|
24 | |
---|
25 | void _CORE_RWLock_Obtain_for_writing( |
---|
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 | * Otherwise, we have to block. |
---|
39 | * If locked for reading and no waiters, then OK to read. |
---|
40 | * If any thread is waiting, then we wait. |
---|
41 | */ |
---|
42 | |
---|
43 | _ISR_Disable( level ); |
---|
44 | switch ( the_rwlock->current_state ) { |
---|
45 | case CORE_RWLOCK_UNLOCKED: |
---|
46 | the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_WRITING; |
---|
47 | _ISR_Enable( level ); |
---|
48 | executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; |
---|
49 | return; |
---|
50 | |
---|
51 | case CORE_RWLOCK_LOCKED_FOR_READING: |
---|
52 | case CORE_RWLOCK_LOCKED_FOR_WRITING: |
---|
53 | break; |
---|
54 | } |
---|
55 | |
---|
56 | /* |
---|
57 | * If the thread is not willing to wait, then return immediately. |
---|
58 | */ |
---|
59 | |
---|
60 | if ( !wait ) { |
---|
61 | _ISR_Enable( level ); |
---|
62 | executing->Wait.return_code = CORE_RWLOCK_UNAVAILABLE; |
---|
63 | return; |
---|
64 | } |
---|
65 | |
---|
66 | /* |
---|
67 | * We need to wait to enter this critical section |
---|
68 | */ |
---|
69 | |
---|
70 | _Thread_queue_Enter_critical_section( &the_rwlock->Wait_queue ); |
---|
71 | executing->Wait.queue = &the_rwlock->Wait_queue; |
---|
72 | executing->Wait.id = id; |
---|
73 | executing->Wait.option = CORE_RWLOCK_THREAD_WAITING_FOR_WRITE; |
---|
74 | executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; |
---|
75 | _ISR_Enable( level ); |
---|
76 | |
---|
77 | _Thread_queue_Enqueue_with_handler( |
---|
78 | &the_rwlock->Wait_queue, |
---|
79 | timeout, |
---|
80 | _CORE_RWLock_Timeout |
---|
81 | ); |
---|
82 | |
---|
83 | |
---|
84 | /* return to API level so it can dispatch and we block */ |
---|
85 | } |
---|