ZeroErr
载入中...
搜索中...
未找到
threadsafe.h
浏览该文件的文档.
1#pragma once
2
3// libc++ offers compatibility-only transitive includes through C++20.
4// Suppress those while loading our explicit dependencies, then restore the
5// caller's setting. This does not change standard-library types or ABI.
6#pragma push_macro("_LIBCPP_REMOVE_TRANSITIVE_INCLUDES")
7#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
8#define _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
9#endif
10
12
20#ifdef ZEROERR_NO_THREAD_SAFE
21
22#define ZEROERR_MUTEX(x)
23#define ZEROERR_LOCK(x)
24#define ZEROERR_ATOMIC(x) x
25#define ZEROERR_LOAD(x) x
26
27#else
28
29#define ZEROERR_MUTEX(x) static zeroerr::detail::Mutex x;
30#define ZEROERR_LOCK(x) zeroerr::detail::LockGuard lock(x);
31#define ZEROERR_ATOMIC(x) std::atomic<x>
32#define ZEROERR_LOAD(x) x.load()
33
34#include <atomic>
35namespace zeroerr { namespace detail {
36class Mutex {
37public:
38 Mutex(); ~Mutex();
39 Mutex(const Mutex&) = delete;
40 Mutex& operator=(const Mutex&) = delete;
41 void lock(); void unlock();
42private: void* value_;
43};
44class LockGuard {
45public: explicit LockGuard(Mutex& mutex) : mutex_(mutex) { mutex_.lock(); }
46 ~LockGuard() { mutex_.unlock(); }
47 LockGuard(const LockGuard&) = delete;
48private: Mutex& mutex_;
49};
50}}
51
52
53#endif
54
55#pragma pop_macro("_LIBCPP_REMOVE_TRANSITIVE_INCLUDES")
Definition threadsafe.h:44
LockGuard(Mutex &mutex)
Definition threadsafe.h:45
LockGuard(const LockGuard &)=delete
~LockGuard()
Definition threadsafe.h:46
Definition threadsafe.h:36
void unlock()
Definition threadsafe.cpp:9
void lock()
Definition threadsafe.cpp:8
Mutex(const Mutex &)=delete
Mutex & operator=(const Mutex &)=delete
Mutex()
Definition threadsafe.cpp:6
~Mutex()
Definition threadsafe.cpp:7
Definition assert_text.cpp:5