| 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 <chrono> |
| 26 | #include <condition_variable> |
| 27 | #include <ctime> |
| 28 | #include <mutex> |
| 29 | #include "mysql/components/component_implementation.h" |
| 30 | #include "mysql/components/service_implementation.h" |
| 31 | #include "mysql/components/services/mysql_cond.h" |
| 32 | |
| 33 | namespace mysql_cond_v1_native { |
| 34 | |
| 35 | using std::condition_variable; |
| 36 | using std::defer_lock; |
| 37 | using std::mutex; |
| 38 | using std::unique_lock; |
| 39 | using std::chrono::time_point; |
| 40 | using mock_clock = std::chrono::system_clock; |
| 41 | using std::cv_status; |
| 42 | using std::chrono::nanoseconds; |
| 43 | |
| 44 | static void _register(const char * /*category*/, PSI_cond_info * /*info*/, |
| 45 | int /*count*/) {} |
| 46 | |
| 47 | static int init(PSI_cond_key /*key*/, mysql_cond_t *that, |
| 48 | const char * /*src_file*/, unsigned int /*src_line*/) { |
| 49 | condition_variable *cond = new condition_variable(); |
| 50 | that->m_psi = reinterpret_cast<PSI_cond *>(cond); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int destroy(mysql_cond_t *that, const char * /*src_file*/, |
| 55 | unsigned int /*src_line*/) { |
| 56 | condition_variable *cond = |
| 57 | reinterpret_cast<condition_variable *>(that->m_psi); |
| 58 | delete cond; |
| 59 | that->m_psi = nullptr; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int wait(mysql_cond_t *that, mysql_mutex_t *mutex_arg, |
| 64 | const char * /*src_file*/, unsigned int /*src_line*/) { |
| 65 | condition_variable *cond = |
| 66 | reinterpret_cast<condition_variable *>(that->m_psi); |
| 67 | mutex *mtx = reinterpret_cast<mutex *>(mutex_arg->m_psi); |
| 68 | unique_lock lck(*mtx, defer_lock); |
| 69 | cond->wait(lck); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int timedwait(mysql_cond_t *that, mysql_mutex_t *mutex_arg, |
| 74 | const struct timespec *abstime, const char * /*src_file*/, |
| 75 | unsigned int /*src_line*/) { |
| 76 | condition_variable *cond = |
| 77 | reinterpret_cast<condition_variable *>(that->m_psi); |
| 78 | mutex *mtx = reinterpret_cast<mutex *>(mutex_arg->m_psi); |
| 79 | unique_lock lck(*mtx, defer_lock); |
| 80 | return cond->wait_until(lck, mock_clock::from_time_t(abstime->tv_sec) + |
| 81 | nanoseconds(abstime->tv_nsec)) == |
| 82 | cv_status::timeout |
| 83 | ? 1 |
| 84 | : 0; |
| 85 | } |
| 86 | |
| 87 | static int signal(mysql_cond_t *that, const char * /*src_file*/, |
| 88 | unsigned int /*src_line*/) { |
| 89 | condition_variable *cond = |
| 90 | reinterpret_cast<condition_variable *>(that->m_psi); |
| 91 | cond->notify_one(); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int broadcast(mysql_cond_t *that, const char * /*src_file*/, |
| 96 | unsigned int /*src_line*/) { |
| 97 | condition_variable *cond = |
| 98 | reinterpret_cast<condition_variable *>(that->m_psi); |
| 99 | cond->notify_all(); |
| 100 | return 0; |
| 101 | } |
| 102 | } // namespace mysql_cond_v1_native |
| 103 | |
| 104 | BEGIN_SERVICE_IMPLEMENTATION(HARNESS_COMPONENT_NAME, mysql_cond_v1) |
| 105 | mysql_cond_v1_native::_register, mysql_cond_v1_native::init, |
| 106 | mysql_cond_v1_native::destroy, mysql_cond_v1_native::wait, |
| 107 | mysql_cond_v1_native::timedwait, mysql_cond_v1_native::signal, |
| 108 | mysql_cond_v1_native::broadcast, END_SERVICE_IMPLEMENTATION(); |