1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Function closes the Message Queue |
---|
5 | * @ingroup POSIXAPI |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * NOTE: The structure of the routines is identical to that of POSIX |
---|
10 | * Message_queues to leave the option of having unnamed message |
---|
11 | * queues at a future date. They are currently not part of the |
---|
12 | * POSIX standard but unnamed message_queues are. This is also |
---|
13 | * the reason for the apparently unnecessary tracking of |
---|
14 | * the process_shared attribute. [In addition to the fact that |
---|
15 | * it would be trivial to add pshared to the mq_attr structure |
---|
16 | * and have process private message queues.] |
---|
17 | * |
---|
18 | * This code ignores the O_RDONLY/O_WRONLY/O_RDWR flag at open |
---|
19 | * time. |
---|
20 | * |
---|
21 | * COPYRIGHT (c) 1989-2008. |
---|
22 | * On-Line Applications Research Corporation (OAR). |
---|
23 | * |
---|
24 | * The license and distribution terms for this file may be |
---|
25 | * found in the file LICENSE in this distribution or at |
---|
26 | * http://www.rtems.org/license/LICENSE. |
---|
27 | */ |
---|
28 | |
---|
29 | #if HAVE_CONFIG_H |
---|
30 | #include "config.h" |
---|
31 | #endif |
---|
32 | |
---|
33 | #include <rtems/posix/mqueueimpl.h> |
---|
34 | |
---|
35 | /* |
---|
36 | * |
---|
37 | * 15.2.2 Close a Message Queue, P1003.1b-1993, p. 275 |
---|
38 | */ |
---|
39 | |
---|
40 | int mq_close( |
---|
41 | mqd_t mqdes |
---|
42 | ) |
---|
43 | { |
---|
44 | POSIX_Message_queue_Control *the_mq; |
---|
45 | Thread_queue_Context queue_context; |
---|
46 | |
---|
47 | _Objects_Allocator_lock(); |
---|
48 | the_mq = _POSIX_Message_queue_Get( mqdes, &queue_context ); |
---|
49 | |
---|
50 | if ( the_mq == NULL ) { |
---|
51 | _Objects_Allocator_unlock(); |
---|
52 | rtems_set_errno_and_return_minus_one( EBADF ); |
---|
53 | } |
---|
54 | |
---|
55 | _CORE_message_queue_Acquire_critical( |
---|
56 | &the_mq->Message_queue, |
---|
57 | &queue_context |
---|
58 | ); |
---|
59 | |
---|
60 | if ( the_mq->open_count == 0 ) { |
---|
61 | _CORE_message_queue_Release( &the_mq->Message_queue, &queue_context ); |
---|
62 | _Objects_Allocator_unlock(); |
---|
63 | rtems_set_errno_and_return_minus_one( EBADF ); |
---|
64 | } |
---|
65 | |
---|
66 | the_mq->open_count -= 1; |
---|
67 | _POSIX_Message_queue_Delete( the_mq, &queue_context ); |
---|
68 | |
---|
69 | _Objects_Allocator_unlock(); |
---|
70 | return 0; |
---|
71 | } |
---|