⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _threads.h

📁 MONA是为数不多的C++语言编写的一个很小的操作系统
💻 H
📖 第 1 页 / 共 2 页
字号:
  }  inline void _M_acquire_lock_nodemand() {     pthread_spin_lock( &_M_lock );   }  inline void _M_release_lock() { pthread_spin_unlock( &_M_lock ); }#  else // !_STLP_USE_PTHREAD_SPINLOCK  pthread_mutex_t _M_lock;  inline void _M_initialize() {    pthread_mutex_init(&_M_lock,_STLP_PTHREAD_ATTR_DEFAULT);  }  inline void _M_destroy() {    pthread_mutex_destroy(&_M_lock);  }  inline void _M_acquire_lock_nodemand() {     pthread_mutex_lock(&_M_lock);  }  inline void _M_acquire_lock() { #    if defined (__hpux) && !defined (PTHREAD_MUTEX_INITIALIZER)      if (!_M_lock.field1)  _M_initialize();#    endif     pthread_mutex_lock(&_M_lock);  }  inline void _M_release_lock() { pthread_mutex_unlock(&_M_lock); }#  endif // !_STLP_USE_PTHREAD_SPINLOCK  # elif defined (_STLP_UITHREADS)  mutex_t _M_lock;  inline void _M_initialize() {    mutex_init(&_M_lock,0,NULL);	  }  inline void _M_destroy() {    mutex_destroy(&_M_lock);  }  inline void _M_acquire_lock() { mutex_lock(&_M_lock); }  inline void _M_release_lock() { mutex_unlock(&_M_lock); }# elif defined(_STLP_OS2THREADS)  HMTX _M_lock;  inline void _M_initialize() { DosCreateMutexSem(NULL, &_M_lock, 0, false); }  inline void _M_destroy() { DosCloseMutexSem(_M_lock); }  inline void _M_acquire_lock_nodemand() {    DosRequestMutexSem(_M_lock, SEM_INDEFINITE_WAIT);  }    inline void _M_acquire_lock() {    if(!_M_lock) _M_initialize();    DosRequestMutexSem(_M_lock, SEM_INDEFINITE_WAIT);  }  inline void _M_release_lock() { DosReleaseMutexSem(_M_lock); }# elif defined(_STLP_BETHREADS)  sem_id sem;  inline void _M_initialize()   {     sem = create_sem(1, "STLPort");     assert(sem > 0);  }  inline void _M_destroy()   {     int t = delete_sem(sem);     assert(t == B_NO_ERROR);  }  inline void _M_acquire_lock_nodemand()  {    status_t t;    t = acquire_sem(sem);    assert(t == B_NO_ERROR);  }  inline void _M_acquire_lock();  inline void _M_release_lock()   {     status_t t = release_sem(sem);     assert(t == B_NO_ERROR);  }# else		//*ty 11/24/2001 - added configuration check#  error "Unknown thread facility configuration"# endif#else /* No threads */  inline void _M_initialize() {}  inline void _M_destroy() {}  inline void _M_acquire_lock() {}  inline void _M_release_lock() {}#endif // _STLP_PTHREADS};#if defined (_STLP_THREADS) && defined (_STLP_MUTEX_NEEDS_ONDEMAND_INITIALIZATION)// for use in _STLP_mutex, our purposes do not require ondemand initialization// also, mutex_base may use some hacks to determine uninitialized state by zero data, which only works for globals.class _STLP_CLASS_DECLSPEC _STLP_mutex_nodemand : public _STLP_mutex_base {  inline void _M_acquire_lock() {     _M_acquire_lock_nodemand();  }};#elsetypedef _STLP_mutex_base _STLP_mutex_nodemand;#endif// Locking class.  The constructor initializes the lock, the destructor destroys it.// Well - behaving class, does not need static initializerclass _STLP_CLASS_DECLSPEC _STLP_mutex : public _STLP_mutex_nodemand {  public:    inline _STLP_mutex () { _M_initialize(); }    inline ~_STLP_mutex () { _M_destroy(); }  private:    _STLP_mutex(const _STLP_mutex&);    void operator=(const _STLP_mutex&);};#if 0 /* def _STLP_DEBUG *//* * Recursive Safe locking class.*/# ifndef _STLP_THREADSclass _STLP_mutex_RS{};# elseclass _STLP_CLASS_DECLSPEC _STLP_mutex_RS : public _STLP_mutex{  public:    _STLP_mutex_RS()      : _count( 0 )#  if defined(_STLP_UITHREADS)        ,_id( __STATIC_CAST(thread_t,-1) )#  elif defined(_STLP_PTHREADS)#   if !defined(__FreeBSD__) && !defined(__DECCXX)        ,_id( __STATIC_CAST(pthread_t,-1) )#   else        ,_id( __STATIC_CAST(pthread_t,0) )#   endif /*__FreeBSD__*/#  elif defined(__FIT_NOVELL_THREADS)        ,_id( -1 )#  elif defined(_STLP_WIN32THREADS)        ,_id( NULL )#  else#   error "New _STLP_mutex_RS class not yet ported to this platform"#  endif    {}    ~_STLP_mutex_RS()    {}    void _M_acquire_lock() {#  if defined(_STLP_PTHREADS)      pthread_t _c_id = pthread_self();#  elif defined(_STLP_UITHREADS)      thread_t _c_id = thr_self();#  elif defined(__FIT_NOVELL_THREADS)      int _c_id = GetThreadID();#  elif defined(_STLP_WIN32THREADS)      DWORD _c_id = GetCurrentThreadId();#  endif      if ( _c_id == _id ) {        ++_count;        return;      }      _STLP_mutex::_M_acquire_lock();      _id = _c_id;      ++_count;    }    void _M_release_lock() {      if ( --_count == 0 ) {#  if defined(_STLP_UITHREADS)        _id = __STATIC_CAST(thread_t,-1);#  elif defined(_STLP_PTHREADS)#   if !defined(__FreeBSD__) && !defined(__DECCXX)        _id =  __STATIC_CAST(pthread_t,-1);#   else        _id =  __STATIC_CAST(pthread_t,0);#   endif /*__FreeBSD__*/#  elif defined(__FIT_NOVELL_THREADS)        _id = -1;#  elif defined(_STLP_WIN32THREADS)        _id = NULL;#  endif        _STLP_mutex::_M_release_lock();      }    }  protected:    unsigned int _count;#  if defined(_STLP_PTHREADS)    pthread_t _id;#  elif defined(_STLP_UITHREADS)    thread_t  _id;#  elif defined(__FIT_NOVELL_THREADS)    int _id;#  elif defined(_STLP_WIN32THREADS)    DWORD _id;#  endif};# endif /* _STLP_THREADS */#endif /* OBSOLETE, _STLP_DEBUG *//* * Class _Refcount_Base provides a type, __stl_atomic_t, a data member, * _M_ref_count, and member functions _M_incr and _M_decr, which perform * atomic preincrement/predecrement.  The constructor initializes  * _M_ref_count. */struct _STLP_CLASS_DECLSPEC _Refcount_Base{  // The data member _M_ref_count  volatile __stl_atomic_t _M_ref_count;# if !defined (_STLP_ATOMIC_EXCHANGE)  _STLP_mutex _M_mutex;# endif  // Constructor  _Refcount_Base(__stl_atomic_t __n) : _M_ref_count(__n) {}  // _M_incr and _M_decr# if defined (_STLP_THREADS) && defined (_STLP_ATOMIC_EXCHANGE)   void _M_incr() { _STLP_ATOMIC_INCREMENT((__stl_atomic_t*)&_M_ref_count); }   void _M_decr() { _STLP_ATOMIC_DECREMENT((__stl_atomic_t*)&_M_ref_count); }# elif defined(_STLP_THREADS)  void _M_incr() {    _M_mutex._M_acquire_lock();    ++_M_ref_count;    _M_mutex._M_release_lock();  }  void _M_decr() {    _M_mutex._M_acquire_lock();    --_M_ref_count;    _M_mutex._M_release_lock();  }# else  /* No threads */  void _M_incr() { ++_M_ref_count; }  void _M_decr() { --_M_ref_count; }# endif};// Atomic swap on unsigned long// This is guaranteed to behave as though it were atomic only if all// possibly concurrent updates use _Atomic_swap.// In some cases the operation is emulated with a lock.# if defined (_STLP_THREADS)#  ifdef _STLP_ATOMIC_EXCHANGEinline __stl_atomic_t _Atomic_swap(volatile __stl_atomic_t * __p, __stl_atomic_t __q) {  return (__stl_atomic_t) _STLP_ATOMIC_EXCHANGE(__p,__q);}#  elif defined(_STLP_PTHREADS) || defined (_STLP_UITHREADS) || defined (_STLP_OS2THREADS) || defined(_STLP_USE_PTHREAD_SPINLOCK)// We use a template here only to get a unique initialized instance.template<int __dummy>struct _Swap_lock_struct {  static _STLP_STATIC_MUTEX _S_swap_lock;};// This should be portable, but performance is expected// to be quite awful.  This really needs platform specific// code.inline __stl_atomic_t _Atomic_swap(volatile __stl_atomic_t * __p, __stl_atomic_t __q) {  _Swap_lock_struct<0>::_S_swap_lock._M_acquire_lock();  __stl_atomic_t __result = *__p;  *__p = __q;  _Swap_lock_struct<0>::_S_swap_lock._M_release_lock();  return __result;}#  endif // _STLP_PTHREADS || _STLP_UITHREADS || _STLP_OS2THREADS || _STLP_USE_PTHREAD_SPINLOCK# else // !_STLP_THREADS/* no threads */static inline __stl_atomic_t  _STLP_CALL_Atomic_swap(volatile __stl_atomic_t * __p, __stl_atomic_t __q) {  __stl_atomic_t __result = *__p;  *__p = __q;  return __result;}# endif // _STLP_THREADS// A locking class that uses _STLP_STATIC_MUTEX.  The constructor takes// a reference to an _STLP_STATIC_MUTEX, and acquires a lock.  The destructor// releases the lock.struct _STLP_CLASS_DECLSPEC _STLP_auto_lock{  _STLP_STATIC_MUTEX& _M_lock;    _STLP_auto_lock(_STLP_STATIC_MUTEX& __lock) : _M_lock(__lock)    { _M_lock._M_acquire_lock(); }  ~_STLP_auto_lock() { _M_lock._M_release_lock(); }private:  void operator=(const _STLP_auto_lock&);  _STLP_auto_lock(const _STLP_auto_lock&);};typedef _STLP_auto_lock _STLP_mutex_lock;#ifdef _STLP_BETHREADStemplate <int __inst>struct _STLP_beos_static_lock_data{	static bool is_init;	struct mutex_t : public _STLP_mutex	{		mutex_t()		{			_STLP_beos_static_lock_data<0>::is_init = true;		}		~mutex_t()		{			_STLP_beos_static_lock_data<0>::is_init = false;		}	};	static mutex_t mut;};template <int __inst>bool _STLP_beos_static_lock_data<__inst>::is_init = false;template <int __inst>typename _STLP_beos_static_lock_data<__inst>::mutex_t _STLP_beos_static_lock_data<__inst>::mut;inline void _STLP_mutex_base::_M_acquire_lock() {	if(sem == 0)	{		// we need to initialise on demand here		// to prevent race conditions use our global		// mutex if it's available:		if(_STLP_beos_static_lock_data<0>::is_init)		{			_STLP_auto_lock al(_STLP_beos_static_lock_data<0>::mut);			if(sem == 0) _M_initialize();		}		else		{			// no lock available, we must still be			// in startup code, THERE MUST BE ONE THREAD			// ONLY active at this point.			_M_initialize();		}	}	_M_acquire_lock_nodemand();}#endif_STLP_END_NAMESPACE# if !defined (_STLP_LINK_TIME_INSTANTIATION)#  include <stl/_threads.c># endif#endif /* _STLP_INTERNAL_THREADS_H */// Local Variables:// mode:C++// End:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -