1 | /* |
---|
2 | * SuperCore RWLock Handler -- Release a RWLock |
---|
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 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <rtems/system.h> |
---|
17 | #include <rtems/score/corerwlock.h> |
---|
18 | #include <rtems/score/states.h> |
---|
19 | #include <rtems/score/thread.h> |
---|
20 | #include <rtems/score/watchdog.h> |
---|
21 | |
---|
22 | /* |
---|
23 | * _CORE_RWLock_Release |
---|
24 | * |
---|
25 | * This function releases the rwlock. |
---|
26 | * |
---|
27 | * Input parameters: |
---|
28 | * the_rwlock - the rwlock control block to initialize |
---|
29 | * |
---|
30 | * Output parameters: NONE |
---|
31 | */ |
---|
32 | |
---|
33 | CORE_RWLock_Status _CORE_RWLock_Release( |
---|
34 | CORE_RWLock_Control *the_rwlock |
---|
35 | ) |
---|
36 | { |
---|
37 | ISR_Level level; |
---|
38 | Thread_Control *executing = _Thread_Executing; |
---|
39 | Thread_Control *next; |
---|
40 | |
---|
41 | /* |
---|
42 | * If unlocked, then OK to read. |
---|
43 | * Otherwise, we have to block. |
---|
44 | * If locked for reading and no waiters, then OK to read. |
---|
45 | * If any thread is waiting, then we wait. |
---|
46 | */ |
---|
47 | |
---|
48 | _ISR_Disable( level ); |
---|
49 | if ( the_rwlock->current_state == CORE_RWLOCK_UNLOCKED){ |
---|
50 | _ISR_Enable( level ); |
---|
51 | executing->Wait.return_code = CORE_RWLOCK_UNAVAILABLE; |
---|
52 | return CORE_RWLOCK_SUCCESSFUL; |
---|
53 | } |
---|
54 | if ( the_rwlock->current_state == CORE_RWLOCK_LOCKED_FOR_READING ) { |
---|
55 | the_rwlock->number_of_readers -= 1; |
---|
56 | if ( the_rwlock->number_of_readers != 0 ) { |
---|
57 | /* must be unlocked again */ |
---|
58 | _ISR_Enable( level ); |
---|
59 | return CORE_RWLOCK_SUCCESSFUL; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | /* CORE_RWLOCK_LOCKED_FOR_WRITING or READING with readers */ |
---|
64 | executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; |
---|
65 | |
---|
66 | /* |
---|
67 | * Implicitly transition to "unlocked" and find another thread interested |
---|
68 | * in obtaining this rwlock. |
---|
69 | */ |
---|
70 | the_rwlock->current_state = CORE_RWLOCK_UNLOCKED; |
---|
71 | _ISR_Enable( level ); |
---|
72 | |
---|
73 | next = _Thread_queue_Dequeue( &the_rwlock->Wait_queue ); |
---|
74 | |
---|
75 | if ( next ) { |
---|
76 | if ( next->Wait.option == CORE_RWLOCK_THREAD_WAITING_FOR_WRITE ) { |
---|
77 | the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_WRITING; |
---|
78 | return CORE_RWLOCK_SUCCESSFUL; |
---|
79 | } |
---|
80 | |
---|
81 | /* |
---|
82 | * Must be CORE_RWLOCK_THREAD_WAITING_FOR_READING |
---|
83 | */ |
---|
84 | the_rwlock->number_of_readers += 1; |
---|
85 | the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_READING; |
---|
86 | |
---|
87 | /* |
---|
88 | * Now see if more readers can be let go. |
---|
89 | */ |
---|
90 | while ( 1 ) { |
---|
91 | next = _Thread_queue_First( &the_rwlock->Wait_queue ); |
---|
92 | if ( !next || |
---|
93 | next->Wait.option == CORE_RWLOCK_THREAD_WAITING_FOR_WRITE ) |
---|
94 | return CORE_RWLOCK_SUCCESSFUL; |
---|
95 | the_rwlock->number_of_readers += 1; |
---|
96 | _Thread_queue_Extract( &the_rwlock->Wait_queue, next ); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | /* indentation is to match _ISR_Disable at top */ |
---|
101 | |
---|
102 | return CORE_RWLOCK_SUCCESSFUL; |
---|
103 | } |
---|