1/*****************************************************************************
2
3Copyright (c) 2013, 2021, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License, version 2.0,
7as published by the Free Software Foundation.
8
9This program is also distributed with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation.  The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have included with MySQL.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU General Public License, version 2.0, for 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 Street, Suite 500, Boston, MA 02110-1335 USA
24
25*****************************************************************************/
26
27/******************************************************************//**
28@file include/sync0policy.ic
29Policy for mutexes.
30
31Created 2012-08-21 Sunny Bains.
32***********************************************************************/
33
34#include "sync0debug.h"
35
36template <typename Mutex>
37std::string GenericPolicy<Mutex>::to_string() const
38{
39	return(sync_mutex_to_string(get_id(), sync_file_created_get(this)));
40}
41
42template <typename Mutex>
43std::string AggregateMutexStatsPolicy<Mutex>::to_string() const
44{
45	switch (m_id) {
46
47	case LATCH_ID_BUF_BLOCK_MUTEX:
48	case LATCH_ID_BUF_POOL_ZIP:
49		/* I don't think it makes sense to keep track of the file name
50		and line number for each block mutex. Too much of overhead.
51		Use the latch id to figure out the location from the source. */
52		return(sync_mutex_to_string(get_id(), "buf0buf.cc:0"));
53
54	case LATCH_ID_AUTOINC:
55		return(sync_mutex_to_string(
56			get_id(), "dict_table_autoinc_alloc():0"));
57
58	default:
59		/* Currently only block mutexes and autoinc mutexes use aggregrate
60		Latch Stat counters. If any new mutex uses this aggregrate,
61		add entry here */
62		ut_ad(0);
63	}
64
65	ut_ad(0);
66	return("unknown");
67}
68
69#ifdef UNIV_DEBUG
70
71template <typename Mutex>
72void MutexDebug<Mutex>::init(latch_id_t id)
73	UNIV_NOTHROW
74{
75	m_context.m_id = id;
76
77	m_context.release();
78
79	m_magic_n = MUTEX_MAGIC_N;
80}
81
82template <typename Mutex>
83void MutexDebug<Mutex>::enter(
84	const Mutex*	mutex,
85	const char*	name,
86	ulint		line)
87	UNIV_NOTHROW
88{
89	ut_ad(!is_owned());
90
91	Context	context(m_context.get_id());
92
93	context.locked(mutex, name, line);
94
95	/* Check for latch order violation. */
96
97	sync_check_lock_validate(&context);
98}
99
100template <typename Mutex>
101void MutexDebug<Mutex>::locked(
102	const Mutex*	mutex,
103	const char*	name,
104	ulint		line)
105	UNIV_NOTHROW
106{
107	ut_ad(!is_owned());
108	ut_ad(m_context.m_thread_id == os_thread_id_t(ULINT_UNDEFINED));
109
110	m_context.locked(mutex, name, line);
111
112	sync_check_lock_granted(&m_context);
113}
114
115template <typename Mutex>
116void MutexDebug<Mutex>::release(const Mutex* mutex)
117	UNIV_NOTHROW
118{
119	ut_ad(is_owned());
120
121	m_context.release();
122
123	sync_check_unlock(&m_context);
124}
125
126#endif /* UNIV_DEBUG */
127