1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSImplClassicEvent |
---|
7 | * |
---|
8 | * @brief This source file contains the implementation of |
---|
9 | * _Event_Surrender(). |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * COPYRIGHT (c) 1989-2008. |
---|
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/rtems/eventimpl.h> |
---|
43 | #include <rtems/rtems/optionsimpl.h> |
---|
44 | #include <rtems/score/threadimpl.h> |
---|
45 | #include <rtems/score/watchdogimpl.h> |
---|
46 | |
---|
47 | static void _Event_Satisfy( |
---|
48 | Thread_Control *the_thread, |
---|
49 | Event_Control *event, |
---|
50 | rtems_event_set pending_events, |
---|
51 | rtems_event_set seized_events |
---|
52 | ) |
---|
53 | { |
---|
54 | event->pending_events = _Event_sets_Clear( pending_events, seized_events ); |
---|
55 | *(rtems_event_set *) the_thread->Wait.return_argument = seized_events; |
---|
56 | } |
---|
57 | |
---|
58 | static bool _Event_Is_blocking_on_event( |
---|
59 | const Thread_Control *the_thread, |
---|
60 | Thread_Wait_flags wait_class |
---|
61 | ) |
---|
62 | { |
---|
63 | Thread_Wait_flags wait_flags; |
---|
64 | |
---|
65 | wait_flags = _Thread_Wait_flags_get( the_thread ); |
---|
66 | |
---|
67 | return ( wait_flags & THREAD_WAIT_CLASS_MASK ) == wait_class; |
---|
68 | } |
---|
69 | |
---|
70 | static bool _Event_Is_satisfied( |
---|
71 | const Thread_Control *the_thread, |
---|
72 | rtems_event_set pending_events, |
---|
73 | rtems_event_set *seized_events |
---|
74 | ) |
---|
75 | { |
---|
76 | rtems_option option_set; |
---|
77 | rtems_event_set event_condition; |
---|
78 | |
---|
79 | option_set = the_thread->Wait.option; |
---|
80 | event_condition = the_thread->Wait.count; |
---|
81 | *seized_events = _Event_sets_Get( pending_events, event_condition ); |
---|
82 | |
---|
83 | return !_Event_sets_Is_empty( *seized_events ) |
---|
84 | && ( *seized_events == event_condition || _Options_Is_any( option_set ) ); |
---|
85 | } |
---|
86 | |
---|
87 | rtems_status_code _Event_Surrender( |
---|
88 | Thread_Control *the_thread, |
---|
89 | rtems_event_set event_in, |
---|
90 | Event_Control *event, |
---|
91 | Thread_Wait_flags wait_class, |
---|
92 | ISR_lock_Context *lock_context |
---|
93 | ) |
---|
94 | { |
---|
95 | rtems_event_set pending_events; |
---|
96 | rtems_event_set seized_events; |
---|
97 | bool unblock; |
---|
98 | |
---|
99 | _Thread_Wait_acquire_default_critical( the_thread, lock_context ); |
---|
100 | |
---|
101 | _Event_sets_Post( event_in, &event->pending_events ); |
---|
102 | pending_events = event->pending_events; |
---|
103 | |
---|
104 | if ( |
---|
105 | _Event_Is_blocking_on_event( the_thread, wait_class ) |
---|
106 | && _Event_Is_satisfied( the_thread, pending_events, &seized_events ) |
---|
107 | ) { |
---|
108 | bool success; |
---|
109 | |
---|
110 | _Event_Satisfy( the_thread, event, pending_events, seized_events ); |
---|
111 | |
---|
112 | success = _Thread_Wait_flags_try_change_release( |
---|
113 | the_thread, |
---|
114 | wait_class | THREAD_WAIT_STATE_INTEND_TO_BLOCK, |
---|
115 | THREAD_WAIT_STATE_READY |
---|
116 | ); |
---|
117 | |
---|
118 | if ( success ) { |
---|
119 | unblock = false; |
---|
120 | } else { |
---|
121 | _Assert( |
---|
122 | _Thread_Wait_flags_get( the_thread ) |
---|
123 | == ( wait_class | THREAD_WAIT_STATE_BLOCKED ) |
---|
124 | ); |
---|
125 | _Thread_Wait_flags_set( the_thread, THREAD_WAIT_STATE_READY ); |
---|
126 | unblock = true; |
---|
127 | } |
---|
128 | } else { |
---|
129 | unblock = false; |
---|
130 | } |
---|
131 | |
---|
132 | if ( unblock ) { |
---|
133 | Per_CPU_Control *cpu_self; |
---|
134 | |
---|
135 | cpu_self = _Thread_Dispatch_disable_critical( lock_context ); |
---|
136 | _Thread_Wait_release_default( the_thread, lock_context ); |
---|
137 | |
---|
138 | _Thread_Timer_remove( the_thread ); |
---|
139 | _Thread_Unblock( the_thread ); |
---|
140 | |
---|
141 | _Thread_Dispatch_enable( cpu_self ); |
---|
142 | } else { |
---|
143 | _Thread_Wait_release_default( the_thread, lock_context ); |
---|
144 | } |
---|
145 | |
---|
146 | return RTEMS_SUCCESSFUL; |
---|
147 | } |
---|