| 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 | |
| 16 | namespace lttng { |
| 17 | namespace pthread { |
| 18 | |
| 19 | namespace 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 | */ |
| 27 | class mutex { |
| 28 | public: |
| 29 | mutex(pthread_mutex_t& mutex_p) : _mutex{mutex_p} |
| 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) { |
| 60 | LTTNG_THROW_POSIX("Failed to unlock mutex", errno); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | pthread_mutex_t& _mutex; |
| 66 | }; |
| 67 | } /* namespace details */ |
| 68 | |
| 69 | /* |
| 70 | * Provides the basic concept of std::lock_guard for posix mutexes. |
| 71 | * |
| 72 | * `lock` is held for the duration of lock_guard's lifetime. |
| 73 | */ |
| 74 | class lock_guard { |
| 75 | public: |
| 76 | lock_guard(pthread_mutex_t& mutex) : _mutex{mutex}, _guard(_mutex) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | lock_guard(const lock_guard &) = delete; |
| 81 | |
| 82 | private: |
| 83 | details::mutex _mutex; |
| 84 | std::lock_guard<details::mutex> _guard; |
| 85 | }; |
| 86 | |
| 87 | } /* namespace pthread */ |
| 88 | } /* namespace lttng */ |
| 89 | |
| 90 | #endif /* LTTNG_PTHREAD_LOCK_H */ |