[82cb78d8] | 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 | * |
---|
[404903b] | 10 | * COPYRIGHT (c) 1989-2007. |
---|
[82cb78d8] | 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 |
---|
[dd687d97] | 15 | * http://www.rtems.com/license/LICENSE. |
---|
[82cb78d8] | 16 | * |
---|
| 17 | * $Id$ |
---|
| 18 | */ |
---|
| 19 | |
---|
[a8eed23] | 20 | #if HAVE_CONFIG_H |
---|
| 21 | #include "config.h" |
---|
| 22 | #endif |
---|
| 23 | |
---|
[82cb78d8] | 24 | #include <rtems/system.h> |
---|
| 25 | #include <rtems/score/chain.h> |
---|
| 26 | #include <rtems/score/isr.h> |
---|
| 27 | #include <rtems/score/object.h> |
---|
| 28 | #include <rtems/score/coremsg.h> |
---|
| 29 | #include <rtems/score/states.h> |
---|
| 30 | #include <rtems/score/thread.h> |
---|
| 31 | #include <rtems/score/wkspace.h> |
---|
| 32 | |
---|
| 33 | /*PAGE |
---|
| 34 | * |
---|
| 35 | * _CORE_message_queue_Seize |
---|
| 36 | * |
---|
| 37 | * This kernel routine dequeues a message, copies the message buffer to |
---|
| 38 | * a given destination buffer, and frees the message buffer to the |
---|
[aae7f1a1] | 39 | * inactive message pool. The thread will be blocked if wait is true, |
---|
[82cb78d8] | 40 | * otherwise an error will be given to the thread if no messages are available. |
---|
| 41 | * |
---|
| 42 | * Input parameters: |
---|
| 43 | * the_message_queue - pointer to message queue |
---|
| 44 | * id - id of object we are waitig on |
---|
| 45 | * buffer - pointer to message buffer to be filled |
---|
[f773c012] | 46 | * size_p - pointer to the size of buffer to be filled |
---|
[aae7f1a1] | 47 | * wait - true if wait is allowed, false otherwise |
---|
[82cb78d8] | 48 | * timeout - time to wait for a message |
---|
| 49 | * |
---|
| 50 | * Output parameters: NONE |
---|
| 51 | * |
---|
| 52 | * NOTE: Dependent on BUFFER_LENGTH |
---|
| 53 | * |
---|
| 54 | * INTERRUPT LATENCY: |
---|
| 55 | * available |
---|
| 56 | * wait |
---|
| 57 | */ |
---|
| 58 | |
---|
| 59 | void _CORE_message_queue_Seize( |
---|
[58243b4] | 60 | CORE_message_queue_Control *the_message_queue, |
---|
| 61 | Objects_Id id, |
---|
| 62 | void *buffer, |
---|
[f773c012] | 63 | size_t *size_p, |
---|
[484a769] | 64 | bool wait, |
---|
[58243b4] | 65 | Watchdog_Interval timeout |
---|
[82cb78d8] | 66 | ) |
---|
| 67 | { |
---|
| 68 | ISR_Level level; |
---|
| 69 | CORE_message_queue_Buffer_control *the_message; |
---|
| 70 | Thread_Control *executing; |
---|
| 71 | |
---|
| 72 | executing = _Thread_Executing; |
---|
| 73 | executing->Wait.return_code = CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL; |
---|
| 74 | _ISR_Disable( level ); |
---|
[c961c06b] | 75 | the_message = _CORE_message_queue_Get_pending_message( the_message_queue ); |
---|
| 76 | if ( the_message != NULL ) { |
---|
[82cb78d8] | 77 | the_message_queue->number_of_pending_messages -= 1; |
---|
| 78 | _ISR_Enable( level ); |
---|
[ea2c1d6] | 79 | |
---|
[f773c012] | 80 | *size_p = the_message->Contents.size; |
---|
[507d382] | 81 | #if defined(RTEMS_SCORE_COREMSG_ENABLE_MESSAGE_PRIORITY) |
---|
| 82 | _Thread_Executing->Wait.count = the_message->priority; |
---|
| 83 | #endif |
---|
[f773c012] | 84 | _CORE_message_queue_Copy_buffer(the_message->Contents.buffer,buffer,*size_p); |
---|
[ea2c1d6] | 85 | |
---|
[507d382] | 86 | #if !defined(RTEMS_SCORE_COREMSG_ENABLE_BLOCKING_SEND) |
---|
| 87 | /* |
---|
| 88 | * There is not an API with blocking sends enabled. So return immediately. |
---|
| 89 | */ |
---|
[ea2c1d6] | 90 | _CORE_message_queue_Free_message_buffer( the_message_queue, the_message ); |
---|
| 91 | return; |
---|
[507d382] | 92 | #else |
---|
| 93 | { |
---|
| 94 | Thread_Control *the_thread; |
---|
[ea2c1d6] | 95 | |
---|
[507d382] | 96 | /* |
---|
| 97 | * There could be a thread waiting to send a message. If there |
---|
| 98 | * is not, then we can go ahead and free the buffer. |
---|
| 99 | * |
---|
| 100 | * NOTE: If we note that the queue was not full before this receive, |
---|
| 101 | * then we can avoid this dequeue. |
---|
| 102 | */ |
---|
| 103 | the_thread = _Thread_queue_Dequeue( &the_message_queue->Wait_queue ); |
---|
| 104 | if ( !the_thread ) { |
---|
| 105 | _CORE_message_queue_Free_message_buffer( the_message_queue, the_message ); |
---|
| 106 | return; |
---|
| 107 | } |
---|
[ea2c1d6] | 108 | |
---|
[507d382] | 109 | /* |
---|
| 110 | * There was a thread waiting to send a message. This code |
---|
| 111 | * puts the messages in the message queue on behalf of the |
---|
| 112 | * waiting task. |
---|
| 113 | */ |
---|
| 114 | #if defined(RTEMS_SCORE_COREMSG_ENABLE_MESSAGE_PRIORITY) |
---|
| 115 | the_message->priority = the_thread->Wait.count; |
---|
| 116 | #endif |
---|
| 117 | the_message->Contents.size = (size_t) the_thread->Wait.option; |
---|
| 118 | _CORE_message_queue_Copy_buffer( |
---|
| 119 | the_thread->Wait.return_argument_second.immutable_object, |
---|
| 120 | the_message->Contents.buffer, |
---|
| 121 | the_message->Contents.size |
---|
| 122 | ); |
---|
[ea2c1d6] | 123 | |
---|
[507d382] | 124 | _CORE_message_queue_Insert_message( |
---|
| 125 | the_message_queue, |
---|
| 126 | the_message, |
---|
| 127 | #if defined(RTEMS_SCORE_COREMSG_ENABLE_MESSAGE_PRIORITY) |
---|
| 128 | the_message->priority, |
---|
| 129 | #else |
---|
| 130 | 0 |
---|
| 131 | #endif |
---|
| 132 | ); |
---|
| 133 | return; |
---|
| 134 | } |
---|
| 135 | #endif |
---|
[82cb78d8] | 136 | } |
---|
| 137 | |
---|
| 138 | if ( !wait ) { |
---|
| 139 | _ISR_Enable( level ); |
---|
| 140 | executing->Wait.return_code = CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED_NOWAIT; |
---|
| 141 | return; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | _Thread_queue_Enter_critical_section( &the_message_queue->Wait_queue ); |
---|
[f773c012] | 145 | executing->Wait.queue = &the_message_queue->Wait_queue; |
---|
| 146 | executing->Wait.id = id; |
---|
| 147 | executing->Wait.return_argument_second.mutable_object = buffer; |
---|
| 148 | executing->Wait.return_argument = size_p; |
---|
[ea2c1d6] | 149 | /* Wait.count will be filled in with the message priority */ |
---|
[82cb78d8] | 150 | _ISR_Enable( level ); |
---|
| 151 | |
---|
| 152 | _Thread_queue_Enqueue( &the_message_queue->Wait_queue, timeout ); |
---|
| 153 | } |
---|