clang-tidy: add Chrome-inspired checks
[lttng-tools.git] / src / common / pthread-lock.hpp
CommitLineData
0a325f4d
JG
1/*
2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8#ifndef LTTNG_PTHREAD_LOCK_H
9#define LTTNG_PTHREAD_LOCK_H
10
11#include <common/exception.hpp>
12
13#include <pthread.h>
14#include <mutex>
15
16namespace lttng {
17namespace pthread {
18
19namespace details {
20/*
21 * Class wrapping pthread mutexes and satisfying the Mutex named requirements, except
22 * for the "Default Constructible" requirement. The class is not default-constructible since the
23 * intention is to ease the transition of existing C-code using pthread mutexes to idiomatic C++.
24 *
25 * New code should use std::mutex.
26 */
27class mutex {
28public:
cd9adb8b 29 explicit mutex(pthread_mutex_t& mutex_p) : _mutex{mutex_p}
0a325f4d
JG
30 {
31 }
32
33 /* "Not copyable" and "not moveable" Mutex requirements. */
34 mutex(mutex const &) = delete;
35 mutex &operator=(mutex const &) = delete;
36
37 void lock()
38 {
39 if (pthread_mutex_lock(&_mutex) != 0) {
40 LTTNG_THROW_POSIX("Failed to lock mutex", errno);
41 }
42 }
43
44 bool try_lock()
45 {
46 const auto ret = pthread_mutex_trylock(&_mutex);
47
48 if (ret == 0) {
49 return true;
50 } else if (errno == EBUSY || errno == EAGAIN) {
51 return false;
52 } else {
53 LTTNG_THROW_POSIX("Failed to try to lock mutex", errno);
54 }
55 }
56
57 void unlock()
58 {
59 if (pthread_mutex_unlock(&_mutex) != 0) {
460bede7
JG
60 /*
61 * Unlock cannot throw as it is called as part of lock_guard's destructor.
62 */
63 abort();
0a325f4d
JG
64 }
65 }
66
67private:
68 pthread_mutex_t& _mutex;
69};
70} /* namespace details */
71
72/*
73 * Provides the basic concept of std::lock_guard for posix mutexes.
74 *
75 * `lock` is held for the duration of lock_guard's lifetime.
76 */
77class lock_guard {
78public:
cd9adb8b 79 explicit lock_guard(pthread_mutex_t& mutex) : _mutex{mutex}, _guard(_mutex)
0a325f4d
JG
80 {
81 }
82
83 lock_guard(const lock_guard &) = delete;
84
85private:
86 details::mutex _mutex;
87 std::lock_guard<details::mutex> _guard;
88};
89
90} /* namespace pthread */
91} /* namespace lttng */
92
93#endif /* LTTNG_PTHREAD_LOCK_H */
This page took 0.028924 seconds and 4 git commands to generate.