Debian Codegraph @

storage/innobase/include/ut0guarded.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2021, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/ut0guarded.h
29 The ut::Guarded template which protects access to another class with mutex. */
30
31 #ifndef ut0guarded_h
32 #define ut0guarded_h
33
34 #ifndef UNIV_LIBRARY
35 #include "ut0cpu_cache.h"
36 #include "ut0mutex.h"
37 #endif
38
39 namespace ut {
40 // TBD: should latch_id be specified at runtime?
41 template <typename Inner, latch_id_t latch_id>
42 class Guarded {
43 #ifndef UNIV_LIBRARY
44 Cacheline_padded<IB_mutex> mutex{latch_id};
45 #endif
46 Inner inner;
47
48 public:
49 template <typename F>
50 auto latch_and_execute(F &&f, const ut::Location &loc) {
51 #ifndef UNIV_LIBRARY
52 IB_mutex_guard guard{&mutex, loc};
53 #endif
54 return std::forward<F>(f)(inner);
55 }
56
57 const Inner &peek() const { return inner; }
58 };
59 } // namespace ut
60 #endif /* ut0guarded_h */