1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreRWLock |
---|
7 | * |
---|
8 | * @brief This source file contains the implementation of |
---|
9 | * _CORE_RWLock_Seize_for_writing(). |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * COPYRIGHT (c) 1989-2006. |
---|
14 | * On-Line Applications Research Corporation (OAR). |
---|
15 | * |
---|
16 | * Redistribution and use in source and binary forms, with or without |
---|
17 | * modification, are permitted provided that the following conditions |
---|
18 | * are met: |
---|
19 | * 1. Redistributions of source code must retain the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer. |
---|
21 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
22 | * notice, this list of conditions and the following disclaimer in the |
---|
23 | * documentation and/or other materials provided with the distribution. |
---|
24 | * |
---|
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
35 | * POSSIBILITY OF SUCH DAMAGE. |
---|
36 | */ |
---|
37 | |
---|
38 | #ifdef HAVE_CONFIG_H |
---|
39 | #include "config.h" |
---|
40 | #endif |
---|
41 | |
---|
42 | #include <rtems/score/corerwlockimpl.h> |
---|
43 | #include <rtems/score/threadimpl.h> |
---|
44 | #include <rtems/score/threadqimpl.h> |
---|
45 | #include <rtems/score/statesimpl.h> |
---|
46 | #include <rtems/score/watchdog.h> |
---|
47 | |
---|
48 | Status_Control _CORE_RWLock_Seize_for_writing( |
---|
49 | CORE_RWLock_Control *the_rwlock, |
---|
50 | bool wait, |
---|
51 | Thread_queue_Context *queue_context |
---|
52 | ) |
---|
53 | { |
---|
54 | Thread_Control *executing; |
---|
55 | |
---|
56 | /* |
---|
57 | * If unlocked, then OK to read. |
---|
58 | * Otherwise, we have to block. |
---|
59 | * If locked for reading and no waiters, then OK to read. |
---|
60 | * If any thread is waiting, then we wait. |
---|
61 | */ |
---|
62 | |
---|
63 | executing = _CORE_RWLock_Acquire( the_rwlock, queue_context ); |
---|
64 | |
---|
65 | switch ( the_rwlock->current_state ) { |
---|
66 | case CORE_RWLOCK_UNLOCKED: |
---|
67 | the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_WRITING; |
---|
68 | _CORE_RWLock_Release( the_rwlock, queue_context ); |
---|
69 | return STATUS_SUCCESSFUL; |
---|
70 | |
---|
71 | case CORE_RWLOCK_LOCKED_FOR_READING: |
---|
72 | case CORE_RWLOCK_LOCKED_FOR_WRITING: |
---|
73 | break; |
---|
74 | } |
---|
75 | |
---|
76 | /* |
---|
77 | * If the thread is not willing to wait, then return immediately. |
---|
78 | */ |
---|
79 | |
---|
80 | if ( !wait ) { |
---|
81 | _CORE_RWLock_Release( the_rwlock, queue_context ); |
---|
82 | return STATUS_UNAVAILABLE; |
---|
83 | } |
---|
84 | |
---|
85 | /* |
---|
86 | * We need to wait to enter this critical section |
---|
87 | */ |
---|
88 | |
---|
89 | executing->Wait.option = CORE_RWLOCK_THREAD_WAITING_FOR_WRITE; |
---|
90 | |
---|
91 | _Thread_queue_Context_set_thread_state( |
---|
92 | queue_context, |
---|
93 | STATES_WAITING_FOR_RWLOCK |
---|
94 | ); |
---|
95 | _Thread_queue_Enqueue( |
---|
96 | &the_rwlock->Queue.Queue, |
---|
97 | CORE_RWLOCK_TQ_OPERATIONS, |
---|
98 | executing, |
---|
99 | queue_context |
---|
100 | ); |
---|
101 | return _Thread_Wait_get_status( executing ); |
---|
102 | } |
---|