Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:26

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSScoreMessageQueue
0007  *
0008  * @brief This source file contains the implementation of
0009  *   _CORE_message_queue_Seize().
0010  */
0011 
0012 /*
0013  *  COPYRIGHT (c) 1989-2007.
0014  *  On-Line Applications Research Corporation (OAR).
0015  *
0016  * Redistribution and use in source and binary forms, with or without
0017  * modification, are permitted provided that the following conditions
0018  * are met:
0019  * 1. Redistributions of source code must retain the above copyright
0020  *    notice, this list of conditions and the following disclaimer.
0021  * 2. Redistributions in binary form must reproduce the above copyright
0022  *    notice, this list of conditions and the following disclaimer in the
0023  *    documentation and/or other materials provided with the distribution.
0024  *
0025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0035  * POSSIBILITY OF SUCH DAMAGE.
0036  */
0037 
0038 #ifdef HAVE_CONFIG_H
0039 #include "config.h"
0040 #endif
0041 
0042 #include <rtems/score/chain.h>
0043 #include <rtems/score/isr.h>
0044 #include <rtems/score/coremsgimpl.h>
0045 #include <rtems/score/threadimpl.h>
0046 #include <rtems/score/statesimpl.h>
0047 
0048 Status_Control _CORE_message_queue_Seize(
0049   CORE_message_queue_Control *the_message_queue,
0050   Thread_Control             *executing,
0051   void                       *buffer,
0052   size_t                     *size_p,
0053   bool                        wait,
0054   Thread_queue_Context       *queue_context
0055 )
0056 {
0057   CORE_message_queue_Buffer *the_message;
0058 
0059   the_message = _CORE_message_queue_Get_pending_message( the_message_queue );
0060   if ( the_message != NULL ) {
0061     the_message_queue->number_of_pending_messages -= 1;
0062 
0063     *size_p = the_message->size;
0064     executing->Wait.count =
0065       _CORE_message_queue_Get_message_priority( the_message );
0066     _CORE_message_queue_Copy_buffer( the_message->buffer, buffer, *size_p );
0067 
0068     #if !defined(RTEMS_SCORE_COREMSG_ENABLE_BLOCKING_SEND)
0069       /*
0070        *  There is not an API with blocking sends enabled.
0071        *  So return immediately.
0072        */
0073       _CORE_message_queue_Free_message_buffer(the_message_queue, the_message);
0074       _CORE_message_queue_Release( the_message_queue, queue_context );
0075       return STATUS_SUCCESSFUL;
0076     #else
0077     {
0078       Thread_queue_Heads *heads;
0079       Thread_Control     *the_thread;
0080 
0081       /*
0082        *  There could be a thread waiting to send a message.  If there
0083        *  is not, then we can go ahead and free the buffer.
0084        *
0085        *  NOTE: If we note that the queue was not full before this receive,
0086        *  then we can avoid this dequeue.
0087        */
0088       heads = the_message_queue->Wait_queue.Queue.heads;
0089       if ( heads == NULL ) {
0090         _CORE_message_queue_Free_message_buffer(
0091           the_message_queue,
0092           the_message
0093         );
0094         _CORE_message_queue_Release( the_message_queue, queue_context );
0095         return STATUS_SUCCESSFUL;
0096       }
0097 
0098       the_thread = ( *the_message_queue->operations->surrender )(
0099         &the_message_queue->Wait_queue.Queue,
0100         heads,
0101         NULL,
0102         queue_context
0103       );
0104 
0105       /*
0106        *  There was a thread waiting to send a message.  This code
0107        *  puts the messages in the message queue on behalf of the
0108        *  waiting task.
0109        */
0110       _CORE_message_queue_Insert_message(
0111         the_message_queue,
0112         the_message,
0113         the_thread->Wait.return_argument_second.immutable_object,
0114         (size_t) the_thread->Wait.option,
0115         (CORE_message_queue_Submit_types) the_thread->Wait.count
0116       );
0117       _Thread_queue_Resume(
0118         &the_message_queue->Wait_queue.Queue,
0119         the_thread,
0120         queue_context
0121       );
0122       return STATUS_SUCCESSFUL;
0123     }
0124     #endif
0125   }
0126 
0127   if ( !wait ) {
0128     _CORE_message_queue_Release( the_message_queue, queue_context );
0129     return STATUS_UNSATISFIED;
0130   }
0131 
0132   executing->Wait.return_argument_second.mutable_object = buffer;
0133   executing->Wait.return_argument = size_p;
0134   /* Wait.count will be filled in with the message priority */
0135 
0136   _Thread_queue_Context_set_thread_state(
0137     queue_context,
0138     STATES_WAITING_FOR_MESSAGE
0139   );
0140   _Thread_queue_Enqueue(
0141     &the_message_queue->Wait_queue.Queue,
0142     the_message_queue->operations,
0143     executing,
0144     queue_context
0145   );
0146   return _Thread_Wait_get_status( executing );
0147 }