| 1 | /* Copyright (c) 2023, 2026, 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 designed to work 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 either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <mutex> |
| 26 | #include "mysql/components/component_implementation.h" |
| 27 | #include "mysql/components/service_implementation.h" |
| 28 | #include "mysql/components/services/mysql_mutex.h" |
| 29 | |
| 30 | namespace mysql_mutex_v1_native { |
| 31 | |
| 32 | static void _register(const char * /*category*/, PSI_mutex_info * /*info*/, |
| 33 | int /*count*/) {} |
| 34 | |
| 35 | static int _init(PSI_mutex_key /*key*/, mysql_mutex_t *that, |
| 36 | const native_mutexattr_t * /*attr*/, const char * /*src_file*/, |
| 37 | unsigned int /*src_line*/) { |
| 38 | std::mutex *mtx = new std::mutex(); |
| 39 | that->m_psi = reinterpret_cast<struct PSI_mutex *>(mtx); |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int _destroy(mysql_mutex_t *that, const char * /*src_file*/, |
| 44 | unsigned int /*src_line*/) { |
| 45 | std::mutex *mtx = reinterpret_cast<std::mutex *>(that->m_psi); |
| 46 | delete mtx; |
| 47 | that->m_psi = nullptr; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int _lock(mysql_mutex_t *that, const char * /*src_file*/, |
| 52 | unsigned int /*src_line*/) { |
| 53 | std::mutex *mtx = reinterpret_cast<std::mutex *>(that->m_psi); |
| 54 | mtx->lock(); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int _trylock(mysql_mutex_t *that, const char * /*src_file*/, |
| 59 | unsigned int /*src_line*/) { |
| 60 | std::mutex *mtx = reinterpret_cast<std::mutex *>(that->m_psi); |
| 61 | return mtx->try_lock() ? 0 : -1; |
| 62 | } |
| 63 | |
| 64 | static int _unlock(mysql_mutex_t *that, const char * /*src_file*/, |
| 65 | unsigned int /*src_line*/) { |
| 66 | std::mutex *mtx = reinterpret_cast<std::mutex *>(that->m_psi); |
| 67 | mtx->unlock(); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | } // namespace mysql_mutex_v1_native |
| 72 | |
| 73 | BEGIN_SERVICE_IMPLEMENTATION(HARNESS_COMPONENT_NAME, mysql_mutex_v1) |
| 74 | mysql_mutex_v1_native::_register, mysql_mutex_v1_native::_init, |
| 75 | mysql_mutex_v1_native::_destroy, mysql_mutex_v1_native::_lock, |
| 76 | mysql_mutex_v1_native::_trylock, mysql_mutex_v1_native::_unlock, |
| 77 | END_SERVICE_IMPLEMENTATION(); |