1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** POSIX wrapper for THREADX                                             */
17 /**                                                                       */
18 /**                                                                       */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 /* Include necessary system files.  */
24 
25 #include "tx_api.h"    /* Threadx API */
26 #include "pthread.h"  /* Posix API */
27 #include "px_int.h"    /* Posix helper functions */
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    mq_close                                            PORTABLE C      */
35 /*                                                           6.1.7        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    close a message queue                                               */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    mqdes                         message queue descriptor              */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    OK                            If successful                         */
51 /*    ERROR                         If error occurs                       */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    posix_internal_error          Generic error handler                 */
56 /*    tx_byte_release               Release bytes                         */
57 /*    tx_thread_identify            Returns currently running thread      */
58 /*    posix_queue_delete            Deletes specific queue                */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Application Code                                                    */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
69 /*                                                                        */
70 /**************************************************************************/
mq_close(mqd_t mqdes)71 INT  mq_close(mqd_t mqdes)
72 {
73 
74 TX_INTERRUPT_SAVE_AREA
75 
76 TX_QUEUE          * Queue;
77 POSIX_MSG_QUEUE   * q_ptr;
78 
79     /* Assign a temporary variable for clarity.  */
80     Queue = &(mqdes->f_data->queue);
81     q_ptr = (POSIX_MSG_QUEUE * )Queue;
82 
83     /* First, check for an invalid queue pointer.  */
84     if ( (!q_ptr) || ( (q_ptr -> px_queue_id) != PX_QUEUE_ID))
85     {
86         /* Queue pointer is invalid, return appropriate error code.  */
87         posix_errno = EBADF;
88 	    posix_set_pthread_errno(EBADF);
89 
90         /* Return ERROR.  */
91         return(ERROR);
92     }
93 
94     /* If we are not calling this routine from a thread context.  */
95     if (!(tx_thread_identify()))
96     {
97         /* return appropriate error code.  */
98         posix_errno = EBADF;
99 	    posix_set_pthread_errno(EBADF);
100 
101         /* Return ERROR.  */
102         return(ERROR);
103     }
104 
105     /* Free the system resource allocated by open call.  */
106     if( tx_byte_release( ( VOID *) mqdes ))
107     {
108         /* return generic error.  */
109         posix_internal_error(100);
110 
111         /* Return error.  */
112         return(ERROR);
113     }
114 
115     /* Disable interrupts.  */
116     TX_DISABLE
117 
118     /* Decrement ref count.  */
119     if( q_ptr->open_count )
120 
121         q_ptr ->open_count--;
122 
123     /* Destroy the basic Queue is the ref count is zero and
124         it is marked by unlink.  */
125     if( (! q_ptr ->open_count ) && (q_ptr->unlink_flag == TX_TRUE))
126     {
127         /* Free the system resource allocated by open call.  */
128         if( posix_queue_delete( q_ptr ))
129         {
130 
131             /* Restore interrupts.  */
132             TX_RESTORE
133 
134             /* return generic type.  */
135             posix_internal_error(100);
136 
137             /* Return error.  */
138             return(ERROR);
139         }
140     }
141     /* Restore interrupts.  */
142     TX_RESTORE
143 
144     /* Return OK.  */
145     return(OK);
146 }
147