1 /* Copyright (c) 2015, 2019, 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 #include "plugin/group_replication/libmysqlgcs/include/mysql/gcs/xplatform/my_xp_mutex.h"
24 
25 #ifndef XCOM_STANDALONE
My_xp_mutex_server()26 My_xp_mutex_server::My_xp_mutex_server()
27     : m_mutex(static_cast<mysql_mutex_t *>(malloc(sizeof(*m_mutex)))) {}
28 
~My_xp_mutex_server()29 My_xp_mutex_server::~My_xp_mutex_server() { free(m_mutex); }
30 
get_native_mutex()31 mysql_mutex_t *My_xp_mutex_server::get_native_mutex() { return m_mutex; }
32 
init(PSI_mutex_key key,const native_mutexattr_t * attr)33 int My_xp_mutex_server::init(PSI_mutex_key key,
34                              const native_mutexattr_t *attr) {
35   if (m_mutex == nullptr) return -1;
36 
37   return mysql_mutex_init(key, m_mutex, attr);
38 }
39 
destroy()40 int My_xp_mutex_server::destroy() { return mysql_mutex_destroy(m_mutex); }
41 
lock()42 int My_xp_mutex_server::lock() { return mysql_mutex_lock(m_mutex); }
43 
trylock()44 int My_xp_mutex_server::trylock() { return mysql_mutex_trylock(m_mutex); }
45 
unlock()46 int My_xp_mutex_server::unlock() { return mysql_mutex_unlock(m_mutex); }
47 #endif
48 
attr_init(native_mutexattr_t * attr)49 int My_xp_mutex_util::attr_init(native_mutexattr_t *attr) {
50 /*
51   On Windows there is no initialization of mutex attributes.
52   Therefore, we simply return 0.
53 */
54 #ifdef _WIN32
55   return 0;
56 #else
57   return pthread_mutexattr_init(attr);
58 #endif
59 }
60 
attr_destroy(native_mutexattr_t * attr)61 int My_xp_mutex_util::attr_destroy(native_mutexattr_t *attr) {
62 /*
63   On Windows there is no destruction of mutex attributes.
64   Therefore, we simply return 0.
65 */
66 #ifdef _WIN32
67   return 0;
68 #else
69   return pthread_mutexattr_destroy(attr);
70 #endif
71 }
72