1/*****************************************************************************
2
3Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24
25*****************************************************************************/
26
27/** @file include/sync0policy.ic
28 Policy for mutexes.
29
30 Created 2012-08-21 Sunny Bains.
31 ***********************************************************************/
32
33#include "sync0debug.h"
34
35template <typename Mutex>
36std::string GenericPolicy<Mutex>::to_string() const {
37  return (sync_mutex_to_string(get_id(), sync_file_created_get(this)));
38}
39
40template <typename Mutex>
41std::string BlockMutexPolicy<Mutex>::to_string() const {
42  /* I don't think it makes sense to keep track of the file name
43  and line number for each block mutex. Too much of overhead. Use the
44  latch id to figure out the location from the source. */
45  return (sync_mutex_to_string(get_id(), "buf0buf.cc:0"));
46}
47
48#ifdef UNIV_DEBUG
49
50template <typename Mutex>
51void MutexDebug<Mutex>::init(latch_id_t id) UNIV_NOTHROW {
52  m_context.m_id = id;
53
54  m_context.release();
55
56  m_magic_n = MUTEX_MAGIC_N;
57}
58
59template <typename Mutex>
60void MutexDebug<Mutex>::enter(const Mutex *mutex, const char *name,
61                              ulint line) UNIV_NOTHROW {
62  ut_ad(!is_owned());
63
64  Context context(m_context.get_id());
65
66  context.locked(mutex, name, line);
67
68  /* Check for latch order violation. */
69
70  sync_check_lock_validate(&context);
71}
72
73template <typename Mutex>
74void MutexDebug<Mutex>::locked(const Mutex *mutex, const char *name,
75                               ulint line) UNIV_NOTHROW {
76  ut_ad(!is_owned());
77  ut_ad(m_context.m_thread_id == os_thread_id_t(ULINT_UNDEFINED));
78
79  m_context.locked(mutex, name, line);
80
81  sync_check_lock_granted(&m_context);
82}
83
84template <typename Mutex>
85void MutexDebug<Mutex>::release(const Mutex *mutex) UNIV_NOTHROW {
86  ut_ad(is_owned());
87
88  m_context.release();
89
90  sync_check_unlock(&m_context);
91}
92
93#endif /* UNIV_DEBUG */
94