1 | /* |
---|
2 | * Message Queue Manager |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * All rights assigned to U.S. Government, 1994. |
---|
8 | * |
---|
9 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
10 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
11 | * notice must appear in all copies of this file and its derivatives. |
---|
12 | * |
---|
13 | * $Id$ |
---|
14 | */ |
---|
15 | |
---|
16 | #include <rtems/system.h> |
---|
17 | #include <rtems/attr.h> |
---|
18 | #include <rtems/chain.h> |
---|
19 | #include <rtems/config.h> |
---|
20 | #include <rtems/isr.h> |
---|
21 | #include <rtems/message.h> |
---|
22 | #include <rtems/object.h> |
---|
23 | #include <rtems/options.h> |
---|
24 | #include <rtems/states.h> |
---|
25 | #include <rtems/thread.h> |
---|
26 | #include <rtems/wkspace.h> |
---|
27 | #include <rtems/mpci.h> |
---|
28 | |
---|
29 | /*PAGE |
---|
30 | * |
---|
31 | * _Message_queue_Manager_initialization |
---|
32 | * |
---|
33 | * This routine initializes all message queue manager related |
---|
34 | * data structures. |
---|
35 | * |
---|
36 | * Input parameters: |
---|
37 | * maximum_message_queues - number of message queues to initialize |
---|
38 | * |
---|
39 | * Output parameters: NONE |
---|
40 | */ |
---|
41 | |
---|
42 | void _Message_queue_Manager_initialization( |
---|
43 | unsigned32 maximum_message_queues |
---|
44 | ) |
---|
45 | { |
---|
46 | _Objects_Initialize_information( |
---|
47 | &_Message_queue_Information, |
---|
48 | OBJECTS_RTEMS_MESSAGE_QUEUES, |
---|
49 | TRUE, |
---|
50 | maximum_message_queues, |
---|
51 | sizeof( Message_queue_Control ) |
---|
52 | ); |
---|
53 | } |
---|
54 | |
---|
55 | /*PAGE |
---|
56 | * |
---|
57 | * _Message_queue_Allocate |
---|
58 | * |
---|
59 | * Allocate a message queue and the space for its messages |
---|
60 | */ |
---|
61 | |
---|
62 | Message_queue_Control *_Message_queue_Allocate ( |
---|
63 | unsigned32 count, |
---|
64 | unsigned32 max_message_size |
---|
65 | ) |
---|
66 | { |
---|
67 | Message_queue_Control *mq = 0; |
---|
68 | unsigned32 message_buffering_required; |
---|
69 | unsigned32 allocated_message_size; |
---|
70 | |
---|
71 | mq = \ |
---|
72 | (Message_queue_Control *)_Objects_Allocate(&_Message_queue_Information); |
---|
73 | |
---|
74 | if (mq == 0) |
---|
75 | goto failed; |
---|
76 | |
---|
77 | mq->maximum_message_size = max_message_size; |
---|
78 | |
---|
79 | /* |
---|
80 | * round size up to multiple of a ptr for chain init |
---|
81 | */ |
---|
82 | |
---|
83 | allocated_message_size = max_message_size; |
---|
84 | if (allocated_message_size & (sizeof(unsigned32) - 1)) { |
---|
85 | allocated_message_size += sizeof(unsigned32); |
---|
86 | allocated_message_size &= ~(sizeof(unsigned32) - 1); |
---|
87 | } |
---|
88 | |
---|
89 | message_buffering_required = |
---|
90 | count * (allocated_message_size + sizeof(Message_queue_Buffer_control)); |
---|
91 | |
---|
92 | mq->message_buffers = |
---|
93 | (Message_queue_Buffer *) _Workspace_Allocate(message_buffering_required); |
---|
94 | |
---|
95 | if (mq->message_buffers == 0) |
---|
96 | goto failed; |
---|
97 | |
---|
98 | _Chain_Initialize |
---|
99 | (&mq->Inactive_messages, |
---|
100 | mq->message_buffers, |
---|
101 | count, |
---|
102 | allocated_message_size + sizeof(Message_queue_Buffer_control) |
---|
103 | ); |
---|
104 | return mq; |
---|
105 | |
---|
106 | failed: |
---|
107 | if (mq) |
---|
108 | _Message_queue_Free(mq); |
---|
109 | return (Message_queue_Control *) 0; |
---|
110 | } |
---|
111 | |
---|
112 | /*PAGE |
---|
113 | * |
---|
114 | * rtems_message_queue_create |
---|
115 | * |
---|
116 | * This directive creates a message queue by allocating and initializing |
---|
117 | * a message queue data structure. |
---|
118 | * |
---|
119 | * Input parameters: |
---|
120 | * name - user defined queue name |
---|
121 | * count - maximum message and reserved buffer count |
---|
122 | * max_message_size - maximum size of each message |
---|
123 | * attribute_set - process method |
---|
124 | * id - pointer to queue |
---|
125 | * |
---|
126 | * Output parameters: |
---|
127 | * id - queue id |
---|
128 | * RTEMS_SUCCESSFUL - if successful |
---|
129 | * error code - if unsuccessful |
---|
130 | */ |
---|
131 | |
---|
132 | rtems_status_code rtems_message_queue_create( |
---|
133 | Objects_Name name, |
---|
134 | unsigned32 count, |
---|
135 | unsigned32 max_message_size, |
---|
136 | rtems_attribute attribute_set, |
---|
137 | Objects_Id *id |
---|
138 | ) |
---|
139 | { |
---|
140 | register Message_queue_Control *the_message_queue; |
---|
141 | |
---|
142 | if ( !_Objects_Is_name_valid( name ) ) |
---|
143 | return ( RTEMS_INVALID_NAME ); |
---|
144 | |
---|
145 | if ( _Attributes_Is_global( attribute_set ) && |
---|
146 | !_Configuration_Is_multiprocessing() ) |
---|
147 | return( RTEMS_MP_NOT_CONFIGURED ); |
---|
148 | |
---|
149 | if (count == 0) |
---|
150 | return RTEMS_INVALID_NUMBER; |
---|
151 | |
---|
152 | if (max_message_size == 0) |
---|
153 | return RTEMS_INVALID_SIZE; |
---|
154 | |
---|
155 | #if 1 |
---|
156 | /* |
---|
157 | * I am not 100% sure this should be an error. |
---|
158 | * It seems reasonable to create a que with a large max size, |
---|
159 | * and then just send smaller msgs from remote (or all) nodes. |
---|
160 | */ |
---|
161 | |
---|
162 | if ( _Attributes_Is_global( attribute_set ) && |
---|
163 | _Configuration_MPCI_table && |
---|
164 | (_Configuration_MPCI_table->maximum_packet_size < max_message_size)) |
---|
165 | { |
---|
166 | return RTEMS_INVALID_SIZE; |
---|
167 | } |
---|
168 | #endif |
---|
169 | |
---|
170 | _Thread_Disable_dispatch(); /* protects object pointer */ |
---|
171 | |
---|
172 | the_message_queue = _Message_queue_Allocate(count, max_message_size); |
---|
173 | |
---|
174 | if ( !the_message_queue ) { |
---|
175 | _Thread_Enable_dispatch(); |
---|
176 | return( RTEMS_TOO_MANY ); |
---|
177 | } |
---|
178 | |
---|
179 | if ( _Attributes_Is_global( attribute_set ) && |
---|
180 | !( _Objects_MP_Open( &_Message_queue_Information, name, |
---|
181 | the_message_queue->Object.id, FALSE ) ) ) { |
---|
182 | _Message_queue_Free( the_message_queue ); |
---|
183 | _Thread_Enable_dispatch(); |
---|
184 | return RTEMS_TOO_MANY; |
---|
185 | } |
---|
186 | |
---|
187 | the_message_queue->maximum_pending_messages = count; |
---|
188 | |
---|
189 | the_message_queue->attribute_set = attribute_set; |
---|
190 | the_message_queue->number_of_pending_messages = 0; |
---|
191 | |
---|
192 | _Chain_Initialize_empty( &the_message_queue->Pending_messages ); |
---|
193 | |
---|
194 | _Thread_queue_Initialize( &the_message_queue->Wait_queue, attribute_set, |
---|
195 | STATES_WAITING_FOR_MESSAGE ); |
---|
196 | |
---|
197 | _Objects_Open( &_Message_queue_Information, |
---|
198 | &the_message_queue->Object, name ); |
---|
199 | |
---|
200 | *id = the_message_queue->Object.id; |
---|
201 | |
---|
202 | if ( _Attributes_Is_global( attribute_set ) ) |
---|
203 | _Message_queue_MP_Send_process_packet( |
---|
204 | MESSAGE_QUEUE_MP_ANNOUNCE_CREATE, |
---|
205 | the_message_queue->Object.id, |
---|
206 | name, |
---|
207 | 0 |
---|
208 | ); |
---|
209 | |
---|
210 | _Thread_Enable_dispatch(); |
---|
211 | return( RTEMS_SUCCESSFUL ); |
---|
212 | } |
---|
213 | |
---|
214 | /*PAGE |
---|
215 | * |
---|
216 | * rtems_message_queue_ident |
---|
217 | * |
---|
218 | * This directive returns the system ID associated with |
---|
219 | * the message queue name. |
---|
220 | * |
---|
221 | * Input parameters: |
---|
222 | * name - user defined message queue name |
---|
223 | * node - node(s) to be searched |
---|
224 | * id - pointer to message queue id |
---|
225 | * |
---|
226 | * Output parameters: |
---|
227 | * *id - message queue id |
---|
228 | * RTEMS_SUCCESSFUL - if successful |
---|
229 | * error code - if unsuccessful |
---|
230 | */ |
---|
231 | |
---|
232 | rtems_status_code rtems_message_queue_ident( |
---|
233 | Objects_Name name, |
---|
234 | unsigned32 node, |
---|
235 | Objects_Id *id |
---|
236 | ) |
---|
237 | { |
---|
238 | return( _Objects_Name_to_id( &_Message_queue_Information, name, |
---|
239 | node, id ) ); |
---|
240 | } |
---|
241 | |
---|
242 | /*PAGE |
---|
243 | * |
---|
244 | * rtems_message_queue_delete |
---|
245 | * |
---|
246 | * This directive allows a thread to delete the message queue specified |
---|
247 | * by the given queue identifier. |
---|
248 | * |
---|
249 | * Input parameters: |
---|
250 | * id - queue id |
---|
251 | * |
---|
252 | * Output parameters: |
---|
253 | * RTEMS_SUCCESSFUL - if successful |
---|
254 | * error code - if unsuccessful |
---|
255 | */ |
---|
256 | |
---|
257 | rtems_status_code rtems_message_queue_delete( |
---|
258 | Objects_Id id |
---|
259 | ) |
---|
260 | { |
---|
261 | register Message_queue_Control *the_message_queue; |
---|
262 | Objects_Locations location; |
---|
263 | |
---|
264 | the_message_queue = _Message_queue_Get( id, &location ); |
---|
265 | switch ( location ) { |
---|
266 | case OBJECTS_ERROR: |
---|
267 | return( RTEMS_INVALID_ID ); |
---|
268 | case OBJECTS_REMOTE: |
---|
269 | _Thread_Dispatch(); |
---|
270 | return( RTEMS_ILLEGAL_ON_REMOTE_OBJECT ); |
---|
271 | case OBJECTS_LOCAL: |
---|
272 | _Objects_Close( &_Message_queue_Information, |
---|
273 | &the_message_queue->Object ); |
---|
274 | |
---|
275 | if ( the_message_queue->number_of_pending_messages != 0 ) |
---|
276 | (void) _Message_queue_Flush_support( the_message_queue ); |
---|
277 | else |
---|
278 | _Thread_queue_Flush( |
---|
279 | &the_message_queue->Wait_queue, |
---|
280 | _Message_queue_MP_Send_object_was_deleted |
---|
281 | ); |
---|
282 | |
---|
283 | _Message_queue_Free( the_message_queue ); |
---|
284 | |
---|
285 | if ( _Attributes_Is_global( the_message_queue->attribute_set ) ) { |
---|
286 | _Objects_MP_Close( |
---|
287 | &_Message_queue_Information, |
---|
288 | the_message_queue->Object.id |
---|
289 | ); |
---|
290 | |
---|
291 | _Message_queue_MP_Send_process_packet( |
---|
292 | MESSAGE_QUEUE_MP_ANNOUNCE_DELETE, |
---|
293 | the_message_queue->Object.id, |
---|
294 | 0, /* Not used */ |
---|
295 | 0 |
---|
296 | ); |
---|
297 | } |
---|
298 | |
---|
299 | _Thread_Enable_dispatch(); |
---|
300 | return( RTEMS_SUCCESSFUL ); |
---|
301 | } |
---|
302 | |
---|
303 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
304 | } |
---|
305 | |
---|
306 | /*PAGE |
---|
307 | * |
---|
308 | * rtems_message_queue_send |
---|
309 | * |
---|
310 | * This routine implements the directives q_send. It sends a |
---|
311 | * message to the specified message queue. |
---|
312 | * |
---|
313 | * Input parameters: |
---|
314 | * id - pointer to message queue |
---|
315 | * buffer - pointer to message buffer |
---|
316 | * |
---|
317 | * Output parameters: |
---|
318 | * RTEMS_SUCCESSFUL - if successful |
---|
319 | * error code - if unsuccessful |
---|
320 | */ |
---|
321 | |
---|
322 | rtems_status_code rtems_message_queue_send( |
---|
323 | Objects_Id id, |
---|
324 | void *buffer, |
---|
325 | unsigned32 size |
---|
326 | ) |
---|
327 | { |
---|
328 | return _Message_queue_Submit(id, buffer, size, MESSAGE_QUEUE_SEND_REQUEST); |
---|
329 | } |
---|
330 | |
---|
331 | /*PAGE |
---|
332 | * |
---|
333 | * rtems_message_queue_urgent |
---|
334 | * |
---|
335 | * This routine implements the directives q_urgent. It urgents a |
---|
336 | * message to the specified message queue. |
---|
337 | * |
---|
338 | * Input parameters: |
---|
339 | * id - pointer to message queue |
---|
340 | * buffer - pointer to message buffer |
---|
341 | * |
---|
342 | * Output parameters: |
---|
343 | * RTEMS_SUCCESSFUL - if successful |
---|
344 | * error code - if unsuccessful |
---|
345 | */ |
---|
346 | |
---|
347 | rtems_status_code rtems_message_queue_urgent( |
---|
348 | Objects_Id id, |
---|
349 | void *buffer, |
---|
350 | unsigned32 size |
---|
351 | ) |
---|
352 | { |
---|
353 | return _Message_queue_Submit(id, buffer, size, MESSAGE_QUEUE_URGENT_REQUEST); |
---|
354 | } |
---|
355 | |
---|
356 | /*PAGE |
---|
357 | * |
---|
358 | * rtems_message_queue_broadcast |
---|
359 | * |
---|
360 | * This directive sends a message for every thread waiting on the queue |
---|
361 | * designated by id. |
---|
362 | * |
---|
363 | * Input parameters: |
---|
364 | * id - pointer to message queue |
---|
365 | * buffer - pointer to message buffer |
---|
366 | * count - pointer to area to store number of threads made ready |
---|
367 | * |
---|
368 | * Output parameters: |
---|
369 | * count - number of threads made ready |
---|
370 | * RTEMS_SUCCESSFUL - if successful |
---|
371 | * error code - if unsuccessful |
---|
372 | */ |
---|
373 | |
---|
374 | rtems_status_code rtems_message_queue_broadcast( |
---|
375 | Objects_Id id, |
---|
376 | void *buffer, |
---|
377 | unsigned32 size, |
---|
378 | unsigned32 *count |
---|
379 | ) |
---|
380 | { |
---|
381 | register Message_queue_Control *the_message_queue; |
---|
382 | Objects_Locations location; |
---|
383 | Thread_Control *the_thread; |
---|
384 | unsigned32 number_broadcasted; |
---|
385 | |
---|
386 | the_message_queue = _Message_queue_Get( id, &location ); |
---|
387 | switch ( location ) { |
---|
388 | case OBJECTS_ERROR: |
---|
389 | return( RTEMS_INVALID_ID ); |
---|
390 | case OBJECTS_REMOTE: |
---|
391 | _Thread_Executing->Wait.return_argument = count; |
---|
392 | |
---|
393 | return |
---|
394 | _Message_queue_MP_Send_request_packet( |
---|
395 | MESSAGE_QUEUE_MP_BROADCAST_REQUEST, |
---|
396 | id, |
---|
397 | buffer, |
---|
398 | &size, |
---|
399 | 0, /* option_set not used */ |
---|
400 | MPCI_DEFAULT_TIMEOUT |
---|
401 | ); |
---|
402 | |
---|
403 | case OBJECTS_LOCAL: |
---|
404 | { |
---|
405 | Thread_Wait_information *waitp; |
---|
406 | unsigned32 constrained_size; |
---|
407 | |
---|
408 | number_broadcasted = 0; |
---|
409 | while ( (the_thread = |
---|
410 | _Thread_queue_Dequeue(&the_message_queue->Wait_queue)) ) { |
---|
411 | waitp = &the_thread->Wait; |
---|
412 | number_broadcasted += 1; |
---|
413 | |
---|
414 | constrained_size = size; |
---|
415 | if (size > the_message_queue->maximum_message_size) |
---|
416 | constrained_size = the_message_queue->maximum_message_size; |
---|
417 | |
---|
418 | _Message_queue_Copy_buffer(buffer, |
---|
419 | waitp->return_argument, |
---|
420 | constrained_size); |
---|
421 | |
---|
422 | *waitp->Extra.message_size_p = constrained_size; |
---|
423 | |
---|
424 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) { |
---|
425 | the_thread->receive_packet->return_code = RTEMS_SUCCESSFUL; |
---|
426 | |
---|
427 | _Message_queue_MP_Send_response_packet( |
---|
428 | MESSAGE_QUEUE_MP_RECEIVE_RESPONSE, |
---|
429 | id, |
---|
430 | the_thread |
---|
431 | ); |
---|
432 | } |
---|
433 | } |
---|
434 | _Thread_Enable_dispatch(); |
---|
435 | *count = number_broadcasted; |
---|
436 | return( RTEMS_SUCCESSFUL ); |
---|
437 | } |
---|
438 | |
---|
439 | default: |
---|
440 | return RTEMS_INTERNAL_ERROR; |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | /*PAGE |
---|
445 | * |
---|
446 | * rtems_message_queue_receive |
---|
447 | * |
---|
448 | * This directive dequeues a message from the designated message queue |
---|
449 | * and copies it into the requesting thread's buffer. |
---|
450 | * |
---|
451 | * Input parameters: |
---|
452 | * id - queue id |
---|
453 | * buffer - pointer to message buffer |
---|
454 | * option_set - options on receive |
---|
455 | * timeout - number of ticks to wait |
---|
456 | * |
---|
457 | * Output parameters: |
---|
458 | * RTEMS_SUCCESSFUL - if successful |
---|
459 | * error code - if unsuccessful |
---|
460 | */ |
---|
461 | |
---|
462 | rtems_status_code rtems_message_queue_receive( |
---|
463 | Objects_Id id, |
---|
464 | void *buffer, |
---|
465 | unsigned32 *size_p, |
---|
466 | unsigned32 option_set, |
---|
467 | rtems_interval timeout |
---|
468 | ) |
---|
469 | { |
---|
470 | register Message_queue_Control *the_message_queue; |
---|
471 | Objects_Locations location; |
---|
472 | |
---|
473 | the_message_queue = _Message_queue_Get( id, &location ); |
---|
474 | switch ( location ) { |
---|
475 | |
---|
476 | case OBJECTS_ERROR: |
---|
477 | return( RTEMS_INVALID_ID ); |
---|
478 | |
---|
479 | case OBJECTS_REMOTE: |
---|
480 | _Thread_Executing->Wait.return_argument = buffer; |
---|
481 | |
---|
482 | return _Message_queue_MP_Send_request_packet( |
---|
483 | MESSAGE_QUEUE_MP_RECEIVE_REQUEST, |
---|
484 | id, |
---|
485 | buffer, |
---|
486 | size_p, |
---|
487 | option_set, |
---|
488 | timeout |
---|
489 | ); |
---|
490 | |
---|
491 | case OBJECTS_LOCAL: |
---|
492 | if ( ! _Message_queue_Seize(the_message_queue, |
---|
493 | option_set, |
---|
494 | buffer, |
---|
495 | size_p)) |
---|
496 | { |
---|
497 | _Thread_queue_Enqueue( &the_message_queue->Wait_queue, timeout ); |
---|
498 | } |
---|
499 | _Thread_Enable_dispatch(); |
---|
500 | return _Thread_Executing->Wait.return_code; |
---|
501 | } |
---|
502 | |
---|
503 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
504 | } |
---|
505 | |
---|
506 | /*PAGE |
---|
507 | * |
---|
508 | * rtems_message_queue_flush |
---|
509 | * |
---|
510 | * This directive removes all pending messages from a queue and returns |
---|
511 | * the number of messages removed. If no messages were present then |
---|
512 | * a count of zero is returned. |
---|
513 | * |
---|
514 | * Input parameters: |
---|
515 | * id - queue id |
---|
516 | * count - return area for count |
---|
517 | * |
---|
518 | * Output parameters: |
---|
519 | * count - number of messages removed ( 0 = empty queue ) |
---|
520 | * RTEMS_SUCCESSFUL - if successful |
---|
521 | * error code - if unsuccessful |
---|
522 | */ |
---|
523 | |
---|
524 | rtems_status_code rtems_message_queue_flush( |
---|
525 | Objects_Id id, |
---|
526 | unsigned32 *count |
---|
527 | ) |
---|
528 | { |
---|
529 | register Message_queue_Control *the_message_queue; |
---|
530 | Objects_Locations location; |
---|
531 | |
---|
532 | the_message_queue = _Message_queue_Get( id, &location ); |
---|
533 | switch ( location ) { |
---|
534 | case OBJECTS_ERROR: |
---|
535 | return( RTEMS_INVALID_ID ); |
---|
536 | case OBJECTS_REMOTE: |
---|
537 | _Thread_Executing->Wait.return_argument = count; |
---|
538 | |
---|
539 | return |
---|
540 | _Message_queue_MP_Send_request_packet( |
---|
541 | MESSAGE_QUEUE_MP_FLUSH_REQUEST, |
---|
542 | id, |
---|
543 | 0, /* buffer not used */ |
---|
544 | 0, /* size_p */ |
---|
545 | 0, /* option_set not used */ |
---|
546 | MPCI_DEFAULT_TIMEOUT |
---|
547 | ); |
---|
548 | |
---|
549 | case OBJECTS_LOCAL: |
---|
550 | if ( the_message_queue->number_of_pending_messages != 0 ) |
---|
551 | *count = _Message_queue_Flush_support( the_message_queue ); |
---|
552 | else |
---|
553 | *count = 0; |
---|
554 | _Thread_Enable_dispatch(); |
---|
555 | return( RTEMS_SUCCESSFUL ); |
---|
556 | } |
---|
557 | |
---|
558 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
559 | } |
---|
560 | |
---|
561 | /*PAGE |
---|
562 | * |
---|
563 | * _Message_queue_Seize |
---|
564 | * |
---|
565 | * This kernel routine dequeues a message, copies the message buffer to |
---|
566 | * a given destination buffer, and frees the message buffer to the |
---|
567 | * inactive message pool. |
---|
568 | * |
---|
569 | * Input parameters: |
---|
570 | * the_message_queue - pointer to message queue |
---|
571 | * option_set - options on receive |
---|
572 | * the_buffer - pointer to message buffer to be filled |
---|
573 | * |
---|
574 | * Output parameters: |
---|
575 | * TRUE - if message received or RTEMS_NO_WAIT and no message |
---|
576 | * FALSE - if thread is to block |
---|
577 | * |
---|
578 | * NOTE: Dependent on BUFFER_LENGTH |
---|
579 | * |
---|
580 | * INTERRUPT LATENCY: |
---|
581 | * available |
---|
582 | * wait |
---|
583 | */ |
---|
584 | |
---|
585 | boolean _Message_queue_Seize( |
---|
586 | Message_queue_Control *the_message_queue, |
---|
587 | rtems_option option_set, |
---|
588 | void *buffer, |
---|
589 | unsigned32 *size_p |
---|
590 | ) |
---|
591 | { |
---|
592 | ISR_Level level; |
---|
593 | Message_queue_Buffer_control *the_message; |
---|
594 | Thread_Control *executing; |
---|
595 | |
---|
596 | executing = _Thread_Executing; |
---|
597 | executing->Wait.return_code = RTEMS_SUCCESSFUL; |
---|
598 | _ISR_Disable( level ); |
---|
599 | if ( the_message_queue->number_of_pending_messages != 0 ) { |
---|
600 | the_message_queue->number_of_pending_messages -= 1; |
---|
601 | |
---|
602 | the_message = _Message_queue_Get_pending_message( the_message_queue ); |
---|
603 | _ISR_Enable( level ); |
---|
604 | *size_p = the_message->Contents.size; |
---|
605 | _Message_queue_Copy_buffer( the_message->Contents.buffer, buffer, *size_p ); |
---|
606 | _Message_queue_Free_message_buffer(the_message_queue, the_message ); |
---|
607 | return( TRUE ); |
---|
608 | } |
---|
609 | |
---|
610 | if ( _Options_Is_no_wait( option_set ) ) { |
---|
611 | _ISR_Enable( level ); |
---|
612 | executing->Wait.return_code = RTEMS_UNSATISFIED; |
---|
613 | return( TRUE ); |
---|
614 | } |
---|
615 | |
---|
616 | the_message_queue->Wait_queue.sync = TRUE; |
---|
617 | executing->Wait.queue = &the_message_queue->Wait_queue; |
---|
618 | executing->Wait.id = the_message_queue->Object.id; |
---|
619 | executing->Wait.option_set = option_set; |
---|
620 | executing->Wait.return_argument = (unsigned32 *)buffer; |
---|
621 | executing->Wait.Extra.message_size_p = size_p; |
---|
622 | _ISR_Enable( level ); |
---|
623 | return FALSE; |
---|
624 | } |
---|
625 | |
---|
626 | /*PAGE |
---|
627 | * |
---|
628 | * _Message_queue_Flush_support |
---|
629 | * |
---|
630 | * This message manager routine removes all messages from a message queue |
---|
631 | * and returns them to the inactive message pool. |
---|
632 | * |
---|
633 | * Input parameters: |
---|
634 | * the_message_queue - pointer to message queue |
---|
635 | * |
---|
636 | * Output parameters: |
---|
637 | * returns - number of messages placed on inactive chain |
---|
638 | * |
---|
639 | * INTERRUPT LATENCY: |
---|
640 | * only case |
---|
641 | */ |
---|
642 | |
---|
643 | unsigned32 _Message_queue_Flush_support( |
---|
644 | Message_queue_Control *the_message_queue |
---|
645 | ) |
---|
646 | { |
---|
647 | ISR_Level level; |
---|
648 | Chain_Node *inactive_first; |
---|
649 | Chain_Node *message_queue_first; |
---|
650 | Chain_Node *message_queue_last; |
---|
651 | unsigned32 count; |
---|
652 | |
---|
653 | _ISR_Disable( level ); |
---|
654 | inactive_first = the_message_queue->Inactive_messages.first; |
---|
655 | message_queue_first = the_message_queue->Pending_messages.first; |
---|
656 | message_queue_last = the_message_queue->Pending_messages.last; |
---|
657 | |
---|
658 | the_message_queue->Inactive_messages.first = message_queue_first; |
---|
659 | message_queue_last->next = inactive_first; |
---|
660 | inactive_first->previous = message_queue_last; |
---|
661 | message_queue_first->previous = |
---|
662 | _Chain_Head( &the_message_queue->Inactive_messages ); |
---|
663 | |
---|
664 | _Chain_Initialize_empty( &the_message_queue->Pending_messages ); |
---|
665 | |
---|
666 | count = the_message_queue->number_of_pending_messages; |
---|
667 | the_message_queue->number_of_pending_messages = 0; |
---|
668 | _ISR_Enable( level ); |
---|
669 | return count; |
---|
670 | } |
---|
671 | |
---|
672 | /*PAGE |
---|
673 | * |
---|
674 | * _Message_queue_Submit |
---|
675 | * |
---|
676 | * This routine implements the directives q_send and q_urgent. It |
---|
677 | * processes a message that is to be submitted to the designated |
---|
678 | * message queue. The message will either be processed as a send |
---|
679 | * send message which it will be inserted at the rear of the queue |
---|
680 | * or it will be processed as an urgent message which will be inserted |
---|
681 | * at the front of the queue. |
---|
682 | * |
---|
683 | * Input parameters: |
---|
684 | * id - pointer to message queue |
---|
685 | * buffer - pointer to message buffer |
---|
686 | * size - size in bytes of message to send |
---|
687 | * submit_type - send or urgent message |
---|
688 | * |
---|
689 | * Output parameters: |
---|
690 | * RTEMS_SUCCESSFUL - if successful |
---|
691 | * error code - if unsuccessful |
---|
692 | */ |
---|
693 | |
---|
694 | rtems_status_code _Message_queue_Submit( |
---|
695 | Objects_Id id, |
---|
696 | void *buffer, |
---|
697 | unsigned32 size, |
---|
698 | Message_queue_Submit_types submit_type |
---|
699 | ) |
---|
700 | { |
---|
701 | register Message_queue_Control *the_message_queue; |
---|
702 | Objects_Locations location; |
---|
703 | Thread_Control *the_thread; |
---|
704 | Message_queue_Buffer_control *the_message; |
---|
705 | |
---|
706 | the_message_queue = _Message_queue_Get( id, &location ); |
---|
707 | switch ( location ) |
---|
708 | { |
---|
709 | case OBJECTS_ERROR: |
---|
710 | return( RTEMS_INVALID_ID ); |
---|
711 | |
---|
712 | case OBJECTS_REMOTE: |
---|
713 | switch ( submit_type ) { |
---|
714 | case MESSAGE_QUEUE_SEND_REQUEST: |
---|
715 | return |
---|
716 | _Message_queue_MP_Send_request_packet( |
---|
717 | MESSAGE_QUEUE_MP_SEND_REQUEST, |
---|
718 | id, |
---|
719 | buffer, |
---|
720 | &size, |
---|
721 | 0, /* option_set */ |
---|
722 | MPCI_DEFAULT_TIMEOUT |
---|
723 | ); |
---|
724 | |
---|
725 | case MESSAGE_QUEUE_URGENT_REQUEST: |
---|
726 | return |
---|
727 | _Message_queue_MP_Send_request_packet( |
---|
728 | MESSAGE_QUEUE_MP_URGENT_REQUEST, |
---|
729 | id, |
---|
730 | buffer, |
---|
731 | &size, |
---|
732 | 0, /* option_set */ |
---|
733 | MPCI_DEFAULT_TIMEOUT |
---|
734 | ); |
---|
735 | } |
---|
736 | |
---|
737 | case OBJECTS_LOCAL: |
---|
738 | if (size > the_message_queue->maximum_message_size) |
---|
739 | { |
---|
740 | _Thread_Enable_dispatch(); |
---|
741 | return RTEMS_INVALID_SIZE; |
---|
742 | } |
---|
743 | |
---|
744 | /* |
---|
745 | * Is there a thread currently waiting on this message queue? |
---|
746 | */ |
---|
747 | |
---|
748 | the_thread = _Thread_queue_Dequeue( &the_message_queue->Wait_queue ); |
---|
749 | if ( the_thread ) |
---|
750 | { |
---|
751 | _Message_queue_Copy_buffer( |
---|
752 | buffer, |
---|
753 | the_thread->Wait.return_argument, |
---|
754 | size |
---|
755 | ); |
---|
756 | *the_thread->Wait.Extra.message_size_p = size; |
---|
757 | |
---|
758 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) { |
---|
759 | the_thread->receive_packet->return_code = RTEMS_SUCCESSFUL; |
---|
760 | |
---|
761 | _Message_queue_MP_Send_response_packet( |
---|
762 | MESSAGE_QUEUE_MP_RECEIVE_RESPONSE, |
---|
763 | id, |
---|
764 | the_thread |
---|
765 | ); |
---|
766 | |
---|
767 | } |
---|
768 | _Thread_Enable_dispatch(); |
---|
769 | return( RTEMS_SUCCESSFUL ); |
---|
770 | } |
---|
771 | |
---|
772 | /* |
---|
773 | * No one waiting on this one currently. |
---|
774 | * Allocate a message buffer and store it away |
---|
775 | */ |
---|
776 | |
---|
777 | if ( the_message_queue->number_of_pending_messages == |
---|
778 | the_message_queue->maximum_pending_messages ) { |
---|
779 | _Thread_Enable_dispatch(); |
---|
780 | return( RTEMS_TOO_MANY ); |
---|
781 | } |
---|
782 | |
---|
783 | the_message = _Message_queue_Allocate_message_buffer(the_message_queue); |
---|
784 | if ( the_message == 0) { |
---|
785 | _Thread_Enable_dispatch(); |
---|
786 | return( RTEMS_UNSATISFIED ); |
---|
787 | } |
---|
788 | |
---|
789 | _Message_queue_Copy_buffer( buffer, the_message->Contents.buffer, size ); |
---|
790 | the_message->Contents.size = size; |
---|
791 | |
---|
792 | the_message_queue->number_of_pending_messages += 1; |
---|
793 | |
---|
794 | switch ( submit_type ) { |
---|
795 | case MESSAGE_QUEUE_SEND_REQUEST: |
---|
796 | _Message_queue_Append( the_message_queue, the_message ); |
---|
797 | break; |
---|
798 | case MESSAGE_QUEUE_URGENT_REQUEST: |
---|
799 | _Message_queue_Prepend( the_message_queue, the_message ); |
---|
800 | break; |
---|
801 | } |
---|
802 | |
---|
803 | _Thread_Enable_dispatch(); |
---|
804 | return( RTEMS_SUCCESSFUL ); |
---|
805 | |
---|
806 | default: |
---|
807 | return RTEMS_INTERNAL_ERROR; /* And they were such nice boys, too! */ |
---|
808 | } |
---|
809 | } |
---|