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