1 /* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
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
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef MY_XP_MUTEX_INCLUDED
24 #define MY_XP_MUTEX_INCLUDED
25 
26 #include "mysql/psi/mysql_mutex.h"
27 
28 /**
29   @class My_xp_mutex
30 
31   Abstract class used to wrap mutex for various implementations.
32 
33   A typical use case is:
34 
35   @code{.cpp}
36 
37   My_xp_mutex *mutex= new My_xp_mutex_impl();
38   mutex->init(mutex_PSI_key, NULL);
39 
40   mutex->lock();
41   ...
42   mutex->unlock();
43 
44   @endcode
45 */
46 class My_xp_mutex {
47  public:
48   /**
49     Initialize mutex.
50 
51     @param key mutex instrumentation key
52     @param attr mutex attributes reference
53     @return success status
54   */
55 
56   virtual int init(PSI_mutex_key key, const native_mutexattr_t *attr) = 0;
57 
58   /**
59     Destroy mutex.
60 
61     @return success status
62   */
63 
64   virtual int destroy() = 0;
65 
66   /**
67     Lock mutex.
68 
69     @return success status
70   */
71 
72   virtual int lock() = 0;
73 
74   /**
75     Trylock mutex.
76 
77     @return success status
78   */
79 
80   virtual int trylock() = 0;
81 
82   /**
83     Unlock mutex.
84 
85     @return success status
86   */
87 
88   virtual int unlock() = 0;
89 
90   /**
91     To get native mutex reference.
92 
93     @return native mutex pointer
94   */
95 
96   virtual mysql_mutex_t *get_native_mutex() = 0;
97 
~My_xp_mutex()98   virtual ~My_xp_mutex() {}
99 };
100 
101 #ifndef XCOM_STANDALONE
102 class My_xp_mutex_server : public My_xp_mutex {
103  public:
104   explicit My_xp_mutex_server();
105   virtual ~My_xp_mutex_server();
106 
107   int init(PSI_mutex_key key, const native_mutexattr_t *attr);
108   int destroy();
109   int lock();
110   int trylock();
111   int unlock();
112   mysql_mutex_t *get_native_mutex();
113 
114  protected:
115   mysql_mutex_t *m_mutex;
116 };
117 #endif
118 
119 #ifndef XCOM_STANDALONE
120 class My_xp_mutex_impl : public My_xp_mutex_server
121 #endif
122 {
123  public:
My_xp_mutex_impl()124   explicit My_xp_mutex_impl() {}
~My_xp_mutex_impl()125   ~My_xp_mutex_impl() {}
126 };
127 
128 class My_xp_mutex_util {
129  public:
130   /**
131     Initialize mutex attributes object
132 
133     @param attr mutex attributes reference
134     @return success status
135   */
136 
137   static int attr_init(native_mutexattr_t *attr);
138 
139   /**
140     Destroy mutex attributes object
141 
142     @param attr mutex attributes reference
143     @return success status
144   */
145 
146   static int attr_destroy(native_mutexattr_t *attr);
147 };
148 
149 #endif  // MY_XP_MUTEX_INCLUDED
150