24 #ifndef _STLP_INTERNAL_THREADS_H 25 #define _STLP_INTERNAL_THREADS_H 32 #ifndef _STLP_INTERNAL_CSTDDEF 36 #ifndef _STLP_INTERNAL_CSTDLIB 41 #if defined (__sun) || (defined (__GNUC__) && defined(__APPLE__)) 42 # define _STLP_MUTEX_INITIALIZER 58 #if defined (_STLP_THREADS) 60 # if defined (_STLP_SGI_THREADS) 64 # if !defined(__add_and_fetch) && \ 65 (__mips < 3 || !(defined (_ABIN32) || defined(_ABI64))) 66 # define __add_and_fetch(__l,__v) add_then_test((unsigned long*)__l,__v) 67 # define __test_and_set(__l,__v) test_and_set(__l,__v) 70 # if __mips < 3 || !(defined (_ABIN32) || defined(_ABI64)) 71 # define _STLP_ATOMIC_EXCHANGE(__p, __q) test_and_set(__p, __q) 73 # define _STLP_ATOMIC_EXCHANGE(__p, __q) __test_and_set((unsigned long*)__p, (unsigned long)__q) 76 # define _STLP_ATOMIC_INCREMENT(__x) __add_and_fetch(__x, 1) 77 # define _STLP_ATOMIC_DECREMENT(__x) __add_and_fetch(__x, (size_t) -1) 80 # elif defined (_STLP_PTHREADS) 83 # if !defined (_STLP_USE_PTHREAD_SPINLOCK) 84 # if defined (PTHREAD_MUTEX_INITIALIZER) && !defined (_STLP_MUTEX_INITIALIZER) && defined (_REENTRANT) 85 # define _STLP_MUTEX_INITIALIZER = { PTHREAD_MUTEX_INITIALIZER } 88 # if defined (_DECTHREADS_) && (defined (_PTHREAD_USE_D4) || defined (__hpux)) && !defined (_CMA_SUPPRESS_EXTERNALS_) 89 # define _STLP_PTHREAD_ATTR_DEFAULT pthread_mutexattr_default 91 # define _STLP_PTHREAD_ATTR_DEFAULT 0 94 # if defined (__OpenBSD__) 95 # include <spinlock.h> 99 # if defined (__GNUC__) && defined (__i386__) 100 # if !defined (_STLP_ATOMIC_INCREMENT) 101 inline long _STLP_atomic_increment_gcc_x86(
long volatile*
p) {
104 (
"lock; xaddl %1, %0;" 110 # define _STLP_ATOMIC_INCREMENT(__x) (_STLP_atomic_increment_gcc_x86((long volatile*)__x)) 113 # if !defined (_STLP_ATOMIC_DECREMENT) 114 inline long _STLP_atomic_decrement_gcc_x86(
long volatile*
p) {
117 (
"lock; xaddl %1, %0;" 123 # define _STLP_ATOMIC_DECREMENT(__x) (_STLP_atomic_decrement_gcc_x86((long volatile*)__x)) 130 # elif defined (_STLP_WIN32THREADS) 132 # if !defined (_STLP_ATOMIC_INCREMENT) 133 # if !defined (_STLP_NEW_PLATFORM_SDK) 134 # define _STLP_ATOMIC_INCREMENT(__x) InterlockedIncrement(__CONST_CAST(long*, __x)) 135 # define _STLP_ATOMIC_DECREMENT(__x) InterlockedDecrement(__CONST_CAST(long*, __x)) 136 # define _STLP_ATOMIC_EXCHANGE(__x, __y) InterlockedExchange(__CONST_CAST(long*, __x), __y) 138 # define _STLP_ATOMIC_INCREMENT(__x) InterlockedIncrement(__x) 139 # define _STLP_ATOMIC_DECREMENT(__x) InterlockedDecrement(__x) 140 # define _STLP_ATOMIC_EXCHANGE(__x, __y) InterlockedExchange(__x, __y) 142 # define _STLP_ATOMIC_EXCHANGE_PTR(__x, __y) STLPInterlockedExchangePointer(__x, __y) 146 # elif defined (__DECC) || defined (__DECCXX) 148 # include <machine/builtins.h> 149 # define _STLP_ATOMIC_EXCHANGE __ATOMIC_EXCH_LONG 150 # define _STLP_ATOMIC_INCREMENT(__x) __ATOMIC_ADD_LONG(__x, 1) 151 # define _STLP_ATOMIC_DECREMENT(__x) __ATOMIC_ADD_LONG(__x, -1) 154 # elif defined (_STLP_SPARC_SOLARIS_THREADS) 159 # elif defined (_STLP_UITHREADS) 164 # ifndef _STLP_INTERNAL_CTIME 167 # if defined (_STLP_USE_NAMESPACES) && ! defined (_STLP_VENDOR_GLOBAL_CSTD) 171 # ifndef _STLP_INTERNAL_CSTDIO 174 # ifndef _STLP_INTERNAL_CWCHAR 179 # elif defined (_STLP_BETHREADS) 184 # define _STLP_MUTEX_INITIALIZER = { 0 } 187 # elif defined (_STLP_NWTHREADS) 189 # include <nwthread.h> 190 # include <nwsemaph.h> 193 # elif defined(_STLP_OS2THREADS) 195 # if defined (__GNUC__) 196 # define INCL_DOSSEMAPHORES 200 typedef unsigned long ULONG;
201 # if !defined (__HEV__) 206 typedef ULONG APIRET;
209 typedef const char*
PCSZ;
210 typedef ULONG BOOL32;
211 APIRET _System DosCreateMutexSem(
PCSZ pszName, PHEV phev,
ULONG flAttr, BOOL32 fState);
212 APIRET _System DosRequestMutexSem(HMTX hmtx,
ULONG ulTimeout);
213 APIRET _System DosReleaseMutexSem(HMTX hmtx);
214 APIRET _System DosCloseMutexSem(HMTX hmtx);
215 # define _STLP_MUTEX_INITIALIZER = { 0 } 227 # define _STLP_ATOMIC_INCREMENT(__x) ++(*__x) 228 # define _STLP_ATOMIC_DECREMENT(__x) --(*__x) 235 #if !defined (_STLP_MUTEX_INITIALIZER) 236 # if defined(_STLP_ATOMIC_EXCHANGE) 237 # define _STLP_MUTEX_INITIALIZER = { 0 } 238 # elif defined(_STLP_UITHREADS) 239 # define _STLP_MUTEX_INITIALIZER = { DEFAULTMUTEX } 241 # define _STLP_MUTEX_INITIALIZER 247 #if defined (_STLP_THREADS) && !defined (_STLP_USE_PTHREAD_SPINLOCK) 250 template <
int __inst>
251 struct _STLP_mutex_spin {
252 enum { __low_max = 30, __high_max = 1000 };
254 static unsigned __max;
257 static void _STLP_CALL _S_nsec_sleep(
int __log_nsec,
unsigned int& __iteration);
259 #endif // !_STLP_USE_PTHREAD_SPINLOCK 276 #if defined (_STLP_ATOMIC_EXCHANGE) || defined (_STLP_SGI_THREADS) 281 #if defined (_STLP_THREADS) 282 # if defined (_STLP_ATOMIC_EXCHANGE) 283 inline void _M_initialize() { _M_lock = 0; }
284 inline void _M_destroy() {}
286 void _M_acquire_lock() {
287 _STLP_mutex_spin<0>::_M_do_lock(&_M_lock);
290 inline void _M_release_lock() {
292 # if defined(_STLP_SGI_THREADS) && defined(__GNUC__) && __mips >= 3 295 # elif defined(_STLP_SGI_THREADS) && __mips >= 3 && \ 296 (defined (_ABIN32) || defined(_ABI64)) 297 __lock_release(__lock);
298 # elif defined (_STLP_SPARC_SOLARIS_THREADS) 299 # if defined (__WORD64) || defined (__arch64__) || defined (__sparcv9) || defined (__sparcv8plus) 300 asm(
"membar #StoreStore ; membar #LoadStore");
311 # elif defined (_STLP_PTHREADS) 312 # if defined (_STLP_USE_PTHREAD_SPINLOCK) 313 # if !defined (__OpenBSD__) 314 pthread_spinlock_t _M_lock;
315 inline void _M_initialize() { pthread_spin_init( &_M_lock, 0 ); }
316 inline void _M_destroy() { pthread_spin_destroy( &_M_lock ); }
328 inline void _M_acquire_lock() { pthread_spin_lock( &_M_lock ); }
329 inline void _M_release_lock() { pthread_spin_unlock( &_M_lock ); }
330 # else // __OpenBSD__ 332 inline void _M_initialize() { _SPINLOCK_INIT( &_M_lock ); }
333 inline void _M_destroy() { }
334 inline void _M_acquire_lock() {
_SPINLOCK( &_M_lock ); }
335 inline void _M_release_lock() {
_SPINUNLOCK( &_M_lock ); }
336 # endif // __OpenBSD__ 337 # else // !_STLP_USE_PTHREAD_SPINLOCK 338 pthread_mutex_t _M_lock;
339 inline void _M_initialize()
340 { pthread_mutex_init(&_M_lock,_STLP_PTHREAD_ATTR_DEFAULT); }
341 inline void _M_destroy()
342 { pthread_mutex_destroy(&_M_lock); }
343 inline void _M_acquire_lock() {
344 # if defined ( __hpux ) && ! defined (PTHREAD_MUTEX_INITIALIZER) 345 if (!_M_lock.field1) _M_initialize();
347 pthread_mutex_lock(&_M_lock);
349 inline void _M_release_lock() { pthread_mutex_unlock(&_M_lock); }
350 # endif // !_STLP_USE_PTHREAD_SPINLOCK 352 # elif defined (_STLP_UITHREADS) 354 inline void _M_initialize()
356 inline void _M_destroy()
357 { mutex_destroy(&_M_lock); }
358 inline void _M_acquire_lock() {
mutex_lock(&_M_lock); }
359 inline void _M_release_lock() {
mutex_unlock(&_M_lock); }
361 # elif defined (_STLP_OS2THREADS) 363 inline void _M_initialize() { DosCreateMutexSem(
NULL, &_M_lock, 0,
false); }
364 inline void _M_destroy() { DosCloseMutexSem(_M_lock); }
365 inline void _M_acquire_lock() {
366 if (!_M_lock) _M_initialize();
367 DosRequestMutexSem(_M_lock, SEM_INDEFINITE_WAIT);
369 inline void _M_release_lock() { DosReleaseMutexSem(_M_lock); }
370 # elif defined (_STLP_BETHREADS) 372 inline void _M_initialize() {
373 sem = create_sem(1,
"STLPort");
376 inline void _M_destroy() {
377 int t = delete_sem(
sem);
380 inline void _M_acquire_lock();
381 inline void _M_release_lock() {
382 status_t
t = release_sem(
sem);
385 # elif defined (_STLP_NWTHREADS) 387 inline void _M_initialize()
388 { _M_lock = OpenLocalSemaphore(1); }
389 inline void _M_destroy()
390 { CloseLocalSemaphore(_M_lock); }
391 inline void _M_acquire_lock()
392 { WaitOnLocalSemaphore(_M_lock); }
393 inline void _M_release_lock() { SignalLocalSemaphore(_M_lock); }
394 # else //*ty 11/24/2001 - added configuration check 395 # error "Unknown thread facility configuration" 402 #endif // _STLP_PTHREADS 425 { _M_lock._M_acquire_lock(); }
427 { _M_lock._M_release_lock(); }
443 #if defined (__DMC__) 448 #if defined (_STLP_THREADS) && \ 449 (!defined (_STLP_ATOMIC_INCREMENT) || !defined (_STLP_ATOMIC_DECREMENT) || \ 450 defined (_STLP_WIN95_LIKE)) 451 # define _STLP_USE_MUTEX 458 #if defined (__BORLANDC__) 463 #if defined (_STLP_THREADS) 464 # if !defined (_STLP_USE_MUTEX) 468 # undef _STLP_USE_MUTEX 471 return ++_M_ref_count;
475 return --_M_ref_count;
496 template <
int __use_ptr_atomic_swap>
499 #if defined (_STLP_THREADS) && \ 500 !defined (_STLP_ATOMIC_EXCHANGE) && \ 501 (defined (_STLP_PTHREADS) || defined (_STLP_UITHREADS) || defined (_STLP_OS2THREADS) || \ 502 defined (_STLP_USE_PTHREAD_SPINLOCK) || defined (_STLP_NWTHREADS)) 503 # define _STLP_USE_ATOMIC_SWAP_MUTEX 508 #if defined (_STLP_THREADS) 509 # if defined (_STLP_ATOMIC_EXCHANGE) 511 # elif defined (_STLP_USE_ATOMIC_SWAP_MUTEX) 512 _S_swap_lock._M_acquire_lock();
515 _S_swap_lock._M_release_lock();
518 # error Missing atomic swap implementation 525 #endif // _STLP_THREADS 529 #if defined (_STLP_THREADS) 530 # if defined (_STLP_ATOMIC_EXCHANGE_PTR) 531 return _STLP_ATOMIC_EXCHANGE_PTR(__p, __q);
532 # elif defined (_STLP_ATOMIC_EXCHANGE) 537 # elif defined (_STLP_USE_ATOMIC_SWAP_MUTEX) 538 _S_swap_lock._M_acquire_lock();
539 void *__result = *__p;
541 _S_swap_lock._M_release_lock();
544 # error Missing pointer atomic swap implementation 548 void *__result = *__p;
558 #if defined (_STLP_THREADS) && \ 559 (!defined (_STLP_ATOMIC_EXCHANGE) || !defined (_STLP_ATOMIC_EXCHANGE_PTR)) && \ 560 (defined (_STLP_PTHREADS) || defined (_STLP_UITHREADS) || defined (_STLP_OS2THREADS) || \ 561 defined (_STLP_USE_PTHREAD_SPINLOCK) || defined (_STLP_NWTHREADS)) 562 # define _STLP_USE_ATOMIC_SWAP_MUTEX 567 #if defined (_STLP_THREADS) 568 # if defined (_STLP_ATOMIC_EXCHANGE) 570 # elif defined (_STLP_USE_ATOMIC_SWAP_MUTEX) 575 _S_swap_lock._M_acquire_lock();
578 _S_swap_lock._M_release_lock();
581 # error Missing atomic swap implementation 588 #endif // _STLP_THREADS 592 #if defined (_STLP_THREADS) 593 # if defined (_STLP_ATOMIC_EXCHANGE_PTR) 594 return _STLP_ATOMIC_EXCHANGE_PTR(__p, __q);
595 # elif defined (_STLP_ATOMIC_EXCHANGE) 600 # elif defined (_STLP_USE_ATOMIC_SWAP_MUTEX) 601 _S_swap_lock._M_acquire_lock();
602 void *__result = *__p;
604 _S_swap_lock._M_release_lock();
607 # error Missing pointer atomic swap implementation 611 void *__result = *__p;
618 #if defined (_STLP_MSVC) && (_STLP_MSVC == 1300) 619 # pragma warning (push) 620 # pragma warning (disable : 4189) //__use_ptr_atomic_swap initialized but not used 624 const int __use_ptr_atomic_swap =
sizeof(
__stl_atomic_t) ==
sizeof(
void*);
629 const int __use_ptr_atomic_swap =
sizeof(
__stl_atomic_t) ==
sizeof(
void*);
633 #if defined (_STLP_MSVC) && (_STLP_MSVC == 1300) 634 # pragma warning (pop) 637 #if defined (_STLP_BETHREADS) 638 template <
int __inst>
639 struct _STLP_beos_static_lock_data {
643 { _STLP_beos_static_lock_data<0>::is_init =
true; }
645 { _STLP_beos_static_lock_data<0>::is_init =
false; }
650 template <
int __inst>
651 bool _STLP_beos_static_lock_data<__inst>::is_init =
false;
652 template <
int __inst>
660 if (_STLP_beos_static_lock_data<0>::is_init) {
672 t = acquire_sem(
sem);
679 #if !defined (_STLP_LINK_TIME_INSTANTIATION)
#define _SPINUNLOCK(_lck)
#define _STLP_STATIC_MUTEX
_Refcount_Base(__stl_atomic_t __n)
_STLP_auto_lock(_STLP_STATIC_MUTEX &__lock)
_STLP_VOLATILE __stl_atomic_t _M_ref_count
__asm__("\t.globl GetPhys\n" "GetPhys:\t\n" "mflr 0\n\t" "stwu 0,-16(1)\n\t" "mfmsr 5\n\t" "andi. 6,5,0xffef\n\t" "mtmsr 6\n\t" "isync\n\t" "sync\n\t" "lwz 3,0(3)\n\t" "mtmsr 5\n\t" "isync\n\t" "sync\n\t" "lwz 0,0(1)\n\t" "addi 1,1,16\n\t" "mtlr 0\n\t" "blr")
#define _STLP_ATOMIC_DECREMENT(__x)
_STLP_INLINE_LOOP _InputIter __last
static void * _S_swap_ptr(void *_STLP_VOLATILE *__p, void *__q)
#define _STLP_ATOMIC_EXCHANGE(__x, __y)
_STLP_STATIC_ASSERT(sizeof(nl_catd_type)<=sizeof(int)) class _STLP_CLASS_DECLSPEC _Catalog_nl_catd_map
void *_STLP_CALL _Atomic_swap_ptr(void *_STLP_VOLATILE *__p, void *__q)
#define _STLP_ATOMIC_INCREMENT(__x)
#define _STLP_CLASS_DECLSPEC
#define __REINTERPRET_CAST(__x, __y)
#define _STLP_TEMPLATE_NULL
#define _STLP_END_NAMESPACE
static __stl_atomic_t _S_swap(_STLP_VOLATILE __stl_atomic_t *__p, __stl_atomic_t __q)
#define _STLP_BEGIN_NAMESPACE
__stl_atomic_t _STLP_CALL _Atomic_swap(_STLP_VOLATILE __stl_atomic_t *__p, __stl_atomic_t __q)
static void * _S_swap_ptr(void *_STLP_VOLATILE *__p, void *__q)
_STLP_STATIC_MUTEX & _M_lock
static __stl_atomic_t _S_swap(_STLP_VOLATILE __stl_atomic_t *__p, __stl_atomic_t __q)