source: rtems/cpukit/score/src/coremsgsubmit.c @ c86da31c

4.115
Last change on this file since c86da31c was 28352fae, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/29/09 at 13:51:53

Whitespace removal.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 *  CORE Message Queue Handler
3 *
4 *  DESCRIPTION:
5 *
6 *  This package is the implementation of the CORE Message Queue Handler.
7 *  This core object provides task synchronization and communication functions
8 *  via messages passed to queue objects.
9 *
10 *  COPYRIGHT (c) 1989-2009.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 *  The license and distribution terms for this file may be
14 *  found in the file LICENSE in this distribution or at
15 *  http://www.rtems.com/license/LICENSE.
16 *
17 *  $Id$
18 */
19
20#if HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#include <rtems/system.h>
25#include <rtems/score/chain.h>
26#include <rtems/score/isr.h>
27#include <rtems/score/object.h>
28#include <rtems/score/coremsg.h>
29#include <rtems/score/states.h>
30#include <rtems/score/thread.h>
31#include <rtems/score/wkspace.h>
32
33/*PAGE
34 *
35 *  _CORE_message_queue_Submit
36 *
37 *  This routine implements the send and urgent message functions. It
38 *  processes a message that is to be submitted to the designated
39 *  message queue.  The message will either be processed as a
40 *  send message which it will be inserted at the rear of the queue
41 *  or it will be processed as an urgent message which will be inserted
42 *  at the front of the queue.
43 *
44 *  Input parameters:
45 *    the_message_queue            - message is submitted to this message queue
46 *    buffer                       - pointer to message buffer
47 *    size                         - size in bytes of message to send
48 *    id                           - id of message queue
49 *    api_message_queue_mp_support - api specific mp support callout
50 *    submit_type                  - send or urgent message
51 *
52 *  Output parameters:
53 *    CORE_MESSAGE_QUEUE_SUCCESSFUL - if successful
54 *    error code                    - if unsuccessful
55 */
56
57CORE_message_queue_Status _CORE_message_queue_Submit(
58  CORE_message_queue_Control                *the_message_queue,
59  const void                                *buffer,
60  size_t                                     size,
61  Objects_Id                                 id,
62  #if defined(RTEMS_MULTIPROCESSING)
63    CORE_message_queue_API_mp_support_callout  api_message_queue_mp_support,
64  #else
65    CORE_message_queue_API_mp_support_callout  api_message_queue_mp_support  __attribute__((unused)),
66  #endif
67  CORE_message_queue_Submit_types            submit_type,
68  bool                                       wait,
69  Watchdog_Interval                          timeout
70)
71{
72  CORE_message_queue_Buffer_control   *the_message;
73  Thread_Control                      *the_thread;
74
75  if ( size > the_message_queue->maximum_message_size ) {
76    return CORE_MESSAGE_QUEUE_STATUS_INVALID_SIZE;
77  }
78
79  /*
80   *  Is there a thread currently waiting on this message queue?
81   */
82  if ( the_message_queue->number_of_pending_messages == 0 ) {
83    the_thread = _Thread_queue_Dequeue( &the_message_queue->Wait_queue );
84    if ( the_thread ) {
85      _CORE_message_queue_Copy_buffer(
86        buffer,
87        the_thread->Wait.return_argument_second.mutable_object,
88        size
89      );
90      *(size_t *) the_thread->Wait.return_argument = size;
91      the_thread->Wait.count = (uint32_t) submit_type;
92
93      #if defined(RTEMS_MULTIPROCESSING)
94        if ( !_Objects_Is_local_id( the_thread->Object.id ) )
95          (*api_message_queue_mp_support) ( the_thread, id );
96      #endif
97      return CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL;
98    }
99  }
100
101  /*
102   *  No one waiting on the message queue at this time, so attempt to
103   *  queue the message up for a future receive.
104   */
105  if ( the_message_queue->number_of_pending_messages <
106       the_message_queue->maximum_pending_messages ) {
107
108    the_message =
109        _CORE_message_queue_Allocate_message_buffer( the_message_queue );
110
111    #if defined(RTEMS_DEBUG)
112      /*
113       *  NOTE: If the system is consistent, this error should never occur.
114       */
115
116      if ( !the_message )
117        return CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED;
118     #endif
119
120    _CORE_message_queue_Copy_buffer(
121      buffer,
122      the_message->Contents.buffer,
123      size
124    );
125    the_message->Contents.size = size;
126    _CORE_message_queue_Set_message_priority( the_message, submit_type );
127
128    _CORE_message_queue_Insert_message(
129       the_message_queue,
130       the_message,
131       submit_type
132    );
133    return CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL;
134  }
135
136  #if !defined(RTEMS_SCORE_COREMSG_ENABLE_BLOCKING_SEND)
137    return CORE_MESSAGE_QUEUE_STATUS_TOO_MANY;
138  #else
139    /*
140     *  No message buffers were available so we may need to return an
141     *  overflow error or block the sender until the message is placed
142     *  on the queue.
143     */
144    if ( !wait ) {
145      return CORE_MESSAGE_QUEUE_STATUS_TOO_MANY;
146    }
147
148    /*
149     *  Do NOT block on a send if the caller is in an ISR.  It is
150     *  deadly to block in an ISR.
151     */
152    if ( _ISR_Is_in_progress() ) {
153      return CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED;
154    }
155
156    /*
157     *  WARNING!! executing should NOT be used prior to this point.
158     *  Thus the unusual choice to open a new scope and declare
159     *  it as a variable.  Doing this emphasizes how dangerous it
160     *  would be to use this variable prior to here.
161     */
162    {
163      Thread_Control  *executing = _Thread_Executing;
164      ISR_Level        level;
165
166      _ISR_Disable( level );
167      _Thread_queue_Enter_critical_section( &the_message_queue->Wait_queue );
168      executing->Wait.queue = &the_message_queue->Wait_queue;
169      executing->Wait.id = id;
170      executing->Wait.return_argument_second.immutable_object = buffer;
171      executing->Wait.option = (uint32_t) size;
172      executing->Wait.count = submit_type;
173      _ISR_Enable( level );
174
175      _Thread_queue_Enqueue( &the_message_queue->Wait_queue, timeout );
176    }
177
178    return CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED_WAIT;
179  #endif
180}
Note: See TracBrowser for help on using the repository browser.