libcopp  2.2.0
Data Structures | Namespaces | Macros
spin_lock.h File Reference

自旋锁 Licensed under the MIT licenses. More...

#include <libcopp/utils/config/libcopp_build_features.h>
#include <libcopp/utils/atomic_int_type.h>
Include dependency graph for spin_lock.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

class  util::lock::spin_lock
 自旋锁 More...
 

Namespaces

 util
 
 util::lock
 

Macros

#define __LIBCOPP_UTIL_LOCK_SPIN_LOCK_PAUSE()
 
#define __LIBCOPP_UTIL_LOCK_SPIN_LOCK_CPU_YIELD()   __LIBCOPP_UTIL_LOCK_SPIN_LOCK_PAUSE()
 
#define __LIBCOPP_UTIL_LOCK_SPIN_LOCK_THREAD_YIELD()
 
#define __LIBCOPP_UTIL_LOCK_SPIN_LOCK_THREAD_SLEEP()   __LIBCOPP_UTIL_LOCK_SPIN_LOCK_CPU_YIELD()
 
#define __LIBCOPP_UTIL_LOCK_SPIN_LOCK_WAIT(x)
 

Detailed Description

自旋锁 Licensed under the MIT licenses.

Version
1.0
Author
OWenT
Date
2013-3-18
Note
VC 2012+, GCC 4.4 + 使用C++0x/11实现实现原子操作
低版本 VC使用InterlockedExchange等实现原子操作
低版本 GCC采用__sync_lock_test_and_set等实现原子操作

@history 2013-12-20

  1. add support for clang & intel compiler
  2. add try unlock function
  3. fix atom operator
  4. add gcc atomic support 2014-07-08
  1. add yield operation 2016-06-15
  1. using atomic_int_type

Definition in file spin_lock.h.

Macro Definition Documentation

◆ __LIBCOPP_UTIL_LOCK_SPIN_LOCK_CPU_YIELD

#define __LIBCOPP_UTIL_LOCK_SPIN_LOCK_CPU_YIELD ( )    __LIBCOPP_UTIL_LOCK_SPIN_LOCK_PAUSE()

defined(CAPO_SPIN_LOCK_PAUSE)


====== cpu yield ======

Definition at line 120 of file spin_lock.h.

◆ __LIBCOPP_UTIL_LOCK_SPIN_LOCK_PAUSE

#define __LIBCOPP_UTIL_LOCK_SPIN_LOCK_PAUSE ( )

====== asm pause ======

Definition at line 100 of file spin_lock.h.

◆ __LIBCOPP_UTIL_LOCK_SPIN_LOCK_THREAD_SLEEP

#define __LIBCOPP_UTIL_LOCK_SPIN_LOCK_THREAD_SLEEP ( )    __LIBCOPP_UTIL_LOCK_SPIN_LOCK_CPU_YIELD()

Definition at line 159 of file spin_lock.h.

◆ __LIBCOPP_UTIL_LOCK_SPIN_LOCK_THREAD_YIELD

#define __LIBCOPP_UTIL_LOCK_SPIN_LOCK_THREAD_YIELD ( )

====== thread yield ======

Definition at line 158 of file spin_lock.h.

◆ __LIBCOPP_UTIL_LOCK_SPIN_LOCK_WAIT

#define __LIBCOPP_UTIL_LOCK_SPIN_LOCK_WAIT (   x)
Value:
{ \
unsigned char try_lock_times = static_cast<unsigned char>(x); \
if (try_lock_times < 4) { \
} else if (try_lock_times < 16) { \
__LIBCOPP_UTIL_LOCK_SPIN_LOCK_PAUSE(); \
} else if (try_lock_times < 32) { \
__LIBCOPP_UTIL_LOCK_SPIN_LOCK_THREAD_YIELD(); \
} else if (try_lock_times < 64) { \
__LIBCOPP_UTIL_LOCK_SPIN_LOCK_CPU_YIELD(); \
} else { \
__LIBCOPP_UTIL_LOCK_SPIN_LOCK_THREAD_SLEEP(); \
} \
}

====== spin lock wait ======

Note
  1. busy-wait
  2. asm pause
  3. thread give up cpu time slice but will not switch to another process
  4. thread give up cpu time slice (may switch to another process)
  5. sleep (will switch to another process when necessary)

Definition at line 174 of file spin_lock.h.