source: rtems/cpukit/score/src/coremsgseize.c @ e6f7f81

4.115
Last change on this file since e6f7f81 was 33a4a56, checked in by Sebastian Huber <sebastian.huber@…>, on 07/18/13 at 14:00:54

score: Avoid direct usage of _Thread_Executing

Pass the executing thread as a function parameter. Obtain the executing
thread inside a thread dispatch critical section to avoid problems on
SMP.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Size a Message from the Message Queue
5 *  @ingroup ScoreMessageQueue
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2007.
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.com/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/system.h>
22#include <rtems/score/chain.h>
23#include <rtems/score/isr.h>
24#include <rtems/score/object.h>
25#include <rtems/score/coremsgimpl.h>
26#include <rtems/score/states.h>
27#include <rtems/score/thread.h>
28#include <rtems/score/wkspace.h>
29
30void _CORE_message_queue_Seize(
31  CORE_message_queue_Control      *the_message_queue,
32  Thread_Control                  *executing,
33  Objects_Id                       id,
34  void                            *buffer,
35  size_t                          *size_p,
36  bool                             wait,
37  Watchdog_Interval                timeout
38)
39{
40  ISR_Level                          level;
41  CORE_message_queue_Buffer_control *the_message;
42
43  executing->Wait.return_code = CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL;
44  _ISR_Disable( level );
45  the_message = _CORE_message_queue_Get_pending_message( the_message_queue );
46  if ( the_message != NULL ) {
47    the_message_queue->number_of_pending_messages -= 1;
48    _ISR_Enable( level );
49
50    *size_p = the_message->Contents.size;
51    executing->Wait.count =
52      _CORE_message_queue_Get_message_priority( the_message );
53    _CORE_message_queue_Copy_buffer(
54      the_message->Contents.buffer,
55      buffer,
56      *size_p
57    );
58
59    #if !defined(RTEMS_SCORE_COREMSG_ENABLE_BLOCKING_SEND)
60      /*
61       *  There is not an API with blocking sends enabled.
62       *  So return immediately.
63       */
64      _CORE_message_queue_Free_message_buffer(the_message_queue, the_message);
65      return;
66    #else
67    {
68      Thread_Control   *the_thread;
69
70      /*
71       *  There could be a thread waiting to send a message.  If there
72       *  is not, then we can go ahead and free the buffer.
73       *
74       *  NOTE: If we note that the queue was not full before this receive,
75       *  then we can avoid this dequeue.
76       */
77      the_thread = _Thread_queue_Dequeue( &the_message_queue->Wait_queue );
78      if ( !the_thread ) {
79        _CORE_message_queue_Free_message_buffer(
80          the_message_queue,
81          the_message
82        );
83        return;
84      }
85
86      /*
87       *  There was a thread waiting to send a message.  This code
88       *  puts the messages in the message queue on behalf of the
89       *  waiting task.
90       */
91      _CORE_message_queue_Set_message_priority(
92        the_message,
93        the_thread->Wait.count
94      );
95      the_message->Contents.size = (size_t) the_thread->Wait.option;
96      _CORE_message_queue_Copy_buffer(
97        the_thread->Wait.return_argument_second.immutable_object,
98        the_message->Contents.buffer,
99        the_message->Contents.size
100      );
101
102      _CORE_message_queue_Insert_message(
103         the_message_queue,
104         the_message,
105         _CORE_message_queue_Get_message_priority( the_message )
106      );
107      return;
108    }
109    #endif
110  }
111
112  if ( !wait ) {
113    _ISR_Enable( level );
114    executing->Wait.return_code = CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED_NOWAIT;
115    return;
116  }
117
118  _Thread_queue_Enter_critical_section( &the_message_queue->Wait_queue );
119  executing->Wait.queue = &the_message_queue->Wait_queue;
120  executing->Wait.id = id;
121  executing->Wait.return_argument_second.mutable_object = buffer;
122  executing->Wait.return_argument = size_p;
123  /* Wait.count will be filled in with the message priority */
124  _ISR_Enable( level );
125
126  _Thread_queue_Enqueue( &the_message_queue->Wait_queue, timeout );
127}
Note: See TracBrowser for help on using the repository browser.