| 1 | /* Copyright (c) 2009, 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 "unittest/gunit/thread_utils.h" |
| 25 | |
| 26 | #include <errno.h> |
| 27 | #include <gtest/gtest.h> |
| 28 | #include <stddef.h> |
| 29 | #include <ostream> |
| 30 | |
| 31 | #include "gtest/gtest-message.h" |
| 32 | #include "mutex_lock.h" |
| 33 | #include "my_inttypes.h" |
| 34 | #include "mysql/psi/mysql_cond.h" |
| 35 | #include "thr_mutex.h" |
| 36 | |
| 37 | namespace thread { |
| 38 | |
| 39 | namespace { |
| 40 | extern "C" void *thread_start_routine(void *arg) { |
| 41 | Thread *start_arg = (Thread *)arg; |
| 42 | Thread::run_wrapper(start_arg); |
| 43 | return nullptr; |
| 44 | } |
| 45 | |
| 46 | // We cannot use ASSERT_FALSE in constructors/destructors, |
| 47 | // so we add a local helper routine. |
| 48 | #define LOCAL_ASSERT_FALSE(arg) assert_false(arg, __LINE__) |
| 49 | void assert_false(int arg, int line) { |
| 50 | ASSERT_FALSE(arg) << "failed with arg " << arg << " at line " << line; |
| 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | |
| 55 | int Thread::start() { |
| 56 | const int retval = |
| 57 | my_thread_create(&m_thread_handle, nullptr, thread_start_routine, this); |
| 58 | if (retval != 0) { |
| 59 | ADD_FAILURE() << " could not start thread, errno: " << errno; |
| 60 | return retval; |
| 61 | } |
| 62 | |
| 63 | return retval; |
| 64 | } |
| 65 | |
| 66 | void Thread::join() { |
| 67 | const int failed = my_thread_join(&m_thread_handle, nullptr); |
| 68 | if (failed) { |
| 69 | ADD_FAILURE() << " could not join thread id " << m_thread_handle.thread |
| 70 | << " failed: " << failed << " errno: " << errno; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void Thread::run_wrapper(Thread *start_arg) { |
| 75 | const bool error = my_thread_init(); |
| 76 | ASSERT_FALSE(error); |
| 77 | start_arg->run(); |
| 78 | my_thread_end(); |
| 79 | } |
| 80 | |
| 81 | Notification::Notification() : m_notified(false) { |
| 82 | const int failed1 = mysql_cond_init(0, &m_cond); |
| 83 | LOCAL_ASSERT_FALSE(failed1); |
| 84 | const int failed2 = mysql_mutex_init(0, &m_mutex, MY_MUTEX_INIT_FAST); |
| 85 | LOCAL_ASSERT_FALSE(failed2); |
| 86 | } |
| 87 | |
| 88 | Notification::~Notification() { |
| 89 | mysql_mutex_destroy(&m_mutex); |
| 90 | mysql_cond_destroy(&m_cond); |
| 91 | } |
| 92 | |
| 93 | bool Notification::has_been_notified() { |
| 94 | MUTEX_LOCK(lock, &m_mutex); |
| 95 | return m_notified; |
| 96 | } |
| 97 | |
| 98 | void Notification::wait_for_notification() { |
| 99 | MUTEX_LOCK(lock, &m_mutex); |
| 100 | while (!m_notified) { |
| 101 | const int failed = mysql_cond_wait(&m_cond, &m_mutex); |
| 102 | ASSERT_FALSE(failed); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void Notification::notify() { |
| 107 | MUTEX_LOCK(lock, &m_mutex); |
| 108 | m_notified = true; |
| 109 | const int failed = mysql_cond_broadcast(&m_cond); |
| 110 | ASSERT_FALSE(failed); |
| 111 | } |
| 112 | |
| 113 | } // namespace thread |