[f7f1d77] | 1 | /** |
---|
| 2 | * @file |
---|
[9c191ee] | 3 | * |
---|
[f7f1d77] | 4 | * @brief RWLock Obtain for Writing |
---|
| 5 | * @ingroup ScoreRWLock |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | /* |
---|
[9c191ee] | 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 |
---|
[c499856] | 14 | * http://www.rtems.org/license/LICENSE. |
---|
[9c191ee] | 15 | */ |
---|
| 16 | |
---|
| 17 | #if HAVE_CONFIG_H |
---|
| 18 | #include "config.h" |
---|
| 19 | #endif |
---|
| 20 | |
---|
[cbdabc8] | 21 | #include <rtems/score/corerwlockimpl.h> |
---|
[a112364] | 22 | #include <rtems/score/threadqimpl.h> |
---|
[9c191ee] | 23 | #include <rtems/score/watchdog.h> |
---|
| 24 | |
---|
| 25 | void _CORE_RWLock_Obtain_for_writing( |
---|
| 26 | CORE_RWLock_Control *the_rwlock, |
---|
[982e9746] | 27 | Thread_Control *executing, |
---|
[9c191ee] | 28 | Objects_Id id, |
---|
[484a769] | 29 | bool wait, |
---|
[9c191ee] | 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; |
---|
[1d56a7a] | 63 | return; |
---|
[9c191ee] | 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 | |
---|
[8a8f5b2] | 77 | _Thread_queue_Enqueue_with_handler( |
---|
| 78 | &the_rwlock->Wait_queue, |
---|
[07332ae] | 79 | executing, |
---|
[8a8f5b2] | 80 | timeout, |
---|
| 81 | _CORE_RWLock_Timeout |
---|
| 82 | ); |
---|
| 83 | |
---|
[9c191ee] | 84 | |
---|
| 85 | /* return to API level so it can dispatch and we block */ |
---|
| 86 | } |
---|