MySQL 9.1.0
Source Code Documentation
read_write_lock_guard.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 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 READ_WRITE_LOCK_GUARD_INCLUDED
25#define READ_WRITE_LOCK_GUARD_INCLUDED
26
27#include "sentry.h" // raii::Sentry
28
29/**
30 Generic sentry class for read locking.
31 For the given callable type the class assumes a read lock can be acquired
32 with rdlock() in the constructor.
33 On deletion the class will unlock with an unlock() invocation
34 */
35template <typename Rd_lockable>
36class Rdlock_guard : public raii::Sentry<> {
37 public:
38 /**
39 Constructor for the class that creates a sentry that will unlock
40 the callable object on destruction and then read locks the object.
41
42 @param lock The callable object to be locked/unlocked
43 */
44 Rdlock_guard(Rd_lockable &lock)
45 : Sentry{[&lock]() -> void { lock.unlock(); }} {
46 lock.rdlock();
47 }
48 virtual ~Rdlock_guard() override = default;
49};
50
51/**
52 Generic sentry class for write locking.
53 For the given callable type the class assumes a write lock can be acquired
54 with wrlock() in the constructor.
55 On deletion the class will unlock with an unlock() invocation
56 */
57template <typename Wr_lockable>
58class Wrlock_guard : public raii::Sentry<> {
59 public:
60 /**
61 Constructor for the class that creates a sentry that will unlock
62 the callable object on destruction and then write locks the object.
63
64 @param lock The callable object to be locked/unlocked
65 */
66 Wrlock_guard(Wr_lockable &lock)
67 : Sentry{[&lock]() -> void { lock.unlock(); }} {
68 lock.wrlock();
69 }
70 virtual ~Wrlock_guard() override = default;
71};
72
73#endif // READ_WRITE_LOCK_GUARD_INCLUDED
Generic sentry class for read locking.
Definition: read_write_lock_guard.h:36
Rdlock_guard(Rd_lockable &lock)
Constructor for the class that creates a sentry that will unlock the callable object on destruction a...
Definition: read_write_lock_guard.h:44
virtual ~Rdlock_guard() override=default
Generic sentry class for write locking.
Definition: read_write_lock_guard.h:58
virtual ~Wrlock_guard() override=default
Wrlock_guard(Wr_lockable &lock)
Constructor for the class that creates a sentry that will unlock the callable object on destruction a...
Definition: read_write_lock_guard.h:66
Generic sentry class that invokes some callable object of type F upon disposal of an instance of this...
Definition: sentry.h:38
Sentry(F dispose)
Constructor for the class that stores the callable object passed as argument, to be invoked upon disp...
Definition: sentry.h:60
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79