YSTest: Mutex.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

Mutex.h
浏览该文件的文档.
1 /*
2 © 2014-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YCL_INC_Mutex_h_
29 #define YCL_INC_Mutex_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YCLib_YCommon
33 #include <ystdex/type_op.hpp> // for ystdex::cond_or_t, ystdex::or_,
34 // std::is_integral, std::is_pointer;
35 #if YF_Multithread == 1
36 # include <atomic> // for std::atomic;
37 # include <ystdex/concurrency.h>
38 # define YCL_Impl_Atomic_Tmpl std::atomic
39 # define YCL_Impl_Ns_Mutex std
40 # define YCL_Impl_Ns_Threading ystdex
41 #else
42 # include <ystdex/pseudo_mutex.h> // for ystdex::identity_t,
43 // ystdex::single_thread, ystdex::threading;
44 # define YCL_Impl_Atomic_Tmpl ystdex::identity_t
45 # define YCL_Impl_Ns_Mutex ystdex::single_thread
46 # define YCL_Impl_Ns_Threading ystdex::threading
47 #endif
48
49 namespace platform
50 {
51
53 namespace Concurrency
54 {
55
62 template<typename _type>
63 using atomic = ystdex::cond_or_t<ystdex::or_<std::is_integral<_type>,
64 std::is_pointer<_type>>, void, YCL_Impl_Atomic_Tmpl, _type>;
65
66 using YCL_Impl_Ns_Mutex::mutex;
67 using YCL_Impl_Ns_Mutex::recursive_mutex;
68 // TODO: Check %_GTHREAD_USE_MUTEX_TIMEDLOCK defined or %YCL_*.
69 //using YCL_Impl_Ns_Mutex::timed_mutex;
70 //using YCL_Impl_Ns_Mutex::recursive_timed_mutex;
71
72 using YCL_Impl_Ns_Mutex::lock_guard;
73 using YCL_Impl_Ns_Mutex::unique_lock;
75 using ystdex::threading::lockable_adaptor;
77 using ystdex::threading::shared_lockable_adaptor;
79
80 #if YF_Multithread == 1 || !defined(NDEBUG)
81 yconstfn bool UseLockDebug(true);
82 #else
83 yconstfn bool UseLockDebug = {};
84 #endif
85 template<class _tMutex>
86 using shared_lock = ystdex::threading::shared_lock<_tMutex, UseLockDebug>;
87
88 template<class _tMutex>
89 using shared_lock_guard = ystdex::threading::lock_guard<_tMutex,
90 UseLockDebug, shared_lockable_adaptor<_tMutex>>;
92
93 using YCL_Impl_Ns_Mutex::lock;
94 using YCL_Impl_Ns_Mutex::try_lock;
95
97 using YCL_Impl_Ns_Mutex::once_flag;
99 using YCL_Impl_Ns_Mutex::call_once;
100
101
103
104 template<class _type, typename _tReference = ystdex::lref<_type>>
105 using AdaptedLock = ystdex::threading::lock_base<_type, UseLockDebug,
106 lockable_adaptor<_type, _tReference>>;
107
108 template<class _type, typename _tReference = ystdex::lref<_type>>
109 using SharedAdaptedLock = ystdex::threading::lock_base<_type, UseLockDebug,
110 shared_lockable_adaptor<_type, _tReference>>;
111
112 template<class _type, typename _tReference = ystdex::lref<_type>>
113 using AdaptedLockGuard = ystdex::threading::lock_guard<_type, UseLockDebug,
114 lockable_adaptor<_type, _tReference>>;
115
116 template<class _type, typename _tReference = ystdex::lref<_type>>
117 using SharedAdaptedLockGuard = ystdex::threading::lock_guard<_type,
118 UseLockDebug, shared_lockable_adaptor<_type, _tReference>>;
119
120 template<class _type>
121 using IndirectLock = AdaptedLock<_type, ystdex::indirect_ref_adaptor<_type>>;
122
123 template<class _type>
124 using SharedIndirectLock
125 = SharedAdaptedLock<_type, ystdex::indirect_ref_adaptor<_type>>;
126
127 template<class _type>
128 using IndirectLockGuard
129 = AdaptedLockGuard<_type, ystdex::indirect_ref_adaptor<_type>>;
130
131 template<class _type>
132 using SharedIndirectLockGuard
133 = SharedAdaptedLockGuard<_type, ystdex::indirect_ref_adaptor<_type>>;
135
136 } // namespace Concurrency;
137
139 namespace Threading
140 {
141
143 using YCL_Impl_Ns_Threading::unlock_delete;
144 using YCL_Impl_Ns_Threading::locked_ptr;
145
146 } // namespace Threading;
147
148 } // namespace platform;
149
150 #undef YCL_Impl_Ns_Threading
151 #undef YCL_Impl_Ns_Mutex
152
153 #endif
154
ystdex::single_thread::try_lock
int try_lock(_tLock1 &&, _tLock2 &&, _tLocks &&...)
Definition: pseudo_mutex.h:983
ystdex::threading::lockable_adaptor
Definition: pseudo_mutex.h:70
YCL_Impl_Atomic_Tmpl
#define YCL_Impl_Atomic_Tmpl
Definition: Mutex.h:44
ystdex::cond_or_t
_t< cond_or< _tCond, _tDefault, _gOp, _tParams... > > cond_or_t
Definition: type_op.hpp:265
ystdex::unlock_delete
threading::unlock_delete< _tMutex, _tLock > unlock_delete
解锁删除器：默认使用标准库互斥量和锁。
Definition: concurrency.h:51
platform::Concurrency::atomic
ystdex::cond_or_t< ystdex::or_< std::is_integral< _type >, std::is_pointer< _type > >, void, ystdex::identity_t, _type > atomic
支持的原子性的整数或指针类型。
Definition: Mutex.h:64
concurrency.h
并发操作。
ystdex::threading::lock_guard
Definition: pseudo_mutex.h:223
ystdex::threading::shared_lock
Definition: pseudo_mutex.h:630
type_op.hpp
C++ 类型操作。
ystdex::threading::lock_base
锁基类：可适配独占锁和共享锁。
Definition: pseudo_mutex.h:277
pseudo_mutex.h
伪互斥量。
ystdex::single_thread::call_once
void call_once(once_flag &flag, _fCallable &&f, _tParams &&... args)
按标识调用函数，保证调用一次。
Definition: pseudo_mutex.h:1021
platform
默认平台命名空间。
Definition: Container.h:56
YModules.h
YSLib 用户界面模块配置文件。
ystdex::single_thread::unique_lock
threading::unique_lock< _tMutex > unique_lock
Definition: pseudo_mutex.h:962
ystdex::locked_ptr
threading::locked_ptr< _type, _tMutex, _tLock > locked_ptr
独占所有权的锁定指针：默认使用标准库互斥量和锁。
Definition: concurrency.h:61
platform::Concurrency::UseLockDebug
yconstfn bool UseLockDebug(true)
ystdex::single_thread::lock
void lock(_tLock1 &&, _tLock2 &&, _tLocks &&...)
Definition: pseudo_mutex.h:990
ystdex::single_thread::lock_guard
threading::lock_guard< _tMutex > lock_guard
Definition: pseudo_mutex.h:953
ystdex::threading::shared_lockable_adaptor
Definition: pseudo_mutex.h:139
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:1073