1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Releases the RWLock |
---|
5 | * |
---|
6 | * @ingroup ScoreRWLock |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2006. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.org/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #if HAVE_CONFIG_H |
---|
19 | #include "config.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <rtems/system.h> |
---|
23 | #include <rtems/score/corerwlockimpl.h> |
---|
24 | #include <rtems/score/threadqimpl.h> |
---|
25 | #include <rtems/score/watchdog.h> |
---|
26 | |
---|
27 | CORE_RWLock_Status _CORE_RWLock_Surrender( |
---|
28 | CORE_RWLock_Control *the_rwlock, |
---|
29 | Thread_Control *executing |
---|
30 | ) |
---|
31 | { |
---|
32 | ISR_Level level; |
---|
33 | Thread_Control *next; |
---|
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 | _ISR_Disable( level ); |
---|
43 | if ( the_rwlock->current_state == CORE_RWLOCK_UNLOCKED){ |
---|
44 | _ISR_Enable( level ); |
---|
45 | executing->Wait.return_code = CORE_RWLOCK_UNAVAILABLE; |
---|
46 | return CORE_RWLOCK_SUCCESSFUL; |
---|
47 | } |
---|
48 | if ( the_rwlock->current_state == CORE_RWLOCK_LOCKED_FOR_READING ) { |
---|
49 | the_rwlock->number_of_readers -= 1; |
---|
50 | if ( the_rwlock->number_of_readers != 0 ) { |
---|
51 | /* must be unlocked again */ |
---|
52 | _ISR_Enable( level ); |
---|
53 | return CORE_RWLOCK_SUCCESSFUL; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | /* CORE_RWLOCK_LOCKED_FOR_WRITING or READING with readers */ |
---|
58 | executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; |
---|
59 | |
---|
60 | /* |
---|
61 | * Implicitly transition to "unlocked" and find another thread interested |
---|
62 | * in obtaining this rwlock. |
---|
63 | */ |
---|
64 | the_rwlock->current_state = CORE_RWLOCK_UNLOCKED; |
---|
65 | _ISR_Enable( level ); |
---|
66 | |
---|
67 | next = _Thread_queue_Dequeue( |
---|
68 | &the_rwlock->Wait_queue, |
---|
69 | CORE_RWLOCK_TQ_OPERATIONS, |
---|
70 | NULL, |
---|
71 | 0 |
---|
72 | ); |
---|
73 | |
---|
74 | if ( next ) { |
---|
75 | if ( next->Wait.option == CORE_RWLOCK_THREAD_WAITING_FOR_WRITE ) { |
---|
76 | the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_WRITING; |
---|
77 | return CORE_RWLOCK_SUCCESSFUL; |
---|
78 | } |
---|
79 | |
---|
80 | /* |
---|
81 | * Must be CORE_RWLOCK_THREAD_WAITING_FOR_READING |
---|
82 | */ |
---|
83 | the_rwlock->number_of_readers += 1; |
---|
84 | the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_READING; |
---|
85 | |
---|
86 | /* |
---|
87 | * Now see if more readers can be let go. |
---|
88 | */ |
---|
89 | while ( 1 ) { |
---|
90 | next = _Thread_queue_First( |
---|
91 | &the_rwlock->Wait_queue, |
---|
92 | CORE_RWLOCK_TQ_OPERATIONS |
---|
93 | ); |
---|
94 | if ( !next || |
---|
95 | next->Wait.option == CORE_RWLOCK_THREAD_WAITING_FOR_WRITE ) |
---|
96 | return CORE_RWLOCK_SUCCESSFUL; |
---|
97 | the_rwlock->number_of_readers += 1; |
---|
98 | _Thread_queue_Extract( next ); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | /* indentation is to match _ISR_Disable at top */ |
---|
103 | |
---|
104 | return CORE_RWLOCK_SUCCESSFUL; |
---|
105 | } |
---|