MySQL 8.4.3
Source Code Documentation
mutex_lock.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2024, 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*/
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*/
41template <class MUTEX>
43 public:
44 Generic_mutex_lock() noexcept = default;
45 Generic_mutex_lock(MUTEX *mutex, const char *src_file, int src_line) noexcept
46 : m_mutex(mutex), m_src_file(src_file), m_src_line(src_line) {
47 if (m_mutex) {
49 }
50 }
52 if (m_mutex) {
54 }
55 }
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
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
82
83#define MUTEX_LOCK(NAME, X) const Mutex_lock NAME(X, __FILE__, __LINE__)
84
85#endif // MUTEX_LOCK_INCLUDED
A simple wrapper around a mutex: Grabs the mutex in the CTOR, releases it in the DTOR.
Definition: mutex_lock.h:42
Generic_mutex_lock(const Generic_mutex_lock &)=delete
const char * m_src_file
Definition: mutex_lock.h:77
Generic_mutex_lock & operator=(const Generic_mutex_lock &)=delete
MUTEX * m_mutex
Definition: mutex_lock.h:76
Generic_mutex_lock(Generic_mutex_lock &&src) noexcept
Definition: mutex_lock.h:57
int m_src_line
Definition: mutex_lock.h:78
~Generic_mutex_lock() noexcept
Definition: mutex_lock.h:51
Generic_mutex_lock & operator=(Generic_mutex_lock &&src) noexcept
Definition: mutex_lock.h:67
Generic_mutex_lock() noexcept=default
#define mysql_mutex_lock_with_src(M, F, L)
Definition: mysql_mutex.h:51
#define mysql_mutex_unlock_with_src(M, F, L)
Definition: mysql_mutex.h:58
Instrumentation helpers for mutexes.
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:663