1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief POSIX Message Queue Receive Support |
---|
5 | * @ingroup POSIX_MQUEUE_P Message Queues Private Support Information |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2011. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/posix/mqueueimpl.h> |
---|
22 | #include <rtems/posix/posixapi.h> |
---|
23 | |
---|
24 | #include <fcntl.h> |
---|
25 | |
---|
26 | THREAD_QUEUE_OBJECT_ASSERT( |
---|
27 | POSIX_Message_queue_Control, |
---|
28 | Message_queue.Wait_queue, |
---|
29 | POSIX_MESSAGE_QUEUE_CONTROL |
---|
30 | ); |
---|
31 | |
---|
32 | /* |
---|
33 | * _POSIX_Message_queue_Receive_support |
---|
34 | * |
---|
35 | * NOTE: XXX Document how size, priority, length, and the buffer go |
---|
36 | * through the layers. |
---|
37 | */ |
---|
38 | |
---|
39 | ssize_t _POSIX_Message_queue_Receive_support( |
---|
40 | mqd_t mqdes, |
---|
41 | char *msg_ptr, |
---|
42 | size_t msg_len, |
---|
43 | unsigned int *msg_prio, |
---|
44 | const struct timespec *abstime, |
---|
45 | Thread_queue_Enqueue_callout enqueue_callout |
---|
46 | ) |
---|
47 | { |
---|
48 | POSIX_Message_queue_Control *the_mq; |
---|
49 | Thread_queue_Context queue_context; |
---|
50 | size_t length_out; |
---|
51 | Thread_Control *executing; |
---|
52 | Status_Control status; |
---|
53 | |
---|
54 | the_mq = _POSIX_Message_queue_Get( mqdes, &queue_context ); |
---|
55 | |
---|
56 | if ( the_mq == NULL ) { |
---|
57 | rtems_set_errno_and_return_minus_one( EBADF ); |
---|
58 | } |
---|
59 | |
---|
60 | if ( ( the_mq->oflag & O_ACCMODE ) == O_WRONLY ) { |
---|
61 | _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context ); |
---|
62 | rtems_set_errno_and_return_minus_one( EBADF ); |
---|
63 | } |
---|
64 | |
---|
65 | if ( msg_len < the_mq->Message_queue.maximum_message_size ) { |
---|
66 | _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context ); |
---|
67 | rtems_set_errno_and_return_minus_one( EMSGSIZE ); |
---|
68 | } |
---|
69 | |
---|
70 | _Thread_queue_Context_set_enqueue_callout( &queue_context, enqueue_callout ); |
---|
71 | _Thread_queue_Context_set_timeout_argument( &queue_context, abstime ); |
---|
72 | |
---|
73 | /* |
---|
74 | * Now if something goes wrong, we return a "length" of -1 |
---|
75 | * to indicate an error. |
---|
76 | */ |
---|
77 | length_out = -1; |
---|
78 | |
---|
79 | _CORE_message_queue_Acquire_critical( |
---|
80 | &the_mq->Message_queue, |
---|
81 | &queue_context |
---|
82 | ); |
---|
83 | |
---|
84 | if ( the_mq->open_count == 0 ) { |
---|
85 | _CORE_message_queue_Release( &the_mq->Message_queue, &queue_context ); |
---|
86 | rtems_set_errno_and_return_minus_one( EBADF ); |
---|
87 | } |
---|
88 | |
---|
89 | /* |
---|
90 | * Now perform the actual message receive |
---|
91 | */ |
---|
92 | executing = _Thread_Executing; |
---|
93 | status = _CORE_message_queue_Seize( |
---|
94 | &the_mq->Message_queue, |
---|
95 | executing, |
---|
96 | msg_ptr, |
---|
97 | &length_out, |
---|
98 | ( the_mq->oflag & O_NONBLOCK ) == 0, |
---|
99 | &queue_context |
---|
100 | ); |
---|
101 | |
---|
102 | if ( status != STATUS_SUCCESSFUL ) { |
---|
103 | rtems_set_errno_and_return_minus_one( _POSIX_Get_error( status ) ); |
---|
104 | } |
---|
105 | |
---|
106 | if ( msg_prio != NULL ) { |
---|
107 | *msg_prio = _POSIX_Message_queue_Priority_from_core( |
---|
108 | executing->Wait.count |
---|
109 | ); |
---|
110 | } |
---|
111 | |
---|
112 | return length_out; |
---|
113 | } |
---|