Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: f703f9d506366a3edb3f32ebe6719668699c77cf
Ticket: 992108ffffffffffffffffffffffffffffffffff
TclFinalizeLock on Unix not correct.
User & Date: mistachkin on 2004-07-17 05:12:15
Changes

  1. icomment:
    Logged In: YES 
    user_id=113501
    
    The following code is from FreeBSD 4.10-STABLE.
    
    -- in /usr/src/include/pthread.h:
    
    #define PTHREAD_MUTEX_INITIALIZER       NULL
    
    -- in /usr/src/lib/libc_r/uthread/uthread_mutex.c:
    
    int
    _pthread_mutex_init(pthread_mutex_t * mutex,
       const pthread_mutexattr_t * mutex_attr)
    {
    enum pthread_mutextypetype;
    intprotocol;
    intceiling;
    pthread_mutex_tpmutex;
    intret = 0;
    
    if (mutex == NULL)
    ret = EINVAL;
    
    /* Check if default mutex attributes: */
    else if (mutex_attr == NULL || *mutex_attr == NULL) {
    /* Default to a (error checking) POSIX mutex: */
    type = PTHREAD_MUTEX_ERRORCHECK;
    protocol = PTHREAD_PRIO_NONE;
    ceiling = PTHREAD_MAX_PRIORITY;
    }
    
    /* Check mutex type: */
    else if (((*mutex_attr)->m_type < PTHREAD_MUTEX_ERRORCHECK) ||
        ((*mutex_attr)->m_type >= MUTEX_TYPE_MAX))
    /* Return an invalid argument error: */
    ret = EINVAL;
    
    /* Check mutex protocol: */
    else if (((*mutex_attr)->m_protocol < PTHREAD_PRIO_NONE) ||
        ((*mutex_attr)->m_protocol > PTHREAD_MUTEX_RECURSIVE))
    /* Return an invalid argument error: */
    ret = EINVAL;
    
    else {
    /* Use the requested mutex type and protocol: */
    type = (*mutex_attr)->m_type;
    protocol = (*mutex_attr)->m_protocol;
    ceiling = (*mutex_attr)->m_ceiling;
    }
    
    /* Check no errors so far: */
    if (ret == 0) {
    if ((pmutex = (pthread_mutex_t)
        malloc(sizeof(struct pthread_mutex))) == NULL)
    ret = ENOMEM;
    else {
    /* Reset the mutex flags: */
    pmutex->m_flags = 0;
    
    /* Process according to mutex type: */
    switch (type) {
    /* case PTHREAD_MUTEX_DEFAULT: */
    case PTHREAD_MUTEX_ERRORCHECK:
    case PTHREAD_MUTEX_NORMAL:
    /* Nothing to do here. */
    break;
    
    /* Single UNIX Spec 2 recursive mutex: */
    case PTHREAD_MUTEX_RECURSIVE:
    /* Reset the mutex count: */
    pmutex->m_data.m_count = 0;
    break;
    
    /* Trap invalid mutex types: */
    default:
    /* Return an invalid argument error: */
    ret = EINVAL;
    break;
    }
    if (ret == 0) {
    /* Initialise the rest of the mutex: */
    TAILQ_INIT(&pmutex->m_queue);
    pmutex->m_flags |= MUTEX_FLAGS_INITED;
    pmutex->m_owner = NULL;
    pmutex->m_type = type;
    pmutex->m_protocol = protocol;
    pmutex->m_refcount = 0;
    if (protocol == PTHREAD_PRIO_PROTECT)
    pmutex->m_prio = ceiling;
    else
    pmutex->m_prio = 0;
    pmutex->m_saved_prio = 0;
    _MUTEX_INIT_LINK(pmutex);
    memset(&pmutex->lock, 0, sizeof(pmutex->lock));
    *mutex = pmutex;
    } else {
    free(pmutex);
    *mutex = NULL;
    }
    }
    }
    /* Return the completion status: */
    return(ret);
    }
    
    int
    _pthread_mutex_destroy(pthread_mutex_t * mutex)
    {
    intret = 0;
    
    if (mutex == NULL || *mutex == NULL)
    ret = EINVAL;
    else {
    /* Lock the mutex structure: */
    _SPINLOCK(&(*mutex)->lock);
    
    /*
     * Check to see if this mutex is in use:
     */
    if (((*mutex)->m_owner != NULL) ||
        (TAILQ_FIRST(&(*mutex)->m_queue) != NULL) ||
        ((*mutex)->m_refcount != 0)) {
    ret = EBUSY;
    
    /* Unlock the mutex structure: */
    _SPINUNLOCK(&(*mutex)->lock);
    }
    else {
    /*
     * Free the memory allocated for the mutex
     * structure:
     */
    _MUTEX_ASSERT_NOT_OWNED(*mutex);
    free(*mutex);
    
    /*
     * Leave the caller's pointer NULL now that
     * the mutex has been destroyed:
     */
    *mutex = NULL;
    }
    }
    
    /* Return the completion status: */
    return (ret);
    }
    
    
    
  2. login: "mistachkin"
  3. mimetype: "text/plain"
  4. username: "mistachkin"