Blame nss/lib/util/nssrwlk.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
Packit 40b132
#include "nssrwlk.h"
Packit 40b132
#include "nspr.h"
Packit 40b132
Packit 40b132
PR_BEGIN_EXTERN_C
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Reader-writer lock
Packit 40b132
 */
Packit 40b132
struct nssRWLockStr {
Packit 40b132
    PZLock *        rw_lock;
Packit 40b132
    char   *        rw_name;            /* lock name                    */
Packit 40b132
    PRUint32        rw_rank;            /* rank of the lock             */
Packit 40b132
    PRInt32         rw_writer_locks;    /* ==  0, if unlocked           */
Packit 40b132
    PRInt32         rw_reader_locks;    /* ==  0, if unlocked           */
Packit 40b132
                                        /* > 0  , # of read locks       */
Packit 40b132
    PRUint32        rw_waiting_readers; /* number of waiting readers    */
Packit 40b132
    PRUint32        rw_waiting_writers; /* number of waiting writers    */
Packit 40b132
    PZCondVar *     rw_reader_waitq;    /* cvar for readers             */
Packit 40b132
    PZCondVar *     rw_writer_waitq;    /* cvar for writers             */
Packit 40b132
    PRThread  *     rw_owner;           /* lock owner for write-lock    */
Packit 40b132
                                        /* Non-null if write lock held. */
Packit 40b132
};
Packit 40b132
Packit 40b132
PR_END_EXTERN_C
Packit 40b132
Packit 40b132
#include <string.h>
Packit 40b132
Packit 40b132
#ifdef DEBUG_RANK_ORDER
Packit 40b132
#define NSS_RWLOCK_RANK_ORDER_DEBUG /* enable deadlock detection using
Packit 40b132
                                       rank-order for locks
Packit 40b132
                                    */
Packit 40b132
#endif
Packit 40b132
Packit 40b132
#ifdef NSS_RWLOCK_RANK_ORDER_DEBUG
Packit 40b132
Packit 40b132
static PRUintn  nss_thread_rwlock_initialized;
Packit 40b132
static PRUintn  nss_thread_rwlock;               /* TPD key for lock stack */
Packit 40b132
static PRUintn  nss_thread_rwlock_alloc_failed;
Packit 40b132
Packit 40b132
#define _NSS_RWLOCK_RANK_ORDER_LIMIT 10
Packit 40b132
Packit 40b132
typedef struct thread_rwlock_stack {
Packit 40b132
    PRInt32     trs_index;                                  /* top of stack */
Packit 40b132
    NSSRWLock    *trs_stack[_NSS_RWLOCK_RANK_ORDER_LIMIT];  /* stack of lock
Packit 40b132
                                                               pointers */
Packit 40b132
} thread_rwlock_stack;
Packit 40b132
Packit 40b132
/* forward static declarations. */
Packit 40b132
static PRUint32 nssRWLock_GetThreadRank(PRThread *me);
Packit 40b132
static void     nssRWLock_SetThreadRank(PRThread *me, NSSRWLock *rwlock);
Packit 40b132
static void     nssRWLock_UnsetThreadRank(PRThread *me, NSSRWLock *rwlock);
Packit 40b132
static void     nssRWLock_ReleaseLockStack(void *lock_stack);
Packit 40b132
Packit 40b132
#endif
Packit 40b132
Packit 40b132
#define UNTIL(x) while(!(x))
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Reader/Writer Locks
Packit 40b132
 */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSSRWLock_New
Packit 40b132
 *      Create a reader-writer lock, with the given lock rank and lock name
Packit 40b132
 *
Packit 40b132
 */
Packit 40b132
Packit 40b132
NSSRWLock *
Packit 40b132
NSSRWLock_New(PRUint32 lock_rank, const char *lock_name)
Packit 40b132
{
Packit 40b132
    NSSRWLock *rwlock;
Packit 40b132
Packit 40b132
    rwlock = PR_NEWZAP(NSSRWLock);
Packit 40b132
    if (rwlock == NULL)
Packit 40b132
        return NULL;
Packit 40b132
Packit 40b132
    rwlock->rw_lock = PZ_NewLock(nssILockRWLock);
Packit 40b132
    if (rwlock->rw_lock == NULL) {
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
    rwlock->rw_reader_waitq = PZ_NewCondVar(rwlock->rw_lock);
Packit 40b132
    if (rwlock->rw_reader_waitq == NULL) {
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
    rwlock->rw_writer_waitq = PZ_NewCondVar(rwlock->rw_lock);
Packit 40b132
    if (rwlock->rw_writer_waitq == NULL) {
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
    if (lock_name != NULL) {
Packit 40b132
        rwlock->rw_name = (char*) PR_Malloc(strlen(lock_name) + 1);
Packit 40b132
        if (rwlock->rw_name == NULL) {
Packit 40b132
	    goto loser;
Packit 40b132
        }
Packit 40b132
        strcpy(rwlock->rw_name, lock_name);
Packit 40b132
    } else {
Packit 40b132
        rwlock->rw_name = NULL;
Packit 40b132
    }
Packit 40b132
    rwlock->rw_rank            = lock_rank;
Packit 40b132
    rwlock->rw_waiting_readers = 0;
Packit 40b132
    rwlock->rw_waiting_writers = 0;
Packit 40b132
    rwlock->rw_reader_locks    = 0;
Packit 40b132
    rwlock->rw_writer_locks    = 0;
Packit 40b132
Packit 40b132
    return rwlock;
Packit 40b132
Packit 40b132
loser:
Packit 40b132
    NSSRWLock_Destroy(rwlock);
Packit 40b132
    return(NULL);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
** Destroy the given RWLock "lock".
Packit 40b132
*/
Packit 40b132
void
Packit 40b132
NSSRWLock_Destroy(NSSRWLock *rwlock)
Packit 40b132
{
Packit 40b132
    PR_ASSERT(rwlock != NULL);
Packit 40b132
    PR_ASSERT(rwlock->rw_waiting_readers == 0);
Packit 40b132
Packit 40b132
    /* XXX Shouldn't we lock the PZLock before destroying this?? */
Packit 40b132
Packit 40b132
    if (rwlock->rw_name)
Packit 40b132
    	PR_Free(rwlock->rw_name);
Packit 40b132
    if (rwlock->rw_reader_waitq)
Packit 40b132
    	PZ_DestroyCondVar(rwlock->rw_reader_waitq);
Packit 40b132
    if (rwlock->rw_writer_waitq)
Packit 40b132
	PZ_DestroyCondVar(rwlock->rw_writer_waitq);
Packit 40b132
    if (rwlock->rw_lock)
Packit 40b132
	PZ_DestroyLock(rwlock->rw_lock);
Packit 40b132
    PR_DELETE(rwlock);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
** Read-lock the RWLock.
Packit 40b132
*/
Packit 40b132
void
Packit 40b132
NSSRWLock_LockRead(NSSRWLock *rwlock)
Packit 40b132
{
Packit 40b132
    PRThread *me = PR_GetCurrentThread();
Packit 40b132
Packit 40b132
    PZ_Lock(rwlock->rw_lock);
Packit 40b132
#ifdef NSS_RWLOCK_RANK_ORDER_DEBUG
Packit 40b132
Packit 40b132
    /*
Packit 40b132
     * assert that rank ordering is not violated; the rank of 'rwlock' should
Packit 40b132
     * be equal to or greater than the highest rank of all the locks held by
Packit 40b132
     * the thread.
Packit 40b132
     */
Packit 40b132
    PR_ASSERT((rwlock->rw_rank == NSS_RWLOCK_RANK_NONE) ||
Packit 40b132
              (rwlock->rw_rank >= nssRWLock_GetThreadRank(me)));
Packit 40b132
#endif
Packit 40b132
    /*
Packit 40b132
     * wait if write-locked or if a writer is waiting; preference for writers
Packit 40b132
     */
Packit 40b132
    UNTIL ( (rwlock->rw_owner == me) ||		  /* I own it, or        */
Packit 40b132
	   ((rwlock->rw_owner == NULL) &&	  /* no-one owns it, and */
Packit 40b132
	    (rwlock->rw_waiting_writers == 0))) { /* no-one is waiting to own */
Packit 40b132
Packit 40b132
	rwlock->rw_waiting_readers++;
Packit 40b132
	PZ_WaitCondVar(rwlock->rw_reader_waitq, PR_INTERVAL_NO_TIMEOUT);
Packit 40b132
	rwlock->rw_waiting_readers--;
Packit 40b132
    }
Packit 40b132
    rwlock->rw_reader_locks++; 		/* Increment read-lock count */
Packit 40b132
Packit 40b132
    PZ_Unlock(rwlock->rw_lock);
Packit 40b132
Packit 40b132
#ifdef NSS_RWLOCK_RANK_ORDER_DEBUG
Packit 40b132
    nssRWLock_SetThreadRank(me, rwlock);/* update thread's lock rank */
Packit 40b132
#endif
Packit 40b132
}
Packit 40b132
Packit 40b132
/* Unlock a Read lock held on this RW lock.
Packit 40b132
*/
Packit 40b132
void
Packit 40b132
NSSRWLock_UnlockRead(NSSRWLock *rwlock)
Packit 40b132
{
Packit 40b132
    PZ_Lock(rwlock->rw_lock);
Packit 40b132
Packit 40b132
    PR_ASSERT(rwlock->rw_reader_locks > 0); /* lock must be read locked */
Packit 40b132
Packit 40b132
    if ((  rwlock->rw_reader_locks  > 0)  &&	/* caller isn't screwey */
Packit 40b132
        (--rwlock->rw_reader_locks == 0)  &&	/* not read locked any more */
Packit 40b132
	(  rwlock->rw_owner        == NULL) &&	/* not write locked */
Packit 40b132
	(  rwlock->rw_waiting_writers > 0)) {	/* someone's waiting. */
Packit 40b132
Packit 40b132
	PZ_NotifyCondVar(rwlock->rw_writer_waitq); /* wake him up. */
Packit 40b132
    }
Packit 40b132
Packit 40b132
    PZ_Unlock(rwlock->rw_lock);
Packit 40b132
Packit 40b132
#ifdef NSS_RWLOCK_RANK_ORDER_DEBUG
Packit 40b132
    /*
Packit 40b132
     * update thread's lock rank
Packit 40b132
     */
Packit 40b132
    nssRWLock_UnsetThreadRank(me, rwlock);
Packit 40b132
#endif
Packit 40b132
    return;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
** Write-lock the RWLock.
Packit 40b132
*/
Packit 40b132
void
Packit 40b132
NSSRWLock_LockWrite(NSSRWLock *rwlock)
Packit 40b132
{
Packit 40b132
    PRThread *me = PR_GetCurrentThread();
Packit 40b132
Packit 40b132
    PZ_Lock(rwlock->rw_lock);
Packit 40b132
#ifdef NSS_RWLOCK_RANK_ORDER_DEBUG
Packit 40b132
    /*
Packit 40b132
     * assert that rank ordering is not violated; the rank of 'rwlock' should
Packit 40b132
     * be equal to or greater than the highest rank of all the locks held by
Packit 40b132
     * the thread.
Packit 40b132
     */
Packit 40b132
    PR_ASSERT((rwlock->rw_rank == NSS_RWLOCK_RANK_NONE) ||
Packit 40b132
                    (rwlock->rw_rank >= nssRWLock_GetThreadRank(me)));
Packit 40b132
#endif
Packit 40b132
    /*
Packit 40b132
     * wait if read locked or write locked.
Packit 40b132
     */
Packit 40b132
    PR_ASSERT(rwlock->rw_reader_locks >= 0);
Packit 40b132
    PR_ASSERT(me != NULL);
Packit 40b132
Packit 40b132
    UNTIL ( (rwlock->rw_owner == me) ||           /* I own write lock, or */
Packit 40b132
	   ((rwlock->rw_owner == NULL) &&	  /* no writer   and */
Packit 40b132
	    (rwlock->rw_reader_locks == 0))) {    /* no readers, either. */
Packit 40b132
Packit 40b132
        rwlock->rw_waiting_writers++;
Packit 40b132
        PZ_WaitCondVar(rwlock->rw_writer_waitq, PR_INTERVAL_NO_TIMEOUT);
Packit 40b132
        rwlock->rw_waiting_writers--;
Packit 40b132
	PR_ASSERT(rwlock->rw_reader_locks >= 0);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    PR_ASSERT(rwlock->rw_reader_locks == 0);
Packit 40b132
    /*
Packit 40b132
     * apply write lock
Packit 40b132
     */
Packit 40b132
    rwlock->rw_owner = me;
Packit 40b132
    rwlock->rw_writer_locks++; 		/* Increment write-lock count */
Packit 40b132
Packit 40b132
    PZ_Unlock(rwlock->rw_lock);
Packit 40b132
Packit 40b132
#ifdef NSS_RWLOCK_RANK_ORDER_DEBUG
Packit 40b132
    /*
Packit 40b132
     * update thread's lock rank
Packit 40b132
     */
Packit 40b132
    nssRWLock_SetThreadRank(me,rwlock);
Packit 40b132
#endif
Packit 40b132
}
Packit 40b132
Packit 40b132
/* Unlock a Read lock held on this RW lock.
Packit 40b132
*/
Packit 40b132
void
Packit 40b132
NSSRWLock_UnlockWrite(NSSRWLock *rwlock)
Packit 40b132
{
Packit 40b132
    PRThread *me = PR_GetCurrentThread();
Packit 40b132
Packit 40b132
    PZ_Lock(rwlock->rw_lock);
Packit 40b132
    PR_ASSERT(rwlock->rw_owner == me); /* lock must be write-locked by me.  */
Packit 40b132
    PR_ASSERT(rwlock->rw_writer_locks > 0); /* lock must be write locked */
Packit 40b132
Packit 40b132
    if (  rwlock->rw_owner        == me  &&	/* I own it, and            */
Packit 40b132
          rwlock->rw_writer_locks  > 0   &&	/* I own it, and            */
Packit 40b132
        --rwlock->rw_writer_locks == 0) {	/* I'm all done with it     */
Packit 40b132
Packit 40b132
	rwlock->rw_owner = NULL;		/* I don't own it any more. */
Packit 40b132
Packit 40b132
	/* Give preference to waiting writers. */
Packit 40b132
	if (rwlock->rw_waiting_writers > 0) {
Packit 40b132
	    if (rwlock->rw_reader_locks == 0)
Packit 40b132
		PZ_NotifyCondVar(rwlock->rw_writer_waitq);
Packit 40b132
	} else if (rwlock->rw_waiting_readers > 0) {
Packit 40b132
	    PZ_NotifyAllCondVar(rwlock->rw_reader_waitq);
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
    PZ_Unlock(rwlock->rw_lock);
Packit 40b132
Packit 40b132
#ifdef NSS_RWLOCK_RANK_ORDER_DEBUG
Packit 40b132
    /*
Packit 40b132
     * update thread's lock rank
Packit 40b132
     */
Packit 40b132
    nssRWLock_UnsetThreadRank(me, rwlock);
Packit 40b132
#endif
Packit 40b132
    return;
Packit 40b132
}
Packit 40b132
Packit 40b132
/* This is primarily for debugging, i.e. for inclusion in ASSERT calls. */
Packit 40b132
PRBool
Packit 40b132
NSSRWLock_HaveWriteLock(NSSRWLock *rwlock) {
Packit 40b132
    PRBool ownWriteLock;
Packit 40b132
    PRThread *me = PR_GetCurrentThread();
Packit 40b132
Packit 40b132
    /* This lock call isn't really necessary.
Packit 40b132
    ** If this thread is the owner, that fact cannot change during this call,
Packit 40b132
    ** because this thread is in this call.
Packit 40b132
    ** If this thread is NOT the owner, the owner could change, but it 
Packit 40b132
    ** could not become this thread.  
Packit 40b132
    */
Packit 40b132
#if UNNECESSARY
Packit 40b132
    PZ_Lock(rwlock->rw_lock);	
Packit 40b132
#endif
Packit 40b132
    ownWriteLock = (PRBool)(me == rwlock->rw_owner);
Packit 40b132
#if UNNECESSARY
Packit 40b132
    PZ_Unlock(rwlock->rw_lock);
Packit 40b132
#endif
Packit 40b132
    return ownWriteLock;
Packit 40b132
}
Packit 40b132
Packit 40b132
#ifdef NSS_RWLOCK_RANK_ORDER_DEBUG
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * nssRWLock_SetThreadRank
Packit 40b132
 *      Set a thread's lock rank, which is the highest of the ranks of all
Packit 40b132
 *      the locks held by the thread. Pointers to the locks are added to a
Packit 40b132
 *      per-thread list, which is anchored off a thread-private data key.
Packit 40b132
 */
Packit 40b132
Packit 40b132
static void
Packit 40b132
nssRWLock_SetThreadRank(PRThread *me, NSSRWLock *rwlock)
Packit 40b132
{
Packit 40b132
    thread_rwlock_stack *lock_stack;
Packit 40b132
    PRStatus rv;
Packit 40b132
Packit 40b132
    /*
Packit 40b132
     * allocated thread-private-data for rwlock list, if not already allocated
Packit 40b132
     */
Packit 40b132
    if (!nss_thread_rwlock_initialized) {
Packit 40b132
        /*
Packit 40b132
         * allocate tpd, only if not failed already
Packit 40b132
         */
Packit 40b132
        if (!nss_thread_rwlock_alloc_failed) {
Packit 40b132
            if (PR_NewThreadPrivateIndex(&nss_thread_rwlock,
Packit 40b132
                                        nssRWLock_ReleaseLockStack)
Packit 40b132
                                                == PR_FAILURE) {
Packit 40b132
                nss_thread_rwlock_alloc_failed = 1;
Packit 40b132
                return;
Packit 40b132
            }
Packit 40b132
        } else
Packit 40b132
            return;
Packit 40b132
    }
Packit 40b132
    /*
Packit 40b132
     * allocate a lock stack
Packit 40b132
     */
Packit 40b132
    if ((lock_stack = PR_GetThreadPrivate(nss_thread_rwlock)) == NULL) {
Packit 40b132
        lock_stack = (thread_rwlock_stack *)
Packit 40b132
                        PR_CALLOC(1 * sizeof(thread_rwlock_stack));
Packit 40b132
        if (lock_stack) {
Packit 40b132
            rv = PR_SetThreadPrivate(nss_thread_rwlock, lock_stack);
Packit 40b132
            if (rv == PR_FAILURE) {
Packit 40b132
                PR_DELETE(lock_stack);
Packit 40b132
                nss_thread_rwlock_alloc_failed = 1;
Packit 40b132
                return;
Packit 40b132
            }
Packit 40b132
        } else {
Packit 40b132
            nss_thread_rwlock_alloc_failed = 1;
Packit 40b132
            return;
Packit 40b132
        }
Packit 40b132
    }
Packit 40b132
    /*
Packit 40b132
     * add rwlock to lock stack, if limit is not exceeded
Packit 40b132
     */
Packit 40b132
    if (lock_stack) {
Packit 40b132
        if (lock_stack->trs_index < _NSS_RWLOCK_RANK_ORDER_LIMIT)
Packit 40b132
            lock_stack->trs_stack[lock_stack->trs_index++] = rwlock;
Packit 40b132
    }
Packit 40b132
    nss_thread_rwlock_initialized = 1;
Packit 40b132
}
Packit 40b132
Packit 40b132
static void
Packit 40b132
nssRWLock_ReleaseLockStack(void *lock_stack)
Packit 40b132
{
Packit 40b132
    PR_ASSERT(lock_stack);
Packit 40b132
    PR_DELETE(lock_stack);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * nssRWLock_GetThreadRank
Packit 40b132
 *
Packit 40b132
 *      return thread's lock rank. If thread-private-data for the lock
Packit 40b132
 *      stack is not allocated, return NSS_RWLOCK_RANK_NONE.
Packit 40b132
 */
Packit 40b132
Packit 40b132
static PRUint32
Packit 40b132
nssRWLock_GetThreadRank(PRThread *me)
Packit 40b132
{
Packit 40b132
    thread_rwlock_stack *lock_stack;
Packit 40b132
Packit 40b132
    if (nss_thread_rwlock_initialized) {
Packit 40b132
        if ((lock_stack = PR_GetThreadPrivate(nss_thread_rwlock)) == NULL)
Packit 40b132
            return (NSS_RWLOCK_RANK_NONE);
Packit 40b132
        else
Packit 40b132
            return(lock_stack->trs_stack[lock_stack->trs_index - 1]->rw_rank);
Packit 40b132
Packit 40b132
    } else
Packit 40b132
            return (NSS_RWLOCK_RANK_NONE);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * nssRWLock_UnsetThreadRank
Packit 40b132
 *
Packit 40b132
 *      remove the rwlock from the lock stack. Since locks may not be
Packit 40b132
 *      unlocked in a FIFO order, the entire lock stack is searched.
Packit 40b132
 */
Packit 40b132
Packit 40b132
static void
Packit 40b132
nssRWLock_UnsetThreadRank(PRThread *me, NSSRWLock *rwlock)
Packit 40b132
{
Packit 40b132
    thread_rwlock_stack *lock_stack;
Packit 40b132
    int new_index = 0, index, done = 0;
Packit 40b132
Packit 40b132
    if (!nss_thread_rwlock_initialized)
Packit 40b132
        return;
Packit 40b132
Packit 40b132
    lock_stack = PR_GetThreadPrivate(nss_thread_rwlock);
Packit 40b132
Packit 40b132
    PR_ASSERT(lock_stack != NULL);
Packit 40b132
Packit 40b132
    index = lock_stack->trs_index - 1;
Packit 40b132
    while (index-- >= 0) {
Packit 40b132
        if ((lock_stack->trs_stack[index] == rwlock) && !done)  {
Packit 40b132
            /*
Packit 40b132
             * reset the slot for rwlock
Packit 40b132
             */
Packit 40b132
            lock_stack->trs_stack[index] = NULL;
Packit 40b132
            done = 1;
Packit 40b132
        }
Packit 40b132
        /*
Packit 40b132
         * search for the lowest-numbered empty slot, above which there are
Packit 40b132
         * no non-empty slots
Packit 40b132
         */
Packit 40b132
        if ((lock_stack->trs_stack[index] != NULL) && !new_index)
Packit 40b132
            new_index = index + 1;
Packit 40b132
        if (done && new_index)
Packit 40b132
            break;
Packit 40b132
    }
Packit 40b132
    /*
Packit 40b132
     * set top of stack to highest numbered empty slot
Packit 40b132
     */
Packit 40b132
    lock_stack->trs_index = new_index;
Packit 40b132
Packit 40b132
}
Packit 40b132
Packit 40b132
#endif  /* NSS_RWLOCK_RANK_ORDER_DEBUG */