source: rtems/cpukit/rtems/src/eventseize.c @ b84a51c

5
Last change on this file since b84a51c was b84a51c, checked in by Sebastian Huber <sebastian.huber@…>, on 11/10/15 at 16:23:12

score: Fix race condition on SMP

We must ensure that the Thread_Control::Wait information update is
visible to the target thread before we update its wait flags, otherwise
we may return out of date events or a wrong status.

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