1
/* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software Foundation,
14
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
20
@file storage/perfschema/pfs_lock.h
21
Performance schema internal locks (declarations).
24
#include "pfs_atomic.h"
27
@addtogroup Performance_schema_buffers
32
State of a free record.
33
Values of a free record should not be read by a reader.
34
Writers can concurrently attempt to allocate a free record.
36
#define PFS_LOCK_FREE 0x00
38
State of a dirty record.
39
Values of a dirty record should not be read by a reader,
40
as the record is currently being modified.
41
Only one writer, the writer which owns the record, should
42
modify the record content.
44
#define PFS_LOCK_DIRTY 0x01
46
State of an allocated record.
47
Values of an allocated record are safe to read by a reader.
48
A writer may modify some but not all properties of the record:
49
only modifying values that can never cause the reader to crash is allowed.
51
#define PFS_LOCK_ALLOCATED 0x02
53
#define VERSION_MASK 0xFFFFFFFC
54
#define STATE_MASK 0x00000003
58
A 'lock' protecting performance schema internal buffers.
59
This lock is used to mark the state of a record.
60
Access to the record is not enforced here,
61
it's up to the readers and writers to look at the record state
62
before making an actual read or write operation.
67
The record internal version and state
70
@sa PFS_LOCK_ALLOCATED
71
The version number is to transform the 'ABA' problem
72
(see http://en.wikipedia.org/wiki/ABA_problem)
73
into an 'A(n)BA(n + 1)' problem, where 'n' is the m_version number.
74
When the performance schema instrumentation deletes a record,
75
then create a different record reusing the same memory allocation,
76
the version number is incremented, so that a reader can detect that
77
the record was changed. Note that the version number is never
78
reset to zero when a new record is created.
79
The version number is stored in the high 30 bits.
80
The state is stored in the low 2 bits.
82
volatile uint32 m_version_state;
84
/** Returns true if the record is free. */
87
uint32 copy= m_version_state; /* non volatile copy, and dirty read */
88
return ((copy & STATE_MASK) == PFS_LOCK_FREE);
91
/** Returns true if the record contains values that can be read. */
92
bool is_populated(void)
94
uint32 copy= m_version_state; /* non volatile copy, and dirty read */
95
return ((copy & STATE_MASK) == PFS_LOCK_ALLOCATED);
99
Execute a free to dirty transition.
100
This transition is safe to execute concurrently by multiple writers.
101
Only one writer will succeed to acquire the record.
102
@return true if the operation succeed
104
bool free_to_dirty(void)
106
uint32 copy= m_version_state; /* non volatile copy, and dirty read */
107
uint32 old_val= (copy & VERSION_MASK) + PFS_LOCK_FREE;
108
uint32 new_val= (copy & VERSION_MASK) + PFS_LOCK_DIRTY;
110
return (PFS_atomic::cas_u32(&m_version_state, &old_val, new_val));
114
Execute an allocated to dirty transition.
115
This transition should be executed by the writer that owns the record,
116
before the record is modified.
118
void allocated_to_dirty(void)
120
uint32 copy= PFS_atomic::load_u32(&m_version_state);
121
/* Make sure the record was ALLOCATED. */
122
DBUG_ASSERT((copy & STATE_MASK) == PFS_LOCK_ALLOCATED);
123
/* Keep the same version, set the DIRTY state */
124
uint32 new_val= (copy & VERSION_MASK) + PFS_LOCK_DIRTY;
125
/* We own the record, no need to use compare and swap. */
126
PFS_atomic::store_u32(&m_version_state, new_val);
130
Execute a dirty to allocated transition.
131
This transition should be executed by the writer that owns the record,
132
after the record is in a state ready to be read.
134
void dirty_to_allocated(void)
136
uint32 copy= PFS_atomic::load_u32(&m_version_state);
137
/* Make sure the record was DIRTY. */
138
DBUG_ASSERT((copy & STATE_MASK) == PFS_LOCK_DIRTY);
139
/* Increment the version, set the ALLOCATED state */
140
uint32 new_val= (copy & VERSION_MASK) + VERSION_INC + PFS_LOCK_ALLOCATED;
141
PFS_atomic::store_u32(&m_version_state, new_val);
145
Initialize a lock to allocated.
146
This transition should be executed by the writer that owns the record and the lock,
147
after the record is in a state ready to be read.
149
void set_allocated(void)
151
/* Do not set the version to 0, read the previous value. */
152
uint32 copy= PFS_atomic::load_u32(&m_version_state);
153
/* Increment the version, set the ALLOCATED state */
154
uint32 new_val= (copy & VERSION_MASK) + VERSION_INC + PFS_LOCK_ALLOCATED;
155
PFS_atomic::store_u32(&m_version_state, new_val);
159
Execute a dirty to free transition.
160
This transition should be executed by the writer that owns the record.
162
void dirty_to_free(void)
164
uint32 copy= PFS_atomic::load_u32(&m_version_state);
165
/* Make sure the record was DIRTY. */
166
DBUG_ASSERT((copy & STATE_MASK) == PFS_LOCK_DIRTY);
167
/* Keep the same version, set the FREE state */
168
uint32 new_val= (copy & VERSION_MASK) + PFS_LOCK_FREE;
169
PFS_atomic::store_u32(&m_version_state, new_val);
173
Execute an allocated to free transition.
174
This transition should be executed by the writer that owns the record.
176
void allocated_to_free(void)
179
extern volatile bool ready_to_exit;
183
If this record is not in the ALLOCATED state and the caller is trying
184
to free it, this is a bug: the caller is confused,
185
and potentially damaging data owned by another thread or object.
186
The correct assert to use here to guarantee data integrity is simply:
187
DBUG_ASSERT(m_state == PFS_LOCK_ALLOCATED);
188
Now, because of Bug#56666 (Race condition between the server main thread
189
and the kill server thread), this assert actually fails during shutdown,
190
and the failure is legitimate, on concurrent calls to mysql_*_destroy(),
191
when destroying the instrumentation of an object ... twice.
192
During shutdown this has no consequences for the performance schema,
193
so the assert is relaxed with the "|| ready_to_exit" condition as a work
194
around until Bug#56666 is fixed.
196
uint32 copy= PFS_atomic::load_u32(&m_version_state);
197
/* Make sure the record was ALLOCATED. */
198
DBUG_ASSERT(((copy & STATE_MASK) == PFS_LOCK_ALLOCATED) || ready_to_exit);
199
/* Keep the same version, set the FREE state */
200
uint32 new_val= (copy & VERSION_MASK) + PFS_LOCK_FREE;
201
PFS_atomic::store_u32(&m_version_state, new_val);
205
Start an optimistic read operation.
206
@sa end_optimist_lock.
208
void begin_optimistic_lock(struct pfs_lock *copy)
210
copy->m_version_state= PFS_atomic::load_u32(&m_version_state);
214
End an optimistic read operation.
215
@sa begin_optimist_lock.
216
@return true if the data read is safe to use.
218
bool end_optimistic_lock(struct pfs_lock *copy)
220
/* Check there was valid data to look at. */
221
if ((copy->m_version_state & STATE_MASK) != PFS_LOCK_ALLOCATED)
224
/* Check the version + state has not changed. */
225
if (copy->m_version_state != PFS_atomic::load_u32(&m_version_state))
233
return (PFS_atomic::load_u32(&m_version_state) & VERSION_MASK);