Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup POSIX_MQUEUE_P Message Queues Private Support Information
0007  *
0008  * @brief POSIX Message Queue Receive Support
0009  */
0010 
0011 /*
0012  *  COPYRIGHT (c) 1989-2011.
0013  *  On-Line Applications Research Corporation (OAR).
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifdef HAVE_CONFIG_H
0038 #include "config.h"
0039 #endif
0040 
0041 #include <rtems/posix/mqueueimpl.h>
0042 #include <rtems/posix/posixapi.h>
0043 
0044 #include <fcntl.h>
0045 
0046 THREAD_QUEUE_OBJECT_ASSERT(
0047   POSIX_Message_queue_Control,
0048   Message_queue.Wait_queue,
0049   POSIX_MESSAGE_QUEUE_CONTROL
0050 );
0051 
0052 /*
0053  *  _POSIX_Message_queue_Receive_support
0054  *
0055  *  NOTE: XXX Document how size, priority, length, and the buffer go
0056  *        through the layers.
0057  */
0058 
0059 ssize_t _POSIX_Message_queue_Receive_support(
0060   mqd_t                         mqdes,
0061   char                         *msg_ptr,
0062   size_t                        msg_len,
0063   unsigned int                 *msg_prio,
0064   const struct timespec        *abstime,
0065   Thread_queue_Enqueue_callout  enqueue_callout
0066 )
0067 {
0068   POSIX_Message_queue_Control *the_mq;
0069   Thread_queue_Context         queue_context;
0070   size_t                       length_out;
0071   Thread_Control              *executing;
0072   Status_Control               status;
0073 
0074   the_mq = _POSIX_Message_queue_Get( mqdes, &queue_context );
0075 
0076   if ( the_mq == NULL ) {
0077     rtems_set_errno_and_return_minus_one( EBADF );
0078   }
0079 
0080   if ( ( the_mq->oflag & O_ACCMODE ) == O_WRONLY ) {
0081     _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context );
0082     rtems_set_errno_and_return_minus_one( EBADF );
0083   }
0084 
0085   if ( msg_len < the_mq->Message_queue.maximum_message_size ) {
0086     _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context );
0087     rtems_set_errno_and_return_minus_one( EMSGSIZE );
0088   }
0089 
0090   _Thread_queue_Context_set_enqueue_callout( &queue_context, enqueue_callout );
0091   _Thread_queue_Context_set_timeout_argument( &queue_context, abstime, true );
0092 
0093   /*
0094    *  Now if something goes wrong, we return a "length" of -1
0095    *  to indicate an error.
0096    */
0097   length_out = -1;
0098 
0099   _CORE_message_queue_Acquire_critical(
0100     &the_mq->Message_queue,
0101     &queue_context
0102   );
0103 
0104   if ( the_mq->open_count == 0 ) {
0105     _CORE_message_queue_Release( &the_mq->Message_queue, &queue_context );
0106     rtems_set_errno_and_return_minus_one( EBADF );
0107   }
0108 
0109   /*
0110    *  Now perform the actual message receive
0111    */
0112   executing = _Thread_Executing;
0113   status = _CORE_message_queue_Seize(
0114     &the_mq->Message_queue,
0115     executing,
0116     msg_ptr,
0117     &length_out,
0118     ( the_mq->oflag & O_NONBLOCK ) == 0,
0119     &queue_context
0120   );
0121 
0122   if ( status != STATUS_SUCCESSFUL ) {
0123     rtems_set_errno_and_return_minus_one( _POSIX_Get_error( status ) );
0124   }
0125 
0126   if ( msg_prio != NULL ) {
0127     *msg_prio = _POSIX_Message_queue_Priority_from_core(
0128       executing->Wait.count
0129     );
0130   }
0131 
0132   return length_out;
0133 }