/src/mysql-server/include/mutex_lock.h
Line | Count | Source |
1 | | /* Copyright (c) 2014, 2025, Oracle and/or its affiliates. |
2 | | |
3 | | This program is free software; you can redistribute it and/or modify |
4 | | it under the terms of the GNU General Public License, version 2.0, |
5 | | as published by the Free Software Foundation. |
6 | | |
7 | | This program is designed to work with certain software (including |
8 | | but not limited to OpenSSL) that is licensed under separate terms, |
9 | | as designated in a particular file or component or in included license |
10 | | documentation. The authors of MySQL hereby grant you an additional |
11 | | permission to link the program and your derivative works with the |
12 | | separately licensed software that they have either included with |
13 | | the program or referenced in the documentation. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, |
16 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | | GNU General Public License, version 2.0, for more details. |
19 | | |
20 | | You should have received a copy of the GNU General Public License |
21 | | along with this program; if not, write to the Free Software |
22 | | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
23 | | |
24 | | #ifndef MUTEX_LOCK_INCLUDED |
25 | | #define MUTEX_LOCK_INCLUDED |
26 | | |
27 | | /** |
28 | | @file include/mutex_lock.h |
29 | | */ |
30 | | #include <mysql/psi/mysql_mutex.h> |
31 | | #include <utility> |
32 | | |
33 | | /** |
34 | | A simple wrapper around a mutex: |
35 | | Grabs the mutex in the CTOR, releases it in the DTOR. |
36 | | The mutex may be NULL, in which case this is a no-op. |
37 | | Templated to allow unit testing with mocked mutex. Not copyable since |
38 | | ownership of a mutex cannot be shared, but movable so that ownership can be |
39 | | transferred to a different Mutex_lock. |
40 | | */ |
41 | | template <class MUTEX> |
42 | | class Generic_mutex_lock { |
43 | | public: |
44 | | Generic_mutex_lock() noexcept = default; |
45 | | Generic_mutex_lock(MUTEX *mutex, const char *src_file, int src_line) noexcept |
46 | 0 | : m_mutex(mutex), m_src_file(src_file), m_src_line(src_line) { |
47 | 0 | if (m_mutex) { |
48 | 0 | mysql_mutex_lock_with_src(m_mutex, m_src_file, m_src_line); |
49 | 0 | } |
50 | 0 | } |
51 | 0 | ~Generic_mutex_lock() noexcept { |
52 | 0 | if (m_mutex) { |
53 | 0 | mysql_mutex_unlock_with_src(m_mutex, m_src_file, m_src_line); |
54 | 0 | } |
55 | 0 | } |
56 | | Generic_mutex_lock(const Generic_mutex_lock &) = delete; |
57 | | Generic_mutex_lock(Generic_mutex_lock &&src) noexcept |
58 | | : m_mutex{src.m_mutex}, |
59 | | m_src_file{src.m_src_file}, |
60 | | m_src_line{src.m_src_line} { |
61 | | src.m_mutex = nullptr; |
62 | | src.m_src_file = nullptr; |
63 | | src.m_src_line = 0; |
64 | | } |
65 | | |
66 | | Generic_mutex_lock &operator=(const Generic_mutex_lock &) = delete; |
67 | | Generic_mutex_lock &operator=(Generic_mutex_lock &&src) noexcept { |
68 | | Generic_mutex_lock tmp{std::move(src)}; |
69 | | std::swap(m_mutex, tmp.m_mutex); |
70 | | m_src_file = tmp.m_src_file; |
71 | | m_src_line = tmp.m_src_line; |
72 | | return *this; |
73 | | } |
74 | | |
75 | | private: |
76 | | MUTEX *m_mutex = nullptr; |
77 | | const char *m_src_file = nullptr; |
78 | | int m_src_line = 0; |
79 | | }; |
80 | | |
81 | | using Mutex_lock = Generic_mutex_lock<mysql_mutex_t>; |
82 | | |
83 | 0 | #define MUTEX_LOCK(NAME, X) const Mutex_lock NAME(X, __FILE__, __LINE__) |
84 | | |
85 | | #endif // MUTEX_LOCK_INCLUDED |