Status_Control _Thread_queue_Enqueue_sticky( Thread_queue_Queue *queue, const Thread_queue_Operations *operations, Thread_Control *the_thread, Thread_queue_Context *queue_context ) { Per_CPU_Control *cpu_self; _Thread_Wait_claim( the_thread, queue ); if ( !_Thread_queue_Path_acquire_critical( queue, the_thread, queue_context ) ) { _Thread_queue_Path_release_critical( queue_context ); _Thread_Wait_restore_default( the_thread ); _Thread_queue_Queue_release( queue, &queue_context->Lock_context.Lock_context ); _Thread_Wait_tranquilize( the_thread ); ( *queue_context->deadlock_callout )( the_thread ); return _Thread_Wait_get_status( the_thread ); } _Thread_queue_Context_clear_priority_updates( queue_context ); _Thread_Wait_claim_finalize( the_thread, operations ); ( *operations->enqueue )( queue, the_thread, queue_context ); _Thread_queue_Path_release_critical( queue_context ); the_thread->Wait.return_code = STATUS_SUCCESSFUL; _Thread_Wait_flags_set( the_thread, THREAD_QUEUE_INTEND_TO_BLOCK ); cpu_self = _Thread_Dispatch_disable_critical( &queue_context->Lock_context.Lock_context ); _Thread_queue_Queue_release( queue, &queue_context->Lock_context.Lock_context ); if ( cpu_self->thread_dispatch_disable_level != 1 ) { _Internal_error( INTERNAL_ERROR_THREAD_QUEUE_ENQUEUE_STICKY_FROM_BAD_STATE ); } _Thread_queue_Timeout( the_thread, cpu_self, queue_context ); _Thread_Priority_update( queue_context ); _Thread_Priority_and_sticky_update( the_thread, 1 ); _Thread_Dispatch_enable( cpu_self ); while ( _Thread_Wait_flags_get_acquire( the_thread ) == THREAD_QUEUE_INTEND_TO_BLOCK ) { /* Wait */ } _Thread_Wait_tranquilize( the_thread ); _Thread_Timer_remove( the_thread ); return _Thread_Wait_get_status( the_thread ); }
static void any_satisfy_before_timeout(rtems_id timer, void *arg) { rtems_status_code sc; test_context *ctx = arg; Thread_Control *thread = ctx->thread; Thread_Wait_flags flags = _Thread_Wait_flags_get(thread); if (blocks_for_event(flags)) { ctx->hit = interrupts_blocking_op(flags); rtems_test_assert( *(rtems_event_set *) thread->Wait.return_argument == DEADBEEF ); rtems_test_assert(_Thread_Wait_get_status(thread) == STATUS_SUCCESSFUL); sc = rtems_event_send(thread->Object.id, GREEN); rtems_test_assert(sc == RTEMS_SUCCESSFUL); rtems_test_assert( *(rtems_event_set *) thread->Wait.return_argument == GREEN ); rtems_test_assert(_Thread_Wait_get_status(thread) == STATUS_SUCCESSFUL); sc = rtems_event_send(thread->Object.id, RED); rtems_test_assert(sc == RTEMS_SUCCESSFUL); rtems_test_assert( *(rtems_event_set *) thread->Wait.return_argument == GREEN ); rtems_test_assert(_Thread_Wait_get_status(thread) == STATUS_SUCCESSFUL); _Thread_Timeout(&thread->Timer.Watchdog); rtems_test_assert( *(rtems_event_set *) thread->Wait.return_argument == GREEN ); rtems_test_assert(_Thread_Wait_get_status(thread) == STATUS_SUCCESSFUL); if (ctx->hit) { rtems_test_assert( _Thread_Wait_flags_get(thread) == (THREAD_WAIT_CLASS_EVENT | THREAD_WAIT_STATE_READY_AGAIN) ); } rtems_test_assert(thread->Wait.count == EVENTS); } sc = rtems_timer_reset(timer); rtems_test_assert(sc == RTEMS_SUCCESSFUL); }
Status_Control _CORE_barrier_Seize( CORE_barrier_Control *the_barrier, Thread_Control *executing, bool wait, Watchdog_Interval timeout, Thread_queue_Context *queue_context ) { uint32_t number_of_waiting_threads; _CORE_barrier_Acquire_critical( the_barrier, queue_context ); number_of_waiting_threads = the_barrier->number_of_waiting_threads; ++number_of_waiting_threads; if ( _CORE_barrier_Is_automatic( &the_barrier->Attributes ) && number_of_waiting_threads == the_barrier->Attributes.maximum_count ) { _CORE_barrier_Surrender( the_barrier, queue_context ); return STATUS_BARRIER_AUTOMATICALLY_RELEASED; } else { the_barrier->number_of_waiting_threads = number_of_waiting_threads; _Thread_queue_Context_set_expected_level( queue_context, 1 ); _Thread_queue_Enqueue_critical( &the_barrier->Wait_queue.Queue, CORE_BARRIER_TQ_OPERATIONS, executing, STATES_WAITING_FOR_BARRIER, timeout, queue_context ); return _Thread_Wait_get_status( executing ); } }
Status_Control _CORE_mutex_Seize_slow( CORE_mutex_Control *the_mutex, const Thread_queue_Operations *operations, Thread_Control *executing, bool wait, Thread_queue_Context *queue_context ) { if ( wait ) { _Thread_queue_Context_set_thread_state( queue_context, STATES_WAITING_FOR_MUTEX ); _Thread_queue_Context_set_do_nothing_enqueue_callout( queue_context ); _Thread_queue_Context_set_deadlock_callout( queue_context, _Thread_queue_Deadlock_status ); _Thread_queue_Enqueue( &the_mutex->Wait_queue.Queue, operations, executing, queue_context ); return _Thread_Wait_get_status( executing ); } else { _CORE_mutex_Release( the_mutex, queue_context ); return STATUS_UNAVAILABLE; } }
static bool test_body(void *arg) { test_context *ctx = arg; int busy; Per_CPU_Control *cpu_self; cpu_self = _Thread_Dispatch_disable(); rtems_test_assert( _Thread_Wait_get_status( ctx->semaphore_task_tcb ) == STATUS_SUCCESSFUL ); /* * Spend some time to make it more likely that we hit the test condition * below. */ for (busy = 0; busy < 1000; ++busy) { __asm__ volatile (""); } if (ctx->semaphore_task_tcb->Wait.queue == NULL) { ctx->thread_queue_was_null = true; } _Thread_Timeout(&ctx->semaphore_task_tcb->Timer.Watchdog); switch (_Thread_Wait_get_status(ctx->semaphore_task_tcb)) { case STATUS_SUCCESSFUL: ctx->status_was_successful = true; break; case STATUS_TIMEOUT: ctx->status_was_timeout = true; break; default: rtems_test_assert(0); break; } _Thread_Dispatch_enable(cpu_self); return ctx->thread_queue_was_null && ctx->status_was_successful && ctx->status_was_timeout; }
Status_Control _CORE_RWLock_Seize_for_writing( CORE_RWLock_Control *the_rwlock, Thread_Control *executing, bool wait, Watchdog_Interval timeout, Thread_queue_Context *queue_context ) { /* * If unlocked, then OK to read. * Otherwise, we have to block. * If locked for reading and no waiters, then OK to read. * If any thread is waiting, then we wait. */ _CORE_RWLock_Acquire_critical( the_rwlock, queue_context ); switch ( the_rwlock->current_state ) { case CORE_RWLOCK_UNLOCKED: the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_WRITING; _CORE_RWLock_Release( the_rwlock, queue_context ); return STATUS_SUCCESSFUL; case CORE_RWLOCK_LOCKED_FOR_READING: case CORE_RWLOCK_LOCKED_FOR_WRITING: break; } /* * If the thread is not willing to wait, then return immediately. */ if ( !wait ) { _CORE_RWLock_Release( the_rwlock, queue_context ); return STATUS_UNAVAILABLE; } /* * We need to wait to enter this critical section */ executing->Wait.option = CORE_RWLOCK_THREAD_WAITING_FOR_WRITE; _Thread_queue_Context_set_expected_level( queue_context, 1 ); _Thread_queue_Enqueue_critical( &the_rwlock->Wait_queue.Queue, CORE_RWLOCK_TQ_OPERATIONS, executing, STATES_WAITING_FOR_RWLOCK, timeout, queue_context ); return _Thread_Wait_get_status( executing ); }
int _Mutex_recursive_Acquire_timed( struct _Mutex_recursive_Control *_mutex, const struct timespec *abstime ) { Mutex_recursive_Control *mutex; Thread_queue_Context queue_context; ISR_Level level; Thread_Control *executing; Thread_Control *owner; mutex = _Mutex_recursive_Get( _mutex ); _Thread_queue_Context_initialize( &queue_context ); _Thread_queue_Context_ISR_disable( &queue_context, level ); executing = _Mutex_Queue_acquire_critical( &mutex->Mutex, &queue_context ); owner = mutex->Mutex.Queue.Queue.owner; if ( __predict_true( owner == NULL ) ) { mutex->Mutex.Queue.Queue.owner = executing; _Thread_Resource_count_increment( executing ); _Mutex_Queue_release( &mutex->Mutex, level, &queue_context ); return 0; } else if ( owner == executing ) { ++mutex->nest_level; _Mutex_Queue_release( &mutex->Mutex, level, &queue_context ); return 0; } else { Watchdog_Interval ticks; switch ( _TOD_Absolute_timeout_to_ticks( abstime, CLOCK_REALTIME, &ticks ) ) { case TOD_ABSOLUTE_TIMEOUT_INVALID: _Mutex_Queue_release( &mutex->Mutex, level, &queue_context ); return EINVAL; case TOD_ABSOLUTE_TIMEOUT_IS_IN_PAST: case TOD_ABSOLUTE_TIMEOUT_IS_NOW: _Mutex_Queue_release( &mutex->Mutex, level, &queue_context ); return ETIMEDOUT; default: break; } _Thread_queue_Context_set_relative_timeout( &queue_context, ticks ); _Mutex_Acquire_slow( &mutex->Mutex, owner, executing, level, &queue_context ); return STATUS_GET_POSIX( _Thread_Wait_get_status( executing ) ); } }
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 }