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.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_writing( |
---|
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 | * Otherwise, we have to block. |
---|
38 | * If locked for reading and no waiters, then OK to read. |
---|
39 | * If any thread is waiting, then we wait. |
---|
40 | */ |
---|
41 | |
---|
42 | executing = _CORE_RWLock_Acquire( the_rwlock, queue_context ); |
---|
43 | |
---|
44 | switch ( the_rwlock->current_state ) { |
---|
45 | case CORE_RWLOCK_UNLOCKED: |
---|
46 | the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_WRITING; |
---|
47 | _CORE_RWLock_Release( the_rwlock, queue_context ); |
---|
48 | return STATUS_SUCCESSFUL; |
---|
49 | |
---|
50 | case CORE_RWLOCK_LOCKED_FOR_READING: |
---|
51 | case CORE_RWLOCK_LOCKED_FOR_WRITING: |
---|
52 | break; |
---|
53 | } |
---|
54 | |
---|
55 | /* |
---|
56 | * If the thread is not willing to wait, then return immediately. |
---|
57 | */ |
---|
58 | |
---|
59 | if ( !wait ) { |
---|
60 | _CORE_RWLock_Release( the_rwlock, queue_context ); |
---|
61 | return STATUS_UNAVAILABLE; |
---|
62 | } |
---|
63 | |
---|
64 | /* |
---|
65 | * We need to wait to enter this critical section |
---|
66 | */ |
---|
67 | |
---|
68 | executing->Wait.option = CORE_RWLOCK_THREAD_WAITING_FOR_WRITE; |
---|
69 | |
---|
70 | _Thread_queue_Context_set_thread_state( |
---|
71 | queue_context, |
---|
72 | STATES_WAITING_FOR_RWLOCK |
---|
73 | ); |
---|
74 | _Thread_queue_Enqueue( |
---|
75 | &the_rwlock->Queue.Queue, |
---|
76 | CORE_RWLOCK_TQ_OPERATIONS, |
---|
77 | executing, |
---|
78 | queue_context |
---|
79 | ); |
---|
80 | return _Thread_Wait_get_status( executing ); |
---|
81 | } |
---|