void _Thread_queue_Extract_with_return_code( Thread_queue_Control *the_thread_queue, Thread_Control *the_thread, uint32_t return_code ) { ISR_lock_Context lock_context; _Thread_queue_Acquire( &lock_context ); if ( !_States_Is_waiting_on_thread_queue( the_thread->current_state ) ) { _Thread_queue_Release( &lock_context ); return; } if ( the_thread_queue->discipline == THREAD_QUEUE_DISCIPLINE_FIFO ) { _Chain_Extract_unprotected( &the_thread->Object.Node ); } else { /* must be THREAD_QUEUE_DISCIPLINE_PRIORITY */ _RBTree_Extract( &the_thread->Wait.queue->Queues.Priority, &the_thread->RBNode ); _Thread_Priority_restore_default_change_handler( the_thread ); _Thread_Lock_restore_default( the_thread ); } the_thread->Wait.return_code = return_code; /* * We found a thread to unblock. * * NOTE: This is invoked with interrupts still disabled. */ _Thread_blocking_operation_Finalize( the_thread, &lock_context ); }
Thread_Control *_Thread_queue_Do_dequeue( Thread_queue_Control *the_thread_queue, const Thread_queue_Operations *operations #if defined(RTEMS_MULTIPROCESSING) , Thread_queue_MP_callout mp_callout #endif ) { Thread_queue_Context queue_context; Thread_Control *the_thread; _Thread_queue_Context_initialize( &queue_context ); _Thread_queue_Context_set_MP_callout( &queue_context, mp_callout ); _Thread_queue_Acquire( the_thread_queue, &queue_context ); the_thread = _Thread_queue_First_locked( the_thread_queue, operations ); if ( the_thread != NULL ) { _Thread_queue_Extract_critical( &the_thread_queue->Queue, operations, the_thread, &queue_context ); } else { _Thread_queue_Release( the_thread_queue, &queue_context ); } return the_thread; }
void _CORE_RWLock_Obtain_for_writing( CORE_RWLock_Control *the_rwlock, Thread_Control *executing, Objects_Id id, bool wait, Watchdog_Interval timeout, CORE_RWLock_API_mp_support_callout api_rwlock_mp_support ) { ISR_lock_Context lock_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. */ _Thread_queue_Acquire( &the_rwlock->Wait_queue, &lock_context ); switch ( the_rwlock->current_state ) { case CORE_RWLOCK_UNLOCKED: the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_WRITING; _Thread_queue_Release( &the_rwlock->Wait_queue, &lock_context ); executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; return; 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 ) { _Thread_queue_Release( &the_rwlock->Wait_queue, &lock_context ); executing->Wait.return_code = CORE_RWLOCK_UNAVAILABLE; return; } /* * We need to wait to enter this critical section */ executing->Wait.id = id; executing->Wait.option = CORE_RWLOCK_THREAD_WAITING_FOR_WRITE; executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL; _Thread_queue_Enqueue_critical( &the_rwlock->Wait_queue, executing, STATES_WAITING_FOR_RWLOCK, timeout, CORE_RWLOCK_TIMEOUT, &lock_context ); /* return to API level so it can dispatch and we block */ }
Thread_Control *_Thread_queue_Dequeue( Thread_queue_Control *the_thread_queue ) { Thread_Control *the_thread; ISR_lock_Context lock_context; Thread_blocking_operation_States sync_state; the_thread = NULL; _Thread_queue_Acquire( &lock_context ); /* * Invoke the discipline specific dequeue method. */ if ( the_thread_queue->discipline == THREAD_QUEUE_DISCIPLINE_FIFO ) { if ( !_Chain_Is_empty( &the_thread_queue->Queues.Fifo ) ) { the_thread = (Thread_Control *) _Chain_Get_first_unprotected( &the_thread_queue->Queues.Fifo ); } } else { /* must be THREAD_QUEUE_DISCIPLINE_PRIORITY */ RBTree_Node *first; first = _RBTree_Get( &the_thread_queue->Queues.Priority, RBT_LEFT ); if ( first ) { the_thread = THREAD_RBTREE_NODE_TO_THREAD( first ); _Thread_Priority_restore_default_change_handler( the_thread ); _Thread_Lock_restore_default( the_thread ); } } if ( the_thread == NULL ) { /* * We did not find a thread to unblock in the queue. Maybe the executing * thread is about to block on this thread queue. */ sync_state = the_thread_queue->sync_state; if ( (sync_state == THREAD_BLOCKING_OPERATION_TIMEOUT) || (sync_state == THREAD_BLOCKING_OPERATION_NOTHING_HAPPENED) ) { the_thread_queue->sync_state = THREAD_BLOCKING_OPERATION_SATISFIED; the_thread = _Thread_Executing; } else { _Thread_queue_Release( &lock_context ); return NULL; } } /* * We found a thread to unblock. * * NOTE: This is invoked with interrupts still disabled. */ _Thread_blocking_operation_Finalize( the_thread, &lock_context ); return the_thread; }
void _CORE_mutex_Seize_interrupt_blocking( CORE_mutex_Control *the_mutex, Thread_Control *executing, Watchdog_Interval timeout, ISR_lock_Context *lock_context ) { #if !defined(RTEMS_SMP) /* * We must disable thread dispatching here since we enable the interrupts for * priority inheritance mutexes. */ _Thread_Dispatch_disable(); #endif if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ) { Thread_Control *holder = the_mutex->holder; #if !defined(RTEMS_SMP) /* * To enable interrupts here works only since exactly one executing thread * exists and only threads are allowed to seize and surrender mutexes with * the priority inheritance protocol. On SMP configurations more than one * executing thread may exist, so here we must not release the lock, since * otherwise the current holder may be no longer the holder of the mutex * once we released the lock. */ _Thread_queue_Release( &the_mutex->Wait_queue, lock_context ); #endif _Thread_Inherit_priority( holder, executing ); #if !defined(RTEMS_SMP) _Thread_queue_Acquire( &the_mutex->Wait_queue, lock_context ); #endif } _Thread_queue_Enqueue_critical( &the_mutex->Wait_queue.Queue, the_mutex->operations, executing, STATES_WAITING_FOR_MUTEX, timeout, CORE_MUTEX_TIMEOUT, lock_context ); #if !defined(RTEMS_SMP) _Thread_Dispatch_enable( _Per_CPU_Get() ); #endif }
void _Thread_queue_Flush( Thread_queue_Control *the_thread_queue, #if defined(RTEMS_MULTIPROCESSING) Thread_queue_Flush_callout remote_extract_callout, #else Thread_queue_Flush_callout remote_extract_callout RTEMS_UNUSED, #endif uint32_t status ) { ISR_lock_Context lock_context; Thread_Control *the_thread; _Thread_queue_Acquire( the_thread_queue, &lock_context ); while ( (the_thread = _Thread_queue_First_locked( the_thread_queue ) ) ) { #if defined(RTEMS_MULTIPROCESSING) if ( _Objects_Is_local_id( the_thread->Object.id ) ) #endif the_thread->Wait.return_code = status; _Thread_queue_Extract_critical( &the_thread_queue->Queue, the_thread_queue->operations, the_thread, &lock_context ); #if defined(RTEMS_MULTIPROCESSING) if ( !_Objects_Is_local_id( the_thread->Object.id ) ) ( *remote_extract_callout )( the_thread ); #endif _Thread_queue_Acquire( the_thread_queue, &lock_context ); } _Thread_queue_Release( the_thread_queue, &lock_context ); }
static void _Thread_Wake_up_joining_threads( Thread_Control *the_thread ) { Thread_Join_context join_context; #if defined(RTEMS_POSIX_API) join_context.exit_value = the_thread->Life.exit_value; #endif _Thread_queue_Context_initialize( &join_context.Base ); _Thread_queue_Acquire( &the_thread->Join_queue, &join_context.Base ); _Thread_queue_Flush_critical( &the_thread->Join_queue.Queue, THREAD_JOIN_TQ_OPERATIONS, #if defined(RTEMS_POSIX_API) _Thread_Join_flush_filter, #else _Thread_queue_Flush_default_filter, #endif &join_context.Base ); }
rtems_status_code rtems_region_get_segment( rtems_id id, uintptr_t size, rtems_option option_set, rtems_interval timeout, void **segment ) { rtems_status_code status; Region_Control *the_region; if ( segment == NULL ) { return RTEMS_INVALID_ADDRESS; } *segment = NULL; if ( size == 0 ) { return RTEMS_INVALID_SIZE; } the_region = _Region_Get_and_lock( id ); if ( the_region == NULL ) { return RTEMS_INVALID_ID; } if ( size > the_region->maximum_segment_size ) { status = RTEMS_INVALID_SIZE; } else { void *the_segment; the_segment = _Region_Allocate_segment( the_region, size ); if ( the_segment != NULL ) { *segment = the_segment; status = RTEMS_SUCCESSFUL; } else if ( _Options_Is_no_wait( option_set ) ) { status = RTEMS_UNSATISFIED; } else { Thread_queue_Context queue_context; Thread_Control *executing; _Thread_queue_Context_initialize( &queue_context ); _Thread_queue_Acquire( &the_region->Wait_queue, &queue_context ); executing = _Thread_Executing; executing->Wait.count = size; executing->Wait.return_argument = segment; /* FIXME: This is a home grown condition variable */ _Thread_queue_Context_set_thread_state( &queue_context, STATES_WAITING_FOR_SEGMENT ); _Thread_queue_Context_set_timeout_ticks( &queue_context, timeout ); _Thread_queue_Context_set_enqueue_callout( &queue_context, _Region_Enqueue_callout ); _Thread_queue_Enqueue( &the_region->Wait_queue.Queue, the_region->wait_operations, executing, &queue_context ); return _Status_Get_after_wait( executing ); } } _Region_Unlock( the_region ); return status; }
void _Thread_queue_Enqueue( Thread_queue_Control *the_thread_queue, Thread_Control *the_thread, States_Control state, Watchdog_Interval timeout ) { ISR_lock_Context lock_context; Thread_blocking_operation_States sync_state; #if defined(RTEMS_MULTIPROCESSING) if ( _Thread_MP_Is_receive( the_thread ) && the_thread->receive_packet ) the_thread = _Thread_MP_Allocate_proxy( state ); else #endif /* * Set the blocking state for this thread queue in the thread. */ _Thread_Set_state( the_thread, state ); /* * If the thread wants to timeout, then schedule its timer. */ if ( timeout ) { _Watchdog_Initialize( &the_thread->Timer, _Thread_queue_Timeout, the_thread->Object.id, NULL ); _Watchdog_Insert_ticks( &the_thread->Timer, timeout ); } /* * Now initiate the enqueuing and checking if the blocking operation * should be completed or the thread has had its blocking condition * satisfied before we got here. */ _Thread_queue_Acquire( &lock_context ); sync_state = the_thread_queue->sync_state; the_thread_queue->sync_state = THREAD_BLOCKING_OPERATION_SYNCHRONIZED; if ( sync_state == THREAD_BLOCKING_OPERATION_NOTHING_HAPPENED ) { /* * Invoke the discipline specific enqueue method. */ if ( the_thread_queue->discipline == THREAD_QUEUE_DISCIPLINE_FIFO ) { _Chain_Append_unprotected( &the_thread_queue->Queues.Fifo, &the_thread->Object.Node ); } else { /* must be THREAD_QUEUE_DISCIPLINE_PRIORITY */ _Thread_Lock_set( the_thread, &_Thread_queue_Lock ); _Thread_Priority_set_change_handler( the_thread, _Thread_queue_Requeue_priority, the_thread_queue ); _RBTree_Insert( &the_thread_queue->Queues.Priority, &the_thread->RBNode, _Thread_queue_Compare_priority, false ); } the_thread->Wait.queue = the_thread_queue; the_thread_queue->sync_state = THREAD_BLOCKING_OPERATION_SYNCHRONIZED; _Thread_queue_Release( &lock_context ); } else { /* Cancel a blocking operation due to ISR */ _Assert( sync_state == THREAD_BLOCKING_OPERATION_TIMEOUT || sync_state == THREAD_BLOCKING_OPERATION_SATISFIED ); _Thread_blocking_operation_Finalize( the_thread, &lock_context ); } }