epoc32/include/stdapis/stlportv5/stl/_threads.c
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
parent 3 e1b950c65cb4
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@2
     1
/*
williamr@4
     2
 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
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@4
    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@4
    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@4
    30
#  include <stl/_threads.h>
williamr@2
    31
#endif
williamr@2
    32
williamr@4
    33
#if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
williamr@2
    34
williamr@4
    35
#if defined (_STLP_SGI_THREADS)
williamr@2
    36
#  include <time.h>
williamr@4
    37
#elif defined (_STLP_UNIX)
williamr@4
    38
#  ifndef _STLP_INTERNAL_CTIME
williamr@4
    39
#    include <stl/_ctime.h>
williamr@4
    40
#  endif
williamr@4
    41
#  if defined (_STLP_USE_NAMESPACES) && !defined (_STLP_VENDOR_GLOBAL_CSTD)
williamr@2
    42
using _STLP_VENDOR_CSTD::time_t;
williamr@4
    43
#  endif
williamr@2
    44
#  include <sys/time.h>
williamr@4
    45
#endif
williamr@2
    46
williamr@2
    47
_STLP_BEGIN_NAMESPACE
williamr@2
    48
williamr@4
    49
#if (_STLP_STATIC_TEMPLATE_DATA > 0)
williamr@2
    50
williamr@4
    51
#  if defined (_STLP_USE_ATOMIC_SWAP_MUTEX)
williamr@4
    52
#if !defined(__SYMBIAN32__WSD__)
williamr@4
    53
template<int __32bits>
williamr@2
    54
_STLP_STATIC_MUTEX
williamr@4
    55
_Atomic_swap_struct<__32bits>::_S_swap_lock _STLP_MUTEX_INITIALIZER;
williamr@4
    56
#endif
williamr@4
    57
#    undef _STLP_USE_ATOMIC_SWAP_MUTEX
williamr@2
    58
#  endif
williamr@2
    59
williamr@4
    60
#  if defined (_STLP_THREADS) && !defined (_STLP_USE_PTHREAD_SPINLOCK)
williamr@4
    61
//Note: For SYMBIAN Emulator, these entries are to be considered WSD.  
williamr@4
    62
//Still, EWSD solution can't be applied since it's templated.
williamr@2
    63
template <int __inst>
williamr@2
    64
unsigned _STLP_mutex_spin<__inst>::__max = _STLP_mutex_spin<__inst>::__low_max;
williamr@2
    65
williamr@2
    66
template <int __inst>
williamr@2
    67
unsigned _STLP_mutex_spin<__inst>::__last = 0;
williamr@4
    68
williamr@2
    69
#  endif // _STLP_USE_PTHREAD_SPINLOCK
williamr@2
    70
williamr@4
    71
#else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
williamr@2
    72
williamr@4
    73
#  if defined (_STLP_USE_ATOMIC_SWAP_MUTEX)
williamr@4
    74
__DECLARE_INSTANCE(_STLP_STATIC_MUTEX, _Atomic_swap_struct<sizeof(__stl_atomic_t) == sizeof(void*)>::_S_swap_lock,
williamr@2
    75
                   _STLP_MUTEX_INITIALIZER  );
williamr@4
    76
#    undef _STLP_USE_ATOMIC_SWAP_MUTEX
williamr@2
    77
#  endif /* _STLP_PTHREADS */
williamr@2
    78
williamr@4
    79
#  if defined (_STLP_THREADS) && !defined (_STLP_USE_PTHREAD_SPINLOCK)
williamr@2
    80
__DECLARE_INSTANCE(unsigned, _STLP_mutex_spin<0>::__max,  =30);
williamr@2
    81
__DECLARE_INSTANCE(unsigned, _STLP_mutex_spin<0>::__last, =0);
williamr@2
    82
#  endif // _STLP_USE_PTHREAD_SPINLOCK
williamr@2
    83
williamr@4
    84
#endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
williamr@2
    85
williamr@4
    86
#if defined (_STLP_THREADS) && !defined (_STLP_USE_PTHREAD_SPINLOCK)
williamr@2
    87
williamr@4
    88
#  if defined (_STLP_SPARC_SOLARIS_THREADS)
williamr@2
    89
// underground function in libc.so; we do not want dependance on librt
williamr@2
    90
extern "C" int __nanosleep(const struct timespec*, struct timespec*);
williamr@4
    91
#    define _STLP_NANOSLEEP __nanosleep
williamr@4
    92
#  else
williamr@4
    93
#    define _STLP_NANOSLEEP nanosleep
williamr@4
    94
#  endif
williamr@2
    95
williamr@2
    96
template <int __inst>
williamr@2
    97
void _STLP_CALL
williamr@2
    98
_STLP_mutex_spin<__inst>::_S_nsec_sleep(int __log_nsec) {
williamr@4
    99
#  if defined (_STLP_WIN32THREADS)
williamr@4
   100
  if (__log_nsec <= 20) {
williamr@4
   101
    // Note from boost (www.boost.org):
williamr@4
   102
    // Changed to Sleep(1) from Sleep(0).
williamr@4
   103
    // According to MSDN, Sleep(0) will never yield
williamr@4
   104
    // to a lower-priority thread, whereas Sleep(1)
williamr@4
   105
    // will. Performance seems not to be affected.
williamr@4
   106
    Sleep(1);
williamr@4
   107
  } else {
williamr@4
   108
    Sleep(1 << (__log_nsec - 20));
williamr@2
   109
  }
williamr@4
   110
#  elif defined(_STLP_OS2THREADS)
williamr@4
   111
  if (__log_nsec <= 20) {
williamr@4
   112
    DosSleep(0);
williamr@4
   113
  } else {
williamr@4
   114
    DosSleep(1 << (__log_nsec - 20));
williamr@4
   115
  }
williamr@4
   116
#  elif defined (_STLP_UNIX)
williamr@4
   117
  timespec __ts;
williamr@4
   118
  /* Max sleep is 2**27nsec ~ 60msec      */
williamr@4
   119
  __ts.tv_sec = 0;
williamr@4
   120
  __ts.tv_nsec = 1 << __log_nsec;
williamr@4
   121
  _STLP_NANOSLEEP(&__ts, 0);
williamr@4
   122
#  endif
williamr@4
   123
}
williamr@2
   124
williamr@2
   125
template <int __inst>
williamr@2
   126
void  _STLP_CALL
williamr@4
   127
_STLP_mutex_spin<__inst>::_M_do_lock(volatile __stl_atomic_t* __lock) {
williamr@4
   128
#  if defined(_STLP_ATOMIC_EXCHANGE)
williamr@2
   129
  if (_Atomic_swap(__lock, 1)) {
williamr@2
   130
    unsigned __my_spin_max = _STLP_mutex_spin<0>::__max;
williamr@2
   131
    unsigned __my_last_spins = _STLP_mutex_spin<0>::__last;
williamr@4
   132
    volatile unsigned __junk = 17;   // Value doesn't matter.
williamr@2
   133
    unsigned  __i;
williamr@4
   134
williamr@2
   135
    for (__i = 0; __i < __my_spin_max; ++__i) {
williamr@2
   136
      if (__i < __my_last_spins/2 || *__lock) {
williamr@2
   137
        __junk *= __junk; __junk *= __junk;
williamr@2
   138
        __junk *= __junk; __junk *= __junk;
williamr@2
   139
      } else {
williamr@2
   140
        if (!_Atomic_swap(__lock, 1)) {
williamr@2
   141
          // got it!
williamr@2
   142
          // Spinning worked.  Thus we're probably not being scheduled
williamr@2
   143
          // against the other process with which we were contending.
williamr@2
   144
          // Thus it makes sense to spin longer the next time.
williamr@2
   145
          _STLP_mutex_spin<0>::__last = __i;
williamr@2
   146
          _STLP_mutex_spin<0>::__max = _STLP_mutex_spin<0>::__high_max;
williamr@4
   147
          return;
williamr@2
   148
        }
williamr@2
   149
      }
williamr@2
   150
    }
williamr@4
   151
williamr@2
   152
    // We are probably being scheduled against the other process.  Sleep.
williamr@2
   153
    _STLP_mutex_spin<0>::__max = _STLP_mutex_spin<0>::__low_max;
williamr@4
   154
williamr@2
   155
    for (__i = 0 ;; ++__i) {
williamr@2
   156
      int __log_nsec = __i + 6;
williamr@4
   157
williamr@2
   158
      if (__log_nsec > 27) __log_nsec = 27;
williamr@2
   159
      if (!_Atomic_swap(__lock, 1)) {
williamr@4
   160
        break;
williamr@2
   161
      }
williamr@2
   162
      _S_nsec_sleep(__log_nsec);
williamr@2
   163
    }
williamr@2
   164
  } /* first _Atomic_swap */
williamr@4
   165
#  endif
williamr@2
   166
}
williamr@2
   167
#endif // _STLP_USE_PTHREAD_SPINLOCK
williamr@2
   168
williamr@2
   169
_STLP_END_NAMESPACE
williamr@2
   170
williamr@4
   171
#endif /* _STLP_EXPOSE_GLOBALS_IMPLEMENTATION */
williamr@2
   172
#endif /*  _STLP_THREADS_C */
williamr@2
   173
williamr@2
   174
// Local Variables:
williamr@2
   175
// mode:C++
williamr@2
   176
// End: