Example #1
0
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 );
}
Example #2
0
void _API_Mutex_Lock( API_Mutex_Control *the_mutex )
{
  Thread_Life_state    previous_thread_life_state;
  Thread_queue_Context queue_context;

  previous_thread_life_state =
    _Thread_Set_life_protection( THREAD_LIFE_PROTECTED );

  _Thread_queue_Context_initialize( &queue_context );
  _ISR_lock_ISR_disable( &queue_context.Lock_context );
  _Thread_queue_Context_set_no_timeout( &queue_context );
  _CORE_recursive_mutex_Seize(
    &the_mutex->Mutex,
    _Thread_Executing,
    true,
    _CORE_recursive_mutex_Seize_nested,
    &queue_context
  );

  if ( the_mutex->Mutex.nest_level == 0 ) {
    the_mutex->previous_thread_life_state = previous_thread_life_state;
  }
}