blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Vintela, Inc., 00015 * nor Novell, Inc., 00016 * nor the names of its contributors or employees may be used to 00017 * endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 00031 *******************************************************************************/ 00032 00033 00039 #ifndef BLOCXX_THREAD_HPP_INCLUDE_GUARD_ 00040 #define BLOCXX_THREAD_HPP_INCLUDE_GUARD_ 00041 #include "blocxx/BLOCXX_config.h" 00042 #include "blocxx/Exception.hpp" 00043 #include "blocxx/String.hpp" 00044 #include "blocxx/ThreadImpl.hpp" 00045 #include "blocxx/IntrusiveReference.hpp" 00046 #include "blocxx/IntrusiveCountableBase.hpp" 00047 #include "blocxx/Assertion.hpp" 00048 #include "blocxx/Condition.hpp" 00049 #include "blocxx/NonRecursiveMutex.hpp" 00050 #include "blocxx/ThreadDoneCallback.hpp" 00051 #include "blocxx/ThreadCancelledException.hpp" 00052 #include "blocxx/Timeout.hpp" 00053 #include "blocxx/AtomicOps.hpp" 00054 00055 namespace BLOCXX_NAMESPACE 00056 { 00057 00059 BLOCXX_DECLARE_APIEXCEPTION(CancellationDenied, BLOCXX_COMMON_API); 00060 BLOCXX_DECLARE_APIEXCEPTION(Thread, BLOCXX_COMMON_API); 00062 00066 class BLOCXX_COMMON_API Thread : public IntrusiveCountableBase 00067 { 00068 public: 00073 Thread(); 00079 virtual ~Thread(); 00088 virtual void start(const ThreadDoneCallbackRef& cb = ThreadDoneCallbackRef(0)); 00089 00095 void shutdown(); 00096 00104 bool shutdown(const Timeout& timeout); 00105 00133 void cooperativeCancel(); 00134 00173 bool definitiveCancel(const Timeout& timeout = Timeout::relative(60)); 00174 bool definitiveCancel(UInt32 waitForCooperativeSecs) BLOCXX_DEPRECATED; // in 4.0.0 00196 void cancel(); 00226 static void testCancel(); 00227 private: 00228 00229 void cancel_internal(bool is_locked); 00230 00240 virtual void doShutdown(); 00241 00256 virtual void doCooperativeCancel(); 00257 00265 virtual void doDefinitiveCancel(); 00266 public: 00270 bool isRunning() 00271 { 00272 return m_isRunning == true; 00273 } 00282 bool timedWait(const Timeout& timeout); 00295 Int32 join(); 00306 Thread_t getId() 00307 { 00308 return m_id; 00309 } 00317 static void sleep(UInt32 milliSeconds) 00318 { 00319 ThreadImpl::sleep(milliSeconds); 00320 } 00328 static void sleep(const Timeout& timeout) 00329 { 00330 ThreadImpl::sleep(timeout); 00331 } 00338 static void yield() 00339 { 00340 ThreadImpl::yield(); 00341 } 00342 private: 00346 virtual Int32 run() = 0; 00347 // thread state 00348 Thread_t m_id; 00349 bool m_isRunning; 00350 bool m_joined; 00351 // used to implement cancellation. 00352 friend void ThreadImpl::testCancel(); 00353 Atomic_t m_cancelRequested; 00354 bool m_cancelled; 00355 00356 NonRecursiveMutex m_stateGuard; 00357 Condition m_stateCond; 00358 00359 static Int32 threadRunner(void* paramPtr); 00360 void doneRunning(const ThreadDoneCallbackRef& cb); 00361 00362 // non-copyable 00363 Thread(const Thread&); 00364 Thread& operator=(const Thread&); 00365 00366 }; 00367 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, IntrusiveReference, Thread); 00368 00369 } // end namespace BLOCXX_NAMESPACE 00370 00371 #endif 00372