misc/lock-monitor/lock-monitor.c
static struct thread *find_thread(void) {
    struct list *tl;
    pthread_t me;
    struct thread *t;

    me = pthread_self();
    real_mutex_lock(&thread_list_lock);
    tl = thread_list;
    real_mutex_unlock(&thread_list_lock);
    for (; tl; tl = tl->next) {
        t = (struct thread*)tl->value;
        if (pthread_equal(me, t->thread))
            /* Found ourselves. */
            return t;
    }

    /* We didn't find the thread struct, so let's create it. */
    t = (struct thread*)malloc(sizeof(struct thread));
    t->thread = me;
    t->locks = NULL;
    tl = (struct list*)malloc(sizeof(struct list));
    tl->value = (void*)t;
    real_mutex_lock(&thread_list_lock);
    tl->next = thread_list;
    thread_list = tl;
    real_mutex_unlock(&thread_list_lock);
    return t;
}
/* Find the current thread in thread_list. If there is no entry for it yet,
 * this is created. Note the locking in this function that looks incorrect. We
 * cheat and only lock thread_list_lock when we are explicitly reading or
 * writing to thread_list. Because nodes are never removed from thread_list and
 * new nodes are only ever added to the front of the list we will never get an
 * inconsistent view of the list as long as no one reads/writes the actual
 * variable thread_list while we are also doing so (see, I told you that
 * add-to-front property would come up later ;).
 */
Find the current thread in thread_list. If there is no entry for it yet,
this is created. Note the locking in this function that looks incorrect. We
cheat and only lock thread_list_lock when we are explicitly reading or
writing to thread_list. Because nodes are never removed from thread_list and
new nodes are only ever added to the front of the list we will never get an
inconsistent view of the list as long as no one reads/writes the actual
variable thread_list while we are also doing so (see, I told you that
add-to-front property would come up later ;).
Find the current thread in thread_list . If there is no entry for it yet this is created . Note the locking in this function that looks incorrect . We cheat and only lock thread_list_lock when we are explicitly reading or writing to thread_list . Because nodes are never removed from thread_list and new nodes are only ever added to the front of the list we will never get an inconsistent view of the list as long as no one reads / writes the actual variable thread_list while we are also doing so ( see I told you that add - to - front property would come up later ; ) .
static struct thread *find_thread(void) {
    struct list *tl;
    pthread_t me;
    struct thread *t;
    me = pthread_self();
    real_mutex_lock(&thread_list_lock);
    tl = thread_list;
    real_mutex_unlock(&thread_list_lock);
    for (; tl; tl = tl->next) {
        t = (struct thread*)tl->value;
        if (pthread_equal(me, t->thread))
            return t;
    }
    t = (struct thread*)malloc(sizeof(struct thread));
    t->thread = me;
    t->locks = NULL;
    tl = (struct list*)malloc(sizeof(struct list));
    tl->value = (void*)t;
    real_mutex_lock(&thread_list_lock);
    tl->next = thread_list;
    thread_list = tl;
    real_mutex_unlock(&thread_list_lock);
    return t;
}
static struct thread * find_thread ( void ) { struct list * tl ; pthread_t me ; struct thread * t ; me = pthread_self ( ) ; real_mutex_lock ( & thread_list_lock ) ; tl = thread_list ; real_mutex_unlock ( & thread_list_lock ) ; for ( ; tl ; tl = tl -> next ) { t = ( struct thread * ) tl -> value ; if ( pthread_equal ( me , t -> thread ) ) return t ; } t = ( struct thread * ) malloc ( sizeof ( struct thread ) ) ; t -> thread = me ; t -> locks = NULL ; tl = ( struct list * ) malloc ( sizeof ( struct list ) ) ; tl -> value = ( void * ) t ; real_mutex_lock ( & thread_list_lock ) ; tl -> next = thread_list ; thread_list = tl ; real_mutex_unlock ( & thread_list_lock ) ; return t ; }
Find the current thread in thread_list.
Find the current thread in thread_list .
/* Found ourselves. */ /* We didn't find the thread struct, so let's create it. */