1.1 --- a/epoc32/include/stdapis/stlportv5/stl/_threads.c Wed Mar 31 12:27:01 2010 +0100
1.2 +++ b/epoc32/include/stdapis/stlportv5/stl/_threads.c Wed Mar 31 12:33:34 2010 +0100
1.3 @@ -1,5 +1,5 @@
1.4 /*
1.5 - *
1.6 + * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.7 *
1.8 * Copyright (c) 1994
1.9 * Hewlett-Packard Company
1.10 @@ -10,13 +10,13 @@
1.11 * Copyright (c) 1997
1.12 * Moscow Center for SPARC Technology
1.13 *
1.14 - * Copyright (c) 1999
1.15 + * Copyright (c) 1999
1.16 * Boris Fomitchev
1.17 *
1.18 * This material is provided "as is", with absolutely no warranty expressed
1.19 * or implied. Any use is at your own risk.
1.20 *
1.21 - * Permission to use or copy this software for any purpose is hereby granted
1.22 + * Permission to use or copy this software for any purpose is hereby granted
1.23 * without fee, provided the above notices are retained on all copies.
1.24 * Permission to modify the code and to distribute modified code is granted,
1.25 * provided the above notices are retained, and a notice that the code was
1.26 @@ -27,106 +27,111 @@
1.27 #define _STLP_THREADS_C
1.28
1.29 #ifndef _STLP_INTERNAL_THREADS_H
1.30 -# include <stl/_threads.h>
1.31 +# include <stl/_threads.h>
1.32 #endif
1.33
1.34 -# if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
1.35 +#if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
1.36
1.37 -# if defined(_STLP_SGI_THREADS)
1.38 +#if defined (_STLP_SGI_THREADS)
1.39 # include <time.h>
1.40 -# elif defined (_STLP_UNIX)
1.41 -# include <ctime>
1.42 -# if defined (_STLP_USE_NAMESPACES) && ! defined (_STLP_VENDOR_GLOBAL_CSTD)
1.43 +#elif defined (_STLP_UNIX)
1.44 +# ifndef _STLP_INTERNAL_CTIME
1.45 +# include <stl/_ctime.h>
1.46 +# endif
1.47 +# if defined (_STLP_USE_NAMESPACES) && !defined (_STLP_VENDOR_GLOBAL_CSTD)
1.48 using _STLP_VENDOR_CSTD::time_t;
1.49 -# endif
1.50 +# endif
1.51 # include <sys/time.h>
1.52 -# endif
1.53 +#endif
1.54
1.55 _STLP_BEGIN_NAMESPACE
1.56
1.57 -# if (_STLP_STATIC_TEMPLATE_DATA > 0)
1.58 +#if (_STLP_STATIC_TEMPLATE_DATA > 0)
1.59
1.60 -# ifdef _STLP_THREADS
1.61 -# if !defined(_STLP_ATOMIC_EXCHANGE) && (defined(_STLP_PTHREADS) || defined(_STLP_UITHREADS) || defined(_STLP_OS2THREADS) || defined(_STLP_USE_PTHREAD_SPINLOCK))
1.62 -template<int __dummy>
1.63 +# if defined (_STLP_USE_ATOMIC_SWAP_MUTEX)
1.64 +#if !defined(__SYMBIAN32__WSD__)
1.65 +template<int __32bits>
1.66 _STLP_STATIC_MUTEX
1.67 -_Swap_lock_struct<__dummy>::_S_swap_lock _STLP_MUTEX_INITIALIZER;
1.68 +_Atomic_swap_struct<__32bits>::_S_swap_lock _STLP_MUTEX_INITIALIZER;
1.69 +#endif
1.70 +# undef _STLP_USE_ATOMIC_SWAP_MUTEX
1.71 # endif
1.72 -# endif //_STLP_THREADS
1.73
1.74 -# ifndef _STLP_USE_PTHREAD_SPINLOCK
1.75 +# if defined (_STLP_THREADS) && !defined (_STLP_USE_PTHREAD_SPINLOCK)
1.76 +//Note: For SYMBIAN Emulator, these entries are to be considered WSD.
1.77 +//Still, EWSD solution can't be applied since it's templated.
1.78 template <int __inst>
1.79 unsigned _STLP_mutex_spin<__inst>::__max = _STLP_mutex_spin<__inst>::__low_max;
1.80
1.81 template <int __inst>
1.82 unsigned _STLP_mutex_spin<__inst>::__last = 0;
1.83 +
1.84 # endif // _STLP_USE_PTHREAD_SPINLOCK
1.85
1.86 -# else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
1.87 +#else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
1.88
1.89 -# if defined(_STLP_PTHREADS) || defined(_STLP_UITHREADS) || defined(_STLP_OS2THREADS)
1.90 -__DECLARE_INSTANCE(_STLP_STATIC_MUTEX, _Swap_lock_struct<0>::_S_swap_lock,
1.91 +# if defined (_STLP_USE_ATOMIC_SWAP_MUTEX)
1.92 +__DECLARE_INSTANCE(_STLP_STATIC_MUTEX, _Atomic_swap_struct<sizeof(__stl_atomic_t) == sizeof(void*)>::_S_swap_lock,
1.93 _STLP_MUTEX_INITIALIZER );
1.94 +# undef _STLP_USE_ATOMIC_SWAP_MUTEX
1.95 # endif /* _STLP_PTHREADS */
1.96
1.97 -# ifndef _STLP_USE_PTHREAD_SPINLOCK
1.98 +# if defined (_STLP_THREADS) && !defined (_STLP_USE_PTHREAD_SPINLOCK)
1.99 __DECLARE_INSTANCE(unsigned, _STLP_mutex_spin<0>::__max, =30);
1.100 __DECLARE_INSTANCE(unsigned, _STLP_mutex_spin<0>::__last, =0);
1.101 # endif // _STLP_USE_PTHREAD_SPINLOCK
1.102
1.103 -# endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
1.104 +#endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
1.105
1.106 -#ifndef _STLP_USE_PTHREAD_SPINLOCK
1.107 +#if defined (_STLP_THREADS) && !defined (_STLP_USE_PTHREAD_SPINLOCK)
1.108
1.109 -#ifdef _STLP_SPARC_SOLARIS_THREADS
1.110 +# if defined (_STLP_SPARC_SOLARIS_THREADS)
1.111 // underground function in libc.so; we do not want dependance on librt
1.112 extern "C" int __nanosleep(const struct timespec*, struct timespec*);
1.113 -# define _STLP_NANOSLEEP __nanosleep
1.114 -#else
1.115 -# define _STLP_NANOSLEEP nanosleep
1.116 -#endif
1.117 +# define _STLP_NANOSLEEP __nanosleep
1.118 +# else
1.119 +# define _STLP_NANOSLEEP nanosleep
1.120 +# endif
1.121
1.122 template <int __inst>
1.123 void _STLP_CALL
1.124 _STLP_mutex_spin<__inst>::_S_nsec_sleep(int __log_nsec) {
1.125 -# if defined(_STLP_WIN32THREADS)
1.126 - if (__log_nsec <= 20) {
1.127 - // Note from boost (www.boost.org):
1.128 - // Changed to Sleep(1) from Sleep(0).
1.129 - // According to MSDN, Sleep(0) will never yield
1.130 - // to a lower-priority thread, whereas Sleep(1)
1.131 - // will. Performance seems not to be affected.
1.132 - Sleep(1);
1.133 - } else {
1.134 - Sleep(1 << (__log_nsec - 20));
1.135 - }
1.136 -# elif defined(_STLP_OS2THREADS)
1.137 - if (__log_nsec <= 20) {
1.138 - DosSleep(0);
1.139 - } else {
1.140 - DosSleep(1 << (__log_nsec - 20));
1.141 - }
1.142 -# elif defined (_STLP_UNIX)
1.143 - timespec __ts;
1.144 - /* Max sleep is 2**27nsec ~ 60msec */
1.145 - __ts.tv_sec = 0;
1.146 - __ts.tv_nsec = 1 << __log_nsec;
1.147 - _STLP_NANOSLEEP(&__ts, 0);
1.148 -# endif
1.149 +# if defined (_STLP_WIN32THREADS)
1.150 + if (__log_nsec <= 20) {
1.151 + // Note from boost (www.boost.org):
1.152 + // Changed to Sleep(1) from Sleep(0).
1.153 + // According to MSDN, Sleep(0) will never yield
1.154 + // to a lower-priority thread, whereas Sleep(1)
1.155 + // will. Performance seems not to be affected.
1.156 + Sleep(1);
1.157 + } else {
1.158 + Sleep(1 << (__log_nsec - 20));
1.159 }
1.160 -
1.161 +# elif defined(_STLP_OS2THREADS)
1.162 + if (__log_nsec <= 20) {
1.163 + DosSleep(0);
1.164 + } else {
1.165 + DosSleep(1 << (__log_nsec - 20));
1.166 + }
1.167 +# elif defined (_STLP_UNIX)
1.168 + timespec __ts;
1.169 + /* Max sleep is 2**27nsec ~ 60msec */
1.170 + __ts.tv_sec = 0;
1.171 + __ts.tv_nsec = 1 << __log_nsec;
1.172 + _STLP_NANOSLEEP(&__ts, 0);
1.173 +# endif
1.174 +}
1.175
1.176 template <int __inst>
1.177 void _STLP_CALL
1.178 -_STLP_mutex_spin<__inst>::_M_do_lock(volatile __stl_atomic_t* __lock)
1.179 -{
1.180 -#if defined(_STLP_ATOMIC_EXCHANGE)
1.181 +_STLP_mutex_spin<__inst>::_M_do_lock(volatile __stl_atomic_t* __lock) {
1.182 +# if defined(_STLP_ATOMIC_EXCHANGE)
1.183 if (_Atomic_swap(__lock, 1)) {
1.184 unsigned __my_spin_max = _STLP_mutex_spin<0>::__max;
1.185 unsigned __my_last_spins = _STLP_mutex_spin<0>::__last;
1.186 - volatile unsigned __junk = 17; // Value doesn't matter.
1.187 + volatile unsigned __junk = 17; // Value doesn't matter.
1.188 unsigned __i;
1.189 -
1.190 +
1.191 for (__i = 0; __i < __my_spin_max; ++__i) {
1.192 if (__i < __my_last_spins/2 || *__lock) {
1.193 __junk *= __junk; __junk *= __junk;
1.194 @@ -139,32 +144,31 @@
1.195 // Thus it makes sense to spin longer the next time.
1.196 _STLP_mutex_spin<0>::__last = __i;
1.197 _STLP_mutex_spin<0>::__max = _STLP_mutex_spin<0>::__high_max;
1.198 - return;
1.199 + return;
1.200 }
1.201 }
1.202 }
1.203 -
1.204 +
1.205 // We are probably being scheduled against the other process. Sleep.
1.206 _STLP_mutex_spin<0>::__max = _STLP_mutex_spin<0>::__low_max;
1.207 -
1.208 +
1.209 for (__i = 0 ;; ++__i) {
1.210 int __log_nsec = __i + 6;
1.211 -
1.212 +
1.213 if (__log_nsec > 27) __log_nsec = 27;
1.214 if (!_Atomic_swap(__lock, 1)) {
1.215 - break;
1.216 + break;
1.217 }
1.218 _S_nsec_sleep(__log_nsec);
1.219 }
1.220 -
1.221 } /* first _Atomic_swap */
1.222 -# endif
1.223 +# endif
1.224 }
1.225 #endif // _STLP_USE_PTHREAD_SPINLOCK
1.226
1.227 _STLP_END_NAMESPACE
1.228
1.229 -# endif /* BUILDING_STLPORT */
1.230 +#endif /* _STLP_EXPOSE_GLOBALS_IMPLEMENTATION */
1.231 #endif /* _STLP_THREADS_C */
1.232
1.233 // Local Variables: