1.1 --- a/epoc32/include/stdapis/stlport/stl/_threads.h Tue Mar 16 16:12:26 2010 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,666 +0,0 @@
1.4 -/*
1.5 - * Copyright (c) 1997-1999
1.6 - * Silicon Graphics Computer Systems, Inc.
1.7 - *
1.8 - * Copyright (c) 1999
1.9 - * Boris Fomitchev
1.10 - *
1.11 - * This material is provided "as is", with absolutely no warranty expressed
1.12 - * or implied. Any use is at your own risk.
1.13 - *
1.14 - * Permission to use or copy this software for any purpose is hereby granted
1.15 - * without fee, provided the above notices are retained on all copies.
1.16 - * Permission to modify the code and to distribute modified code is granted,
1.17 - * provided the above notices are retained, and a notice that the code was
1.18 - * modified is included with the above copyright notice.
1.19 - *
1.20 - */
1.21 -
1.22 -// WARNING: This is an internal header file, included by other C++
1.23 -// standard library headers. You should not attempt to use this header
1.24 -// file directly.
1.25 -// Stl_config.h should be included before this file.
1.26 -
1.27 -
1.28 -#ifndef _STLP_INTERNAL_THREADS_H
1.29 -#define _STLP_INTERNAL_THREADS_H
1.30 -
1.31 -// Supported threading models are native SGI, pthreads, uithreads
1.32 -// (similar to pthreads, but based on an earlier draft of the Posix
1.33 -// threads standard), and Win32 threads. Uithread support by Jochen
1.34 -// Schlick, 1999, and Solaris threads generalized to them.
1.35 -
1.36 -#ifndef _STLP_CONFIG_H
1.37 -#include <stl/_config.h>
1.38 -#endif
1.39 -
1.40 -# if ! defined (_STLP_CSTDDEF)
1.41 -# include <cstddef>
1.42 -# endif
1.43 -
1.44 -# if ! defined (_STLP_CSTDLIB)
1.45 -# include <cstdlib>
1.46 -# endif
1.47 -
1.48 -// On SUN and Mac OS X gcc, zero-initialization works just fine...
1.49 -# if defined (__sun) || ( defined(__GNUC__) && defined(__APPLE__) )
1.50 -# define _STLP_MUTEX_INITIALIZER
1.51 -# endif
1.52 -
1.53 -# if defined (_STLP_WIN32) || defined (__sgi) || defined (_STLP_SPARC_SOLARIS_THREADS)
1.54 - typedef long __stl_atomic_t;
1.55 -# else
1.56 -# if defined (_STLP_USE_NAMESPACES) && ! defined (_STLP_VENDOR_GLOBAL_CSTD)
1.57 -// using _STLP_VENDOR_CSTD::size_t;
1.58 -using namespace _STLP_VENDOR_CSTD;
1.59 -# endif
1.60 - typedef size_t __stl_atomic_t;
1.61 -#endif
1.62 -
1.63 -# if defined(_STLP_SGI_THREADS)
1.64 -# include <mutex.h>
1.65 -// Hack for SGI o32 compilers.
1.66 -#if !defined(__add_and_fetch) && \
1.67 - (__mips < 3 || !(defined (_ABIN32) || defined(_ABI64)))
1.68 -# define __add_and_fetch(__l,__v) add_then_test((unsigned long*)__l,__v)
1.69 -# define __test_and_set(__l,__v) test_and_set(__l,__v)
1.70 -#endif /* o32 */
1.71 -
1.72 -# if __mips < 3 || !(defined (_ABIN32) || defined(_ABI64))
1.73 -# define _STLP_ATOMIC_EXCHANGE(__p, __q) test_and_set(__p, __q)
1.74 -# else
1.75 -# define _STLP_ATOMIC_EXCHANGE(__p, __q) __test_and_set((unsigned long*)__p, (unsigned long)__q)
1.76 -# endif
1.77 -
1.78 -# define _STLP_ATOMIC_INCREMENT(__x) __add_and_fetch(__x, 1)
1.79 -# define _STLP_ATOMIC_DECREMENT(__x) __add_and_fetch(__x, (size_t) -1)
1.80 -
1.81 -# elif defined (__GNUC__) && defined (__i386__) && defined (__unix__) && defined (_STLP_USE_INLINE_X86_SPINLOCK)
1.82 -
1.83 -// gcc on i386 linux, freebsd, etc.
1.84 -
1.85 -// This enables the memory caching on x86 linux. It is critical for SMP
1.86 -// without it the performace is DISMAL!
1.87 -static inline unsigned long __xchg(volatile __stl_atomic_t* target, int source)
1.88 -{
1.89 -
1.90 - // The target is refernce in memory rather than the register
1.91 - // because making a copy of it from memory to the register and
1.92 - // back again would ruin the atomic nature of the call.
1.93 - // the source does not need to be delt with atomicly so it can
1.94 - // be copied about as needed.
1.95 - //
1.96 - // The casting of the source is used to prevent gcc from optimizing
1.97 - // in such a way that breaks the atomic nature of this call.
1.98 - //
1.99 - __asm__ __volatile__("xchgl %1,%0"
1.100 - :"=m" (*(volatile long *) target), "=r" (source)
1.101 - :"m" (*(volatile long *) target), "r" (source) );
1.102 - return source;
1.103 -
1.104 - // The assembly above does the following atomicly:
1.105 - // int temp=source;
1.106 - // source=(int)(*target);
1.107 - // (int)(*target)=temp;
1.108 - // return source
1.109 -}
1.110 -
1.111 -static inline void __inc_and_fetch(volatile __stl_atomic_t* __x)
1.112 -{
1.113 - // Referenced in memory rather than register to preserve the atomic nature.
1.114 - //
1.115 - __asm__ __volatile__(
1.116 - "lock; incl %0"
1.117 - :"=m" (*__x)
1.118 - :"m" (*__x) );
1.119 -
1.120 - // The assembly above does the following atomicly:
1.121 - // ++(int)(*__x);
1.122 -
1.123 -}
1.124 -static inline void __dec_and_fetch(volatile __stl_atomic_t* __x)
1.125 -{
1.126 - // Referenced in memory rather than register to preserve the atomic nature.
1.127 - //
1.128 - __asm__ __volatile__(
1.129 - "lock; decl %0"
1.130 - :"=m" (*__x)
1.131 - :"m" (*__x) );
1.132 -
1.133 - // The assembly above does the following atomicly:
1.134 - // --(int)(*__x);
1.135 -}
1.136 -
1.137 -# define _STLP_ATOMIC_EXCHANGE(target, newValue) ((__xchg(target, newValue)))
1.138 -# define _STLP_ATOMIC_INCREMENT(__x) __inc_and_fetch(__x)
1.139 -# define _STLP_ATOMIC_DECREMENT(__x) __dec_and_fetch(__x)
1.140 -
1.141 -# elif defined(_STLP_PTHREADS)
1.142 -
1.143 -# include <pthread.h>
1.144 -# ifndef _STLP_USE_PTHREAD_SPINLOCK
1.145 -# if defined(PTHREAD_MUTEX_INITIALIZER) && !defined(_STLP_MUTEX_INITIALIZER)
1.146 -# define _STLP_MUTEX_INITIALIZER = { PTHREAD_MUTEX_INITIALIZER }
1.147 -# endif
1.148 -
1.149 -//HPUX variants have (on some platforms optional) non-standard "DCE" pthreads impl
1.150 -# if defined(_DECTHREADS_) && (defined(_PTHREAD_USE_D4) || defined(__hpux)) && !defined(_CMA_SUPPRESS_EXTERNALS_)
1.151 -# define _STLP_PTHREAD_ATTR_DEFAULT pthread_mutexattr_default
1.152 -# else
1.153 -# define _STLP_PTHREAD_ATTR_DEFAULT 0
1.154 -# endif
1.155 -# endif // !_STLP_USE_PTHREAD_SPINLOCK
1.156 -
1.157 -# elif defined(_STLP_WIN32THREADS)
1.158 -# if !defined (_STLP_WINDOWS_H_INCLUDED) && ! defined (_WINDOWS_H)
1.159 -# if ! (defined ( _STLP_MSVC ) || defined (__BORLANDC__) || defined (__ICL) || defined (__WATCOMC__) || defined (__MINGW32__) || defined (__DMC__))
1.160 -# ifdef _STLP_USE_MFC
1.161 -# include <afx.h>
1.162 -# else
1.163 -# include <windows.h>
1.164 -# endif
1.165 -# define _STLP_WINDOWS_H_INCLUDED
1.166 -# else
1.167 -// This section serves as a replacement for windows.h header for Visual C++
1.168 -extern "C" {
1.169 -# if (defined(_M_MRX000) || defined(_M_ALPHA) \
1.170 - || (defined(_M_PPC) && (_MSC_VER >= 1000))) && !defined(RC_INVOKED)
1.171 -# define InterlockedIncrement _InterlockedIncrement
1.172 -# define InterlockedDecrement _InterlockedDecrement
1.173 -# define InterlockedExchange _InterlockedExchange
1.174 -# define _STLP_STDCALL
1.175 -# else
1.176 -# ifdef _MAC
1.177 -# define _STLP_STDCALL _cdecl
1.178 -# else
1.179 -# define _STLP_STDCALL __stdcall
1.180 -# endif
1.181 -# endif
1.182 -
1.183 -#if (_MSC_VER >= 1300) || defined (_STLP_NEW_PLATFORM_SDK)
1.184 -_STLP_IMPORT_DECLSPEC long _STLP_STDCALL InterlockedIncrement(long volatile *);
1.185 -_STLP_IMPORT_DECLSPEC long _STLP_STDCALL InterlockedDecrement(long volatile *);
1.186 -_STLP_IMPORT_DECLSPEC long _STLP_STDCALL InterlockedExchange(long volatile *, long);
1.187 -#else
1.188 - // boris : for the latest SDK, you may actually need the other version of the declaration (above)
1.189 - // even for earlier VC++ versions. There is no way to tell SDK versions apart, sorry ...
1.190 -_STLP_IMPORT_DECLSPEC long _STLP_STDCALL InterlockedIncrement(long*);
1.191 -_STLP_IMPORT_DECLSPEC long _STLP_STDCALL InterlockedDecrement(long*);
1.192 -_STLP_IMPORT_DECLSPEC long _STLP_STDCALL InterlockedExchange(long*, long);
1.193 -#endif
1.194 -
1.195 -_STLP_IMPORT_DECLSPEC void _STLP_STDCALL Sleep(unsigned long);
1.196 -_STLP_IMPORT_DECLSPEC void _STLP_STDCALL OutputDebugStringA( const char* lpOutputString );
1.197 -
1.198 -#ifdef _STLP_DEBUG
1.199 -typedef unsigned long DWORD;
1.200 -_STLP_IMPORT_DECLSPEC DWORD _STLP_STDCALL GetCurrentThreadId();
1.201 -#endif /* _STLP_DEBUG */
1.202 -
1.203 -# if defined (InterlockedIncrement)
1.204 -# pragma intrinsic(_InterlockedIncrement)
1.205 -# pragma intrinsic(_InterlockedDecrement)
1.206 -# pragma intrinsic(_InterlockedExchange)
1.207 -# endif
1.208 -} /* extern "C" */
1.209 -
1.210 -# endif /* STL_MSVC */
1.211 -
1.212 -# define _STLP_WINDOWS_H_INCLUDED
1.213 -
1.214 -# endif /* _STLP_WIN32 */
1.215 -
1.216 -# ifndef _STLP_ATOMIC_INCREMENT
1.217 -# define _STLP_ATOMIC_INCREMENT(__x) InterlockedIncrement((long*)__x)
1.218 -# define _STLP_ATOMIC_DECREMENT(__x) InterlockedDecrement((long*)__x)
1.219 -# define _STLP_ATOMIC_EXCHANGE(__x, __y) InterlockedExchange((long*)__x, (long)__y)
1.220 -# endif
1.221 -# elif defined(__DECC) || defined(__DECCXX)
1.222 -# include <machine/builtins.h>
1.223 -# define _STLP_ATOMIC_EXCHANGE __ATOMIC_EXCH_LONG
1.224 -# define _STLP_ATOMIC_INCREMENT(__x) __ATOMIC_ADD_LONG(__x, 1)
1.225 -# define _STLP_ATOMIC_DECREMENT(__x) __ATOMIC_ADD_LONG(__x, -1)
1.226 -# elif defined(_STLP_SPARC_SOLARIS_THREADS)
1.227 -# include <stl/_sparc_atomic.h>
1.228 -# elif defined (_STLP_UITHREADS)
1.229 -// this inclusion is potential hazard to bring up all sorts
1.230 -// of old-style headers. Let's assume vendor already know how
1.231 -// to deal with that.
1.232 -# include <ctime>
1.233 -# if defined (_STLP_USE_NAMESPACES) && ! defined (_STLP_VENDOR_GLOBAL_CSTD)
1.234 -using _STLP_VENDOR_CSTD::time_t;
1.235 -# endif
1.236 -# include <synch.h>
1.237 -# include <cstdio>
1.238 -# include <stl/_cwchar.h>
1.239 -# elif defined (_STLP_BETHREADS)
1.240 -# include <OS.h>
1.241 -#include <cassert>
1.242 -#include <stdio.h>
1.243 -# define _STLP_MUTEX_INITIALIZER = { 0 }
1.244 -#elif defined(_STLP_OS2THREADS)
1.245 -# ifdef __GNUC__
1.246 -# define INCL_DOSSEMAPHORES
1.247 -# include <os2.h>
1.248 -# else
1.249 - // This section serves to replace os2.h for VisualAge C++
1.250 - typedef unsigned long ULONG;
1.251 - #ifndef __HEV__ /* INCL_SEMAPHORE may also define HEV */
1.252 - #define __HEV__
1.253 - typedef ULONG HEV;
1.254 - typedef HEV* PHEV;
1.255 - #endif
1.256 - typedef ULONG APIRET;
1.257 - typedef ULONG HMTX;
1.258 - typedef HMTX* PHMTX;
1.259 - typedef const char* PCSZ;
1.260 - typedef ULONG BOOL32;
1.261 - APIRET _System DosCreateMutexSem(PCSZ pszName, PHEV phev, ULONG flAttr, BOOL32 fState);
1.262 - APIRET _System DosRequestMutexSem(HMTX hmtx, ULONG ulTimeout);
1.263 - APIRET _System DosReleaseMutexSem(HMTX hmtx);
1.264 - APIRET _System DosCloseMutexSem(HMTX hmtx);
1.265 -# define _STLP_MUTEX_INITIALIZER = { 0 };
1.266 -# endif /* GNUC */
1.267 -# elif defined(_STLP_VXWORKS_THREADS)
1.268 -# include "semLib.h"
1.269 -# endif
1.270 -
1.271 -# ifndef _STLP_MUTEX_INITIALIZER
1.272 -# if defined(_STLP_ATOMIC_EXCHANGE)
1.273 -// we are using our own spinlock.
1.274 -# define _STLP_MUTEX_INITIALIZER = { 0 }
1.275 -# elif defined(_STLP_UITHREADS)
1.276 -// known case
1.277 -# define _STLP_MUTEX_INITIALIZER = { DEFAULTMUTEX }
1.278 -# else
1.279 -// we do not have static initializer available. therefore, on-demand synchronization is needed.
1.280 -# define _STLP_MUTEX_INITIALIZER
1.281 -# define _STLP_MUTEX_NEEDS_ONDEMAND_INITIALIZATION
1.282 -# endif
1.283 -# endif
1.284 -
1.285 -_STLP_BEGIN_NAMESPACE
1.286 -
1.287 -#ifndef _STLP_USE_PTHREAD_SPINLOCK
1.288 -// Helper struct. This is a workaround for various compilers that don't
1.289 -// handle static variables in inline functions properly.
1.290 -template <int __inst>
1.291 -struct _STLP_mutex_spin {
1.292 - enum { __low_max = 30, __high_max = 1000 };
1.293 - // Low if we suspect uniprocessor, high for multiprocessor.
1.294 - static unsigned __max;
1.295 - static unsigned __last;
1.296 - static void _STLP_CALL _M_do_lock(volatile __stl_atomic_t* __lock);
1.297 - static void _STLP_CALL _S_nsec_sleep(int __log_nsec);
1.298 -};
1.299 -#endif // !_STLP_USE_PTHREAD_SPINLOCK
1.300 -
1.301 -
1.302 -// Locking class. Note that this class *does not have a constructor*.
1.303 -// It must be initialized either statically, with _STLP_MUTEX_INITIALIZER,
1.304 -// or dynamically, by explicitly calling the _M_initialize member function.
1.305 -// (This is similar to the ways that a pthreads mutex can be initialized.)
1.306 -// There are explicit member functions for acquiring and releasing the lock.
1.307 -
1.308 -// There is no constructor because static initialization is essential for
1.309 -// some uses, and only a class aggregate (see section 8.5.1 of the C++
1.310 -// standard) can be initialized that way. That means we must have no
1.311 -// constructors, no base classes, no virtual functions, and no private or
1.312 -// protected members.
1.313 -
1.314 -// For non-static cases, clients should use _STLP_mutex.
1.315 -
1.316 -struct _STLP_CLASS_DECLSPEC _STLP_mutex_base
1.317 -{
1.318 -#if defined(_STLP_ATOMIC_EXCHANGE) || defined(_STLP_SGI_THREADS)
1.319 - // It should be relatively easy to get this to work on any modern Unix.
1.320 - volatile __stl_atomic_t _M_lock;
1.321 -#endif
1.322 -
1.323 -#ifdef _STLP_THREADS
1.324 -
1.325 -# ifdef _STLP_ATOMIC_EXCHANGE
1.326 - inline void _M_initialize() { _M_lock=0; }
1.327 - inline void _M_destroy() {}
1.328 -
1.329 - void _M_acquire_lock() {
1.330 - _STLP_mutex_spin<0>::_M_do_lock(&_M_lock);
1.331 - }
1.332 -
1.333 - inline void _M_release_lock() {
1.334 - volatile __stl_atomic_t* __lock = &_M_lock;
1.335 -# if defined(_STLP_SGI_THREADS) && defined(__GNUC__) && __mips >= 3
1.336 - asm("sync");
1.337 - *__lock = 0;
1.338 -# elif defined(_STLP_SGI_THREADS) && __mips >= 3 \
1.339 - && (defined (_ABIN32) || defined(_ABI64))
1.340 - __lock_release(__lock);
1.341 -# elif defined (_STLP_SPARC_SOLARIS_THREADS)
1.342 -# if defined (__WORD64) || defined (__arch64__) || defined (__sparcv9) || defined (__sparcv8plus)
1.343 - asm("membar #StoreStore ; membar #LoadStore");
1.344 -# else
1.345 - asm(" stbar ");
1.346 -# endif
1.347 - *__lock = 0;
1.348 -# else
1.349 - *__lock = 0;
1.350 - // This is not sufficient on many multiprocessors, since
1.351 - // writes to protected variables and the lock may be reordered.
1.352 -# endif
1.353 - }
1.354 -# elif defined(_STLP_PTHREADS)
1.355 -# ifdef _STLP_USE_PTHREAD_SPINLOCK
1.356 - pthread_spinlock_t _M_lock;
1.357 - inline void _M_initialize() { pthread_spin_init( &_M_lock, 0 ); }
1.358 - inline void _M_destroy() { pthread_spin_destroy( &_M_lock ); }
1.359 -
1.360 - inline void _M_acquire_lock() {
1.361 - // we do not care about race conditions here : there is only one thread at this point
1.362 - if(!_M_lock) pthread_spin_init( &_M_lock, 0 );
1.363 -
1.364 - // fbp: here, initialization on demand should happen before the lock
1.365 - // we use simple strategy as we are sure this only happens on initialization
1.366 - pthread_spin_lock( &_M_lock );
1.367 - }
1.368 -
1.369 - inline void _M_acquire_lock_nodemand() {
1.370 - pthread_spin_lock( &_M_lock );
1.371 - }
1.372 - inline void _M_release_lock() { pthread_spin_unlock( &_M_lock ); }
1.373 -# else // !_STLP_USE_PTHREAD_SPINLOCK
1.374 - pthread_mutex_t _M_lock;
1.375 -
1.376 - inline void _M_initialize() {
1.377 - pthread_mutex_init(&_M_lock,_STLP_PTHREAD_ATTR_DEFAULT);
1.378 - }
1.379 - inline void _M_destroy() {
1.380 - pthread_mutex_destroy(&_M_lock);
1.381 - }
1.382 - inline void _M_acquire_lock_nodemand() {
1.383 - pthread_mutex_lock(&_M_lock);
1.384 - }
1.385 -
1.386 - inline void _M_acquire_lock() {
1.387 -# if defined (__hpux) && !defined (PTHREAD_MUTEX_INITIALIZER)
1.388 - if (!_M_lock.field1) _M_initialize();
1.389 -# endif
1.390 - pthread_mutex_lock(&_M_lock);
1.391 - }
1.392 - inline void _M_release_lock() { pthread_mutex_unlock(&_M_lock); }
1.393 -# endif // !_STLP_USE_PTHREAD_SPINLOCK
1.394 -
1.395 -# elif defined (_STLP_UITHREADS)
1.396 - mutex_t _M_lock;
1.397 - inline void _M_initialize() {
1.398 - mutex_init(&_M_lock,0,NULL);
1.399 - }
1.400 - inline void _M_destroy() {
1.401 - mutex_destroy(&_M_lock);
1.402 - }
1.403 - inline void _M_acquire_lock() { mutex_lock(&_M_lock); }
1.404 - inline void _M_release_lock() { mutex_unlock(&_M_lock); }
1.405 -
1.406 -# elif defined(_STLP_OS2THREADS)
1.407 - HMTX _M_lock;
1.408 - inline void _M_initialize() { DosCreateMutexSem(NULL, &_M_lock, 0, false); }
1.409 - inline void _M_destroy() { DosCloseMutexSem(_M_lock); }
1.410 - inline void _M_acquire_lock_nodemand() {
1.411 - DosRequestMutexSem(_M_lock, SEM_INDEFINITE_WAIT);
1.412 - }
1.413 - inline void _M_acquire_lock() {
1.414 - if(!_M_lock) _M_initialize();
1.415 - DosRequestMutexSem(_M_lock, SEM_INDEFINITE_WAIT);
1.416 - }
1.417 - inline void _M_release_lock() { DosReleaseMutexSem(_M_lock); }
1.418 -# elif defined(_STLP_BETHREADS)
1.419 - sem_id sem;
1.420 - inline void _M_initialize()
1.421 - {
1.422 - sem = create_sem(1, "STLPort");
1.423 - assert(sem > 0);
1.424 - }
1.425 - inline void _M_destroy()
1.426 - {
1.427 - int t = delete_sem(sem);
1.428 - assert(t == B_NO_ERROR);
1.429 - }
1.430 - inline void _M_acquire_lock_nodemand()
1.431 - {
1.432 - status_t t;
1.433 - t = acquire_sem(sem);
1.434 - assert(t == B_NO_ERROR);
1.435 - }
1.436 - inline void _M_acquire_lock();
1.437 - inline void _M_release_lock()
1.438 - {
1.439 - status_t t = release_sem(sem);
1.440 - assert(t == B_NO_ERROR);
1.441 - }
1.442 -# elif defined(_STLP_VXWORKS_THREADS)
1.443 - SEM_ID _M_sem;
1.444 - inline void _M_initialize()
1.445 - {
1.446 - _M_sem = semMCreate(SEM_Q_FIFO);
1.447 - assert(_M_sem > 0);
1.448 - }
1.449 - inline void _M_destroy()
1.450 - {
1.451 - STATUS __s;
1.452 - semDelete (_M_sem);
1.453 - assert(__s == OK);
1.454 - }
1.455 - inline void _M_acquire_lock_nodemand()
1.456 - {
1.457 - STATUS __s;
1.458 - semTake (_M_sem, WAIT_FOREVER);
1.459 - assert(__s == OK);
1.460 - }
1.461 - inline void _M_acquire_lock()
1.462 - {
1.463 - if (!_M_sem)
1.464 - _M_initialize();
1.465 - _M_acquire_lock_nodemand();
1.466 - }
1.467 - inline void _M_release_lock()
1.468 - {
1.469 - STATUS __s;
1.470 - semGive (_M_sem, WAIT_FOREVER);
1.471 - assert(__s == OK);
1.472 - }
1.473 -# else //*ty 11/24/2001 - added configuration check
1.474 -# error "Unknown thread facility configuration"
1.475 -# endif
1.476 -#else /* No threads */
1.477 - inline void _M_initialize() {}
1.478 - inline void _M_destroy() {}
1.479 - inline void _M_acquire_lock() {}
1.480 - inline void _M_release_lock() {}
1.481 -#endif // _STLP_PTHREADS
1.482 -};
1.483 -
1.484 -
1.485 -#if defined (_STLP_THREADS) && defined (_STLP_MUTEX_NEEDS_ONDEMAND_INITIALIZATION)
1.486 -// for use in _STLP_mutex, our purposes do not require ondemand initialization
1.487 -// also, mutex_base may use some hacks to determine uninitialized state by zero data, which only works for globals.
1.488 -class _STLP_CLASS_DECLSPEC _STLP_mutex_nodemand : public _STLP_mutex_base {
1.489 - inline void _M_acquire_lock() {
1.490 - _M_acquire_lock_nodemand();
1.491 - }
1.492 -};
1.493 -#else
1.494 -typedef _STLP_mutex_base _STLP_mutex_nodemand;
1.495 -#endif
1.496 -
1.497 -
1.498 -// Locking class. The constructor initializes the lock, the destructor destroys it.
1.499 -// Well - behaving class, does not need static initializer
1.500 -class _STLP_CLASS_DECLSPEC _STLP_mutex : public _STLP_mutex_nodemand {
1.501 - public:
1.502 - inline _STLP_mutex () { _M_initialize(); }
1.503 - inline ~_STLP_mutex () { _M_destroy(); }
1.504 - private:
1.505 - _STLP_mutex(const _STLP_mutex&);
1.506 - void operator=(const _STLP_mutex&);
1.507 -};
1.508 -
1.509 -
1.510 -
1.511 -/*
1.512 - * Class _Refcount_Base provides a type, __stl_atomic_t, a data member,
1.513 - * _M_ref_count, and member functions _M_incr and _M_decr, which perform
1.514 - * atomic preincrement/predecrement. The constructor initializes
1.515 - * _M_ref_count.
1.516 - */
1.517 -struct _STLP_CLASS_DECLSPEC _Refcount_Base
1.518 -{
1.519 - // The data member _M_ref_count
1.520 - volatile __stl_atomic_t _M_ref_count;
1.521 -
1.522 -# if !defined (_STLP_ATOMIC_EXCHANGE)
1.523 - _STLP_mutex _M_mutex;
1.524 -# endif
1.525 -
1.526 - // Constructor
1.527 - _Refcount_Base(__stl_atomic_t __n) : _M_ref_count(__n) {}
1.528 -
1.529 - // _M_incr and _M_decr
1.530 -# if defined (_STLP_THREADS) && defined (_STLP_ATOMIC_EXCHANGE)
1.531 - void _M_incr() { _STLP_ATOMIC_INCREMENT((__stl_atomic_t*)&_M_ref_count); }
1.532 - void _M_decr() { _STLP_ATOMIC_DECREMENT((__stl_atomic_t*)&_M_ref_count); }
1.533 -# elif defined(_STLP_THREADS)
1.534 - void _M_incr() {
1.535 - _M_mutex._M_acquire_lock();
1.536 - ++_M_ref_count;
1.537 - _M_mutex._M_release_lock();
1.538 - }
1.539 - void _M_decr() {
1.540 - _M_mutex._M_acquire_lock();
1.541 - --_M_ref_count;
1.542 - _M_mutex._M_release_lock();
1.543 - }
1.544 -# else /* No threads */
1.545 - void _M_incr() { ++_M_ref_count; }
1.546 - void _M_decr() { --_M_ref_count; }
1.547 -# endif
1.548 -};
1.549 -
1.550 -// Atomic swap on unsigned long
1.551 -// This is guaranteed to behave as though it were atomic only if all
1.552 -// possibly concurrent updates use _Atomic_swap.
1.553 -// In some cases the operation is emulated with a lock.
1.554 -# if defined (_STLP_THREADS)
1.555 -# ifdef _STLP_ATOMIC_EXCHANGE
1.556 -inline __stl_atomic_t _Atomic_swap(volatile __stl_atomic_t * __p, __stl_atomic_t __q) {
1.557 - return (__stl_atomic_t) _STLP_ATOMIC_EXCHANGE(__p,__q);
1.558 -}
1.559 -# elif defined(_STLP_PTHREADS) || defined (_STLP_UITHREADS) || defined (_STLP_OS2THREADS) || defined(_STLP_USE_PTHREAD_SPINLOCK)
1.560 -// We use a template here only to get a unique initialized instance.
1.561 -template<int __dummy>
1.562 -struct _Swap_lock_struct {
1.563 - static _STLP_STATIC_MUTEX _S_swap_lock;
1.564 -};
1.565 -
1.566 -
1.567 -// This should be portable, but performance is expected
1.568 -// to be quite awful. This really needs platform specific
1.569 -// code.
1.570 -inline __stl_atomic_t _Atomic_swap(volatile __stl_atomic_t * __p, __stl_atomic_t __q) {
1.571 - _Swap_lock_struct<0>::_S_swap_lock._M_acquire_lock();
1.572 - __stl_atomic_t __result = *__p;
1.573 - *__p = __q;
1.574 - _Swap_lock_struct<0>::_S_swap_lock._M_release_lock();
1.575 - return __result;
1.576 -}
1.577 -# endif // _STLP_PTHREADS || _STLP_UITHREADS || _STLP_OS2THREADS || _STLP_USE_PTHREAD_SPINLOCK
1.578 -# else // !_STLP_THREADS
1.579 -/* no threads */
1.580 -static inline __stl_atomic_t _STLP_CALL
1.581 -_Atomic_swap(volatile __stl_atomic_t * __p, __stl_atomic_t __q) {
1.582 - __stl_atomic_t __result = *__p;
1.583 - *__p = __q;
1.584 - return __result;
1.585 -}
1.586 -# endif // _STLP_THREADS
1.587 -
1.588 -// A locking class that uses _STLP_STATIC_MUTEX. The constructor takes
1.589 -// a reference to an _STLP_STATIC_MUTEX, and acquires a lock. The destructor
1.590 -// releases the lock.
1.591 -
1.592 -struct _STLP_CLASS_DECLSPEC _STLP_auto_lock
1.593 -{
1.594 - _STLP_STATIC_MUTEX& _M_lock;
1.595 -
1.596 - _STLP_auto_lock(_STLP_STATIC_MUTEX& __lock) : _M_lock(__lock)
1.597 - { _M_lock._M_acquire_lock(); }
1.598 - ~_STLP_auto_lock() { _M_lock._M_release_lock(); }
1.599 -
1.600 -private:
1.601 - void operator=(const _STLP_auto_lock&);
1.602 - _STLP_auto_lock(const _STLP_auto_lock&);
1.603 -};
1.604 -
1.605 -typedef _STLP_auto_lock _STLP_mutex_lock;
1.606 -
1.607 -#ifdef _STLP_BETHREADS
1.608 -
1.609 -template <int __inst>
1.610 -struct _STLP_beos_static_lock_data
1.611 -{
1.612 - static bool is_init;
1.613 - struct mutex_t : public _STLP_mutex
1.614 - {
1.615 - mutex_t()
1.616 - {
1.617 - _STLP_beos_static_lock_data<0>::is_init = true;
1.618 - }
1.619 - ~mutex_t()
1.620 - {
1.621 - _STLP_beos_static_lock_data<0>::is_init = false;
1.622 - }
1.623 - };
1.624 - static mutex_t mut;
1.625 -};
1.626 -
1.627 -template <int __inst>
1.628 -bool _STLP_beos_static_lock_data<__inst>::is_init = false;
1.629 -template <int __inst>
1.630 -typename _STLP_beos_static_lock_data<__inst>::mutex_t _STLP_beos_static_lock_data<__inst>::mut;
1.631 -
1.632 -
1.633 -inline void _STLP_mutex_base::_M_acquire_lock()
1.634 -{
1.635 - if(sem == 0)
1.636 - {
1.637 - // we need to initialise on demand here
1.638 - // to prevent race conditions use our global
1.639 - // mutex if it's available:
1.640 - if(_STLP_beos_static_lock_data<0>::is_init)
1.641 - {
1.642 - _STLP_auto_lock al(_STLP_beos_static_lock_data<0>::mut);
1.643 - if(sem == 0) _M_initialize();
1.644 - }
1.645 - else
1.646 - {
1.647 - // no lock available, we must still be
1.648 - // in startup code, THERE MUST BE ONE THREAD
1.649 - // ONLY active at this point.
1.650 - _M_initialize();
1.651 - }
1.652 - }
1.653 - _M_acquire_lock_nodemand();
1.654 -}
1.655 -
1.656 -#endif
1.657 -
1.658 -_STLP_END_NAMESPACE
1.659 -
1.660 -# if !defined (_STLP_LINK_TIME_INSTANTIATION)
1.661 -# include <stl/_threads.c>
1.662 -# endif
1.663 -
1.664 -#endif /* _STLP_INTERNAL_THREADS_H */
1.665 -
1.666 -// Local Variables:
1.667 -// mode:C++
1.668 -// End:
1.669 -