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

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

#include <libcopp/utils/config/build_feature.h>
#include "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 __UTIL_LOCK_SPIN_LOCK_PAUSE()
 
#define __UTIL_LOCK_SPIN_LOCK_CPU_YIELD()   __UTIL_LOCK_SPIN_LOCK_PAUSE()
 
#define __UTIL_LOCK_SPIN_LOCK_THREAD_YIELD()
 
#define __UTIL_LOCK_SPIN_LOCK_THREAD_SLEEP()   __UTIL_LOCK_SPIN_LOCK_CPU_YIELD()
 
#define __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等实现原子操作

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

#define __UTIL_LOCK_SPIN_LOCK_CPU_YIELD ( )    __UTIL_LOCK_SPIN_LOCK_PAUSE()

defined(CAPO_SPIN_LOCK_PAUSE)

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

Definition at line 109 of file spin_lock.h.

#define __UTIL_LOCK_SPIN_LOCK_PAUSE ( )

==============================================

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

Definition at line 88 of file spin_lock.h.

#define __UTIL_LOCK_SPIN_LOCK_THREAD_SLEEP ( )    __UTIL_LOCK_SPIN_LOCK_CPU_YIELD()

Definition at line 129 of file spin_lock.h.

#define __UTIL_LOCK_SPIN_LOCK_THREAD_YIELD ( )

==============================================

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

Definition at line 128 of file spin_lock.h.

#define __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) { \
} else if (try_lock_times < 32) { \
} else if (try_lock_times < 64) { \
} else { \
} \
}
#define __UTIL_LOCK_SPIN_LOCK_THREAD_YIELD()
Definition: spin_lock.h:128
#define __UTIL_LOCK_SPIN_LOCK_PAUSE()
Definition: spin_lock.h:88
#define __UTIL_LOCK_SPIN_LOCK_THREAD_SLEEP()
Definition: spin_lock.h:129
libcopp if(LIBCOTASK_MACRO_ENABLED) add_subdirectory("$
Definition: CMakeLists.txt:8
#define __UTIL_LOCK_SPIN_LOCK_CPU_YIELD()
Definition: spin_lock.h:109

==============================================

====== 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 144 of file spin_lock.h.

Referenced by util::lock::spin_lock::lock().