int mq_close( mqd_t mqdes ) { POSIX_Message_queue_Control *the_mq; Thread_queue_Context queue_context; _Objects_Allocator_lock(); the_mq = _POSIX_Message_queue_Get( mqdes, &queue_context ); if ( the_mq == NULL ) { _Objects_Allocator_unlock(); rtems_set_errno_and_return_minus_one( EBADF ); } _CORE_message_queue_Acquire_critical( &the_mq->Message_queue, &queue_context ); if ( the_mq->open_count == 0 ) { _CORE_message_queue_Release( &the_mq->Message_queue, &queue_context ); _Objects_Allocator_unlock(); rtems_set_errno_and_return_minus_one( EBADF ); } the_mq->open_count -= 1; _POSIX_Message_queue_Delete( the_mq, &queue_context ); _Objects_Allocator_unlock(); return 0; }
Status_Control _CORE_message_queue_Broadcast( CORE_message_queue_Control *the_message_queue, const void *buffer, size_t size, uint32_t *count, Thread_queue_Context *queue_context ) { Thread_Control *the_thread; uint32_t number_broadcasted; if ( size > the_message_queue->maximum_message_size ) { _ISR_lock_ISR_enable( &queue_context->Lock_context ); return STATUS_MESSAGE_INVALID_SIZE; } number_broadcasted = 0; _CORE_message_queue_Acquire_critical( the_message_queue, queue_context ); while ( ( the_thread = _CORE_message_queue_Dequeue_receiver( the_message_queue, buffer, size, 0, queue_context ) ) ) { number_broadcasted += 1; _CORE_message_queue_Acquire( the_message_queue, queue_context ); } _CORE_message_queue_Release( the_message_queue, queue_context ); *count = number_broadcasted; return STATUS_SUCCESSFUL; }
ssize_t _POSIX_Message_queue_Receive_support( mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned int *msg_prio, bool wait, Watchdog_Interval timeout ) { POSIX_Message_queue_Control *the_mq; Thread_queue_Context queue_context; size_t length_out; bool do_wait; Thread_Control *executing; Status_Control status; the_mq = _POSIX_Message_queue_Get( mqdes, &queue_context ); if ( the_mq == NULL ) { rtems_set_errno_and_return_minus_one( EBADF ); } if ( ( the_mq->oflag & O_ACCMODE ) == O_WRONLY ) { _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context ); rtems_set_errno_and_return_minus_one( EBADF ); } if ( msg_len < the_mq->Message_queue.maximum_message_size ) { _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context ); rtems_set_errno_and_return_minus_one( EMSGSIZE ); } /* * Now if something goes wrong, we return a "length" of -1 * to indicate an error. */ length_out = -1; /* * A timed receive with a bad time will do a poll regardless. */ if ( wait ) { do_wait = ( the_mq->oflag & O_NONBLOCK ) == 0; } else { do_wait = wait; } _CORE_message_queue_Acquire_critical( &the_mq->Message_queue, &queue_context ); if ( the_mq->open_count == 0 ) { _CORE_message_queue_Release( &the_mq->Message_queue, &queue_context ); rtems_set_errno_and_return_minus_one( EBADF ); } /* * Now perform the actual message receive */ executing = _Thread_Executing; _Thread_queue_Context_set_relative_timeout( &queue_context, timeout ); status = _CORE_message_queue_Seize( &the_mq->Message_queue, executing, msg_ptr, &length_out, do_wait, &queue_context ); if ( msg_prio != NULL ) { *msg_prio = _POSIX_Message_queue_Priority_from_core( executing->Wait.count ); } if ( status != STATUS_SUCCESSFUL ) { rtems_set_errno_and_return_minus_one( _POSIX_Get_error( status ) ); } return length_out; }
CORE_message_queue_Status _CORE_message_queue_Submit( CORE_message_queue_Control *the_message_queue, Thread_Control *executing, const void *buffer, size_t size, Objects_Id id, #if defined(RTEMS_MULTIPROCESSING) CORE_message_queue_API_mp_support_callout api_message_queue_mp_support, #else CORE_message_queue_API_mp_support_callout api_message_queue_mp_support RTEMS_UNUSED, #endif CORE_message_queue_Submit_types submit_type, bool wait, Watchdog_Interval timeout, ISR_lock_Context *lock_context ) { CORE_message_queue_Buffer_control *the_message; Thread_Control *the_thread; if ( size > the_message_queue->maximum_message_size ) { _ISR_lock_ISR_enable( lock_context ); return CORE_MESSAGE_QUEUE_STATUS_INVALID_SIZE; } _CORE_message_queue_Acquire_critical( the_message_queue, lock_context ); /* * Is there a thread currently waiting on this message queue? */ the_thread = _CORE_message_queue_Dequeue_receiver( the_message_queue, buffer, size, submit_type, lock_context ); if ( the_thread != NULL ) { #if defined(RTEMS_MULTIPROCESSING) if ( !_Objects_Is_local_id( the_thread->Object.id ) ) (*api_message_queue_mp_support) ( the_thread, id ); _Thread_Dispatch_enable( _Per_CPU_Get() ); #endif return CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL; } /* * No one waiting on the message queue at this time, so attempt to * queue the message up for a future receive. */ the_message = _CORE_message_queue_Allocate_message_buffer( the_message_queue ); if ( the_message ) { the_message->Contents.size = size; _CORE_message_queue_Set_message_priority( the_message, submit_type ); _CORE_message_queue_Copy_buffer( buffer, the_message->Contents.buffer, size ); _CORE_message_queue_Insert_message( the_message_queue, the_message, submit_type ); _CORE_message_queue_Release( the_message_queue, lock_context ); return CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL; } #if !defined(RTEMS_SCORE_COREMSG_ENABLE_BLOCKING_SEND) _CORE_message_queue_Release( the_message_queue, lock_context ); return CORE_MESSAGE_QUEUE_STATUS_TOO_MANY; #else /* * No message buffers were available so we may need to return an * overflow error or block the sender until the message is placed * on the queue. */ if ( !wait ) { _CORE_message_queue_Release( the_message_queue, lock_context ); return CORE_MESSAGE_QUEUE_STATUS_TOO_MANY; } /* * Do NOT block on a send if the caller is in an ISR. It is * deadly to block in an ISR. */ if ( _ISR_Is_in_progress() ) { _CORE_message_queue_Release( the_message_queue, lock_context ); return CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED; } /* * WARNING!! executing should NOT be used prior to this point. * Thus the unusual choice to open a new scope and declare * it as a variable. Doing this emphasizes how dangerous it * would be to use this variable prior to here. */ executing->Wait.id = id; executing->Wait.return_argument_second.immutable_object = buffer; executing->Wait.option = (uint32_t) size; executing->Wait.count = submit_type; _Thread_queue_Enqueue_critical( &the_message_queue->Wait_queue.Queue, the_message_queue->Wait_queue.operations, executing, STATES_WAITING_FOR_MESSAGE, timeout, CORE_MESSAGE_QUEUE_STATUS_TIMEOUT, lock_context ); #if defined(RTEMS_MULTIPROCESSING) _Thread_Dispatch_enable( _Per_CPU_Get() ); #endif return CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED_WAIT; #endif }
Status_Control _CORE_message_queue_Submit( CORE_message_queue_Control *the_message_queue, Thread_Control *executing, const void *buffer, size_t size, CORE_message_queue_Submit_types submit_type, bool wait, Thread_queue_Context *queue_context ) { CORE_message_queue_Buffer_control *the_message; Thread_Control *the_thread; if ( size > the_message_queue->maximum_message_size ) { _CORE_message_queue_Release( the_message_queue, queue_context ); return STATUS_MESSAGE_INVALID_SIZE; } /* * Is there a thread currently waiting on this message queue? */ the_thread = _CORE_message_queue_Dequeue_receiver( the_message_queue, buffer, size, submit_type, queue_context ); if ( the_thread != NULL ) { return STATUS_SUCCESSFUL; } /* * No one waiting on the message queue at this time, so attempt to * queue the message up for a future receive. */ the_message = _CORE_message_queue_Allocate_message_buffer( the_message_queue ); if ( the_message ) { _CORE_message_queue_Insert_message( the_message_queue, the_message, buffer, size, submit_type ); #if defined(RTEMS_SCORE_COREMSG_ENABLE_NOTIFICATION) /* * According to POSIX, does this happen before or after the message * is actually enqueued. It is logical to think afterwards, because * the message is actually in the queue at this point. */ if ( the_message_queue->number_of_pending_messages == 1 && the_message_queue->notify_handler != NULL ) { ( *the_message_queue->notify_handler )( the_message_queue, queue_context ); } else { _CORE_message_queue_Release( the_message_queue, queue_context ); } #else _CORE_message_queue_Release( the_message_queue, queue_context ); #endif return STATUS_SUCCESSFUL; } #if !defined(RTEMS_SCORE_COREMSG_ENABLE_BLOCKING_SEND) _CORE_message_queue_Release( the_message_queue, queue_context ); return STATUS_TOO_MANY; #else /* * No message buffers were available so we may need to return an * overflow error or block the sender until the message is placed * on the queue. */ if ( !wait ) { _CORE_message_queue_Release( the_message_queue, queue_context ); return STATUS_TOO_MANY; } /* * Do NOT block on a send if the caller is in an ISR. It is * deadly to block in an ISR. */ if ( _ISR_Is_in_progress() ) { _CORE_message_queue_Release( the_message_queue, queue_context ); return STATUS_MESSAGE_QUEUE_WAIT_IN_ISR; } /* * WARNING!! executing should NOT be used prior to this point. * Thus the unusual choice to open a new scope and declare * it as a variable. Doing this emphasizes how dangerous it * would be to use this variable prior to here. */ executing->Wait.return_argument_second.immutable_object = buffer; executing->Wait.option = (uint32_t) size; executing->Wait.count = submit_type; _Thread_queue_Context_set_thread_state( queue_context, STATES_WAITING_FOR_MESSAGE ); _Thread_queue_Context_set_do_nothing_enqueue_callout( queue_context ); _Thread_queue_Enqueue( &the_message_queue->Wait_queue.Queue, the_message_queue->operations, executing, queue_context ); return _Thread_Wait_get_status( executing ); #endif }