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 POSIXAPI
0007  *
0008  * @brief Function closes the Message Queue 
0009  */
0010 
0011 /*
0012  *  NOTE:  The structure of the routines is identical to that of POSIX
0013  *         Message_queues to leave the option of having unnamed message
0014  *         queues at a future date.  They are currently not part of the
0015  *         POSIX standard but unnamed message_queues are.  This is also
0016  *         the reason for the apparently unnecessary tracking of
0017  *         the process_shared attribute.  [In addition to the fact that
0018  *         it would be trivial to add pshared to the mq_attr structure
0019  *         and have process private message queues.]
0020  *
0021  *         This code ignores the O_RDONLY/O_WRONLY/O_RDWR flag at open
0022  *         time.
0023  *
0024  *  COPYRIGHT (c) 1989-2008.
0025  *  On-Line Applications Research Corporation (OAR).
0026  *
0027  * Redistribution and use in source and binary forms, with or without
0028  * modification, are permitted provided that the following conditions
0029  * are met:
0030  * 1. Redistributions of source code must retain the above copyright
0031  *    notice, this list of conditions and the following disclaimer.
0032  * 2. Redistributions in binary form must reproduce the above copyright
0033  *    notice, this list of conditions and the following disclaimer in the
0034  *    documentation and/or other materials provided with the distribution.
0035  *
0036  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0037  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0038  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0039  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0040  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0041  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0042  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0043  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0044  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0045  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0046  * POSSIBILITY OF SUCH DAMAGE.
0047  */
0048 
0049 #ifdef HAVE_CONFIG_H
0050 #include "config.h"
0051 #endif
0052 
0053 #include <rtems/posix/mqueueimpl.h>
0054 
0055 /*
0056  *
0057  *  15.2.2 Close a Message Queue, P1003.1b-1993, p. 275
0058  */
0059 
0060 int mq_close(
0061   mqd_t  mqdes
0062 )
0063 {
0064   POSIX_Message_queue_Control *the_mq;
0065   Thread_queue_Context         queue_context;
0066 
0067   _Objects_Allocator_lock();
0068   the_mq = _POSIX_Message_queue_Get( mqdes, &queue_context );
0069 
0070   if ( the_mq == NULL ) {
0071     _Objects_Allocator_unlock();
0072     rtems_set_errno_and_return_minus_one( EBADF );
0073   }
0074 
0075   _CORE_message_queue_Acquire_critical(
0076     &the_mq->Message_queue,
0077     &queue_context
0078   );
0079 
0080   if ( the_mq->open_count == 0 ) {
0081     _CORE_message_queue_Release( &the_mq->Message_queue, &queue_context );
0082     _Objects_Allocator_unlock();
0083     rtems_set_errno_and_return_minus_one( EBADF );
0084   }
0085 
0086   the_mq->open_count -= 1;
0087   _POSIX_Message_queue_Delete( the_mq, &queue_context );
0088 
0089   _Objects_Allocator_unlock();
0090   return 0;
0091 }