| 1 | /* Copyright (c) 2006, 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 | /* |
| 25 | As this file may be included from inside a namespace, we cannot do |
| 26 | #includes here. However, you should |
| 27 | |
| 28 | #include "my_systime.h" |
| 29 | #include "mysql/components/services/my_thread_bits.h" |
| 30 | |
| 31 | outside that namespace. |
| 32 | */ |
| 33 | |
| 34 | #include <atomic> |
| 35 | #include <vector> |
| 36 | |
| 37 | std::atomic<int32> bad; |
| 38 | my_thread_attr_t thr_attr; |
| 39 | |
| 40 | const int THREADS = 30; |
| 41 | const int CYCLES = 3000; |
| 42 | |
| 43 | void test_concurrently(const char *test, my_start_routine handler, int n, |
| 44 | int m) { |
| 45 | std::vector<my_thread_handle> t_vec; |
| 46 | t_vec.resize(n); |
| 47 | ulonglong now = my_getsystime(); |
| 48 | int num_threads = n; |
| 49 | |
| 50 | my_thread_attr_init(&thr_attr); |
| 51 | bad = 0; |
| 52 | |
| 53 | for (; n; n--) { |
| 54 | if (my_thread_create(&t_vec[n - 1], &thr_attr, handler, &m) != 0) { |
| 55 | ADD_FAILURE() << "Could not create thread"; |
| 56 | abort(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | for (int i = 0; i < num_threads; ++i) { |
| 61 | my_thread_join(&t_vec[i], nullptr); |
| 62 | } |
| 63 | |
| 64 | now = my_getsystime() - now; |
| 65 | EXPECT_FALSE(bad) << "tested " << test << " in " << ((double)now) / 1e7 |
| 66 | << " secs " |
| 67 | << "(" << bad.load() << ")"; |
| 68 | } |