MySQL 8.0.29
Source Code Documentation
mutex_lock.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2021, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef MUTEX_LOCK_INCLUDED
24#define MUTEX_LOCK_INCLUDED
25
26/**
27 @file include/mutex_lock.h
28*/
30#include <utility>
31
32/**
33 A simple wrapper around a mutex:
34 Grabs the mutex in the CTOR, releases it in the DTOR.
35 The mutex may be NULL, in which case this is a no-op.
36 Templated to allow unit testing with mocked mutex. Not copyable since
37 ownership of a mutex cannot be shared, but movable so that ownership can be
38 transferred to a different Mutex_lock.
39*/
40template <class MUTEX>
42 public:
43 Generic_mutex_lock() noexcept = default;
44 Generic_mutex_lock(MUTEX *mutex, const char *src_file, int src_line) noexcept
45 : m_mutex(mutex), m_src_file(src_file), m_src_line(src_line) {
46 if (m_mutex) {
48 }
49 }
51 if (m_mutex) {
53 }
54 }
57 : m_mutex{src.m_mutex},
58 m_src_file{src.m_src_file},
59 m_src_line{src.m_src_line} {
60 src.m_mutex = nullptr;
61 src.m_src_file = nullptr;
62 src.m_src_line = 0;
63 }
64
67 Generic_mutex_lock tmp{std::move(src)};
68 std::swap(m_mutex, tmp.m_mutex);
69 m_src_file = tmp.m_src_file;
70 m_src_line = tmp.m_src_line;
71 return *this;
72 }
73
74 private:
75 MUTEX *m_mutex = nullptr;
76 const char *m_src_file = nullptr;
77 int m_src_line = 0;
78};
79
81
82#define MUTEX_LOCK(NAME, X) Mutex_lock NAME(X, __FILE__, __LINE__)
83
84#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:41
Generic_mutex_lock(const Generic_mutex_lock &)=delete
const char * m_src_file
Definition: mutex_lock.h:76
Generic_mutex_lock & operator=(const Generic_mutex_lock &)=delete
MUTEX * m_mutex
Definition: mutex_lock.h:75
Generic_mutex_lock(Generic_mutex_lock &&src) noexcept
Definition: mutex_lock.h:56
int m_src_line
Definition: mutex_lock.h:77
~Generic_mutex_lock() noexcept
Definition: mutex_lock.h:50
Generic_mutex_lock & operator=(Generic_mutex_lock &&src) noexcept
Definition: mutex_lock.h:66
Generic_mutex_lock() noexcept=default
#define mysql_mutex_lock_with_src(M, F, L)
Definition: mysql_mutex.h:50
#define mysql_mutex_unlock_with_src(M, F, L)
Definition: mysql_mutex.h:57
Instrumentation helpers for mutexes.
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:611