1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Event Manager Initialization |
---|
5 | * @ingroup ClassicEvent Events |
---|
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/sysinit.h> |
---|
22 | #include <rtems/rtems/eventimpl.h> |
---|
23 | #include <rtems/rtems/optionsimpl.h> |
---|
24 | #include <rtems/score/threadimpl.h> |
---|
25 | #include <rtems/score/watchdogimpl.h> |
---|
26 | |
---|
27 | /* |
---|
28 | * INTERRUPT LATENCY: |
---|
29 | * available |
---|
30 | * wait |
---|
31 | * check sync |
---|
32 | */ |
---|
33 | |
---|
34 | void _Event_Seize( |
---|
35 | rtems_event_set event_in, |
---|
36 | rtems_option option_set, |
---|
37 | rtems_interval ticks, |
---|
38 | rtems_event_set *event_out, |
---|
39 | Thread_Control *executing, |
---|
40 | Event_Control *event, |
---|
41 | Thread_Wait_flags wait_class, |
---|
42 | States_Control block_state, |
---|
43 | ISR_lock_Context *lock_context |
---|
44 | ) |
---|
45 | { |
---|
46 | rtems_event_set seized_events; |
---|
47 | rtems_event_set pending_events; |
---|
48 | bool success; |
---|
49 | Thread_Wait_flags intend_to_block; |
---|
50 | Per_CPU_Control *cpu_self; |
---|
51 | |
---|
52 | executing->Wait.return_code = RTEMS_SUCCESSFUL; |
---|
53 | |
---|
54 | pending_events = event->pending_events; |
---|
55 | seized_events = _Event_sets_Get( pending_events, event_in ); |
---|
56 | |
---|
57 | if ( !_Event_sets_Is_empty( seized_events ) && |
---|
58 | (seized_events == event_in || _Options_Is_any( option_set )) ) { |
---|
59 | event->pending_events = |
---|
60 | _Event_sets_Clear( pending_events, seized_events ); |
---|
61 | _Thread_Lock_release_default( executing, lock_context ); |
---|
62 | *event_out = seized_events; |
---|
63 | return; |
---|
64 | } |
---|
65 | |
---|
66 | if ( _Options_Is_no_wait( option_set ) ) { |
---|
67 | _Thread_Lock_release_default( executing, lock_context ); |
---|
68 | executing->Wait.return_code = RTEMS_UNSATISFIED; |
---|
69 | *event_out = seized_events; |
---|
70 | return; |
---|
71 | } |
---|
72 | |
---|
73 | intend_to_block = wait_class | THREAD_WAIT_STATE_INTEND_TO_BLOCK; |
---|
74 | |
---|
75 | /* |
---|
76 | * Note what we are waiting for BEFORE we enter the critical section. |
---|
77 | * The interrupt critical section management code needs this to be |
---|
78 | * set properly when we are marked as in the event critical section. |
---|
79 | * |
---|
80 | * NOTE: Since interrupts are disabled, this isn't that much of an |
---|
81 | * issue but better safe than sorry. |
---|
82 | */ |
---|
83 | executing->Wait.option = option_set; |
---|
84 | executing->Wait.count = event_in; |
---|
85 | executing->Wait.return_argument = event_out; |
---|
86 | _Thread_Wait_flags_set( executing, intend_to_block ); |
---|
87 | |
---|
88 | cpu_self = _Thread_Dispatch_disable_critical( lock_context ); |
---|
89 | _Thread_Lock_release_default( executing, lock_context ); |
---|
90 | |
---|
91 | if ( ticks ) { |
---|
92 | _Thread_Wait_set_timeout_code( executing, RTEMS_TIMEOUT ); |
---|
93 | _Watchdog_Initialize( |
---|
94 | &executing->Timer, |
---|
95 | _Thread_Timeout, |
---|
96 | 0, |
---|
97 | executing |
---|
98 | ); |
---|
99 | _Watchdog_Insert_ticks( &executing->Timer, ticks ); |
---|
100 | } |
---|
101 | |
---|
102 | _Thread_Set_state( executing, block_state ); |
---|
103 | |
---|
104 | /* |
---|
105 | * See _Event_Surrender() and _Thread_Timeout(), corresponding atomic |
---|
106 | * variable is Thread_Control::Wait::flags. |
---|
107 | */ |
---|
108 | _Atomic_Fence( ATOMIC_ORDER_ACQUIRE ); |
---|
109 | |
---|
110 | success = _Thread_Wait_flags_try_change( |
---|
111 | executing, |
---|
112 | intend_to_block, |
---|
113 | wait_class | THREAD_WAIT_STATE_BLOCKED |
---|
114 | ); |
---|
115 | if ( !success ) { |
---|
116 | _Watchdog_Remove_ticks( &executing->Timer ); |
---|
117 | _Thread_Unblock( executing ); |
---|
118 | } |
---|
119 | |
---|
120 | _Thread_Dispatch_enable( cpu_self ); |
---|
121 | } |
---|
122 | |
---|
123 | #if defined(RTEMS_MULTIPROCESSING) |
---|
124 | static void _Event_Manager_initialization( void ) |
---|
125 | { |
---|
126 | _MPCI_Register_packet_processor( MP_PACKET_EVENT, _Event_MP_Process_packet ); |
---|
127 | } |
---|
128 | |
---|
129 | RTEMS_SYSINIT_ITEM( |
---|
130 | _Event_Manager_initialization, |
---|
131 | RTEMS_SYSINIT_CLASSIC_EVENT, |
---|
132 | RTEMS_SYSINIT_ORDER_MIDDLE |
---|
133 | ); |
---|
134 | #endif |
---|