1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/stlport/stl/_threads.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,176 @@
1.4 +/*
1.5 + * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.6 + *
1.7 + * Copyright (c) 1994
1.8 + * Hewlett-Packard Company
1.9 + *
1.10 + * Copyright (c) 1996,1997
1.11 + * Silicon Graphics Computer Systems, Inc.
1.12 + *
1.13 + * Copyright (c) 1997
1.14 + * Moscow Center for SPARC Technology
1.15 + *
1.16 + * Copyright (c) 1999
1.17 + * Boris Fomitchev
1.18 + *
1.19 + * This material is provided "as is", with absolutely no warranty expressed
1.20 + * or implied. Any use is at your own risk.
1.21 + *
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 + * modified is included with the above copyright notice.
1.27 + *
1.28 + */
1.29 +#ifndef _STLP_THREADS_C
1.30 +#define _STLP_THREADS_C
1.31 +
1.32 +#ifndef _STLP_INTERNAL_THREADS_H
1.33 +# include <stl/_threads.h>
1.34 +#endif
1.35 +
1.36 +#if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
1.37 +
1.38 +#if defined (_STLP_SGI_THREADS)
1.39 +# include <time.h>
1.40 +#elif defined (_STLP_UNIX)
1.41 +# ifndef _STLP_INTERNAL_CTIME
1.42 +# include <stl/_ctime.h>
1.43 +# endif
1.44 +# if defined (_STLP_USE_NAMESPACES) && !defined (_STLP_VENDOR_GLOBAL_CSTD)
1.45 +using _STLP_VENDOR_CSTD::time_t;
1.46 +# endif
1.47 +# include <sys/time.h>
1.48 +#endif
1.49 +
1.50 +_STLP_BEGIN_NAMESPACE
1.51 +
1.52 +#if (_STLP_STATIC_TEMPLATE_DATA > 0)
1.53 +
1.54 +# if defined (_STLP_USE_ATOMIC_SWAP_MUTEX)
1.55 +#if !defined(__SYMBIAN32__WSD__)
1.56 +template<int __32bits>
1.57 +_STLP_STATIC_MUTEX
1.58 +_Atomic_swap_struct<__32bits>::_S_swap_lock _STLP_MUTEX_INITIALIZER;
1.59 +#endif
1.60 +# undef _STLP_USE_ATOMIC_SWAP_MUTEX
1.61 +# endif
1.62 +
1.63 +# if defined (_STLP_THREADS) && !defined (_STLP_USE_PTHREAD_SPINLOCK)
1.64 +//Note: For SYMBIAN Emulator, these entries are to be considered WSD.
1.65 +//Still, EWSD solution can't be applied since it's templated.
1.66 +template <int __inst>
1.67 +unsigned _STLP_mutex_spin<__inst>::__max = _STLP_mutex_spin<__inst>::__low_max;
1.68 +
1.69 +template <int __inst>
1.70 +unsigned _STLP_mutex_spin<__inst>::__last = 0;
1.71 +
1.72 +# endif // _STLP_USE_PTHREAD_SPINLOCK
1.73 +
1.74 +#else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
1.75 +
1.76 +# if defined (_STLP_USE_ATOMIC_SWAP_MUTEX)
1.77 +__DECLARE_INSTANCE(_STLP_STATIC_MUTEX, _Atomic_swap_struct<sizeof(__stl_atomic_t) == sizeof(void*)>::_S_swap_lock,
1.78 + _STLP_MUTEX_INITIALIZER );
1.79 +# undef _STLP_USE_ATOMIC_SWAP_MUTEX
1.80 +# endif /* _STLP_PTHREADS */
1.81 +
1.82 +# if defined (_STLP_THREADS) && !defined (_STLP_USE_PTHREAD_SPINLOCK)
1.83 +__DECLARE_INSTANCE(unsigned, _STLP_mutex_spin<0>::__max, =30);
1.84 +__DECLARE_INSTANCE(unsigned, _STLP_mutex_spin<0>::__last, =0);
1.85 +# endif // _STLP_USE_PTHREAD_SPINLOCK
1.86 +
1.87 +#endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
1.88 +
1.89 +#if defined (_STLP_THREADS) && !defined (_STLP_USE_PTHREAD_SPINLOCK)
1.90 +
1.91 +# if defined (_STLP_SPARC_SOLARIS_THREADS)
1.92 +// underground function in libc.so; we do not want dependance on librt
1.93 +extern "C" int __nanosleep(const struct timespec*, struct timespec*);
1.94 +# define _STLP_NANOSLEEP __nanosleep
1.95 +# else
1.96 +# define _STLP_NANOSLEEP nanosleep
1.97 +# endif
1.98 +
1.99 +template <int __inst>
1.100 +void _STLP_CALL
1.101 +_STLP_mutex_spin<__inst>::_S_nsec_sleep(int __log_nsec) {
1.102 +# if defined (_STLP_WIN32THREADS)
1.103 + if (__log_nsec <= 20) {
1.104 + // Note from boost (www.boost.org):
1.105 + // Changed to Sleep(1) from Sleep(0).
1.106 + // According to MSDN, Sleep(0) will never yield
1.107 + // to a lower-priority thread, whereas Sleep(1)
1.108 + // will. Performance seems not to be affected.
1.109 + Sleep(1);
1.110 + } else {
1.111 + Sleep(1 << (__log_nsec - 20));
1.112 + }
1.113 +# elif defined(_STLP_OS2THREADS)
1.114 + if (__log_nsec <= 20) {
1.115 + DosSleep(0);
1.116 + } else {
1.117 + DosSleep(1 << (__log_nsec - 20));
1.118 + }
1.119 +# elif defined (_STLP_UNIX)
1.120 + timespec __ts;
1.121 + /* Max sleep is 2**27nsec ~ 60msec */
1.122 + __ts.tv_sec = 0;
1.123 + __ts.tv_nsec = 1 << __log_nsec;
1.124 + _STLP_NANOSLEEP(&__ts, 0);
1.125 +# endif
1.126 +}
1.127 +
1.128 +template <int __inst>
1.129 +void _STLP_CALL
1.130 +_STLP_mutex_spin<__inst>::_M_do_lock(volatile __stl_atomic_t* __lock) {
1.131 +# if defined(_STLP_ATOMIC_EXCHANGE)
1.132 + if (_Atomic_swap(__lock, 1)) {
1.133 + unsigned __my_spin_max = _STLP_mutex_spin<0>::__max;
1.134 + unsigned __my_last_spins = _STLP_mutex_spin<0>::__last;
1.135 + volatile unsigned __junk = 17; // Value doesn't matter.
1.136 + unsigned __i;
1.137 +
1.138 + for (__i = 0; __i < __my_spin_max; ++__i) {
1.139 + if (__i < __my_last_spins/2 || *__lock) {
1.140 + __junk *= __junk; __junk *= __junk;
1.141 + __junk *= __junk; __junk *= __junk;
1.142 + } else {
1.143 + if (!_Atomic_swap(__lock, 1)) {
1.144 + // got it!
1.145 + // Spinning worked. Thus we're probably not being scheduled
1.146 + // against the other process with which we were contending.
1.147 + // Thus it makes sense to spin longer the next time.
1.148 + _STLP_mutex_spin<0>::__last = __i;
1.149 + _STLP_mutex_spin<0>::__max = _STLP_mutex_spin<0>::__high_max;
1.150 + return;
1.151 + }
1.152 + }
1.153 + }
1.154 +
1.155 + // We are probably being scheduled against the other process. Sleep.
1.156 + _STLP_mutex_spin<0>::__max = _STLP_mutex_spin<0>::__low_max;
1.157 +
1.158 + for (__i = 0 ;; ++__i) {
1.159 + int __log_nsec = __i + 6;
1.160 +
1.161 + if (__log_nsec > 27) __log_nsec = 27;
1.162 + if (!_Atomic_swap(__lock, 1)) {
1.163 + break;
1.164 + }
1.165 + _S_nsec_sleep(__log_nsec);
1.166 + }
1.167 + } /* first _Atomic_swap */
1.168 +# endif
1.169 +}
1.170 +#endif // _STLP_USE_PTHREAD_SPINLOCK
1.171 +
1.172 +_STLP_END_NAMESPACE
1.173 +
1.174 +#endif /* _STLP_EXPOSE_GLOBALS_IMPLEMENTATION */
1.175 +#endif /* _STLP_THREADS_C */
1.176 +
1.177 +// Local Variables:
1.178 +// mode:C++
1.179 +// End: