1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Surrender Event |
---|
5 | * @ingroup ClassicEvent |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2008. |
---|
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/rtems/eventimpl.h> |
---|
22 | #include <rtems/rtems/optionsimpl.h> |
---|
23 | #include <rtems/score/threadimpl.h> |
---|
24 | #include <rtems/score/watchdogimpl.h> |
---|
25 | |
---|
26 | static void _Event_Satisfy( |
---|
27 | Thread_Control *the_thread, |
---|
28 | Event_Control *event, |
---|
29 | rtems_event_set pending_events, |
---|
30 | rtems_event_set seized_events |
---|
31 | ) |
---|
32 | { |
---|
33 | event->pending_events = _Event_sets_Clear( pending_events, seized_events ); |
---|
34 | *(rtems_event_set *) the_thread->Wait.return_argument = seized_events; |
---|
35 | } |
---|
36 | |
---|
37 | static bool _Event_Is_blocking_on_event( |
---|
38 | const Thread_Control *the_thread, |
---|
39 | Thread_Wait_flags wait_class |
---|
40 | ) |
---|
41 | { |
---|
42 | Thread_Wait_flags wait_flags; |
---|
43 | Thread_Wait_flags wait_mask; |
---|
44 | |
---|
45 | wait_flags = _Thread_Wait_flags_get( the_thread ); |
---|
46 | wait_mask = THREAD_WAIT_CLASS_MASK | THREAD_WAIT_STATE_READY_AGAIN; |
---|
47 | |
---|
48 | return ( wait_flags & wait_mask ) == wait_class; |
---|
49 | } |
---|
50 | |
---|
51 | static bool _Event_Is_satisfied( |
---|
52 | const Thread_Control *the_thread, |
---|
53 | rtems_event_set pending_events, |
---|
54 | rtems_event_set *seized_events |
---|
55 | ) |
---|
56 | { |
---|
57 | rtems_option option_set; |
---|
58 | rtems_event_set event_condition; |
---|
59 | |
---|
60 | option_set = the_thread->Wait.option; |
---|
61 | event_condition = the_thread->Wait.count; |
---|
62 | *seized_events = _Event_sets_Get( pending_events, event_condition ); |
---|
63 | |
---|
64 | return !_Event_sets_Is_empty( *seized_events ) |
---|
65 | && ( *seized_events == event_condition || _Options_Is_any( option_set ) ); |
---|
66 | } |
---|
67 | |
---|
68 | void _Event_Surrender( |
---|
69 | Thread_Control *the_thread, |
---|
70 | rtems_event_set event_in, |
---|
71 | Event_Control *event, |
---|
72 | Thread_Wait_flags wait_class, |
---|
73 | ISR_lock_Context *lock_context |
---|
74 | ) |
---|
75 | { |
---|
76 | rtems_event_set pending_events; |
---|
77 | rtems_event_set seized_events; |
---|
78 | bool unblock; |
---|
79 | |
---|
80 | _Thread_Lock_acquire_default_critical( the_thread, lock_context ); |
---|
81 | |
---|
82 | _Event_sets_Post( event_in, &event->pending_events ); |
---|
83 | pending_events = event->pending_events; |
---|
84 | |
---|
85 | if ( |
---|
86 | _Event_Is_blocking_on_event( the_thread, wait_class ) |
---|
87 | && _Event_Is_satisfied( the_thread, pending_events, &seized_events ) |
---|
88 | ) { |
---|
89 | Thread_Wait_flags ready_again; |
---|
90 | bool success; |
---|
91 | |
---|
92 | _Event_Satisfy( the_thread, event, pending_events, seized_events ); |
---|
93 | |
---|
94 | /* See _Event_Seize() */ |
---|
95 | _Atomic_Fence( ATOMIC_ORDER_RELEASE ); |
---|
96 | |
---|
97 | ready_again = wait_class | THREAD_WAIT_STATE_READY_AGAIN; |
---|
98 | success = _Thread_Wait_flags_try_change_critical( |
---|
99 | the_thread, |
---|
100 | wait_class | THREAD_WAIT_STATE_INTEND_TO_BLOCK, |
---|
101 | ready_again |
---|
102 | ); |
---|
103 | |
---|
104 | if ( success ) { |
---|
105 | unblock = false; |
---|
106 | } else { |
---|
107 | _Assert( |
---|
108 | _Thread_Wait_flags_get( the_thread ) |
---|
109 | == ( wait_class | THREAD_WAIT_STATE_BLOCKED ) |
---|
110 | ); |
---|
111 | _Thread_Wait_flags_set( the_thread, ready_again ); |
---|
112 | unblock = true; |
---|
113 | } |
---|
114 | } else { |
---|
115 | unblock = false; |
---|
116 | } |
---|
117 | |
---|
118 | if ( unblock ) { |
---|
119 | Per_CPU_Control *cpu_self; |
---|
120 | |
---|
121 | cpu_self = _Thread_Dispatch_disable_critical( lock_context ); |
---|
122 | _Thread_Lock_release_default( the_thread, lock_context ); |
---|
123 | |
---|
124 | _Watchdog_Remove_ticks( &the_thread->Timer ); |
---|
125 | _Thread_Unblock( the_thread ); |
---|
126 | |
---|
127 | _Thread_Dispatch_enable( cpu_self ); |
---|
128 | } else { |
---|
129 | _Thread_Lock_release_default( the_thread, lock_context ); |
---|
130 | } |
---|
131 | } |
---|