summaryrefslogtreecommitdiff
path: root/storage/innobase/include/ut0mutex.h
blob: 2f9bb5b75fc3eba89c65fa57dbdab72ad7d3667a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*****************************************************************************

Copyright (c) 2012, 2015, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2017, 2022, MariaDB Corporation.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA

*****************************************************************************/

/******************************************************************//**
@file include/ut0mutex.h
Policy based mutexes.

Created 2012-03-24 Sunny Bains.
***********************************************************************/

#pragma once
#ifndef UNIV_INNOCHECKSUM
#include "sync0policy.h"
#include "ib0mutex.h"

/** Create a typedef using the MutexType<PolicyType>
@param[in]	M		Mutex type
@param[in[	P		Policy type
@param[in]	T		The resulting typedef alias */
#define UT_MUTEX_TYPE(M, P, T) typedef PolicyMutex<M<P> > T;

# ifdef __linux__
UT_MUTEX_TYPE(TTASFutexMutex, GenericPolicy, FutexMutex);
# endif /* __linux__ */

UT_MUTEX_TYPE(TTASMutex, GenericPolicy, SpinMutex);
UT_MUTEX_TYPE(OSTrackMutex, GenericPolicy, SysMutex);
UT_MUTEX_TYPE(TTASEventMutex, GenericPolicy, SyncArrayMutex);

#ifdef MUTEX_FUTEX
/** The default mutex type. */
typedef FutexMutex ib_mutex_t;
#define MUTEX_TYPE	"Uses futexes"
#elif defined(MUTEX_SYS)
typedef SysMutex ib_mutex_t;
#define MUTEX_TYPE	"Uses system mutexes"
#elif defined(MUTEX_EVENT)
typedef SyncArrayMutex ib_mutex_t;
#define MUTEX_TYPE	"Uses event mutexes"
#else
#error "ib_mutex_t type is unknown"
#endif /* MUTEX_FUTEX */

extern uint	srv_spin_wait_delay;
extern ulong	srv_n_spin_wait_rounds;

#define mutex_create(I, M)		mutex_init((M), (I),		\
						   __FILE__, __LINE__)

#define mutex_enter_loc(M,file,line)	(M)->enter(			\
					uint32_t(srv_n_spin_wait_rounds), \
					uint32_t(srv_spin_wait_delay),	\
					file, line)
#define mutex_enter(M)			mutex_enter_loc(M, __FILE__, __LINE__)

#define mutex_enter_nospin(M)		(M)->enter(			\
					0,				\
					0,				\
					__FILE__, uint32_t(__LINE__))

#define mutex_enter_nowait(M)		(M)->trylock(__FILE__,		\
						     uint32_t(__LINE__))

#define mutex_exit(M)			(M)->exit()

#define mutex_free(M)			mutex_destroy(M)

#ifdef UNIV_DEBUG
/**
Checks that the mutex has been initialized. */
#define mutex_validate(M)		(M)->validate()

/**
Checks that the current thread owns the mutex. Works only
in the debug version. */
#define mutex_own(M)			(M)->is_owned()
#else
#define mutex_own(M)			/* No op */
#define mutex_validate(M)		/* No op */
#endif /* UNIV_DEBUG */

/** Iterate over the mutex meta data */
class MutexMonitor {
public:
	/** Constructor */
	MutexMonitor() { }

	/** Destructor */
	~MutexMonitor() { }

	/** Enable the mutex monitoring */
	void enable();

	/** Disable the mutex monitoring */
	void disable();

	/** Reset the mutex monitoring values */
	void reset();

	/** Invoke the callback for each active mutex collection
	@param[in,out]	callback	Functor to call
	@return false if callback returned false */
	template<typename Callback>
	bool iterate(Callback& callback) const
		UNIV_NOTHROW
	{
		LatchMetaData::iterator	end = latch_meta.end();

		for (LatchMetaData::iterator it = latch_meta.begin();
		     it != end;
		     ++it) {

			/* Some of the slots will be null in non-debug mode */

			if (latch_meta_t* l= *it) {
				if (!callback(*l)) {
					return false;
				}
			}
		}

		return(true);
	}
};

/** Defined in sync0sync.cc */
extern MutexMonitor	mutex_monitor;

/**
Creates, or rather, initializes a mutex object in a specified memory
location (which must be appropriately aligned). The mutex is initialized
in the reset state. Explicit freeing of the mutex with mutex_free is
necessary only if the memory block containing it is freed.
Add the mutex instance to the global mutex list.
@param[in,out]	mutex		mutex to initialise
@param[in]	id		The mutex ID (Latch ID)
@param[in]	filename	Filename from where it was called
@param[in]	line		Line number in filename from where called */
template <typename Mutex>
void mutex_init(
	Mutex*		mutex,
	latch_id_t	id,
	const char*	file_name,
	uint32_t	line)
{
	new(mutex) Mutex();

	mutex->init(id, file_name, line);
}

/**
Removes a mutex instance from the mutex list. The mutex is checked to
be in the reset state.
@param[in,out]	 mutex		mutex instance to destroy */
template <typename Mutex>
void mutex_destroy(
	Mutex*		mutex)
{
	mutex->destroy();
}

#endif /* UNIV_INNOCHECKSUM */