| 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 | #ifndef SQL_THREAD_INCLUDED |
| 25 | #define SQL_THREAD_INCLUDED |
| 26 | |
| 27 | #include <mysql/psi/mysql_cond.h> |
| 28 | #include <mysql/psi/mysql_thread.h> |
| 29 | |
| 30 | #include "my_thread.h" |
| 31 | #include "mysql/psi/mysql_mutex.h" |
| 32 | |
| 33 | namespace thread { |
| 34 | |
| 35 | /* |
| 36 | An abstract class for creating/running/joining threads. |
| 37 | Thread::start() will create a new pthread, and execute the run() function. |
| 38 | */ |
| 39 | class Thread { |
| 40 | public: |
| 41 | Thread() = default; |
| 42 | virtual ~Thread() = default; |
| 43 | |
| 44 | /* |
| 45 | Will create a new pthread, and invoke run(); |
| 46 | Returns the value from my_thread_create(). |
| 47 | */ |
| 48 | int start(); |
| 49 | |
| 50 | /* |
| 51 | You may invoke this to wait for the thread to finish. |
| 52 | You should probalbly join() a thread before deleting it. |
| 53 | */ |
| 54 | void join(); |
| 55 | |
| 56 | // The handle of the thread (valid only if it is actually running). |
| 57 | my_thread_handle thread_handle() const { return m_thread_handle; } |
| 58 | |
| 59 | /* |
| 60 | A wrapper for the run() function. |
| 61 | Users should *not* call this function directly, they should rather |
| 62 | invoke the start() function. |
| 63 | */ |
| 64 | static void run_wrapper(Thread *); |
| 65 | |
| 66 | protected: |
| 67 | /* |
| 68 | Define this function in derived classes. |
| 69 | Users should *not* call this function directly, they should rather |
| 70 | invoke the start() function. |
| 71 | */ |
| 72 | virtual void run() = 0; |
| 73 | |
| 74 | private: |
| 75 | my_thread_handle m_thread_handle; |
| 76 | |
| 77 | Thread(const Thread &); /* Not copyable. */ |
| 78 | void operator=(const Thread &); /* Not assignable. */ |
| 79 | }; |
| 80 | |
| 81 | // A barrier which can be used for one-time synchronization between threads. |
| 82 | class Notification { |
| 83 | public: |
| 84 | Notification(); |
| 85 | ~Notification(); |
| 86 | |
| 87 | bool has_been_notified(); |
| 88 | void wait_for_notification(); |
| 89 | void notify(); |
| 90 | |
| 91 | private: |
| 92 | bool m_notified; |
| 93 | mysql_cond_t m_cond; |
| 94 | mysql_mutex_t m_mutex; |
| 95 | |
| 96 | Notification(const Notification &); /* Not copyable. */ |
| 97 | void operator=(const Notification &); /* Not assignable. */ |
| 98 | }; |
| 99 | |
| 100 | } // namespace thread |
| 101 | |
| 102 | #endif // SQL_THREAD_INCLUDED |