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