|
reSIProcate/rutil
9694
|
00001 #include <iostream> 00002 #include "rutil/Log.hxx" 00003 #include "rutil/SharedPtr.hxx" 00004 #include "rutil/Data.hxx" 00005 #include "rutil/ThreadIf.hxx" 00006 #include "rutil/Timer.hxx" 00007 #ifndef WIN32 00008 #include <unistd.h> 00009 #endif 00010 00011 using namespace resip; 00012 using namespace std; 00013 00014 void sleepMS(unsigned int ms) 00015 { 00016 #ifdef WIN32 00017 Sleep(ms); 00018 #else 00019 usleep(ms*1000); 00020 #endif 00021 } 00022 00023 class Foo 00024 { 00025 public: 00026 Foo(const Data& val) 00027 : mVal(val) 00028 { 00029 cerr << "Foo created." << endl; 00030 } 00031 00032 ~Foo() 00033 { 00034 cerr << "Foo destroyed." << endl; 00035 } 00036 00037 Data mVal; 00038 }; 00039 00040 class TestThread : public ThreadIf 00041 { 00042 public: 00043 TestThread(SharedPtr<Foo>&); 00044 virtual ~TestThread() 00045 { 00046 #ifdef VERBOSE 00047 cerr << "TestThread finishing..." << endl; 00048 #endif 00049 shutdown(); 00050 join(); 00051 #ifdef VERBOSE 00052 cerr << "TestThread finished" << endl; 00053 #endif 00054 }; 00055 00056 void thread(); 00057 00058 private: 00059 SharedPtr<Foo> mFoo; 00060 }; 00061 00062 TestThread::TestThread(SharedPtr<Foo>& f) : 00063 mFoo(f) 00064 {} 00065 00066 void TestThread::thread() 00067 { 00068 while (!mShutdown) 00069 { 00070 sleepMS(5); 00071 assert(mFoo->mVal == Data("data")); 00072 } 00073 mFoo.reset(); 00074 } 00075 00076 int 00077 main() 00078 { 00079 Log::initialize(Log::Cout, Log::Debug, Data::Empty); 00080 00081 { 00082 cerr << "!! Test counter logic" << endl; 00083 00084 SharedPtr<Foo> foo1(new Foo("data")); 00085 cerr << "foo1 mVal=" << foo1->mVal << " use count=" << foo1.use_count() << endl; 00086 assert(foo1.use_count() == 1); 00087 { 00088 SharedPtr<Foo> foo2(foo1); 00089 00090 cerr << "foo1 mVal=" << foo1->mVal << " use count=" << foo1.use_count() << endl; 00091 assert(foo1.use_count() == 2); 00092 cerr << "foo2 mVal=" << foo2->mVal << " use count=" << foo2.use_count() << endl; 00093 assert(foo2.use_count() == 2); 00094 } 00095 cerr << "foo1 mVal=" << foo1->mVal << " use count=" << foo1.use_count() << endl; 00096 assert(foo1.use_count() == 1); 00097 } 00098 00099 { 00100 cerr << "!! Test threads" << endl; 00101 00102 SharedPtr<Foo> foo(new Foo("data")); 00103 00104 cerr << "foo mVal=" << foo->mVal << " use count=" << foo.use_count() << endl; 00105 assert(foo.use_count() == 1); 00106 00107 TestThread thread1(foo); 00108 TestThread thread2(foo); 00109 TestThread thread3(foo); 00110 TestThread thread4(foo); 00111 TestThread thread5(foo); 00112 TestThread thread6(foo); 00113 TestThread thread7(foo); 00114 TestThread thread8(foo); 00115 TestThread thread9(foo); 00116 TestThread thread10(foo); 00117 00118 cerr << "foo mVal=" << foo->mVal << " use count=" << foo.use_count() << endl; 00119 assert(foo.use_count() == 11); 00120 00121 thread1.run(); 00122 thread2.run(); 00123 thread3.run(); 00124 thread4.run(); 00125 thread5.run(); 00126 thread6.run(); 00127 thread7.run(); 00128 thread8.run(); 00129 thread9.run(); 00130 thread10.run(); 00131 00132 sleepMS(500); 00133 00134 thread1.shutdown(); 00135 thread2.shutdown(); 00136 thread3.shutdown(); 00137 thread4.shutdown(); 00138 thread5.shutdown(); 00139 thread6.shutdown(); 00140 thread7.shutdown(); 00141 thread8.shutdown(); 00142 thread9.shutdown(); 00143 thread10.shutdown(); 00144 00145 thread1.join(); 00146 thread2.join(); 00147 thread3.join(); 00148 thread4.join(); 00149 thread5.join(); 00150 thread6.join(); 00151 thread7.join(); 00152 thread8.join(); 00153 thread9.join(); 00154 thread10.join(); 00155 00156 cerr << "foo mVal=" << foo->mVal << " use count=" << foo.use_count() << endl; 00157 assert(foo.use_count() == 1); 00158 } 00159 00160 cerr << "All OK" << endl; 00161 return 0; 00162 } 00163 00164 /* ==================================================================== 00165 * The Vovida Software License, Version 1.0 00166 * 00167 * Redistribution and use in source and binary forms, with or without 00168 * modification, are permitted provided that the following conditions 00169 * are met: 00170 * 00171 * 1. Redistributions of source code must retain the above copyright 00172 * notice, this list of conditions and the following disclaimer. 00173 * 00174 * 2. Redistributions in binary form must reproduce the above copyright 00175 * notice, this list of conditions and the following disclaimer in 00176 * the documentation and/or other materials provided with the 00177 * distribution. 00178 * 00179 * 3. The names "VOCAL", "Vovida Open Communication Application Library", 00180 * and "Vovida Open Communication Application Library (VOCAL)" must 00181 * not be used to endorse or promote products derived from this 00182 * software without prior written permission. For written 00183 * permission, please contact vocal@vovida.org. 00184 * 00185 * 4. Products derived from this software may not be called "VOCAL", nor 00186 * may "VOCAL" appear in their name, without prior written 00187 * permission of Vovida Networks, Inc. 00188 * 00189 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED 00190 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00191 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND 00192 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA 00193 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES 00194 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, 00195 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00196 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00197 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00198 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00199 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00200 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 00201 * DAMAGE. 00202 * 00203 * ==================================================================== 00204 * 00205 * This software consists of voluntary contributions made by Vovida 00206 * Networks, Inc. and many individuals on behalf of Vovida Networks, 00207 * Inc. For more information on Vovida Networks, Inc., please see 00208 * <http://www.vovida.org/>. 00209 * 00210 */
1.7.5.1