1 | /* |
---|
2 | * Event Manager |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2008. |
---|
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/rtems/status.h> |
---|
18 | #include <rtems/rtems/event.h> |
---|
19 | #include <rtems/score/isr.h> |
---|
20 | #include <rtems/score/object.h> |
---|
21 | #include <rtems/rtems/options.h> |
---|
22 | #include <rtems/score/states.h> |
---|
23 | #include <rtems/score/thread.h> |
---|
24 | #include <rtems/rtems/tasks.h> |
---|
25 | |
---|
26 | /* |
---|
27 | * _Event_Seize |
---|
28 | * |
---|
29 | * This routine attempts to satisfy the requested event condition |
---|
30 | * for the running thread. |
---|
31 | * |
---|
32 | * Input parameters: |
---|
33 | * event_in - the event condition to satisfy |
---|
34 | * option_set - acquire event options |
---|
35 | * ticks - interval to wait |
---|
36 | * event_out - pointer to event set output area |
---|
37 | * |
---|
38 | * Output parameters: NONE |
---|
39 | * *event_out - event set output area filled in |
---|
40 | * |
---|
41 | * INTERRUPT LATENCY: |
---|
42 | * available |
---|
43 | * wait |
---|
44 | * check sync |
---|
45 | */ |
---|
46 | |
---|
47 | void _Event_Seize( |
---|
48 | rtems_event_set event_in, |
---|
49 | rtems_option option_set, |
---|
50 | rtems_interval ticks, |
---|
51 | rtems_event_set *event_out |
---|
52 | ) |
---|
53 | { |
---|
54 | Thread_Control *executing; |
---|
55 | rtems_event_set seized_events; |
---|
56 | rtems_event_set pending_events; |
---|
57 | ISR_Level level; |
---|
58 | RTEMS_API_Control *api; |
---|
59 | Thread_blocking_operation_States sync_state; |
---|
60 | |
---|
61 | executing = _Thread_Executing; |
---|
62 | executing->Wait.return_code = RTEMS_SUCCESSFUL; |
---|
63 | |
---|
64 | api = executing->API_Extensions[ THREAD_API_RTEMS ]; |
---|
65 | |
---|
66 | _ISR_Disable( level ); |
---|
67 | pending_events = api->pending_events; |
---|
68 | seized_events = _Event_sets_Get( pending_events, event_in ); |
---|
69 | |
---|
70 | if ( !_Event_sets_Is_empty( seized_events ) && |
---|
71 | (seized_events == event_in || _Options_Is_any( option_set )) ) { |
---|
72 | api->pending_events = |
---|
73 | _Event_sets_Clear( pending_events, seized_events ); |
---|
74 | _ISR_Enable( level ); |
---|
75 | *event_out = seized_events; |
---|
76 | return; |
---|
77 | } |
---|
78 | |
---|
79 | if ( _Options_Is_no_wait( option_set ) ) { |
---|
80 | _ISR_Enable( level ); |
---|
81 | executing->Wait.return_code = RTEMS_UNSATISFIED; |
---|
82 | *event_out = seized_events; |
---|
83 | return; |
---|
84 | } |
---|
85 | |
---|
86 | /* |
---|
87 | * Note what we are waiting for BEFORE we enter the critical section. |
---|
88 | * The interrupt critical section management code needs this to be |
---|
89 | * set properly when we are marked as in the event critical section. |
---|
90 | * |
---|
91 | * NOTE: Since interrupts are disabled, this isn't that much of an |
---|
92 | * issue but better safe than sorry. |
---|
93 | */ |
---|
94 | executing->Wait.option = (uint32_t) option_set; |
---|
95 | executing->Wait.count = (uint32_t) event_in; |
---|
96 | executing->Wait.return_argument = event_out; |
---|
97 | |
---|
98 | _Event_Sync_state = THREAD_BLOCKING_OPERATION_NOTHING_HAPPENED; |
---|
99 | |
---|
100 | _ISR_Enable( level ); |
---|
101 | |
---|
102 | if ( ticks ) { |
---|
103 | _Watchdog_Initialize( |
---|
104 | &executing->Timer, |
---|
105 | _Event_Timeout, |
---|
106 | executing->Object.id, |
---|
107 | NULL |
---|
108 | ); |
---|
109 | _Watchdog_Insert_ticks( &executing->Timer, ticks ); |
---|
110 | } |
---|
111 | |
---|
112 | _Thread_Set_state( executing, STATES_WAITING_FOR_EVENT ); |
---|
113 | |
---|
114 | _ISR_Disable( level ); |
---|
115 | |
---|
116 | sync_state = _Event_Sync_state; |
---|
117 | _Event_Sync_state = THREAD_BLOCKING_OPERATION_SYNCHRONIZED; |
---|
118 | if ( sync_state == THREAD_BLOCKING_OPERATION_NOTHING_HAPPENED ) { |
---|
119 | _ISR_Enable( level ); |
---|
120 | return; |
---|
121 | } |
---|
122 | |
---|
123 | /* |
---|
124 | * An interrupt completed the thread's blocking request. |
---|
125 | * The blocking thread was satisfied by an ISR or timed out. |
---|
126 | * |
---|
127 | * WARNING! Entering with interrupts disabled and returning with interrupts |
---|
128 | * enabled! |
---|
129 | */ |
---|
130 | _Thread_blocking_operation_Cancel( sync_state, executing, level ); |
---|
131 | } |
---|