summaryrefslogtreecommitdiff
path: root/utility/rtos_compatibility_layers/posix/px_mq_close.c
blob: 69cfc2b9f12d23ce612344a827e0245bdd9ec676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** POSIX wrapper for THREADX                                             */
/**                                                                       */
/**                                                                       */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

/* Include necessary system files.  */

#include "tx_api.h"    /* Threadx API */
#include "pthread.h"  /* Posix API */
#include "px_int.h"    /* Posix helper functions */


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    mq_close                                            PORTABLE C      */
/*                                                           6.1.7        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    close a message queue                                               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    mqdes                         message queue descriptor              */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    OK                            If successful                         */
/*    ERROR                         If error occurs                       */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    posix_internal_error          Generic error handler                 */
/*    tx_byte_release               Release bytes                         */
/*    tx_thread_identify            Returns currently running thread      */
/*    posix_queue_delete            Deletes specific queue                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
INT  mq_close(mqd_t mqdes)
{

TX_INTERRUPT_SAVE_AREA

TX_QUEUE          * Queue;
POSIX_MSG_QUEUE   * q_ptr;

    /* Assign a temporary variable for clarity.  */
    Queue = &(mqdes->f_data->queue);
    q_ptr = (POSIX_MSG_QUEUE * )Queue;

    /* First, check for an invalid queue pointer.  */
    if ( (!q_ptr) || ( (q_ptr -> px_queue_id) != PX_QUEUE_ID))
    {
        /* Queue pointer is invalid, return appropriate error code.  */
        posix_errno = EBADF;
	    posix_set_pthread_errno(EBADF);

        /* Return ERROR.  */
        return(ERROR);
    }

    /* If we are not calling this routine from a thread context.  */
    if (!(tx_thread_identify()))
    {
        /* return appropriate error code.  */
        posix_errno = EBADF;
	    posix_set_pthread_errno(EBADF);

        /* Return ERROR.  */
        return(ERROR);
    }

    /* Free the system resource allocated by open call.  */
    if( tx_byte_release( ( VOID *) mqdes ))
    {
        /* return generic error.  */
        posix_internal_error(100);

        /* Return error.  */
        return(ERROR);
    }

    /* Disable interrupts.  */
    TX_DISABLE

    /* Decrement ref count.  */
    if( q_ptr->open_count )

        q_ptr ->open_count--;

    /* Destroy the basic Queue is the ref count is zero and
        it is marked by unlink.  */
    if( (! q_ptr ->open_count ) && (q_ptr->unlink_flag == TX_TRUE))
    {
        /* Free the system resource allocated by open call.  */
        if( posix_queue_delete( q_ptr ))
        {

            /* Restore interrupts.  */
            TX_RESTORE

            /* return generic type.  */
            posix_internal_error(100);

            /* Return error.  */
            return(ERROR);
        }
    }
    /* Restore interrupts.  */
    TX_RESTORE

    /* Return OK.  */
    return(OK);
}