andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** Thread Private Data andre@0: ** andre@0: ** There is an aribitrary limit on the number of keys that will be allocated andre@0: ** by the runtime. It's largish, so it is intended to be a sanity check, not andre@0: ** an impediment. andre@0: ** andre@0: ** There is a counter, initialized to zero and incremented every time a andre@0: ** client asks for a new key, that holds the high water mark for keys. All andre@0: ** threads logically have the same high water mark and are permitted to andre@0: ** ask for TPD up to that key value. andre@0: ** andre@0: ** The vector to hold the TPD are allocated when PR_SetThreadPrivate() is andre@0: ** called. The size of the vector will be some value greater than or equal andre@0: ** to the current high water mark. Each thread has its own TPD length and andre@0: ** vector. andre@0: ** andre@0: ** Threads that get private data for keys they have not set (or perhaps andre@0: ** don't even exist for that thread) get a NULL return. If the key is andre@0: ** beyond the high water mark, an error will be returned. andre@0: */ andre@0: andre@0: /* andre@0: ** As of this time, BeOS has its own TPD implementation. Integrating andre@0: ** this standard one is a TODO for anyone with a bit of spare time on andre@0: ** their hand. For now, we just #ifdef out this whole file and use andre@0: ** the routines in pr/src/btthreads/ andre@0: */ andre@0: andre@0: #ifndef XP_BEOS andre@0: andre@0: #include "primpl.h" andre@0: andre@0: #include andre@0: andre@0: #if defined(WIN95) andre@0: /* andre@0: ** Some local variables report warnings on Win95 because the code paths andre@0: ** using them are conditioned on HAVE_CUSTOME_USER_THREADS. andre@0: ** The pragma suppresses the warning. andre@0: ** andre@0: */ andre@0: #pragma warning(disable : 4101) andre@0: #endif andre@0: andre@0: #define _PR_TPD_LIMIT 128 /* arbitary limit on the TPD slots */ andre@0: static PRInt32 _pr_tpd_length = 0; /* current length of destructor vector */ andre@0: static PRInt32 _pr_tpd_highwater = 0; /* next TPD key to be assigned */ andre@0: static PRThreadPrivateDTOR *_pr_tpd_destructors = NULL; andre@0: /* the destructors are associated with andre@0: the keys, therefore asserting that andre@0: the TPD key depicts the data's 'type' */ andre@0: andre@0: /* andre@0: ** Initialize the thread private data manipulation andre@0: */ andre@0: void _PR_InitTPD(void) andre@0: { andre@0: _pr_tpd_destructors = (PRThreadPrivateDTOR*) andre@0: PR_CALLOC(_PR_TPD_LIMIT * sizeof(PRThreadPrivateDTOR*)); andre@0: PR_ASSERT(NULL != _pr_tpd_destructors); andre@0: _pr_tpd_length = _PR_TPD_LIMIT; andre@0: } andre@0: andre@0: /* andre@0: ** Clean up the thread private data manipulation andre@0: */ andre@0: void _PR_CleanupTPD(void) andre@0: { andre@0: } /* _PR_CleanupTPD */ andre@0: andre@0: /* andre@0: ** This routine returns a new index for per-thread-private data table. andre@0: ** The index is visible to all threads within a process. This index can andre@0: ** be used with the PR_SetThreadPrivate() and PR_GetThreadPrivate() routines andre@0: ** to save and retrieve data associated with the index for a thread. andre@0: ** andre@0: ** The index independently maintains specific values for each binding thread. andre@0: ** A thread can only get access to its own thread-specific-data. andre@0: ** andre@0: ** Upon a new index return the value associated with the index for all threads andre@0: ** is NULL, and upon thread creation the value associated with all indices for andre@0: ** that thread is NULL. andre@0: ** andre@0: ** "dtor" is the destructor function to invoke when the private andre@0: ** data is set or destroyed andre@0: ** andre@0: ** Returns PR_FAILURE if the total number of indices will exceed the maximun andre@0: ** allowed. andre@0: */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_NewThreadPrivateIndex( andre@0: PRUintn *newIndex, PRThreadPrivateDTOR dtor) andre@0: { andre@0: PRStatus rv; andre@0: PRInt32 index; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: PR_ASSERT(NULL != newIndex); andre@0: PR_ASSERT(NULL != _pr_tpd_destructors); andre@0: andre@0: index = PR_ATOMIC_INCREMENT(&_pr_tpd_highwater) - 1; /* allocate index */ andre@0: if (_PR_TPD_LIMIT <= index) andre@0: { andre@0: PR_SetError(PR_TPD_RANGE_ERROR, 0); andre@0: rv = PR_FAILURE; /* that's just wrong */ andre@0: } andre@0: else andre@0: { andre@0: _pr_tpd_destructors[index] = dtor; /* record destructor @index */ andre@0: *newIndex = (PRUintn)index; /* copy into client's location */ andre@0: rv = PR_SUCCESS; /* that's okay */ andre@0: } andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: ** Define some per-thread-private data. andre@0: ** "index" is an index into the per-thread private data table andre@0: ** "priv" is the per-thread-private data andre@0: ** andre@0: ** If the per-thread private data table has a previously registered andre@0: ** destructor function and a non-NULL per-thread-private data value, andre@0: ** the destructor function is invoked. andre@0: ** andre@0: ** This can return PR_FAILURE if index is invalid (ie., beyond the current andre@0: ** high water mark) or memory is insufficient to allocate an exanded vector. andre@0: */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_SetThreadPrivate(PRUintn index, void *priv) andre@0: { andre@0: PRThread *self = PR_GetCurrentThread(); andre@0: andre@0: /* andre@0: ** The index being set might not have a sufficient vector in this andre@0: ** thread. But if the index has been allocated, it's okay to go andre@0: ** ahead and extend this one now. andre@0: */ andre@0: if ((index >= _PR_TPD_LIMIT) || (index >= _pr_tpd_highwater)) andre@0: { andre@0: PR_SetError(PR_TPD_RANGE_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: PR_ASSERT(((NULL == self->privateData) && (0 == self->tpdLength)) andre@0: || ((NULL != self->privateData) && (0 != self->tpdLength))); andre@0: andre@0: if ((NULL == self->privateData) || (self->tpdLength <= index)) andre@0: { andre@0: void *extension = PR_CALLOC(_pr_tpd_length * sizeof(void*)); andre@0: if (NULL == extension) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: if (self->privateData) { andre@0: (void)memcpy( andre@0: extension, self->privateData, andre@0: self->tpdLength * sizeof(void*)); andre@0: PR_DELETE(self->privateData); andre@0: } andre@0: self->tpdLength = _pr_tpd_length; andre@0: self->privateData = (void**)extension; andre@0: } andre@0: /* andre@0: ** There wasn't much chance of having to call the destructor andre@0: ** unless the slot already existed. andre@0: */ andre@0: else if (self->privateData[index] && _pr_tpd_destructors[index]) andre@0: { andre@0: void *data = self->privateData[index]; andre@0: self->privateData[index] = NULL; andre@0: (*_pr_tpd_destructors[index])(data); andre@0: } andre@0: andre@0: PR_ASSERT(index < self->tpdLength); andre@0: self->privateData[index] = priv; andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: /* andre@0: ** Recover the per-thread-private data for the current thread. "index" is andre@0: ** the index into the per-thread private data table. andre@0: ** andre@0: ** The returned value may be NULL which is indistinguishable from an error andre@0: ** condition. andre@0: ** andre@0: */ andre@0: andre@0: PR_IMPLEMENT(void*) PR_GetThreadPrivate(PRUintn index) andre@0: { andre@0: PRThread *self = PR_GetCurrentThread(); andre@0: void *tpd = ((NULL == self->privateData) || (index >= self->tpdLength)) ? andre@0: NULL : self->privateData[index]; andre@0: andre@0: return tpd; andre@0: } andre@0: andre@0: /* andre@0: ** Destroy the thread's private data, if any exists. This is called at andre@0: ** thread termination time only. There should be no threading issues andre@0: ** since this is being called by the thread itself. andre@0: */ andre@0: void _PR_DestroyThreadPrivate(PRThread* self) andre@0: { andre@0: #define _PR_TPD_DESTRUCTOR_ITERATIONS 4 andre@0: andre@0: if (NULL != self->privateData) /* we have some */ andre@0: { andre@0: PRBool clean; andre@0: PRUint32 index; andre@0: PRInt32 passes = _PR_TPD_DESTRUCTOR_ITERATIONS; andre@0: PR_ASSERT(0 != self->tpdLength); andre@0: do andre@0: { andre@0: clean = PR_TRUE; andre@0: for (index = 0; index < self->tpdLength; ++index) andre@0: { andre@0: void *priv = self->privateData[index]; /* extract */ andre@0: if (NULL != priv) /* we have data at this index */ andre@0: { andre@0: if (NULL != _pr_tpd_destructors[index]) andre@0: { andre@0: self->privateData[index] = NULL; /* precondition */ andre@0: (*_pr_tpd_destructors[index])(priv); /* destroy */ andre@0: clean = PR_FALSE; /* unknown side effects */ andre@0: } andre@0: } andre@0: } andre@0: } while ((--passes > 0) && !clean); /* limit # of passes */ andre@0: /* andre@0: ** We give up after a fixed number of passes. Any non-NULL andre@0: ** thread-private data value with a registered destructor andre@0: ** function is not destroyed. andre@0: */ andre@0: memset(self->privateData, 0, self->tpdLength * sizeof(void*)); andre@0: } andre@0: } /* _PR_DestroyThreadPrivate */ andre@0: andre@0: #endif /* !XP_BEOS */