int _POSIX_Mutex_Lock_support( pthread_mutex_t *mutex, bool wait, Watchdog_Interval timeout ) { POSIX_Mutex_Control *the_mutex; Thread_queue_Context queue_context; Thread_Control *executing; Status_Control status; the_mutex = _POSIX_Mutex_Get( mutex, &queue_context ); if ( the_mutex == NULL ) { return EINVAL; } executing = _Thread_Executing; _Thread_queue_Context_set_relative_timeout( &queue_context, timeout ); switch ( the_mutex->protocol ) { case POSIX_MUTEX_PRIORITY_CEILING: status = _CORE_ceiling_mutex_Seize( &the_mutex->Mutex, executing, wait, _POSIX_Mutex_Lock_nested, &queue_context ); break; case POSIX_MUTEX_NO_PROTOCOL: status = _CORE_recursive_mutex_Seize( &the_mutex->Mutex.Recursive, POSIX_MUTEX_NO_PROTOCOL_TQ_OPERATIONS, executing, wait, _POSIX_Mutex_Lock_nested, &queue_context ); break; default: _Assert( the_mutex->protocol == POSIX_MUTEX_PRIORITY_INHERIT ); status = _CORE_recursive_mutex_Seize( &the_mutex->Mutex.Recursive, CORE_MUTEX_TQ_PRIORITY_INHERIT_OPERATIONS, executing, wait, _POSIX_Mutex_Lock_nested, &queue_context ); break; } return _POSIX_Get_error( status ); }
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 ) ); } }
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; }
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; }