source: rtems/cpukit/score/src/threadqextractpriority.c @ 2369b10

4.115
Last change on this file since 2369b10 was 91e7b0c, checked in by Sebastian Huber <sebastian.huber@…>, on 03/27/14 at 08:04:47

score: PR2172: _Thread_queue_Extract()

Add _Thread_queue_Extract_with_return_code(). On SMP this sequence in
_Thread_queue_Process_timeout() was broken:

[...]

/*

  • After we enable interrupts here, a lot may happen in the
  • meantime, e.g. nested interrupts may release the resource that
  • times out here. So we enter _Thread_queue_Extract()
  • speculatively. Inside this function we check the actual status
  • under ISR disable protection. This ensures that exactly one
  • executing context performs the extract operation (other parties
  • may call _Thread_queue_Dequeue()). If this context won, then
  • we have a timeout. *
  • We can use the_thread_queue pointer here even if
  • the_thread->Wait.queue is already set to NULL since the extract
  • operation will only use the thread queue discipline to select
  • the right extract operation. The timeout status is set during
  • thread queue initialization. */

we_did_it = _Thread_queue_Extract( the_thread_queue, the_thread );
if ( we_did_it ) {

the_thread->Wait.return_code = the_thread_queue->timeout_status;

}

[...]

In case _Thread_queue_Extract() successfully extracted a thread, then
this thread may start execution on a remote processor immediately and
read the the_thread->Wait.return_code before we update it here with the
timeout status. Thus it observes a successful operation even if it
timed out.

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/**
2 * @file
3 *
4 * @brief Thread queue Extract priority Helper
5 * @ingroup ScoreThreadQ
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/score/threadqimpl.h>
22#include <rtems/score/chainimpl.h>
23#include <rtems/score/isrlevel.h>
24#include <rtems/score/threadimpl.h>
25#include <rtems/score/watchdogimpl.h>
26
27void _Thread_queue_Extract_priority_helper(
28  Thread_Control       *the_thread,
29  uint32_t              return_code,
30  bool                  requeuing
31)
32{
33  ISR_Level       level;
34  Chain_Node     *head;
35  Chain_Node     *tail;
36  Chain_Node     *the_node;
37  Chain_Node     *next_node;
38  Chain_Node     *previous_node;
39  Thread_Control *new_first_thread;
40  Chain_Node     *new_first_node;
41  Chain_Node     *new_second_node;
42  Chain_Node     *last_node;
43
44  the_node = (Chain_Node *) the_thread;
45  _ISR_Disable( level );
46  if ( !_States_Is_waiting_on_thread_queue( the_thread->current_state ) ) {
47    _ISR_Enable( level );
48    return;
49  }
50
51  /*
52   *  The thread was actually waiting on a thread queue so let's remove it.
53   */
54
55  next_node     = the_node->next;
56  previous_node = the_node->previous;
57
58  if ( !_Chain_Is_empty( &the_thread->Wait.Block2n ) ) {
59    new_first_node   = _Chain_First( &the_thread->Wait.Block2n );
60    new_first_thread = (Thread_Control *) new_first_node;
61    last_node        = _Chain_Last( &the_thread->Wait.Block2n );
62    new_second_node  = new_first_node->next;
63
64    previous_node->next      = new_first_node;
65    next_node->previous      = new_first_node;
66    new_first_node->next     = next_node;
67    new_first_node->previous = previous_node;
68
69    if ( !_Chain_Has_only_one_node( &the_thread->Wait.Block2n ) ) {
70                                        /* > two threads on 2-n */
71      head = _Chain_Head( &new_first_thread->Wait.Block2n );
72      tail = _Chain_Tail( &new_first_thread->Wait.Block2n );
73
74      new_second_node->previous = head;
75      head->next = new_second_node;
76      tail->previous = last_node;
77      last_node->next = tail;
78    }
79  } else {
80    previous_node->next = next_node;
81    next_node->previous = previous_node;
82  }
83
84  /*
85   *  If we are not supposed to touch timers or the thread's state, return.
86   */
87
88  if ( requeuing ) {
89    _ISR_Enable( level );
90    return;
91  }
92
93  the_thread->Wait.queue = NULL;
94  the_thread->Wait.return_code = return_code;
95
96  if ( !_Watchdog_Is_active( &the_thread->Timer ) ) {
97    _ISR_Enable( level );
98  } else {
99    _Watchdog_Deactivate( &the_thread->Timer );
100    _ISR_Enable( level );
101    (void) _Watchdog_Remove( &the_thread->Timer );
102  }
103  _Thread_Unblock( the_thread );
104
105#if defined(RTEMS_MULTIPROCESSING)
106  if ( !_Objects_Is_local_id( the_thread->Object.id ) )
107    _Thread_MP_Free_proxy( the_thread );
108#endif
109}
Note: See TracBrowser for help on using the repository browser.