misc/lock-monitor/lock-monitor.c
static void lock(const void *lock) {
    struct thread *t;

    /* If we haven't seen the lock before, assume that it comes after every
     * other lock currently in the partial ordering. Note that this is the only
     * place locks are added to ordering. In other words, if you want to
     * explicitly define a partial ordering you may want to immediately lock
     * all your semaphores/mutexes on creation in the order you want to define,
     * unlock them, and only then commence using them in your parallel code.
     */
    real_mutex_lock(&ordering_lock);
    if (!known(lock))
        add(lock);
    assert(known(lock));
    real_mutex_unlock(&ordering_lock);

    t = find_thread();
    assert(t);

    /* Modify the check below to <= 0 if you want to prevent your semaphores
     * mutexes from being re-entrant (nested). Note that we don't do any
     * locking on ->locks even though we do all manner of operations on it.
     * This is not a mistake. We will only ever be reading/modifying ->locks
     * from the thread associated with this item in thread_list and hence there
     * will never be any concurrent access.
     */
    if (t->locks && lockcmp(lock, t->locks->value) < 0)
        lock_monitor_exception("Lock Monitor: Thread %p attempted to lock "
            "%p while holding the lock %p, that comes after it in the partial "
            "ordering.\n", (void*)t->thread, lock, t->locks->value);
        /* Unreachable. */
    else {
        /* Allow the lock to succeed. */
        struct list *l;

        l = (struct list*)malloc(sizeof(struct list));
        l->value = lock;
        l->next = t->locks;
        t->locks = l;
    }
}
/* Register with the internal data structures that we are about to lock a
 * semaphore/mutex. This is our chance to cacth a thread red-handed if it is
 * grabbing a lock out of order.
 */
Register with the internal data structures that we are about to lock a
semaphore/mutex. This is our chance to cacth a thread red-handed if it is
grabbing a lock out of order.
Register with the internal data structures that we are about to lock a semaphore / mutex . This is our chance to cacth a thread red - handed if it is grabbing a lock out of order .
static void lock(const void *lock) {
    struct thread *t;
    real_mutex_lock(&ordering_lock);
    if (!known(lock))
        add(lock);
    assert(known(lock));
    real_mutex_unlock(&ordering_lock);
    t = find_thread();
    assert(t);
    if (t->locks && lockcmp(lock, t->locks->value) < 0)
        lock_monitor_exception("Lock Monitor: Thread %p attempted to lock "
            "%p while holding the lock %p, that comes after it in the partial "
            "ordering.\n", (void*)t->thread, lock, t->locks->value);
    else {
        struct list *l;
        l = (struct list*)malloc(sizeof(struct list));
        l->value = lock;
        l->next = t->locks;
        t->locks = l;
    }
}
static void lock ( const void * lock ) { struct thread * t ; real_mutex_lock ( & ordering_lock ) ; if ( ! known ( lock ) ) add ( lock ) ; assert ( known ( lock ) ) ; real_mutex_unlock ( & ordering_lock ) ; t = find_thread ( ) ; assert ( t ) ; if ( t -> locks && lockcmp ( lock , t -> locks -> value ) < 0 ) lock_monitor_exception ( " " " " " \n " , ( void * ) t -> thread , lock , t -> locks -> value ) ; else { struct list * l ; l = ( struct list * ) malloc ( sizeof ( struct list ) ) ; l -> value = lock ; l -> next = t -> locks ; t -> locks = l ; } }
Register with the internal data structures that we are about to lock a
 semaphore/mutex.
Register with the internal data structures that we are about to lock a semaphore / mutex .
/* If we haven't seen the lock before, assume that it comes after every
     * other lock currently in the partial ordering. Note that this is the only
     * place locks are added to ordering. In other words, if you want to
     * explicitly define a partial ordering you may want to immediately lock
     * all your semaphores/mutexes on creation in the order you want to define,
     * unlock them, and only then commence using them in your parallel code.
     */ /* Modify the check below to <= 0 if you want to prevent your semaphores
     * mutexes from being re-entrant (nested). Note that we don't do any
     * locking on ->locks even though we do all manner of operations on it.
     * This is not a mistake. We will only ever be reading/modifying ->locks
     * from the thread associated with this item in thread_list and hence there
     * will never be any concurrent access.
     */ /* Unreachable. */ /* Allow the lock to succeed. */