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