X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Furcu.hpp;fp=src%2Fcommon%2Furcu.hpp;h=2ea2001cd589ca4725e9afccf818570ad3c5c9d8;hb=0a325f4dec3f6d553dad5d2c26ebfdc78201c363;hp=0000000000000000000000000000000000000000;hpb=332a2147fece4548eac38a0929b208230a8da31f;p=lttng-tools.git diff --git a/src/common/urcu.hpp b/src/common/urcu.hpp new file mode 100644 index 000000000..2ea2001cd --- /dev/null +++ b/src/common/urcu.hpp @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2022 Jérémie Galarneau + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#ifndef LTTNG_URCU_H +#define LTTNG_URCU_H + +#define _LGPL_SOURCE +#include +#include + +namespace lttng { +namespace urcu { + +namespace details { +/* + * Wrapper around an urcu read lock which satisfies the 'Mutex' named + * requirements of C++11. Satisfying those requirements facilitates the use of + * standard concurrency support library facilities. + * + * read_lock is under the details namespace since it is unlikely to be used + * directly by exception-safe code. See read_lock_guard. + */ +class read_lock { +public: + read_lock() = default; + + /* "Not copyable" and "not moveable" Mutex requirements. */ + read_lock(read_lock const &) = delete; + read_lock &operator=(read_lock const &) = delete; + + void lock() + { + rcu_read_lock(); + } + + bool try_lock() + { + lock(); + return true; + } + + void unlock() + { + rcu_read_unlock(); + } +}; +} /* namespace details */ + +/* + * Provides the basic concept of std::lock_guard for rcu reader locks. + * + * The RCU reader lock is held for the duration of lock_guard's lifetime. + */ +class read_lock_guard { +public: + read_lock_guard() : _guard(_lock) + { + } + + read_lock_guard(const read_lock_guard &) = delete; + +private: + details::read_lock _lock; + std::lock_guard _guard; +}; + +} /* namespace urcu */ +} /* namespace lttng */ + +#endif /* LTTNG_URCU_H */