1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief CORE Message Queue Submit |
---|
5 | * |
---|
6 | * @ingroup ScoreMessageQueue |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2009. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.com/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #if HAVE_CONFIG_H |
---|
19 | #include "config.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <rtems/system.h> |
---|
23 | #include <rtems/score/chain.h> |
---|
24 | #include <rtems/score/isr.h> |
---|
25 | #include <rtems/score/object.h> |
---|
26 | #include <rtems/score/coremsg.h> |
---|
27 | #include <rtems/score/states.h> |
---|
28 | #include <rtems/score/thread.h> |
---|
29 | #include <rtems/score/wkspace.h> |
---|
30 | |
---|
31 | CORE_message_queue_Status _CORE_message_queue_Submit( |
---|
32 | CORE_message_queue_Control *the_message_queue, |
---|
33 | const void *buffer, |
---|
34 | size_t size, |
---|
35 | Objects_Id id, |
---|
36 | #if defined(RTEMS_MULTIPROCESSING) |
---|
37 | CORE_message_queue_API_mp_support_callout api_message_queue_mp_support, |
---|
38 | #else |
---|
39 | CORE_message_queue_API_mp_support_callout api_message_queue_mp_support __attribute__((unused)), |
---|
40 | #endif |
---|
41 | CORE_message_queue_Submit_types submit_type, |
---|
42 | bool wait, |
---|
43 | Watchdog_Interval timeout |
---|
44 | ) |
---|
45 | { |
---|
46 | CORE_message_queue_Buffer_control *the_message; |
---|
47 | Thread_Control *the_thread; |
---|
48 | |
---|
49 | if ( size > the_message_queue->maximum_message_size ) { |
---|
50 | return CORE_MESSAGE_QUEUE_STATUS_INVALID_SIZE; |
---|
51 | } |
---|
52 | |
---|
53 | /* |
---|
54 | * Is there a thread currently waiting on this message queue? |
---|
55 | */ |
---|
56 | if ( the_message_queue->number_of_pending_messages == 0 ) { |
---|
57 | the_thread = _Thread_queue_Dequeue( &the_message_queue->Wait_queue ); |
---|
58 | if ( the_thread ) { |
---|
59 | _CORE_message_queue_Copy_buffer( |
---|
60 | buffer, |
---|
61 | the_thread->Wait.return_argument_second.mutable_object, |
---|
62 | size |
---|
63 | ); |
---|
64 | *(size_t *) the_thread->Wait.return_argument = size; |
---|
65 | the_thread->Wait.count = (uint32_t) submit_type; |
---|
66 | |
---|
67 | #if defined(RTEMS_MULTIPROCESSING) |
---|
68 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) |
---|
69 | (*api_message_queue_mp_support) ( the_thread, id ); |
---|
70 | #endif |
---|
71 | return CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | /* |
---|
76 | * No one waiting on the message queue at this time, so attempt to |
---|
77 | * queue the message up for a future receive. |
---|
78 | */ |
---|
79 | the_message = |
---|
80 | _CORE_message_queue_Allocate_message_buffer( the_message_queue ); |
---|
81 | if ( the_message ) { |
---|
82 | _CORE_message_queue_Copy_buffer( |
---|
83 | buffer, |
---|
84 | the_message->Contents.buffer, |
---|
85 | size |
---|
86 | ); |
---|
87 | the_message->Contents.size = size; |
---|
88 | _CORE_message_queue_Set_message_priority( the_message, submit_type ); |
---|
89 | |
---|
90 | _CORE_message_queue_Insert_message( |
---|
91 | the_message_queue, |
---|
92 | the_message, |
---|
93 | submit_type |
---|
94 | ); |
---|
95 | return CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL; |
---|
96 | } |
---|
97 | |
---|
98 | #if !defined(RTEMS_SCORE_COREMSG_ENABLE_BLOCKING_SEND) |
---|
99 | return CORE_MESSAGE_QUEUE_STATUS_TOO_MANY; |
---|
100 | #else |
---|
101 | /* |
---|
102 | * No message buffers were available so we may need to return an |
---|
103 | * overflow error or block the sender until the message is placed |
---|
104 | * on the queue. |
---|
105 | */ |
---|
106 | if ( !wait ) { |
---|
107 | return CORE_MESSAGE_QUEUE_STATUS_TOO_MANY; |
---|
108 | } |
---|
109 | |
---|
110 | /* |
---|
111 | * Do NOT block on a send if the caller is in an ISR. It is |
---|
112 | * deadly to block in an ISR. |
---|
113 | */ |
---|
114 | if ( _ISR_Is_in_progress() ) { |
---|
115 | return CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED; |
---|
116 | } |
---|
117 | |
---|
118 | /* |
---|
119 | * WARNING!! executing should NOT be used prior to this point. |
---|
120 | * Thus the unusual choice to open a new scope and declare |
---|
121 | * it as a variable. Doing this emphasizes how dangerous it |
---|
122 | * would be to use this variable prior to here. |
---|
123 | */ |
---|
124 | { |
---|
125 | Thread_Control *executing = _Thread_Executing; |
---|
126 | ISR_Level level; |
---|
127 | |
---|
128 | _ISR_Disable( level ); |
---|
129 | _Thread_queue_Enter_critical_section( &the_message_queue->Wait_queue ); |
---|
130 | executing->Wait.queue = &the_message_queue->Wait_queue; |
---|
131 | executing->Wait.id = id; |
---|
132 | executing->Wait.return_argument_second.immutable_object = buffer; |
---|
133 | executing->Wait.option = (uint32_t) size; |
---|
134 | executing->Wait.count = submit_type; |
---|
135 | _ISR_Enable( level ); |
---|
136 | |
---|
137 | _Thread_queue_Enqueue( &the_message_queue->Wait_queue, timeout ); |
---|
138 | } |
---|
139 | |
---|
140 | return CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED_WAIT; |
---|
141 | #endif |
---|
142 | } |
---|