michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ** PR Atomic operations michael@0: */ michael@0: michael@0: michael@0: #include "pratom.h" michael@0: #include "primpl.h" michael@0: michael@0: #include michael@0: michael@0: /* michael@0: * The following is a fallback implementation that emulates michael@0: * atomic operations for platforms without atomic operations. michael@0: * If a platform has atomic operations, it should define the michael@0: * macro _PR_HAVE_ATOMIC_OPS, and the following will not be michael@0: * compiled in. michael@0: */ michael@0: michael@0: #if !defined(_PR_HAVE_ATOMIC_OPS) michael@0: michael@0: #if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS) michael@0: /* michael@0: * PR_AtomicDecrement() is used in NSPR's thread-specific data michael@0: * destructor. Because thread-specific data destructors may be michael@0: * invoked after a PR_Cleanup() call, we need an implementation michael@0: * of the atomic routines that doesn't need NSPR to be initialized. michael@0: */ michael@0: michael@0: /* michael@0: * We use a set of locks for all the emulated atomic operations. michael@0: * By hashing on the address of the integer to be locked the michael@0: * contention between multiple threads should be lessened. michael@0: * michael@0: * The number of atomic locks can be set by the environment variable michael@0: * NSPR_ATOMIC_HASH_LOCKS michael@0: */ michael@0: michael@0: /* michael@0: * lock counts should be a power of 2 michael@0: */ michael@0: #define DEFAULT_ATOMIC_LOCKS 16 /* should be in sync with the number of initializers michael@0: below */ michael@0: #define MAX_ATOMIC_LOCKS (4 * 1024) michael@0: michael@0: static pthread_mutex_t static_atomic_locks[DEFAULT_ATOMIC_LOCKS] = { michael@0: PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, michael@0: PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, michael@0: PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, michael@0: PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, michael@0: PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, michael@0: PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, michael@0: PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, michael@0: PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER }; michael@0: michael@0: #ifdef DEBUG michael@0: static PRInt32 static_hash_lock_counts[DEFAULT_ATOMIC_LOCKS]; michael@0: static PRInt32 *hash_lock_counts = static_hash_lock_counts; michael@0: #endif michael@0: michael@0: static PRUint32 num_atomic_locks = DEFAULT_ATOMIC_LOCKS; michael@0: static pthread_mutex_t *atomic_locks = static_atomic_locks; michael@0: static PRUint32 atomic_hash_mask = DEFAULT_ATOMIC_LOCKS - 1; michael@0: michael@0: #define _PR_HASH_FOR_LOCK(ptr) \ michael@0: ((PRUint32) (((PRUptrdiff) (ptr) >> 2) ^ \ michael@0: ((PRUptrdiff) (ptr) >> 8)) & \ michael@0: atomic_hash_mask) michael@0: michael@0: void _PR_MD_INIT_ATOMIC() michael@0: { michael@0: char *eval; michael@0: int index; michael@0: michael@0: michael@0: PR_ASSERT(PR_FloorLog2(MAX_ATOMIC_LOCKS) == michael@0: PR_CeilingLog2(MAX_ATOMIC_LOCKS)); michael@0: michael@0: PR_ASSERT(PR_FloorLog2(DEFAULT_ATOMIC_LOCKS) == michael@0: PR_CeilingLog2(DEFAULT_ATOMIC_LOCKS)); michael@0: michael@0: if (((eval = getenv("NSPR_ATOMIC_HASH_LOCKS")) != NULL) && michael@0: ((num_atomic_locks = atoi(eval)) != DEFAULT_ATOMIC_LOCKS)) { michael@0: michael@0: if (num_atomic_locks > MAX_ATOMIC_LOCKS) michael@0: num_atomic_locks = MAX_ATOMIC_LOCKS; michael@0: else if (num_atomic_locks < 1) michael@0: num_atomic_locks = 1; michael@0: else { michael@0: num_atomic_locks = PR_FloorLog2(num_atomic_locks); michael@0: num_atomic_locks = 1L << num_atomic_locks; michael@0: } michael@0: atomic_locks = (pthread_mutex_t *) PR_Malloc(sizeof(pthread_mutex_t) * michael@0: num_atomic_locks); michael@0: if (atomic_locks) { michael@0: for (index = 0; index < num_atomic_locks; index++) { michael@0: if (pthread_mutex_init(&atomic_locks[index], NULL)) { michael@0: PR_DELETE(atomic_locks); michael@0: atomic_locks = NULL; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: #ifdef DEBUG michael@0: if (atomic_locks) { michael@0: hash_lock_counts = PR_CALLOC(num_atomic_locks * sizeof(PRInt32)); michael@0: if (hash_lock_counts == NULL) { michael@0: PR_DELETE(atomic_locks); michael@0: atomic_locks = NULL; michael@0: } michael@0: } michael@0: #endif michael@0: if (atomic_locks == NULL) { michael@0: /* michael@0: * Use statically allocated locks michael@0: */ michael@0: atomic_locks = static_atomic_locks; michael@0: num_atomic_locks = DEFAULT_ATOMIC_LOCKS; michael@0: #ifdef DEBUG michael@0: hash_lock_counts = static_hash_lock_counts; michael@0: #endif michael@0: } michael@0: atomic_hash_mask = num_atomic_locks - 1; michael@0: } michael@0: PR_ASSERT(PR_FloorLog2(num_atomic_locks) == michael@0: PR_CeilingLog2(num_atomic_locks)); michael@0: } michael@0: michael@0: PRInt32 michael@0: _PR_MD_ATOMIC_INCREMENT(PRInt32 *val) michael@0: { michael@0: PRInt32 rv; michael@0: PRInt32 idx = _PR_HASH_FOR_LOCK(val); michael@0: michael@0: pthread_mutex_lock(&atomic_locks[idx]); michael@0: rv = ++(*val); michael@0: #ifdef DEBUG michael@0: hash_lock_counts[idx]++; michael@0: #endif michael@0: pthread_mutex_unlock(&atomic_locks[idx]); michael@0: return rv; michael@0: } michael@0: michael@0: PRInt32 michael@0: _PR_MD_ATOMIC_ADD(PRInt32 *ptr, PRInt32 val) michael@0: { michael@0: PRInt32 rv; michael@0: PRInt32 idx = _PR_HASH_FOR_LOCK(ptr); michael@0: michael@0: pthread_mutex_lock(&atomic_locks[idx]); michael@0: rv = ((*ptr) += val); michael@0: #ifdef DEBUG michael@0: hash_lock_counts[idx]++; michael@0: #endif michael@0: pthread_mutex_unlock(&atomic_locks[idx]); michael@0: return rv; michael@0: } michael@0: michael@0: PRInt32 michael@0: _PR_MD_ATOMIC_DECREMENT(PRInt32 *val) michael@0: { michael@0: PRInt32 rv; michael@0: PRInt32 idx = _PR_HASH_FOR_LOCK(val); michael@0: michael@0: pthread_mutex_lock(&atomic_locks[idx]); michael@0: rv = --(*val); michael@0: #ifdef DEBUG michael@0: hash_lock_counts[idx]++; michael@0: #endif michael@0: pthread_mutex_unlock(&atomic_locks[idx]); michael@0: return rv; michael@0: } michael@0: michael@0: PRInt32 michael@0: _PR_MD_ATOMIC_SET(PRInt32 *val, PRInt32 newval) michael@0: { michael@0: PRInt32 rv; michael@0: PRInt32 idx = _PR_HASH_FOR_LOCK(val); michael@0: michael@0: pthread_mutex_lock(&atomic_locks[idx]); michael@0: rv = *val; michael@0: *val = newval; michael@0: #ifdef DEBUG michael@0: hash_lock_counts[idx]++; michael@0: #endif michael@0: pthread_mutex_unlock(&atomic_locks[idx]); michael@0: return rv; michael@0: } michael@0: #else /* _PR_PTHREADS && !_PR_DCETHREADS */ michael@0: /* michael@0: * We use a single lock for all the emulated atomic operations. michael@0: * The lock contention should be acceptable. michael@0: */ michael@0: static PRLock *atomic_lock = NULL; michael@0: void _PR_MD_INIT_ATOMIC(void) michael@0: { michael@0: if (atomic_lock == NULL) { michael@0: atomic_lock = PR_NewLock(); michael@0: } michael@0: } michael@0: michael@0: PRInt32 michael@0: _PR_MD_ATOMIC_INCREMENT(PRInt32 *val) michael@0: { michael@0: PRInt32 rv; michael@0: michael@0: if (!_pr_initialized) { michael@0: _PR_ImplicitInitialization(); michael@0: } michael@0: PR_Lock(atomic_lock); michael@0: rv = ++(*val); michael@0: PR_Unlock(atomic_lock); michael@0: return rv; michael@0: } michael@0: michael@0: PRInt32 michael@0: _PR_MD_ATOMIC_ADD(PRInt32 *ptr, PRInt32 val) michael@0: { michael@0: PRInt32 rv; michael@0: michael@0: if (!_pr_initialized) { michael@0: _PR_ImplicitInitialization(); michael@0: } michael@0: PR_Lock(atomic_lock); michael@0: rv = ((*ptr) += val); michael@0: PR_Unlock(atomic_lock); michael@0: return rv; michael@0: } michael@0: michael@0: PRInt32 michael@0: _PR_MD_ATOMIC_DECREMENT(PRInt32 *val) michael@0: { michael@0: PRInt32 rv; michael@0: michael@0: if (!_pr_initialized) { michael@0: _PR_ImplicitInitialization(); michael@0: } michael@0: PR_Lock(atomic_lock); michael@0: rv = --(*val); michael@0: PR_Unlock(atomic_lock); michael@0: return rv; michael@0: } michael@0: michael@0: PRInt32 michael@0: _PR_MD_ATOMIC_SET(PRInt32 *val, PRInt32 newval) michael@0: { michael@0: PRInt32 rv; michael@0: michael@0: if (!_pr_initialized) { michael@0: _PR_ImplicitInitialization(); michael@0: } michael@0: PR_Lock(atomic_lock); michael@0: rv = *val; michael@0: *val = newval; michael@0: PR_Unlock(atomic_lock); michael@0: return rv; michael@0: } michael@0: #endif /* _PR_PTHREADS && !_PR_DCETHREADS */ michael@0: michael@0: #endif /* !_PR_HAVE_ATOMIC_OPS */ michael@0: michael@0: void _PR_InitAtomic(void) michael@0: { michael@0: _PR_MD_INIT_ATOMIC(); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRInt32) michael@0: PR_AtomicIncrement(PRInt32 *val) michael@0: { michael@0: return _PR_MD_ATOMIC_INCREMENT(val); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRInt32) michael@0: PR_AtomicDecrement(PRInt32 *val) michael@0: { michael@0: return _PR_MD_ATOMIC_DECREMENT(val); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRInt32) michael@0: PR_AtomicSet(PRInt32 *val, PRInt32 newval) michael@0: { michael@0: return _PR_MD_ATOMIC_SET(val, newval); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRInt32) michael@0: PR_AtomicAdd(PRInt32 *ptr, PRInt32 val) michael@0: { michael@0: return _PR_MD_ATOMIC_ADD(ptr, val); michael@0: } michael@0: /* michael@0: * For platforms, which don't support the CAS (compare-and-swap) instruction michael@0: * (or an equivalent), the stack operations are implemented by use of PRLock michael@0: */ michael@0: michael@0: PR_IMPLEMENT(PRStack *) michael@0: PR_CreateStack(const char *stack_name) michael@0: { michael@0: PRStack *stack; michael@0: michael@0: if (!_pr_initialized) { michael@0: _PR_ImplicitInitialization(); michael@0: } michael@0: michael@0: if ((stack = PR_NEW(PRStack)) == NULL) { michael@0: return NULL; michael@0: } michael@0: if (stack_name) { michael@0: stack->prstk_name = (char *) PR_Malloc(strlen(stack_name) + 1); michael@0: if (stack->prstk_name == NULL) { michael@0: PR_DELETE(stack); michael@0: return NULL; michael@0: } michael@0: strcpy(stack->prstk_name, stack_name); michael@0: } else michael@0: stack->prstk_name = NULL; michael@0: michael@0: #ifndef _PR_HAVE_ATOMIC_CAS michael@0: stack->prstk_lock = PR_NewLock(); michael@0: if (stack->prstk_lock == NULL) { michael@0: PR_Free(stack->prstk_name); michael@0: PR_DELETE(stack); michael@0: return NULL; michael@0: } michael@0: #endif /* !_PR_HAVE_ATOMIC_CAS */ michael@0: michael@0: stack->prstk_head.prstk_elem_next = NULL; michael@0: michael@0: return stack; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) michael@0: PR_DestroyStack(PRStack *stack) michael@0: { michael@0: if (stack->prstk_head.prstk_elem_next != NULL) { michael@0: PR_SetError(PR_INVALID_STATE_ERROR, 0); michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: if (stack->prstk_name) michael@0: PR_Free(stack->prstk_name); michael@0: #ifndef _PR_HAVE_ATOMIC_CAS michael@0: PR_DestroyLock(stack->prstk_lock); michael@0: #endif /* !_PR_HAVE_ATOMIC_CAS */ michael@0: PR_DELETE(stack); michael@0: michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: #ifndef _PR_HAVE_ATOMIC_CAS michael@0: michael@0: PR_IMPLEMENT(void) michael@0: PR_StackPush(PRStack *stack, PRStackElem *stack_elem) michael@0: { michael@0: PR_Lock(stack->prstk_lock); michael@0: stack_elem->prstk_elem_next = stack->prstk_head.prstk_elem_next; michael@0: stack->prstk_head.prstk_elem_next = stack_elem; michael@0: PR_Unlock(stack->prstk_lock); michael@0: return; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStackElem *) michael@0: PR_StackPop(PRStack *stack) michael@0: { michael@0: PRStackElem *element; michael@0: michael@0: PR_Lock(stack->prstk_lock); michael@0: element = stack->prstk_head.prstk_elem_next; michael@0: if (element != NULL) { michael@0: stack->prstk_head.prstk_elem_next = element->prstk_elem_next; michael@0: element->prstk_elem_next = NULL; /* debugging aid */ michael@0: } michael@0: PR_Unlock(stack->prstk_lock); michael@0: return element; michael@0: } michael@0: #endif /* !_PR_HAVE_ATOMIC_CAS */