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 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 30 | namespace apache { namespace thrift { namespace concurrency { namespace test { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 31 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 32 | using boost::shared_ptr; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 33 | using namespace apache::thrift::concurrency; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 34 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 35 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 36 | * ThreadManagerTests class |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 37 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 38 | * @version $Id:$ |
| 39 | */ |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 40 | class ThreadFactoryTests { |
| 41 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 42 | public: |
| 43 | |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 44 | static const double TEST_TOLERANCE; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 45 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 46 | class Task: public Runnable { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 47 | |
| 48 | public: |
| 49 | |
| 50 | Task() {} |
| 51 | |
| 52 | void run() { |
| 53 | std::cout << "\t\t\tHello World" << std::endl; |
| 54 | } |
| 55 | }; |
| 56 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Hello world test |
| 59 | */ |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 60 | bool helloWorldTest() { |
| 61 | |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 62 | PlatformThreadFactory threadFactory = PlatformThreadFactory(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 63 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 64 | shared_ptr<Task> task = shared_ptr<Task>(new ThreadFactoryTests::Task()); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 65 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 66 | shared_ptr<Thread> thread = threadFactory.newThread(task); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 67 | |
| 68 | thread->start(); |
| 69 | |
| 70 | thread->join(); |
| 71 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 72 | std::cout << "\t\t\tSuccess!" << std::endl; |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 77 | /** |
| 78 | * Reap N threads |
| 79 | */ |
| 80 | class ReapNTask: public Runnable { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 81 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 82 | public: |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 83 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 84 | ReapNTask(Monitor& monitor, int& activeCount) : |
| 85 | _monitor(monitor), |
| 86 | _count(activeCount) {} |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 87 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 88 | void run() { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 89 | Synchronized s(_monitor); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 90 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 91 | _count--; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 92 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 93 | //std::cout << "\t\t\tthread count: " << _count << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 94 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 95 | if (_count == 0) { |
| 96 | _monitor.notify(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | Monitor& _monitor; |
| 101 | |
| 102 | int& _count; |
| 103 | }; |
| 104 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 105 | bool reapNThreads(int loop=1, int count=10) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 106 | |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 107 | PlatformThreadFactory threadFactory = PlatformThreadFactory(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 108 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 109 | Monitor* monitor = new Monitor(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 110 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 111 | for(int lix = 0; lix < loop; lix++) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 112 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 113 | int* activeCount = new int(count); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 114 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 115 | std::set<shared_ptr<Thread> > threads; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 116 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 117 | int tix; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 118 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 119 | for (tix = 0; tix < count; tix++) { |
| 120 | try { |
| 121 | threads.insert(threadFactory.newThread(shared_ptr<Runnable>(new ReapNTask(*monitor, *activeCount)))); |
| 122 | } catch(SystemResourceException& e) { |
| 123 | std::cout << "\t\t\tfailed to create " << lix * count + tix << " thread " << e.what() << std::endl; |
| 124 | throw e; |
| 125 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 126 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 127 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 128 | tix = 0; |
| 129 | for (std::set<shared_ptr<Thread> >::const_iterator thread = threads.begin(); thread != threads.end(); tix++, ++thread) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 130 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 131 | try { |
| 132 | (*thread)->start(); |
| 133 | } catch(SystemResourceException& e) { |
| 134 | std::cout << "\t\t\tfailed to start " << lix * count + tix << " thread " << e.what() << std::endl; |
| 135 | throw e; |
| 136 | } |
| 137 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 138 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 139 | { |
| 140 | Synchronized s(*monitor); |
| 141 | while (*activeCount > 0) { |
| 142 | monitor->wait(1000); |
| 143 | } |
| 144 | } |
Roger Meier | 3075ffc | 2011-08-04 22:36:07 +0000 | [diff] [blame] | 145 | delete activeCount; |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 146 | std::cout << "\t\t\treaped " << lix * count << " threads" << std::endl; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | std::cout << "\t\t\tSuccess!" << std::endl; |
| 150 | |
| 151 | return true; |
| 152 | } |
| 153 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 154 | class SynchStartTask: public Runnable { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 155 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 156 | public: |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 157 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 158 | enum STATE { |
Marc Slemko | 03dedd9 | 2006-07-20 00:58:47 +0000 | [diff] [blame] | 159 | UNINITIALIZED, |
| 160 | STARTING, |
| 161 | STARTED, |
| 162 | STOPPING, |
| 163 | STOPPED |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 166 | SynchStartTask(Monitor& monitor, volatile STATE& state) : |
| 167 | _monitor(monitor), |
| 168 | _state(state) {} |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 169 | |
| 170 | void run() { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 171 | { |
| 172 | Synchronized s(_monitor); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 173 | if (_state == SynchStartTask::STARTING) { |
| 174 | _state = SynchStartTask::STARTED; |
| 175 | _monitor.notify(); |
| 176 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 179 | { |
| 180 | Synchronized s(_monitor); |
| 181 | while (_state == SynchStartTask::STARTED) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 182 | _monitor.wait(); |
| 183 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 184 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 185 | if (_state == SynchStartTask::STOPPING) { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 186 | _state = SynchStartTask::STOPPED; |
| 187 | _monitor.notifyAll(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 188 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 192 | private: |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 193 | Monitor& _monitor; |
| 194 | volatile STATE& _state; |
| 195 | }; |
| 196 | |
| 197 | bool synchStartTest() { |
| 198 | |
| 199 | Monitor monitor; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 200 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 201 | SynchStartTask::STATE state = SynchStartTask::UNINITIALIZED; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 202 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 203 | shared_ptr<SynchStartTask> task = shared_ptr<SynchStartTask>(new SynchStartTask(monitor, state)); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 204 | |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 205 | PlatformThreadFactory threadFactory = PlatformThreadFactory(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 206 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 207 | shared_ptr<Thread> thread = threadFactory.newThread(task); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 208 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 209 | if (state == SynchStartTask::UNINITIALIZED) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 210 | |
| 211 | state = SynchStartTask::STARTING; |
| 212 | |
| 213 | thread->start(); |
| 214 | } |
| 215 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 216 | { |
| 217 | Synchronized s(monitor); |
| 218 | while (state == SynchStartTask::STARTING) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 219 | monitor.wait(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | assert(state != SynchStartTask::STARTING); |
| 224 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 225 | { |
| 226 | Synchronized s(monitor); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 227 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 228 | try { |
| 229 | monitor.wait(100); |
| 230 | } catch(TimedOutException& e) { |
| 231 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 232 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 233 | if (state == SynchStartTask::STARTED) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 234 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 235 | state = SynchStartTask::STOPPING; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 236 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 237 | monitor.notify(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 238 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 239 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 240 | while (state == SynchStartTask::STOPPING) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 241 | monitor.wait(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | assert(state == SynchStartTask::STOPPED); |
| 246 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 247 | bool success = true; |
| 248 | |
| 249 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "!" << std::endl; |
| 250 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 254 | /** See how accurate monitor timeout is. */ |
| 255 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 256 | bool monitorTimeoutTest(size_t count=1000, int64_t timeout=10) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 257 | |
| 258 | Monitor monitor; |
| 259 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 260 | int64_t startTime = Util::currentTime(); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 261 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 262 | for (size_t ix = 0; ix < count; ix++) { |
| 263 | { |
| 264 | Synchronized s(monitor); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 265 | try { |
| 266 | monitor.wait(timeout); |
| 267 | } catch(TimedOutException& e) { |
| 268 | } |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 272 | int64_t endTime = Util::currentTime(); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 273 | |
| 274 | double error = ((endTime - startTime) - (count * timeout)) / (double)(count * timeout); |
| 275 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 276 | if (error < 0.0) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 277 | |
| 278 | error *= 1.0; |
| 279 | } |
| 280 | |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 281 | bool success = error < ThreadFactoryTests::TEST_TOLERANCE; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 282 | |
| 283 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "! expected time: " << count * timeout << "ms elapsed time: "<< endTime - startTime << "ms error%: " << error * 100.0 << std::endl; |
| 284 | |
| 285 | return success; |
| 286 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 287 | |
| 288 | |
| 289 | class FloodTask : public Runnable { |
| 290 | public: |
| 291 | |
| 292 | FloodTask(const size_t id) :_id(id) {} |
| 293 | ~FloodTask(){ |
| 294 | if(_id % 1000 == 0) { |
| 295 | std::cout << "\t\tthread " << _id << " done" << std::endl; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void run(){ |
| 300 | if(_id % 1000 == 0) { |
| 301 | std::cout << "\t\tthread " << _id << " started" << std::endl; |
| 302 | } |
| 303 | |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 304 | THRIFT_SLEEP_USEC(1); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 305 | } |
| 306 | const size_t _id; |
| 307 | }; |
| 308 | |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 309 | void foo(PlatformThreadFactory *tf) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 310 | (void) tf; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | bool floodNTest(size_t loop=1, size_t count=100000) { |
| 314 | |
| 315 | bool success = false; |
| 316 | |
| 317 | for(size_t lix = 0; lix < loop; lix++) { |
| 318 | |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 319 | PlatformThreadFactory threadFactory = PlatformThreadFactory(); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 320 | threadFactory.setDetached(true); |
| 321 | |
| 322 | for(size_t tix = 0; tix < count; tix++) { |
| 323 | |
| 324 | try { |
| 325 | |
| 326 | shared_ptr<FloodTask> task(new FloodTask(lix * count + tix )); |
| 327 | |
| 328 | shared_ptr<Thread> thread = threadFactory.newThread(task); |
| 329 | |
| 330 | thread->start(); |
| 331 | |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 332 | THRIFT_SLEEP_USEC(1); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 333 | |
| 334 | } catch (TException& e) { |
| 335 | |
| 336 | std::cout << "\t\t\tfailed to start " << lix * count + tix << " thread " << e.what() << std::endl; |
| 337 | |
| 338 | return success; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | std::cout << "\t\t\tflooded " << (lix + 1) * count << " threads" << std::endl; |
| 343 | |
| 344 | success = true; |
| 345 | } |
| 346 | |
| 347 | return success; |
| 348 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 349 | }; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 350 | |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 351 | const double ThreadFactoryTests::TEST_TOLERANCE = .20; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 352 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 353 | }}}} // apache::thrift::concurrency::test |