Re[3]: О функции pthread_mutex_timedlock
От: McQwerty Россия  
Дата: 19.05.06 10:23
Оценка: 82 (3) :)
Здравствуйте, McQwerty, Вы писали:

MQ>при этом всём если использовать нерекурсивные мьютексы — тайм-аут будет!

MQ>при рекурсивных — никак!
Дело оказалось в бобине!!
Вот кусочек из mutex.c (выделения мои):
/* Linuxthreads - a simple clone()-based implementation of Posix        */
/* threads for Linux.                                                   */
/* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
/*                                                                      */
/* This program is free software; you can redistribute it and/or        */
/* modify it under the terms of the GNU Library General Public License  */
/* as published by the Free Software Foundation; either version 2       */
/* of the License, or (at your option) any later version.               */
/*                                                                      */
/* This program is distributed in the hope that it will be useful,      */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
/* GNU Library General Public License for more details.                 */

int __pthread_mutex_timedlock (pthread_mutex_t *mutex,
                               const struct timespec *abstime)
{
  pthread_descr self;
  int res;

  if (__builtin_expect (abstime->tv_nsec, 0) < 0
      || __builtin_expect (abstime->tv_nsec, 0) >= 1000000000)
    return EINVAL;

  switch(mutex->__m_kind) {
  case PTHREAD_MUTEX_ADAPTIVE_NP:
    __pthread_lock(&mutex->__m_lock, NULL);
    return 0;
  case PTHREAD_MUTEX_RECURSIVE_NP:
    self = thread_self();
    if (mutex->__m_owner == self) {
      mutex->__m_count++;
      return 0;
    }
    __pthread_lock(&mutex->__m_lock, self);
    mutex->__m_owner = self;
    mutex->__m_count = 0;
    return 0;
  case PTHREAD_MUTEX_ERRORCHECK_NP:
    self = thread_self();
    if (mutex->__m_owner == self) return EDEADLK;
    res = __pthread_alt_timedlock(&mutex->__m_lock, self, abstime);
    if (res != 0)
      {
        mutex->__m_owner = self;
        return 0;
      }
    return ETIMEDOUT;
  case PTHREAD_MUTEX_TIMED_NP:
    /* Only this type supports timed out lock. */
    return (__pthread_alt_timedlock(&mutex->__m_lock, NULL, abstime)
            ? 0 : ETIMEDOUT);
  default:
    return EINVAL;
  }
}


При ожидании рекурсивного мутанта время вообще не используется

При рассмотрении соседней функции __pthread_mutex_lock, оказалось, что методом квадратно-гнездового копирования код ожидания из неё перекочевал в __pthread_mutex_timedlock.

int __pthread_mutex_lock(pthread_mutex_t * mutex)
{
<...хрум...>
  case PTHREAD_MUTEX_RECURSIVE_NP:
    self = thread_self();
    if (mutex->__m_owner == self) {
      mutex->__m_count++;
      return 0;
    }
    __pthread_lock(&mutex->__m_lock, self);
    mutex->__m_owner = self;
    mutex->__m_count = 0;
    return 0;
  }
<...хрум...>
}

...зато бисплатна....
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.