1 /* Copyright (c) 2012, 2021, Oracle and/or its affiliates.
2 
3   This program is free software; you can redistribute it and/or modify
4   it under the terms of the GNU General Public License, version 2.0,
5   as published by the Free Software Foundation.
6 
7   This program is also distributed with certain software (including
8   but not limited to OpenSSL) that is licensed under separate terms,
9   as designated in a particular file or component or in included license
10   documentation.  The authors of MySQL hereby grant you an additional
11   permission to link the program and your derivative works with the
12   separately licensed software that they have included with MySQL.
13 
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License, version 2.0, for more details.
18 
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software Foundation,
21   51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 #ifndef MYSQL_MDL_H
24 #define MYSQL_MDL_H
25 
26 /**
27   @file mysql/psi/mysql_mdl.h
28   Instrumentation helpers for metadata locks.
29 */
30 
31 #include "mysql/psi/psi.h"
32 
33 #ifdef HAVE_PSI_METADATA_INTERFACE
34 
35 #ifndef PSI_METADATA_CALL
36 #define PSI_METADATA_CALL(M) PSI_DYNAMIC_CALL(M)
37 #endif
38 
39 #define PSI_CALL_start_metadata_wait(A,B,C,D) PSI_METADATA_CALL(start_metadata_wait)(A,B,C,D)
40 #define PSI_CALL_end_metadata_wait(A,B) PSI_METADATA_CALL(end_metadata_wait)(A,B)
41 #define PSI_CALL_create_metadata_lock(A,B,C,D,E,F,G) PSI_METADATA_CALL(create_metadata_lock)(A,B,C,D,E,F,G)
42 #define PSI_CALL_set_metadata_lock_status(A,B) PSI_METADATA_CALL(set_metadata_lock_status)(A,B)
43 #define PSI_CALL_destroy_metadata_lock(A) PSI_METADATA_CALL(destroy_metadata_lock)(A)
44 #else
45 #define PSI_CALL_start_metadata_wait(A,B,C,D) 0
46 #define PSI_CALL_end_metadata_wait(A,B) do { } while(0)
47 #define PSI_CALL_create_metadata_lock(A,B,C,D,E,F,G) 0
48 #define PSI_CALL_set_metadata_lock_status(A,B) do {} while(0)
49 #define PSI_CALL_destroy_metadata_lock(A) do {} while(0)
50 #endif
51 
52 /**
53   @defgroup Thread_instrumentation Metadata Instrumentation
54   @ingroup Instrumentation_interface
55   @{
56 */
57 
58 /**
59   @def mysql_mdl_create(K, M, A)
60   Instrumented metadata lock creation.
61   @param I Metadata lock identity
62   @param K Metadata key
63   @param T Metadata lock type
64   @param D Metadata lock duration
65   @param S Metadata lock status
66   @param F request source file
67   @param L request source line
68 */
69 
70 #ifdef HAVE_PSI_METADATA_INTERFACE
71   #define mysql_mdl_create(I, K, T, D, S, F, L) \
72     inline_mysql_mdl_create(I, K, T, D, S, F, L)
73 #else
74   #define mysql_mdl_create(I, K, T, D, S, F, L) NULL
75 #endif
76 
77 #ifdef HAVE_PSI_METADATA_INTERFACE
78   #define mysql_mdl_set_status(L, S) \
79     inline_mysql_mdl_set_status(L, S)
80 #else
81   #define mysql_mdl_set_status(L, S) \
82     do {} while (0)
83 #endif
84 
85 
86 /**
87   @def mysql_mdl_destroy(M)
88   Instrumented metadata lock destruction.
89   @param M Metadata lock
90 */
91 #ifdef HAVE_PSI_METADATA_INTERFACE
92   #define mysql_mdl_destroy(M) \
93     inline_mysql_mdl_destroy(M, __FILE__, __LINE__)
94 #else
95   #define mysql_mdl_destroy(M) \
96     do {} while (0)
97 #endif
98 
99 #ifdef HAVE_PSI_METADATA_INTERFACE
100 
101 static inline PSI_metadata_lock *
inline_mysql_mdl_create(void * identity,const MDL_key * mdl_key,enum_mdl_type mdl_type,enum_mdl_duration mdl_duration,MDL_ticket::enum_psi_status mdl_status,const char * src_file,uint src_line)102 inline_mysql_mdl_create(void *identity,
103                         const MDL_key *mdl_key,
104                         enum_mdl_type mdl_type,
105                         enum_mdl_duration mdl_duration,
106                         MDL_ticket::enum_psi_status mdl_status,
107                         const char *src_file, uint src_line)
108 {
109   PSI_metadata_lock *result;
110 
111   /* static_cast: Fit a round C++ enum peg into a square C int hole ... */
112   result= PSI_METADATA_CALL(create_metadata_lock)
113     (identity,
114      mdl_key,
115      static_cast<opaque_mdl_type> (mdl_type),
116      static_cast<opaque_mdl_duration> (mdl_duration),
117      static_cast<opaque_mdl_status> (mdl_status),
118      src_file, src_line);
119 
120   return result;
121 }
122 
inline_mysql_mdl_set_status(PSI_metadata_lock * psi,MDL_ticket::enum_psi_status mdl_status)123 static inline void inline_mysql_mdl_set_status(
124   PSI_metadata_lock *psi,
125   MDL_ticket::enum_psi_status mdl_status)
126 {
127   if (psi != NULL)
128     PSI_METADATA_CALL(set_metadata_lock_status)(psi, mdl_status);
129 }
130 
inline_mysql_mdl_destroy(PSI_metadata_lock * psi,const char * src_file,uint src_line)131 static inline void inline_mysql_mdl_destroy(
132   PSI_metadata_lock *psi,
133   const char *src_file, uint src_line)
134 {
135   if (psi != NULL)
136     PSI_METADATA_CALL(destroy_metadata_lock)(psi);
137 }
138 #endif /* HAVE_PSI_METADATA_INTERFACE */
139 
140 /** @} (end of group Metadata_instrumentation) */
141 
142 #endif
143 
144