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