static int _POSIX_Threads_Join( pthread_t thread, void **value_ptr ) { Thread_Control *the_thread; Thread_queue_Context queue_context; Per_CPU_Control *cpu_self; Thread_Control *executing; void *value; _Thread_queue_Context_initialize( &queue_context ); _Thread_queue_Context_set_expected_level( &queue_context, 1 ); the_thread = _Thread_Get( thread, &queue_context.Lock_context ); if ( the_thread == NULL ) { return ESRCH; } cpu_self = _Per_CPU_Get(); executing = _Per_CPU_Get_executing( cpu_self ); if ( executing == the_thread ) { _ISR_lock_ISR_enable( &queue_context.Lock_context ); return EDEADLK; } _Thread_State_acquire_critical( the_thread, &queue_context.Lock_context ); if ( !_Thread_Is_joinable( the_thread ) ) { _Thread_State_release( the_thread, &queue_context.Lock_context ); return EINVAL; } if ( _States_Is_waiting_for_join_at_exit( the_thread->current_state ) ) { value = the_thread->Life.exit_value; _Thread_Clear_state_locked( the_thread, STATES_WAITING_FOR_JOIN_AT_EXIT ); _Thread_Dispatch_disable_with_CPU( cpu_self, &queue_context.Lock_context ); _Thread_State_release( the_thread, &queue_context.Lock_context ); _Thread_Dispatch_enable( cpu_self ); } else { _Thread_Join( the_thread, STATES_INTERRUPTIBLE_BY_SIGNAL | STATES_WAITING_FOR_JOIN, executing, &queue_context ); if ( _POSIX_Get_error_after_wait( executing ) != 0 ) { _Assert( _POSIX_Get_error_after_wait( executing ) == EINTR ); return EINTR; } value = executing->Wait.return_argument; } if ( value_ptr != NULL ) { *value_ptr = value; } return 0; }
int sched_yield( void ) { Per_CPU_Control *cpu_self; cpu_self = _Thread_Dispatch_disable(); _Thread_Yield( _Per_CPU_Get_executing( cpu_self ) ); _Thread_Dispatch_direct( cpu_self ); return 0; }
void pthread_exit( void *value_ptr ) { Thread_Control *executing; Per_CPU_Control *cpu_self; cpu_self = _Thread_Dispatch_disable(); executing = _Per_CPU_Get_executing( cpu_self ); _Thread_Exit( executing, THREAD_LIFE_TERMINATING, value_ptr ); _Thread_Dispatch_enable( cpu_self ); RTEMS_UNREACHABLE(); }
rtems_status_code rtems_task_delete( rtems_id id ) { Thread_Control *the_thread; ISR_lock_Context lock_context; Thread_Control *executing; Per_CPU_Control *cpu_self; the_thread = _Thread_Get( id, &lock_context ); if ( the_thread == NULL ) { #if defined(RTEMS_MULTIPROCESSING) if ( _Thread_MP_Is_remote( id ) ) { return RTEMS_ILLEGAL_ON_REMOTE_OBJECT; } #endif return RTEMS_INVALID_ID; } cpu_self = _Thread_Dispatch_disable_critical( &lock_context ); _ISR_lock_ISR_enable( &lock_context ); executing = _Per_CPU_Get_executing( cpu_self ); if ( the_thread == executing ) { /* * The Classic tasks are neither detached nor joinable. In case of * self deletion, they are detached, otherwise joinable by default. */ _Thread_Exit( executing, THREAD_LIFE_TERMINATING | THREAD_LIFE_DETACHED, NULL ); } else { _Thread_Close( the_thread, executing ); } _Thread_Dispatch_enable( cpu_self ); return RTEMS_SUCCESSFUL; }
void _pthread_cleanup_pop( struct _pthread_cleanup_context *context, int execute ) { Per_CPU_Control *cpu_self; Thread_Control *executing; if ( execute != 0 ) { ( *context->_routine )( context->_arg ); } cpu_self = _Thread_Dispatch_disable(); executing = _Per_CPU_Get_executing( cpu_self ); executing->last_cleanup_context = context->_previous; _Thread_Dispatch_enable( cpu_self ); }
void _pthread_cleanup_push( struct _pthread_cleanup_context *context, void ( *routine )( void * ), void *arg ) { Per_CPU_Control *cpu_self; Thread_Control *executing; context->_routine = routine; context->_arg = arg; /* This value is unused, just provide a deterministic value */ context->_canceltype = -1; cpu_self = _Thread_Dispatch_disable(); executing = _Per_CPU_Get_executing( cpu_self ); context->_previous = executing->last_cleanup_context; executing->last_cleanup_context = context; _Thread_Dispatch_enable( cpu_self ); }
int _POSIX_Condition_variables_Wait_support( pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime ) { POSIX_Condition_variables_Control *the_cond; Thread_queue_Context queue_context; int error; int mutex_error; Per_CPU_Control *cpu_self; Thread_Control *executing; Watchdog_Interval timeout; bool already_timedout; TOD_Absolute_timeout_conversion_results status; if ( mutex == NULL ) { return EINVAL; } the_cond = _POSIX_Condition_variables_Get( cond, &queue_context ); if ( the_cond == NULL ) { return EINVAL; } already_timedout = false; if ( abstime != NULL ) { /* * POSIX requires that blocking calls with timeouts that take * an absolute timeout must ignore issues with the absolute * time provided if the operation would otherwise succeed. * So we check the abstime provided, and hold on to whether it * is valid or not. If it isn't correct and in the future, * then we do a polling operation and convert the UNSATISFIED * status into the appropriate error. */ _Assert( the_cond->clock ); status = _TOD_Absolute_timeout_to_ticks(abstime, the_cond->clock, &timeout); if ( status == TOD_ABSOLUTE_TIMEOUT_INVALID ) return EINVAL; if ( status == TOD_ABSOLUTE_TIMEOUT_IS_IN_PAST || status == TOD_ABSOLUTE_TIMEOUT_IS_NOW ) { already_timedout = true; } else { _Thread_queue_Context_set_relative_timeout( &queue_context, timeout ); } } else { _Thread_queue_Context_set_no_timeout( &queue_context ); } _POSIX_Condition_variables_Acquire_critical( the_cond, &queue_context ); if ( the_cond->mutex != POSIX_CONDITION_VARIABLES_NO_MUTEX && the_cond->mutex != *mutex ) { _POSIX_Condition_variables_Release( the_cond, &queue_context ); return EINVAL; } the_cond->mutex = *mutex; cpu_self = _Thread_Dispatch_disable_critical( &queue_context.Lock_context ); executing = _Per_CPU_Get_executing( cpu_self ); if ( !already_timedout ) { _Thread_queue_Context_set_expected_level( &queue_context, 2 ); _Thread_queue_Enqueue_critical( &the_cond->Wait_queue.Queue, POSIX_CONDITION_VARIABLES_TQ_OPERATIONS, executing, STATES_WAITING_FOR_CONDITION_VARIABLE, &queue_context ); } else { _POSIX_Condition_variables_Release( the_cond, &queue_context ); executing->Wait.return_code = STATUS_TIMEOUT; } mutex_error = pthread_mutex_unlock( mutex ); if ( mutex_error != 0 ) { /* * Historically, we ignored the unlock status since the behavior * is undefined by POSIX. But GNU/Linux returns EPERM in this * case, so we follow their lead. */ _Assert( mutex_error == EINVAL || mutex_error == EPERM ); _Thread_queue_Extract( executing ); _Thread_Dispatch_enable( cpu_self ); return EPERM; } /* * Switch ourself out because we blocked as a result of the * _Thread_queue_Enqueue_critical(). */ _Thread_Dispatch_enable( cpu_self ); error = _POSIX_Get_error_after_wait( executing ); /* * If the thread is interrupted, while in the thread queue, by * a POSIX signal, then pthread_cond_wait returns spuriously, * according to the POSIX standard. It means that pthread_cond_wait * returns a success status, except for the fact that it was not * woken up a pthread_cond_signal() or a pthread_cond_broadcast(). */ if ( error == EINTR ) { error = 0; } /* * When we get here the dispatch disable level is 0. */ mutex_error = pthread_mutex_lock( mutex ); if ( mutex_error != 0 ) { _Assert( mutex_error == EINVAL ); return EINVAL; } return error; }