1 | /* |
---|
2 | * CORE Message Queue Handler |
---|
3 | * |
---|
4 | * DESCRIPTION: |
---|
5 | * |
---|
6 | * This package is the implementation of the CORE Message Queue Handler. |
---|
7 | * This core object provides task synchronization and communication functions |
---|
8 | * via messages passed to queue objects. |
---|
9 | * |
---|
10 | * COPYRIGHT (c) 1989-1999. |
---|
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.OARcorp.com/rtems/license.html. |
---|
16 | * |
---|
17 | * $Id$ |
---|
18 | */ |
---|
19 | |
---|
20 | #include <rtems/system.h> |
---|
21 | #include <rtems/score/chain.h> |
---|
22 | #include <rtems/score/isr.h> |
---|
23 | #include <rtems/score/object.h> |
---|
24 | #include <rtems/score/coremsg.h> |
---|
25 | #include <rtems/score/states.h> |
---|
26 | #include <rtems/score/thread.h> |
---|
27 | #include <rtems/score/wkspace.h> |
---|
28 | #if defined(RTEMS_MULTIPROCESSING) |
---|
29 | #include <rtems/score/mpci.h> |
---|
30 | #endif |
---|
31 | |
---|
32 | /*PAGE |
---|
33 | * |
---|
34 | * _CORE_message_queue_Submit |
---|
35 | * |
---|
36 | * This routine implements the send and urgent message functions. It |
---|
37 | * processes a message that is to be submitted to the designated |
---|
38 | * message queue. The message will either be processed as a |
---|
39 | * send message which it will be inserted at the rear of the queue |
---|
40 | * or it will be processed as an urgent message which will be inserted |
---|
41 | * at the front of the queue. |
---|
42 | * |
---|
43 | * Input parameters: |
---|
44 | * the_message_queue - message is submitted to this message queue |
---|
45 | * buffer - pointer to message buffer |
---|
46 | * size - size in bytes of message to send |
---|
47 | * id - id of message queue |
---|
48 | * api_message_queue_mp_support - api specific mp support callout |
---|
49 | * submit_type - send or urgent message |
---|
50 | * |
---|
51 | * Output parameters: |
---|
52 | * CORE_MESSAGE_QUEUE_SUCCESSFUL - if successful |
---|
53 | * error code - if unsuccessful |
---|
54 | */ |
---|
55 | |
---|
56 | CORE_message_queue_Status _CORE_message_queue_Submit( |
---|
57 | CORE_message_queue_Control *the_message_queue, |
---|
58 | void *buffer, |
---|
59 | unsigned32 size, |
---|
60 | Objects_Id id, |
---|
61 | CORE_message_queue_API_mp_support_callout api_message_queue_mp_support, |
---|
62 | CORE_message_queue_Submit_types submit_type |
---|
63 | ) |
---|
64 | { |
---|
65 | CORE_message_queue_Buffer_control *the_message; |
---|
66 | Thread_Control *the_thread; |
---|
67 | |
---|
68 | if ( size > the_message_queue->maximum_message_size ) |
---|
69 | return CORE_MESSAGE_QUEUE_STATUS_INVALID_SIZE; |
---|
70 | |
---|
71 | /* |
---|
72 | * Is there a thread currently waiting on this message queue? |
---|
73 | */ |
---|
74 | |
---|
75 | the_thread = _Thread_queue_Dequeue( &the_message_queue->Wait_queue ); |
---|
76 | if ( the_thread ) { |
---|
77 | _CORE_message_queue_Copy_buffer( |
---|
78 | buffer, |
---|
79 | the_thread->Wait.return_argument, |
---|
80 | size |
---|
81 | ); |
---|
82 | *(unsigned32 *)the_thread->Wait.return_argument_1 = size; |
---|
83 | the_thread->Wait.count = submit_type; |
---|
84 | |
---|
85 | #if defined(RTEMS_MULTIPROCESSING) |
---|
86 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) |
---|
87 | (*api_message_queue_mp_support) ( the_thread, id ); |
---|
88 | #endif |
---|
89 | |
---|
90 | return CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL; |
---|
91 | } |
---|
92 | |
---|
93 | /* |
---|
94 | * No one waiting on this one currently. |
---|
95 | * Allocate a message buffer and store it away |
---|
96 | */ |
---|
97 | |
---|
98 | if ( the_message_queue->number_of_pending_messages == |
---|
99 | the_message_queue->maximum_pending_messages ) { |
---|
100 | return CORE_MESSAGE_QUEUE_STATUS_TOO_MANY; |
---|
101 | } |
---|
102 | |
---|
103 | the_message = _CORE_message_queue_Allocate_message_buffer(the_message_queue); |
---|
104 | if ( the_message == 0 ) |
---|
105 | return CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED; |
---|
106 | |
---|
107 | _CORE_message_queue_Copy_buffer( buffer, the_message->Contents.buffer, size ); |
---|
108 | the_message->Contents.size = size; |
---|
109 | the_message->priority = submit_type; |
---|
110 | |
---|
111 | the_message_queue->number_of_pending_messages += 1; |
---|
112 | |
---|
113 | switch ( submit_type ) { |
---|
114 | case CORE_MESSAGE_QUEUE_SEND_REQUEST: |
---|
115 | _CORE_message_queue_Append( the_message_queue, the_message ); |
---|
116 | break; |
---|
117 | case CORE_MESSAGE_QUEUE_URGENT_REQUEST: |
---|
118 | _CORE_message_queue_Prepend( the_message_queue, the_message ); |
---|
119 | break; |
---|
120 | default: |
---|
121 | /* XXX interrupt critical section needs to be addressed */ |
---|
122 | { |
---|
123 | CORE_message_queue_Buffer_control *this_message; |
---|
124 | Chain_Node *the_node; |
---|
125 | |
---|
126 | the_message->priority = submit_type; |
---|
127 | for ( the_node = the_message_queue->Pending_messages.first ; |
---|
128 | !_Chain_Is_tail( &the_message_queue->Pending_messages, the_node ) ; |
---|
129 | the_node = the_node->next ) { |
---|
130 | |
---|
131 | this_message = (CORE_message_queue_Buffer_control *) the_node; |
---|
132 | |
---|
133 | if ( this_message->priority >= the_message->priority ) |
---|
134 | continue; |
---|
135 | |
---|
136 | _Chain_Insert( the_node, &the_message->Node ); |
---|
137 | break; |
---|
138 | } |
---|
139 | } |
---|
140 | break; |
---|
141 | } |
---|
142 | |
---|
143 | /* |
---|
144 | * According to POSIX, does this happen before or after the message |
---|
145 | * is actually enqueued. It is logical to think afterwards, because |
---|
146 | * the message is actually in the queue at this point. |
---|
147 | */ |
---|
148 | |
---|
149 | if ( the_message_queue->number_of_pending_messages == 1 && |
---|
150 | the_message_queue->notify_handler ) |
---|
151 | (*the_message_queue->notify_handler)( the_message_queue->notify_argument ); |
---|
152 | |
---|
153 | return CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL; |
---|
154 | } |
---|