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 #include "xplatform/my_xp_mutex.h"
24 
25 #ifdef _WIN32
My_xp_mutex_win()26 My_xp_mutex_win::My_xp_mutex_win()
27   :m_mutex(static_cast<native_mutex_t *>(malloc(sizeof(*m_mutex))))
28 {}
29 
30 
~My_xp_mutex_win()31 My_xp_mutex_win::~My_xp_mutex_win()
32 {
33   free(m_mutex);
34 }
35 
36 
get_native_mutex()37 native_mutex_t *My_xp_mutex_win::get_native_mutex()
38 {
39   return m_mutex;
40 }
41 
42 
init(const native_mutexattr_t * attr)43 int My_xp_mutex_win::init(const native_mutexattr_t *attr)
44 {
45   InitializeCriticalSection(m_mutex);
46   return 0;
47 };
48 
49 
destroy()50 int My_xp_mutex_win::destroy()
51 {
52   DeleteCriticalSection(m_mutex);
53   return 0;
54 }
55 
56 
lock()57 int My_xp_mutex_win::lock()
58 {
59   EnterCriticalSection(m_mutex);
60   return 0;
61 }
62 
63 
trylock()64 int My_xp_mutex_win::trylock()
65 {
66   if (TryEnterCriticalSection(m_mutex))
67   {
68     /* Don't allow recursive lock */
69     if (m_mutex->RecursionCount > 1)
70     {
71       LeaveCriticalSection(m_mutex);
72       return EBUSY;
73     }
74     return 0;
75   }
76   return EBUSY;
77 }
78 
79 
unlock()80 int My_xp_mutex_win::unlock()
81 {
82   LeaveCriticalSection(m_mutex);
83   return 0;
84 }
85 
86 
attr_init(native_mutexattr_t * attr)87 int My_xp_mutex_util::attr_init(native_mutexattr_t *attr)
88 {
89   return 0;
90 }
91 
92 
attr_destroy(native_mutexattr_t * attr)93 int My_xp_mutex_util::attr_destroy(native_mutexattr_t *attr)
94 {
95   return 0;
96 }
97 
98 #else
My_xp_mutex_pthread()99 My_xp_mutex_pthread::My_xp_mutex_pthread()
100   :m_mutex(static_cast<native_mutex_t *>(malloc(sizeof(*m_mutex))))
101 {}
102 
103 
~My_xp_mutex_pthread()104 My_xp_mutex_pthread::~My_xp_mutex_pthread()
105 {
106   free(m_mutex);
107 }
108 
109 
get_native_mutex()110 native_mutex_t *My_xp_mutex_pthread::get_native_mutex()
111 {
112   return m_mutex;
113 }
114 
init(const native_mutexattr_t * attr)115 int My_xp_mutex_pthread::init(const native_mutexattr_t *attr)
116 {
117   if (m_mutex == NULL)
118     return -1;
119 
120   return pthread_mutex_init(m_mutex, attr);
121 };
122 
123 
destroy()124 int My_xp_mutex_pthread::destroy()
125 {
126   return pthread_mutex_destroy(m_mutex);
127 }
128 
129 
lock()130 int My_xp_mutex_pthread::lock()
131 {
132   return pthread_mutex_lock(m_mutex);
133 }
134 
135 /* purecov: begin deadcode */
trylock()136 int My_xp_mutex_pthread::trylock()
137 {
138   return pthread_mutex_trylock(m_mutex);
139 }
140 /* purecov: end */
141 
unlock()142 int My_xp_mutex_pthread::unlock()
143 {
144   return pthread_mutex_unlock(m_mutex);
145 }
146 
147 /* purecov: begin deadcode */
attr_init(native_mutexattr_t * attr)148 int My_xp_mutex_util::attr_init(native_mutexattr_t *attr)
149 {
150   return pthread_mutexattr_init(attr);
151 }
152 
153 
attr_destroy(native_mutexattr_t * attr)154 int My_xp_mutex_util::attr_destroy(native_mutexattr_t *attr)
155 {
156   return pthread_mutexattr_destroy(attr);
157 }
158 /* purecov: end */
159 #endif
160