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

5
Last change on this file since c3105894 was c3105894, checked in by Sebastian Huber <sebastian.huber@…>, on 10/19/17 at 11:47:57

score: Move thread queue timeout handling

Update #3117.
Update #3182.

  • Property mode set to 100644
File size: 3.6 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.org/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/coremsgimpl.h>
25#include <rtems/score/threadimpl.h>
26#include <rtems/score/statesimpl.h>
27
28Status_Control _CORE_message_queue_Seize(
29  CORE_message_queue_Control *the_message_queue,
30  Thread_Control             *executing,
31  void                       *buffer,
32  size_t                     *size_p,
33  bool                        wait,
34  Thread_queue_Context       *queue_context
35)
36{
37  CORE_message_queue_Buffer_control *the_message;
38
39  the_message = _CORE_message_queue_Get_pending_message( the_message_queue );
40  if ( the_message != NULL ) {
41    the_message_queue->number_of_pending_messages -= 1;
42
43    *size_p = the_message->Contents.size;
44    executing->Wait.count =
45      _CORE_message_queue_Get_message_priority( the_message );
46    _CORE_message_queue_Copy_buffer(
47      the_message->Contents.buffer,
48      buffer,
49      *size_p
50    );
51
52    #if !defined(RTEMS_SCORE_COREMSG_ENABLE_BLOCKING_SEND)
53      /*
54       *  There is not an API with blocking sends enabled.
55       *  So return immediately.
56       */
57      _CORE_message_queue_Free_message_buffer(the_message_queue, the_message);
58      _CORE_message_queue_Release( the_message_queue, queue_context );
59      return STATUS_SUCCESSFUL;
60    #else
61    {
62      Thread_Control   *the_thread;
63
64      /*
65       *  There could be a thread waiting to send a message.  If there
66       *  is not, then we can go ahead and free the buffer.
67       *
68       *  NOTE: If we note that the queue was not full before this receive,
69       *  then we can avoid this dequeue.
70       */
71      the_thread = _Thread_queue_First_locked(
72        &the_message_queue->Wait_queue,
73        the_message_queue->operations
74      );
75      if ( the_thread == NULL ) {
76        _CORE_message_queue_Free_message_buffer(
77          the_message_queue,
78          the_message
79        );
80        _CORE_message_queue_Release( the_message_queue, queue_context );
81        return STATUS_SUCCESSFUL;
82      }
83
84      /*
85       *  There was a thread waiting to send a message.  This code
86       *  puts the messages in the message queue on behalf of the
87       *  waiting task.
88       */
89      _CORE_message_queue_Insert_message(
90        the_message_queue,
91        the_message,
92        the_thread->Wait.return_argument_second.immutable_object,
93        (size_t) the_thread->Wait.option,
94        (CORE_message_queue_Submit_types) the_thread->Wait.count
95      );
96      _Thread_queue_Extract_critical(
97        &the_message_queue->Wait_queue.Queue,
98        the_message_queue->operations,
99        the_thread,
100        queue_context
101      );
102      return STATUS_SUCCESSFUL;
103    }
104    #endif
105  }
106
107  if ( !wait ) {
108    _CORE_message_queue_Release( the_message_queue, queue_context );
109    return STATUS_UNSATISFIED;
110  }
111
112  executing->Wait.return_argument_second.mutable_object = buffer;
113  executing->Wait.return_argument = size_p;
114  /* Wait.count will be filled in with the message priority */
115
116  _Thread_queue_Context_set_thread_state(
117    queue_context,
118    STATES_WAITING_FOR_MESSAGE
119  );
120  _Thread_queue_Enqueue(
121    &the_message_queue->Wait_queue.Queue,
122    the_message_queue->operations,
123    executing,
124    queue_context
125  );
126  return _Thread_Wait_get_status( executing );
127}
Note: See TracBrowser for help on using the repository browser.