David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 20 | #include <thrift/thrift-config.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 21 | #include <thrift/concurrency/Thread.h> |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 22 | #include <thrift/concurrency/ThreadFactory.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 23 | #include <thrift/concurrency/Monitor.h> |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 24 | #include <thrift/concurrency/Mutex.h> |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 25 | |
| 26 | #include <assert.h> |
| 27 | #include <iostream> |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 28 | #include <vector> |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 29 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 30 | namespace apache { |
| 31 | namespace thrift { |
| 32 | namespace concurrency { |
| 33 | namespace test { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 34 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 35 | using std::shared_ptr; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 36 | using namespace apache::thrift::concurrency; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 37 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 38 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 39 | * ThreadManagerTests class |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 40 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 41 | * @version $Id:$ |
| 42 | */ |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 43 | class ThreadFactoryTests { |
| 44 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 45 | public: |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 46 | /** |
| 47 | * Reap N threads |
| 48 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 49 | class ReapNTask : public Runnable { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 50 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 51 | public: |
| 52 | ReapNTask(Monitor& monitor, int& activeCount) : _monitor(monitor), _count(activeCount) {} |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 53 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 54 | void run() override { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 55 | Synchronized s(_monitor); |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 56 | |
| 57 | if (--_count == 0) { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 58 | _monitor.notify(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | Monitor& _monitor; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 63 | int& _count; |
| 64 | }; |
| 65 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 66 | bool reapNThreads(int loop = 1, int count = 10) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 67 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 68 | ThreadFactory threadFactory = ThreadFactory(); |
James E. King, III | 3620090 | 2016-10-05 14:47:18 -0400 | [diff] [blame] | 69 | shared_ptr<Monitor> monitor(new Monitor); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 70 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 71 | for (int lix = 0; lix < loop; lix++) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 72 | |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 73 | int activeCount = 0; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 74 | |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 75 | std::vector<shared_ptr<Thread> > threads; |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 76 | int tix; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 77 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 78 | for (tix = 0; tix < count; tix++) { |
| 79 | try { |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 80 | ++activeCount; |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 81 | threads.push_back( |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 82 | threadFactory.newThread(shared_ptr<Runnable>(new ReapNTask(*monitor, activeCount)))); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 83 | } catch (SystemResourceException& e) { |
| 84 | std::cout << "\t\t\tfailed to create " << lix* count + tix << " thread " << e.what() |
| 85 | << std::endl; |
Gaurav Singh | 77c4afa | 2020-02-02 20:13:07 -0500 | [diff] [blame] | 86 | throw; |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 87 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 88 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 89 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 90 | tix = 0; |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 91 | for (std::vector<shared_ptr<Thread> >::const_iterator thread = threads.begin(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 92 | thread != threads.end(); |
| 93 | tix++, ++thread) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 94 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 95 | try { |
| 96 | (*thread)->start(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 97 | } catch (SystemResourceException& e) { |
| 98 | std::cout << "\t\t\tfailed to start " << lix* count + tix << " thread " << e.what() |
| 99 | << std::endl; |
Gaurav Singh | 77c4afa | 2020-02-02 20:13:07 -0500 | [diff] [blame] | 100 | throw; |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 103 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 104 | { |
| 105 | Synchronized s(*monitor); |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 106 | while (activeCount > 0) { |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 107 | monitor->wait(1000); |
| 108 | } |
| 109 | } |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 110 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 111 | std::cout << "\t\t\treaped " << lix* count << " threads" << std::endl; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | std::cout << "\t\t\tSuccess!" << std::endl; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 118 | class SynchStartTask : public Runnable { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 119 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 120 | public: |
| 121 | enum STATE { UNINITIALIZED, STARTING, STARTED, STOPPING, STOPPED }; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 122 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 123 | SynchStartTask(Monitor& monitor, volatile STATE& state) : _monitor(monitor), _state(state) {} |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 124 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 125 | void run() override { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 126 | { |
| 127 | Synchronized s(_monitor); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 128 | if (_state == SynchStartTask::STARTING) { |
| 129 | _state = SynchStartTask::STARTED; |
| 130 | _monitor.notify(); |
| 131 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 134 | { |
| 135 | Synchronized s(_monitor); |
| 136 | while (_state == SynchStartTask::STARTED) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 137 | _monitor.wait(); |
| 138 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 139 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 140 | if (_state == SynchStartTask::STOPPING) { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 141 | _state = SynchStartTask::STOPPED; |
| 142 | _monitor.notifyAll(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 143 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 147 | private: |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 148 | Monitor& _monitor; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 149 | volatile STATE& _state; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | bool synchStartTest() { |
| 153 | |
| 154 | Monitor monitor; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 155 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 156 | SynchStartTask::STATE state = SynchStartTask::UNINITIALIZED; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 157 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 158 | shared_ptr<SynchStartTask> task |
| 159 | = shared_ptr<SynchStartTask>(new SynchStartTask(monitor, state)); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 160 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 161 | ThreadFactory threadFactory = ThreadFactory(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 162 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 163 | shared_ptr<Thread> thread = threadFactory.newThread(task); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 164 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 165 | if (state == SynchStartTask::UNINITIALIZED) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 166 | |
| 167 | state = SynchStartTask::STARTING; |
| 168 | |
| 169 | thread->start(); |
| 170 | } |
| 171 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 172 | { |
| 173 | Synchronized s(monitor); |
| 174 | while (state == SynchStartTask::STARTING) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 175 | monitor.wait(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | assert(state != SynchStartTask::STARTING); |
| 180 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 181 | { |
| 182 | Synchronized s(monitor); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 183 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 184 | try { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 185 | monitor.wait(100); |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 186 | } catch (TimedOutException&) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 187 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 188 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 189 | if (state == SynchStartTask::STARTED) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 190 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 191 | state = SynchStartTask::STOPPING; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 192 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 193 | monitor.notify(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 194 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 195 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 196 | while (state == SynchStartTask::STOPPING) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 197 | monitor.wait(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | assert(state == SynchStartTask::STOPPED); |
| 202 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 203 | bool success = true; |
| 204 | |
| 205 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "!" << std::endl; |
| 206 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 210 | /** |
| 211 | * The only guarantee a monitor timeout can give you is that |
| 212 | * it will take "at least" as long as the timeout, no less. |
| 213 | * There is absolutely no guarantee around regaining execution |
| 214 | * near the timeout. On a busy system (like inside a third party |
| 215 | * CI environment) it could take quite a bit longer than the |
| 216 | * requested timeout, and that's ok. |
| 217 | */ |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 218 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 219 | bool monitorTimeoutTest(int64_t count = 1000, int64_t timeout = 2) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 220 | |
| 221 | Monitor monitor; |
| 222 | |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 223 | int64_t startTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 224 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 225 | for (int64_t ix = 0; ix < count; ix++) { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 226 | { |
| 227 | Synchronized s(monitor); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 228 | try { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 229 | monitor.wait(timeout); |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 230 | } catch (TimedOutException&) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 231 | } |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 235 | int64_t endTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 236 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 237 | bool success = (endTime - startTime) >= (count * timeout); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 238 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 239 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 240 | << ": minimum required time to elapse " << count * timeout |
| 241 | << "ms; actual elapsed time " << endTime - startTime << "ms" |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 242 | << std::endl; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 243 | |
| 244 | return success; |
| 245 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 246 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 247 | class FloodTask : public Runnable { |
| 248 | public: |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 249 | FloodTask(const size_t id, Monitor& mon) : _id(id), _mon(mon) {} |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 250 | ~FloodTask() override { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 251 | if (_id % 10000 == 0) { |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 252 | Synchronized sync(_mon); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 253 | std::cout << "\t\tthread " << _id << " done" << std::endl; |
| 254 | } |
| 255 | } |
| 256 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 257 | void run() override { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 258 | if (_id % 10000 == 0) { |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 259 | Synchronized sync(_mon); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 260 | std::cout << "\t\tthread " << _id << " started" << std::endl; |
| 261 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 262 | } |
| 263 | const size_t _id; |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 264 | Monitor& _mon; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 267 | void foo(ThreadFactory* tf) { (void)tf; } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 268 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 269 | bool floodNTest(size_t loop = 1, size_t count = 100000) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 270 | |
| 271 | bool success = false; |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 272 | Monitor mon; |
| 273 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 274 | for (size_t lix = 0; lix < loop; lix++) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 275 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 276 | ThreadFactory threadFactory = ThreadFactory(); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 277 | threadFactory.setDetached(true); |
| 278 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 279 | for (size_t tix = 0; tix < count; tix++) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 280 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 281 | try { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 282 | |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 283 | shared_ptr<FloodTask> task(new FloodTask(lix * count + tix, mon)); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 284 | shared_ptr<Thread> thread = threadFactory.newThread(task); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 285 | thread->start(); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 286 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 287 | } catch (TException& e) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 288 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 289 | std::cout << "\t\t\tfailed to start " << lix* count + tix << " thread " << e.what() |
| 290 | << std::endl; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 291 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 292 | return success; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 293 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 294 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 295 | |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 296 | Synchronized sync(mon); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 297 | std::cout << "\t\t\tflooded " << (lix + 1) * count << " threads" << std::endl; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 298 | success = true; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | return success; |
| 302 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 303 | }; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 304 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | } // apache::thrift::concurrency::test |