1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Seize a Message from the Message Queue |
---|
5 | * @ingroup RTEMSScoreMessageQueue |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2007. |
---|
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/score/chain.h> |
---|
22 | #include <rtems/score/isr.h> |
---|
23 | #include <rtems/score/coremsgimpl.h> |
---|
24 | #include <rtems/score/threadimpl.h> |
---|
25 | #include <rtems/score/statesimpl.h> |
---|
26 | |
---|
27 | Status_Control _CORE_message_queue_Seize( |
---|
28 | CORE_message_queue_Control *the_message_queue, |
---|
29 | Thread_Control *executing, |
---|
30 | void *buffer, |
---|
31 | size_t *size_p, |
---|
32 | bool wait, |
---|
33 | Thread_queue_Context *queue_context |
---|
34 | ) |
---|
35 | { |
---|
36 | CORE_message_queue_Buffer_control *the_message; |
---|
37 | |
---|
38 | the_message = _CORE_message_queue_Get_pending_message( the_message_queue ); |
---|
39 | if ( the_message != NULL ) { |
---|
40 | the_message_queue->number_of_pending_messages -= 1; |
---|
41 | |
---|
42 | *size_p = the_message->Contents.size; |
---|
43 | executing->Wait.count = |
---|
44 | _CORE_message_queue_Get_message_priority( the_message ); |
---|
45 | _CORE_message_queue_Copy_buffer( |
---|
46 | the_message->Contents.buffer, |
---|
47 | buffer, |
---|
48 | *size_p |
---|
49 | ); |
---|
50 | |
---|
51 | #if !defined(RTEMS_SCORE_COREMSG_ENABLE_BLOCKING_SEND) |
---|
52 | /* |
---|
53 | * There is not an API with blocking sends enabled. |
---|
54 | * So return immediately. |
---|
55 | */ |
---|
56 | _CORE_message_queue_Free_message_buffer(the_message_queue, the_message); |
---|
57 | _CORE_message_queue_Release( the_message_queue, queue_context ); |
---|
58 | return STATUS_SUCCESSFUL; |
---|
59 | #else |
---|
60 | { |
---|
61 | Thread_Control *the_thread; |
---|
62 | |
---|
63 | /* |
---|
64 | * There could be a thread waiting to send a message. If there |
---|
65 | * is not, then we can go ahead and free the buffer. |
---|
66 | * |
---|
67 | * NOTE: If we note that the queue was not full before this receive, |
---|
68 | * then we can avoid this dequeue. |
---|
69 | */ |
---|
70 | the_thread = _Thread_queue_First_locked( |
---|
71 | &the_message_queue->Wait_queue, |
---|
72 | the_message_queue->operations |
---|
73 | ); |
---|
74 | if ( the_thread == NULL ) { |
---|
75 | _CORE_message_queue_Free_message_buffer( |
---|
76 | the_message_queue, |
---|
77 | the_message |
---|
78 | ); |
---|
79 | _CORE_message_queue_Release( the_message_queue, queue_context ); |
---|
80 | return STATUS_SUCCESSFUL; |
---|
81 | } |
---|
82 | |
---|
83 | /* |
---|
84 | * There was a thread waiting to send a message. This code |
---|
85 | * puts the messages in the message queue on behalf of the |
---|
86 | * waiting task. |
---|
87 | */ |
---|
88 | _CORE_message_queue_Insert_message( |
---|
89 | the_message_queue, |
---|
90 | the_message, |
---|
91 | the_thread->Wait.return_argument_second.immutable_object, |
---|
92 | (size_t) the_thread->Wait.option, |
---|
93 | (CORE_message_queue_Submit_types) the_thread->Wait.count |
---|
94 | ); |
---|
95 | _Thread_queue_Extract_critical( |
---|
96 | &the_message_queue->Wait_queue.Queue, |
---|
97 | the_message_queue->operations, |
---|
98 | the_thread, |
---|
99 | queue_context |
---|
100 | ); |
---|
101 | return STATUS_SUCCESSFUL; |
---|
102 | } |
---|
103 | #endif |
---|
104 | } |
---|
105 | |
---|
106 | if ( !wait ) { |
---|
107 | _CORE_message_queue_Release( the_message_queue, queue_context ); |
---|
108 | return STATUS_UNSATISFIED; |
---|
109 | } |
---|
110 | |
---|
111 | executing->Wait.return_argument_second.mutable_object = buffer; |
---|
112 | executing->Wait.return_argument = size_p; |
---|
113 | /* Wait.count will be filled in with the message priority */ |
---|
114 | |
---|
115 | _Thread_queue_Context_set_thread_state( |
---|
116 | queue_context, |
---|
117 | STATES_WAITING_FOR_MESSAGE |
---|
118 | ); |
---|
119 | _Thread_queue_Enqueue( |
---|
120 | &the_message_queue->Wait_queue.Queue, |
---|
121 | the_message_queue->operations, |
---|
122 | executing, |
---|
123 | queue_context |
---|
124 | ); |
---|
125 | return _Thread_Wait_get_status( executing ); |
---|
126 | } |
---|