1 /* Copyright (c) 2015, 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 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 <errno.h> 27 #include <stdlib.h> 28 29 #ifdef _WIN32 30 #include <windows.h> 31 32 typedef CRITICAL_SECTION native_mutex_t; 33 typedef int native_mutexattr_t; 34 #else 35 #include <pthread.h> 36 #include <stdio.h> 37 38 typedef pthread_mutex_t native_mutex_t; 39 typedef pthread_mutexattr_t native_mutexattr_t; 40 #endif 41 42 /** 43 @class My_xp_mutex 44 45 Abstract class used to wrap mutex for various platforms. 46 47 A typical use case is: 48 49 @code{.cpp} 50 51 My_xp_mutex mutex= new My_xp_mutex_impl(); 52 mutex->init(NULL); 53 54 mutex->lock(); 55 ... 56 mutex->unlock(); 57 58 @endcode 59 */ 60 class My_xp_mutex 61 { 62 public: 63 /** 64 Initialize mutex. 65 66 @param mutex attributes reference 67 @return success status 68 */ 69 70 virtual int init(const native_mutexattr_t *attr)= 0; 71 72 73 /** 74 Destroy mutex. 75 76 @return success status 77 */ 78 79 virtual int destroy()= 0; 80 81 82 /** 83 Lock mutex. 84 85 @return success status 86 */ 87 88 virtual int lock()= 0; 89 90 91 /** 92 Trylock mutex. 93 94 @return success status 95 */ 96 97 virtual int trylock()= 0; 98 99 100 /** 101 Unlock mutex. 102 103 @return success status 104 */ 105 106 virtual int unlock()= 0; 107 108 109 /** 110 To get native mutex reference. 111 112 @return native mutex pointer 113 */ 114 115 virtual native_mutex_t *get_native_mutex()= 0; 116 117 ~My_xp_mutex()118 virtual ~My_xp_mutex() {} 119 }; 120 121 #ifdef _WIN32 122 class My_xp_mutex_win : public My_xp_mutex 123 { 124 private: 125 /* 126 Disabling the copy constructor and assignment operator. 127 */ 128 My_xp_mutex_win(My_xp_mutex_win const&); 129 My_xp_mutex_win& operator=(My_xp_mutex_win const&); 130 public: 131 explicit My_xp_mutex_win(); 132 virtual ~My_xp_mutex_win(); 133 #else 134 class My_xp_mutex_pthread : public My_xp_mutex 135 { 136 private: 137 /* 138 Disabling the copy constructor and assignment operator. 139 */ 140 My_xp_mutex_pthread(My_xp_mutex_pthread const&); 141 My_xp_mutex_pthread& operator=(My_xp_mutex_pthread const&); 142 public: 143 explicit My_xp_mutex_pthread(); 144 virtual ~My_xp_mutex_pthread(); 145 #endif 146 int init(const native_mutexattr_t *attr); 147 int destroy(); 148 int lock(); 149 int trylock(); 150 int unlock(); 151 native_mutex_t *get_native_mutex(); 152 153 protected: 154 native_mutex_t *m_mutex; 155 }; 156 157 #ifdef _WIN32 158 class My_xp_mutex_impl : public My_xp_mutex_win 159 #else 160 class My_xp_mutex_impl : public My_xp_mutex_pthread 161 #endif 162 { 163 public: My_xp_mutex_impl()164 explicit My_xp_mutex_impl() {} ~My_xp_mutex_impl()165 ~My_xp_mutex_impl() {} 166 }; 167 168 class My_xp_mutex_util 169 { 170 public: 171 /** 172 Initialize mutex attributes object 173 174 @param mutex attributes reference 175 @return success status 176 */ 177 178 static int attr_init(native_mutexattr_t *attr); 179 180 181 /** 182 Destroy mutex attributes object 183 184 @param mutex attributes reference 185 @return success status 186 */ 187 188 static int attr_destroy(native_mutexattr_t *attr); 189 }; 190 191 #endif // MY_XP_MUTEX_INCLUDED 192