source: rtems/cpukit/rtems/src/eventsurrender.c @ 66cb142

Last change on this file since 66cb142 was 1fcac5a, checked in by Sebastian Huber <sebastian.huber@…>, on Jul 25, 2016 at 2:35:37 PM

score: Turn thread lock into thread wait lock

The _Thread_Lock_acquire() function had a potentially infinite run-time
due to the lack of fairness at atomic operations level.

Update #2412.
Update #2556.
Update #2765.

  • Property mode set to 100644
File size: 3.3 KB
Line 
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
26static 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
37static 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
51static 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
68rtems_status_code _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_Wait_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    ready_again = wait_class | THREAD_WAIT_STATE_READY_AGAIN;
95    success = _Thread_Wait_flags_try_change_release(
96      the_thread,
97      wait_class | THREAD_WAIT_STATE_INTEND_TO_BLOCK,
98      ready_again
99    );
100
101    if ( success ) {
102      unblock = false;
103    } else {
104      _Assert(
105        _Thread_Wait_flags_get( the_thread )
106          == ( wait_class | THREAD_WAIT_STATE_BLOCKED )
107      );
108      _Thread_Wait_flags_set( the_thread, ready_again );
109      unblock = true;
110    }
111  } else {
112    unblock = false;
113  }
114
115  if ( unblock ) {
116    Per_CPU_Control *cpu_self;
117
118    cpu_self = _Thread_Dispatch_disable_critical( lock_context );
119    _Thread_Wait_release_default( the_thread, lock_context );
120
121    _Thread_Timer_remove( the_thread );
122    _Thread_Unblock( the_thread );
123
124    _Thread_Dispatch_enable( cpu_self );
125  } else {
126    _Thread_Wait_release_default( the_thread, lock_context );
127  }
128
129  return RTEMS_SUCCESSFUL;
130}
Note: See TracBrowser for help on using the repository browser.