mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
调整代码格式和注释
This commit is contained in:
parent
b30eeaf1f6
commit
c32615dbda
@ -16,57 +16,57 @@
|
|||||||
#include "libipc/detect_plat.h"
|
#include "libipc/detect_plat.h"
|
||||||
#include "libipc/def.h"
|
#include "libipc/def.h"
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////
|
/**
|
||||||
/// Gives hint to processor that improves performance of spin-wait loops.
|
* @brief Gives hint to processor that improves performance of spin-wait loops.
|
||||||
////////////////////////////////////////////////////////////////
|
*/
|
||||||
|
|
||||||
#pragma push_macro("LIBIPC_LOCK_PAUSE_")
|
#pragma push_macro("LIBIPC_LOCK_PAUSE_")
|
||||||
#undef LIBIPC_LOCK_PAUSE_
|
#undef LIBIPC_LOCK_PAUSE_
|
||||||
|
|
||||||
#if defined(LIBIPC_CC_MSVC)
|
#if defined(LIBIPC_CC_MSVC)
|
||||||
# include <Windows.h> // YieldProcessor
|
# include <Windows.h> // YieldProcessor
|
||||||
/*
|
/**
|
||||||
|
* @brief Not for intel c++ compiler, so ignore http://software.intel.com/en-us/forums/topic/296168
|
||||||
* @see http://msdn.microsoft.com/en-us/library/windows/desktop/ms687419(v=vs.85).aspx
|
* @see http://msdn.microsoft.com/en-us/library/windows/desktop/ms687419(v=vs.85).aspx
|
||||||
* Not for intel c++ compiler, so ignore http://software.intel.com/en-us/forums/topic/296168
|
|
||||||
*/
|
*/
|
||||||
# define LIBIPC_LOCK_PAUSE_() YieldProcessor()
|
# define LIBIPC_LOCK_PAUSE_() YieldProcessor()
|
||||||
#elif defined(LIBIPC_CC_GNUC)
|
#elif defined(LIBIPC_CC_GNUC)
|
||||||
# if defined(LIBIPC_INSTR_X86_64)
|
# if defined(LIBIPC_INSTR_X86_64)
|
||||||
/*
|
/**
|
||||||
* @see Intel(R) 64 and IA-32 Architectures Software Developer's Manual V2
|
* @brief Intel(R) 64 and IA-32 Architectures Software Developer's Manual V2
|
||||||
* PAUSE-Spin Loop Hint, 4-57
|
* PAUSE-Spin Loop Hint, 4-57
|
||||||
* http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.html?wapkw=instruction+set+reference
|
* @see http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.html?wapkw=instruction+set+reference
|
||||||
*/
|
*/
|
||||||
# define LIBIPC_LOCK_PAUSE_() __asm__ __volatile__("pause")
|
# define LIBIPC_LOCK_PAUSE_() __asm__ __volatile__("pause")
|
||||||
# elif defined(LIBIPC_INSTR_I64)
|
# elif defined(LIBIPC_INSTR_I64)
|
||||||
/*
|
/**
|
||||||
* @see Intel(R) Itanium(R) Architecture Developer's Manual, Vol.3
|
* @brief Intel(R) Itanium(R) Architecture Developer's Manual, Vol.3
|
||||||
* hint - Performance Hint, 3:145
|
* hint - Performance Hint, 3:145
|
||||||
* http://www.intel.com/content/www/us/en/processors/itanium/itanium-architecture-vol-3-manual.html
|
* @see http://www.intel.com/content/www/us/en/processors/itanium/itanium-architecture-vol-3-manual.html
|
||||||
*/
|
*/
|
||||||
# define LIBIPC_LOCK_PAUSE_() __asm__ __volatile__ ("hint @pause")
|
# define LIBIPC_LOCK_PAUSE_() __asm__ __volatile__ ("hint @pause")
|
||||||
# elif defined(LIBIPC_INSTR_ARM)
|
# elif defined(LIBIPC_INSTR_ARM)
|
||||||
/*
|
/**
|
||||||
* @see ARM Architecture Reference Manuals (YIELD)
|
* @brief ARM Architecture Reference Manuals (YIELD)
|
||||||
* http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.subset.architecture.reference/index.html
|
* @see http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.subset.architecture.reference/index.html
|
||||||
*/
|
*/
|
||||||
# define LIBIPC_LOCK_PAUSE_() __asm__ __volatile__ ("yield")
|
# define LIBIPC_LOCK_PAUSE_() __asm__ __volatile__ ("yield")
|
||||||
# endif
|
# endif
|
||||||
#endif /*compilers*/
|
#endif /*compilers*/
|
||||||
|
|
||||||
#if !defined(LIBIPC_LOCK_PAUSE_)
|
#if !defined(LIBIPC_LOCK_PAUSE_)
|
||||||
/*
|
/**
|
||||||
* Just use a compiler fence, prevent compiler from optimizing loop
|
* @brief Just use a compiler fence, prevent compiler from optimizing loop
|
||||||
*/
|
*/
|
||||||
# define LIBIPC_LOCK_PAUSE_() std::atomic_signal_fence(std::memory_order_seq_cst)
|
# define LIBIPC_LOCK_PAUSE_() std::atomic_signal_fence(std::memory_order_seq_cst)
|
||||||
#endif /*!defined(LIBIPC_LOCK_PAUSE_)*/
|
#endif /*!defined(LIBIPC_LOCK_PAUSE_)*/
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////
|
|
||||||
/// Yield to other threads
|
|
||||||
////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
LIBIPC_NAMESPACE_BEG_
|
LIBIPC_NAMESPACE_BEG_
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Yield to other threads
|
||||||
|
*/
|
||||||
|
|
||||||
template <typename K>
|
template <typename K>
|
||||||
inline void yield(K &k) noexcept {
|
inline void yield(K &k) noexcept {
|
||||||
if (k < 4) { /* Do nothing */ }
|
if (k < 4) { /* Do nothing */ }
|
||||||
@ -100,12 +100,9 @@ inline void sleep(K &k) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ipc
|
/**
|
||||||
|
* @brief Basic spin lock
|
||||||
#pragma pop_macro("LIBIPC_LOCK_PAUSE_")
|
*/
|
||||||
|
|
||||||
namespace ipc {
|
|
||||||
|
|
||||||
class spin_lock {
|
class spin_lock {
|
||||||
std::atomic<unsigned> lc_ {0};
|
std::atomic<unsigned> lc_ {0};
|
||||||
|
|
||||||
@ -120,6 +117,9 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Support for shared mode spin lock
|
||||||
|
*/
|
||||||
class rw_lock {
|
class rw_lock {
|
||||||
using lc_ui_t = unsigned;
|
using lc_ui_t = unsigned;
|
||||||
|
|
||||||
@ -127,11 +127,11 @@ class rw_lock {
|
|||||||
|
|
||||||
enum : lc_ui_t {
|
enum : lc_ui_t {
|
||||||
w_mask = (std::numeric_limits<std::make_signed_t<lc_ui_t>>::max)(), // b 0111 1111
|
w_mask = (std::numeric_limits<std::make_signed_t<lc_ui_t>>::max)(), // b 0111 1111
|
||||||
w_flag = w_mask + 1 // b 1000 0000
|
w_flag = w_mask + 1, // b 1000 0000
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
rw_lock() = default;
|
rw_lock() noexcept = default;
|
||||||
|
|
||||||
rw_lock(const rw_lock &) = delete;
|
rw_lock(const rw_lock &) = delete;
|
||||||
rw_lock &operator=(const rw_lock &) = delete;
|
rw_lock &operator=(const rw_lock &) = delete;
|
||||||
@ -177,3 +177,5 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
LIBIPC_NAMESPACE_END_
|
LIBIPC_NAMESPACE_END_
|
||||||
|
|
||||||
|
#pragma pop_macro("LIBIPC_LOCK_PAUSE_")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user