|
reSIProcate/rutil
9694
|
00001 #include "rutil/ThreadIf.hxx" 00002 #include "rutil/Timer.hxx" 00003 00004 #ifndef WIN32 00005 #include <unistd.h> 00006 #endif 00007 #include <iostream> 00008 #include <vector> 00009 #include <cassert> 00010 00011 using namespace resip; 00012 using namespace std; 00013 00014 #ifdef WIN32 00015 #define usleep(x) Sleep(x/1000) 00016 #define sleep(x) Sleep(x) 00017 #endif 00018 00019 class Every4 : public ThreadIf 00020 { 00021 public: 00022 void thread() 00023 { 00024 while(!waitForShutdown(4000)) 00025 { 00026 cerr << Timer::getTimeMs() << endl; 00027 } 00028 cerr << "Stopped at: " << Timer::getTimeMs() << endl; 00029 } 00030 }; 00031 00032 class Old2 : public ThreadIf 00033 { 00034 public: 00035 void thread() 00036 { 00037 while(!isShutdown()) 00038 { 00039 sleep(2); 00040 cerr << Timer::getTimeMs() << endl; 00041 } 00042 cerr << "Stopped at: " << Timer::getTimeMs() << endl; 00043 } 00044 }; 00045 00046 class ShutdownSelf : public ThreadIf 00047 { 00048 public: 00049 void thread() 00050 { 00051 while(!waitForShutdown(4000)) 00052 { 00053 } 00054 } 00055 }; 00056 00057 class DerivedShutsDown : public ThreadIf 00058 { 00059 public: 00060 DerivedShutsDown() {} 00061 ~DerivedShutsDown() 00062 { 00063 shutdown(); 00064 join(); 00065 } 00066 00067 void thread() 00068 { 00069 while(!isShutdown()) 00070 { 00071 } 00072 } 00073 }; 00074 00075 00076 00077 int main() 00078 { 00079 { 00080 DerivedShutsDown d; 00081 } 00082 00083 { 00084 vector<DerivedShutsDown*> threads; 00085 int numThreads = 20; 00086 00087 for (int i=0; i < numThreads; i++) 00088 { 00089 threads.push_back(new DerivedShutsDown); 00090 threads.back()->run(); 00091 } 00092 sleep(1); 00093 for (int i=0; i < numThreads; i++) 00094 { 00095 delete threads[i]; 00096 } 00097 cerr << "finished many-thread test" << endl; 00098 } 00099 00100 { 00101 DerivedShutsDown d; 00102 d.run(); 00103 usleep(10000); 00104 } 00105 { 00106 ShutdownSelf* s= new ShutdownSelf(); 00107 s->run(); 00108 usleep(10000); 00109 s->shutdown(); 00110 delete s; 00111 } 00112 { 00113 Every4 e; 00114 e.run(); 00115 sleep(5); 00116 UInt64 t = Timer::getTimeMs(); 00117 cerr << "Stopping thread at: " << Timer::getTimeMs() << endl; 00118 e.shutdown(); 00119 e.join(); 00120 UInt64 j = Timer::getTimeMs(); 00121 cerr << j - t << endl; 00122 00123 assert(j - t < 20); 00124 } 00125 //this should not cause problems(signal is being called on a condition with 00126 //no waiting threads). 00127 { 00128 Old2 o; 00129 o.run(); 00130 sleep(3); 00131 o.shutdown(); 00132 o.join(); 00133 } 00134 cerr << "OK" << endl; 00135 return 0; 00136 } 00137 /* ==================================================================== 00138 * The Vovida Software License, Version 1.0 00139 * 00140 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. 00141 * 00142 * Redistribution and use in source and binary forms, with or without 00143 * modification, are permitted provided that the following conditions 00144 * are met: 00145 * 00146 * 1. Redistributions of source code must retain the above copyright 00147 * notice, this list of conditions and the following disclaimer. 00148 * 00149 * 2. Redistributions in binary form must reproduce the above copyright 00150 * notice, this list of conditions and the following disclaimer in 00151 * the documentation and/or other materials provided with the 00152 * distribution. 00153 * 00154 * 3. The names "VOCAL", "Vovida Open Communication Application Library", 00155 * and "Vovida Open Communication Application Library (VOCAL)" must 00156 * not be used to endorse or promote products derived from this 00157 * software without prior written permission. For written 00158 * permission, please contact vocal@vovida.org. 00159 * 00160 * 4. Products derived from this software may not be called "VOCAL", nor 00161 * may "VOCAL" appear in their name, without prior written 00162 * permission of Vovida Networks, Inc. 00163 * 00164 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED 00165 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00166 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND 00167 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA 00168 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES 00169 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, 00170 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00171 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00172 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00173 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00174 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00175 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 00176 * DAMAGE. 00177 * 00178 * ==================================================================== 00179 * 00180 * This software consists of voluntary contributions made by Vovida 00181 * Networks, Inc. and many individuals on behalf of Vovida Networks, 00182 * Inc. For more information on Vovida Networks, Inc., please see 00183 * <http://www.vovida.org/>. 00184 * 00185 */
1.7.5.1