epoc32/include/stdapis/stlportv5/stl/_threads.c
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
parent 2 epoc32/include/stdapis/stlport/stl/_threads.c@2fe1408b6811
child 4 837f303aceeb
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
williamr@2
     1
/*
williamr@2
     2
 *
williamr@2
     3
 *
williamr@2
     4
 * Copyright (c) 1994
williamr@2
     5
 * Hewlett-Packard Company
williamr@2
     6
 *
williamr@2
     7
 * Copyright (c) 1996,1997
williamr@2
     8
 * Silicon Graphics Computer Systems, Inc.
williamr@2
     9
 *
williamr@2
    10
 * Copyright (c) 1997
williamr@2
    11
 * Moscow Center for SPARC Technology
williamr@2
    12
 *
williamr@2
    13
 * Copyright (c) 1999 
williamr@2
    14
 * Boris Fomitchev
williamr@2
    15
 *
williamr@2
    16
 * This material is provided "as is", with absolutely no warranty expressed
williamr@2
    17
 * or implied. Any use is at your own risk.
williamr@2
    18
 *
williamr@2
    19
 * Permission to use or copy this software for any purpose is hereby granted 
williamr@2
    20
 * without fee, provided the above notices are retained on all copies.
williamr@2
    21
 * Permission to modify the code and to distribute modified code is granted,
williamr@2
    22
 * provided the above notices are retained, and a notice that the code was
williamr@2
    23
 * modified is included with the above copyright notice.
williamr@2
    24
 *
williamr@2
    25
 */
williamr@2
    26
#ifndef _STLP_THREADS_C
williamr@2
    27
#define _STLP_THREADS_C
williamr@2
    28
williamr@2
    29
#ifndef _STLP_INTERNAL_THREADS_H
williamr@2
    30
# include <stl/_threads.h>
williamr@2
    31
#endif
williamr@2
    32
williamr@2
    33
# if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
williamr@2
    34
williamr@2
    35
# if defined(_STLP_SGI_THREADS)
williamr@2
    36
#  include <time.h>
williamr@2
    37
# elif defined (_STLP_UNIX)
williamr@2
    38
#  include <ctime>
williamr@2
    39
# if defined (_STLP_USE_NAMESPACES) && ! defined (_STLP_VENDOR_GLOBAL_CSTD)
williamr@2
    40
using _STLP_VENDOR_CSTD::time_t;
williamr@2
    41
# endif
williamr@2
    42
#  include <sys/time.h>
williamr@2
    43
# endif
williamr@2
    44
williamr@2
    45
_STLP_BEGIN_NAMESPACE
williamr@2
    46
williamr@2
    47
# if (_STLP_STATIC_TEMPLATE_DATA > 0)
williamr@2
    48
williamr@2
    49
#  ifdef _STLP_THREADS
williamr@2
    50
#  if !defined(_STLP_ATOMIC_EXCHANGE) && (defined(_STLP_PTHREADS) || defined(_STLP_UITHREADS) || defined(_STLP_OS2THREADS) || defined(_STLP_USE_PTHREAD_SPINLOCK))
williamr@2
    51
template<int __dummy>
williamr@2
    52
_STLP_STATIC_MUTEX
williamr@2
    53
_Swap_lock_struct<__dummy>::_S_swap_lock _STLP_MUTEX_INITIALIZER;
williamr@2
    54
#  endif
williamr@2
    55
#  endif //_STLP_THREADS
williamr@2
    56
williamr@2
    57
#  ifndef _STLP_USE_PTHREAD_SPINLOCK
williamr@2
    58
template <int __inst>
williamr@2
    59
unsigned _STLP_mutex_spin<__inst>::__max = _STLP_mutex_spin<__inst>::__low_max;
williamr@2
    60
williamr@2
    61
template <int __inst>
williamr@2
    62
unsigned _STLP_mutex_spin<__inst>::__last = 0;
williamr@2
    63
#  endif // _STLP_USE_PTHREAD_SPINLOCK
williamr@2
    64
williamr@2
    65
# else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
williamr@2
    66
williamr@2
    67
#  if defined(_STLP_PTHREADS) || defined(_STLP_UITHREADS) || defined(_STLP_OS2THREADS)
williamr@2
    68
__DECLARE_INSTANCE(_STLP_STATIC_MUTEX, _Swap_lock_struct<0>::_S_swap_lock, 
williamr@2
    69
                   _STLP_MUTEX_INITIALIZER  );
williamr@2
    70
#  endif /* _STLP_PTHREADS */
williamr@2
    71
williamr@2
    72
#  ifndef _STLP_USE_PTHREAD_SPINLOCK
williamr@2
    73
__DECLARE_INSTANCE(unsigned, _STLP_mutex_spin<0>::__max,  =30);
williamr@2
    74
__DECLARE_INSTANCE(unsigned, _STLP_mutex_spin<0>::__last, =0);
williamr@2
    75
#  endif // _STLP_USE_PTHREAD_SPINLOCK
williamr@2
    76
williamr@2
    77
# endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
williamr@2
    78
williamr@2
    79
#ifndef _STLP_USE_PTHREAD_SPINLOCK
williamr@2
    80
williamr@2
    81
#ifdef _STLP_SPARC_SOLARIS_THREADS
williamr@2
    82
// underground function in libc.so; we do not want dependance on librt
williamr@2
    83
extern "C" int __nanosleep(const struct timespec*, struct timespec*);
williamr@2
    84
# define _STLP_NANOSLEEP __nanosleep
williamr@2
    85
#else
williamr@2
    86
# define _STLP_NANOSLEEP nanosleep
williamr@2
    87
#endif
williamr@2
    88
williamr@2
    89
template <int __inst>
williamr@2
    90
void _STLP_CALL
williamr@2
    91
_STLP_mutex_spin<__inst>::_S_nsec_sleep(int __log_nsec) {
williamr@2
    92
#     if defined(_STLP_WIN32THREADS)
williamr@2
    93
	  if (__log_nsec <= 20) {
williamr@2
    94
        // Note from boost (www.boost.org): 
williamr@2
    95
        // Changed to Sleep(1) from Sleep(0).
williamr@2
    96
        // According to MSDN, Sleep(0) will never yield
williamr@2
    97
        // to a lower-priority thread, whereas Sleep(1)
williamr@2
    98
        // will. Performance seems not to be affected.
williamr@2
    99
	      Sleep(1);
williamr@2
   100
	  } else {
williamr@2
   101
	      Sleep(1 << (__log_nsec - 20));
williamr@2
   102
	  }
williamr@2
   103
#    elif defined(_STLP_OS2THREADS)
williamr@2
   104
      if (__log_nsec <= 20) {
williamr@2
   105
         DosSleep(0);
williamr@2
   106
      } else {
williamr@2
   107
         DosSleep(1 << (__log_nsec - 20));
williamr@2
   108
      }
williamr@2
   109
#     elif defined (_STLP_UNIX)
williamr@2
   110
          timespec __ts;
williamr@2
   111
          /* Max sleep is 2**27nsec ~ 60msec      */
williamr@2
   112
          __ts.tv_sec = 0;
williamr@2
   113
          __ts.tv_nsec = 1 << __log_nsec;
williamr@2
   114
          _STLP_NANOSLEEP(&__ts, 0);
williamr@2
   115
#     endif
williamr@2
   116
  }
williamr@2
   117
williamr@2
   118
williamr@2
   119
template <int __inst>
williamr@2
   120
void  _STLP_CALL
williamr@2
   121
_STLP_mutex_spin<__inst>::_M_do_lock(volatile __stl_atomic_t* __lock)
williamr@2
   122
{
williamr@2
   123
#if defined(_STLP_ATOMIC_EXCHANGE)
williamr@2
   124
  if (_Atomic_swap(__lock, 1)) {
williamr@2
   125
    unsigned __my_spin_max = _STLP_mutex_spin<0>::__max;
williamr@2
   126
    unsigned __my_last_spins = _STLP_mutex_spin<0>::__last;
williamr@2
   127
    volatile unsigned __junk = 17; 	// Value doesn't matter.
williamr@2
   128
    unsigned  __i;
williamr@2
   129
    
williamr@2
   130
    for (__i = 0; __i < __my_spin_max; ++__i) {
williamr@2
   131
      if (__i < __my_last_spins/2 || *__lock) {
williamr@2
   132
        __junk *= __junk; __junk *= __junk;
williamr@2
   133
        __junk *= __junk; __junk *= __junk;
williamr@2
   134
      } else {
williamr@2
   135
        if (!_Atomic_swap(__lock, 1)) {
williamr@2
   136
          // got it!
williamr@2
   137
          // Spinning worked.  Thus we're probably not being scheduled
williamr@2
   138
          // against the other process with which we were contending.
williamr@2
   139
          // Thus it makes sense to spin longer the next time.
williamr@2
   140
          _STLP_mutex_spin<0>::__last = __i;
williamr@2
   141
          _STLP_mutex_spin<0>::__max = _STLP_mutex_spin<0>::__high_max;
williamr@2
   142
	    return;
williamr@2
   143
        }
williamr@2
   144
      }
williamr@2
   145
    }
williamr@2
   146
    
williamr@2
   147
    // We are probably being scheduled against the other process.  Sleep.
williamr@2
   148
    _STLP_mutex_spin<0>::__max = _STLP_mutex_spin<0>::__low_max;
williamr@2
   149
    
williamr@2
   150
    for (__i = 0 ;; ++__i) {
williamr@2
   151
      int __log_nsec = __i + 6;
williamr@2
   152
      
williamr@2
   153
      if (__log_nsec > 27) __log_nsec = 27;
williamr@2
   154
      if (!_Atomic_swap(__lock, 1)) {
williamr@2
   155
	  break;
williamr@2
   156
      }
williamr@2
   157
      _S_nsec_sleep(__log_nsec);
williamr@2
   158
    }
williamr@2
   159
    
williamr@2
   160
  } /* first _Atomic_swap */
williamr@2
   161
# endif
williamr@2
   162
}
williamr@2
   163
#endif // _STLP_USE_PTHREAD_SPINLOCK
williamr@2
   164
williamr@2
   165
_STLP_END_NAMESPACE
williamr@2
   166
williamr@2
   167
# endif /* BUILDING_STLPORT */
williamr@2
   168
#endif /*  _STLP_THREADS_C */
williamr@2
   169
williamr@2
   170
// Local Variables:
williamr@2
   171
// mode:C++
williamr@2
   172
// End: