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 | |
---|
23 | #include <fcntl.h> |
---|
24 | |
---|
25 | THREAD_QUEUE_OBJECT_ASSERT( |
---|
26 | POSIX_Message_queue_Control, |
---|
27 | Message_queue.Wait_queue |
---|
28 | ); |
---|
29 | |
---|
30 | /* |
---|
31 | * _POSIX_Message_queue_Receive_support |
---|
32 | * |
---|
33 | * NOTE: XXX Document how size, priority, length, and the buffer go |
---|
34 | * through the layers. |
---|
35 | */ |
---|
36 | |
---|
37 | ssize_t _POSIX_Message_queue_Receive_support( |
---|
38 | mqd_t mqdes, |
---|
39 | char *msg_ptr, |
---|
40 | size_t msg_len, |
---|
41 | unsigned int *msg_prio, |
---|
42 | bool wait, |
---|
43 | Watchdog_Interval timeout |
---|
44 | ) |
---|
45 | { |
---|
46 | POSIX_Message_queue_Control *the_mq; |
---|
47 | Thread_queue_Context queue_context; |
---|
48 | size_t length_out; |
---|
49 | bool do_wait; |
---|
50 | Thread_Control *executing; |
---|
51 | |
---|
52 | the_mq = _POSIX_Message_queue_Get( mqdes, &queue_context ); |
---|
53 | |
---|
54 | if ( the_mq == NULL ) { |
---|
55 | rtems_set_errno_and_return_minus_one( EBADF ); |
---|
56 | } |
---|
57 | |
---|
58 | if ( ( the_mq->oflag & O_ACCMODE ) == O_WRONLY ) { |
---|
59 | _ISR_lock_ISR_enable( &queue_context.Lock_context ); |
---|
60 | rtems_set_errno_and_return_minus_one( EBADF ); |
---|
61 | } |
---|
62 | |
---|
63 | if ( msg_len < the_mq->Message_queue.maximum_message_size ) { |
---|
64 | _ISR_lock_ISR_enable( &queue_context.Lock_context ); |
---|
65 | rtems_set_errno_and_return_minus_one( EMSGSIZE ); |
---|
66 | } |
---|
67 | |
---|
68 | /* |
---|
69 | * Now if something goes wrong, we return a "length" of -1 |
---|
70 | * to indicate an error. |
---|
71 | */ |
---|
72 | |
---|
73 | length_out = -1; |
---|
74 | |
---|
75 | /* |
---|
76 | * A timed receive with a bad time will do a poll regardless. |
---|
77 | */ |
---|
78 | if ( wait ) { |
---|
79 | do_wait = ( the_mq->oflag & O_NONBLOCK ) == 0; |
---|
80 | } else { |
---|
81 | do_wait = wait; |
---|
82 | } |
---|
83 | |
---|
84 | _CORE_message_queue_Acquire_critical( |
---|
85 | &the_mq->Message_queue, |
---|
86 | &queue_context |
---|
87 | ); |
---|
88 | |
---|
89 | if ( the_mq->open_count == 0 ) { |
---|
90 | _CORE_message_queue_Release( &the_mq->Message_queue, &queue_context ); |
---|
91 | rtems_set_errno_and_return_minus_one( EBADF ); |
---|
92 | } |
---|
93 | |
---|
94 | /* |
---|
95 | * Now perform the actual message receive |
---|
96 | */ |
---|
97 | executing = _Thread_Executing; |
---|
98 | _CORE_message_queue_Seize( |
---|
99 | &the_mq->Message_queue, |
---|
100 | executing, |
---|
101 | msg_ptr, |
---|
102 | &length_out, |
---|
103 | do_wait, |
---|
104 | timeout, |
---|
105 | &queue_context |
---|
106 | ); |
---|
107 | |
---|
108 | if ( msg_prio != NULL ) { |
---|
109 | *msg_prio = _POSIX_Message_queue_Priority_from_core( |
---|
110 | executing->Wait.count |
---|
111 | ); |
---|
112 | } |
---|
113 | |
---|
114 | if ( executing->Wait.return_code != CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL ) { |
---|
115 | rtems_set_errno_and_return_minus_one( |
---|
116 | _POSIX_Message_queue_Translate_core_message_queue_return_code( |
---|
117 | executing->Wait.return_code |
---|
118 | ) |
---|
119 | ); |
---|
120 | } |
---|
121 | |
---|
122 | return length_out; |
---|
123 | } |
---|