coherence/lang/Thread.hpp

00001 /*
00002 * Thread.hpp
00003 *
00004 * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
00015 */
00016 #ifndef COH_THREAD_HPP
00017 #define COH_THREAD_HPP
00018 
00019 #include "coherence/lang/compatibility.hpp"
00020 
00021 #include "coherence/lang/FinalHandle.hpp"
00022 #include "coherence/lang/FinalView.hpp"
00023 #include "coherence/lang/Object.hpp"
00024 #include "coherence/lang/ObjectArray.hpp"
00025 #include "coherence/lang/MemberView.hpp"
00026 #include "coherence/lang/Runnable.hpp"
00027 #include "coherence/lang/String.hpp"
00028 #include "coherence/lang/TypedHandle.hpp"
00029 #include "coherence/lang/Volatile.hpp"
00030 
00031 COH_OPEN_NAMESPACE2(coherence,lang)
00032 
00033 
00034 /**
00035 * An Object representing a thread of execution.
00036 *
00037 * @author mf 2007.12.10
00038 */
00039 class COH_EXPORT Thread
00040     : public class_spec<Thread,
00041         extends<Object>,
00042         implements<Runnable> >
00043     {
00044     friend class factory<Thread>;
00045 
00046     // ----- State definition -----------------------------------------------
00047 
00048     public:
00049         typedef enum
00050             {
00051             NEW         = 0, // the thread has not yet started
00052             RUNNABLE    = 1, // the thread has started
00053             INTERRUPTED = 2, // the thread has been interrupted
00054             TERMINATED  = 3  // the thread has completed execution
00055             } State;
00056 
00057 
00058     // ----- constructors ---------------------------------------------------
00059 
00060     private:
00061         /**
00062         * Create a Thread.
00063         *
00064         * @param hRunnable    the Runnable to execute
00065         * @param vsName       the thread's name, or NULL for default
00066         * @param cBytesStack  the stack size or 0 for default
00067         *
00068         * @return the handle to the new Thread
00069         */
00070         Thread(Runnable::Handle hRunnable,
00071                 String::View vsName = String::NULL_STRING,
00072                 size32_t cBytesStack = 0);
00073 
00074 
00075     // ----- Runnable interface ---------------------------------------------
00076 
00077     public:
00078         /**
00079         * {@inheritDoc}
00080         */
00081         virtual void run();
00082 
00083 
00084     // ----- Thread interface -----------------------------------------------
00085 
00086     public:
00087         /**
00088         * Return the Thread's unique ID.
00089         *
00090         * No two live thread's will have the same ID and no thread will have
00091         * an id of 0.
00092         *
00093         * @return the Thread's unique ID.
00094         */
00095         virtual int64_t getId() const;
00096 
00097         /**
00098         * Return the Thread's current state.
00099         *
00100         * @return the Thread's current state
00101         */
00102         virtual State getState() const;
00103 
00104         /**
00105         * Return the Thread's name.
00106         *
00107         * @return the Thread's name
00108         */
00109         virtual String::View getName() const;
00110 
00111         /**
00112         * Begin executing the Thread's runnable on a new system thread.
00113         *
00114         * This method may only be called once per Thread instance.
00115         *
00116         * @throw IllegalStateException if a called on a Thread which is not
00117         *        in the state NEW.
00118         */
00119         virtual void start();
00120 
00121         /**
00122         * Request that the Thread stop executing.
00123         */
00124         virtual void interrupt();
00125 
00126         /**
00127         * Return true iff the thread is interrupted.
00128         *
00129         * @return true iff the thread is interrupted
00130         */
00131         virtual bool isInterrupted() const;
00132 
00133         /**
00134         * Wait for the Thread to reach the TERMINATED state.
00135         */
00136         virtual void join() const;
00137 
00138         /**
00139         * Wait for the Thread to reach the TERMINATED state.
00140         *
00141         * @param cMillis  the maximum duration to wait, or zero for infinite
00142         */
00143         virtual void join(int64_t cMillis) const;
00144 
00145         /**
00146         * Set the Thread's name.
00147         *
00148         * @param vsName  the Thread's name
00149         */
00150         virtual void setName(String::View vsName);
00151 
00152     protected:
00153         /**
00154         * Called automatically on the thread prior to termination.
00155         *
00156         * Any class extending this Thread and overriding this method much
00157         * call this implementation to ensure that join() operations complete.
00158         */
00159         virtual void onExit();
00160 
00161         /**
00162         * Instruct the thread to collect its stack.
00163         */
00164         virtual void collectStackTrace() const;
00165 
00166 
00167     // ----- static helper methods ------------------------------------------
00168 
00169     public:
00170         /**
00171         * Return the Thread on which the caller is executing.
00172         *
00173         * @return the caller's thread
00174         */
00175         static Thread::Handle currentThread();
00176 
00177         /**
00178         * Return an array of all known threads.
00179         *
00180         * @return an array of Thread views.
00181         */
00182         static ObjectArray enumerate();
00183 
00184         /**
00185         * Return a stack trace for this thread.
00186         *
00187         * @param cTrim  the number of frames to trim from the top of the stack
00188         *
00189         * @return an array of StackTraceElements describing as much of the
00190         *         thread's stack as can be determined
00191         */
00192         static ObjectArray::Handle getStackTrace(size32_t cTrim = 0);
00193 
00194         /**
00195         * Output a stack trace to the supplied stream.
00196         *
00197         * @param out      the stream to output to
00198         * @param vaStack  the array of StackTraceElements to output, or NULL
00199         *                 for the stack of the current thread
00200         */
00201         static void outputStackTrace(std::ostream& out,
00202                 ObjectArray::View vaStack = NULL);
00203 
00204         /**
00205         * Output the stack trace of all known threads to the specified stream.
00206         *
00207         * This is a blocking operation, while the calling thread waits for
00208         * all known threads to report their stack.
00209         *
00210         * Note: This operation can also be manually triggered by sending a
00211         * SIGQUIT signal to the process. This can be done via CTRL+/ on UNIX
00212         * and CTRL+BREAK on Windows.
00213         *
00214         * @param out             the stream to output to
00215         * @param cMillisTimeout  the maximum amount of time to wait for all
00216         *                        threads to report their stacks, or -1 for
00217         *                        the system default of twice the interrupt
00218         *                        resolution
00219         */
00220         static void dumpStacks(std::ostream& out, int64_t cMillisTimeout = -1);
00221 
00222         /**
00223         * Return true iff the caller's thread has been interrupted.
00224         *
00225         * The thread's interrupt state is reset as part of this operation.
00226         *
00227         * @return true iff the caller's thread has been interrupted
00228         */
00229         static bool interrupted();
00230 
00231         /**
00232         * Pause the current thread for the specified duration.
00233         *
00234         * @param cMillis  the duration to pause the thread for.
00235         *
00236         * @throw InterruptedException if the thread is interrupted while
00237         *        sleeping.
00238         */
00239         static void sleep(int64_t cMillis);
00240 
00241         /**
00242         * Temporarily pause the current thread.
00243         */
00244         static void yield();
00245 
00246 
00247     // ----- Object interface -----------------------------------------------
00248 
00249     public:
00250         /**
00251         * {@inheritDoc}
00252         *
00253         * If the associated Runnable implements the
00254         * coherence::util::Describable interface that content will be included
00255         * as well.
00256         */
00257         virtual void toStream(std::ostream &out) const;
00258 
00259 
00260     // ----- data members ---------------------------------------------------
00261 
00262     protected:
00263         /**
00264         * The inner runnable associated with this Thread.
00265         */
00266         MemberHandle<Runnable> m_hRunnable;
00267 
00268         /**
00269         * The thread's name.
00270         */
00271         MemberView<String> m_vsName;
00272 
00273         /**
00274         * The thread's description.
00275         */
00276         FinalView<String> m_vsDescription;
00277 
00278         /**
00279         * The configured stack size;
00280         */
00281         size32_t m_cBytesStack;
00282 
00283         /**
00284         * The current thread state.
00285         */
00286         Volatile<int32_t> m_nState;
00287 
00288         /**
00289         * True if a request has been made for a stack dump.
00290         */
00291         mutable bool m_fCollectStack;
00292 
00293 
00294     // ----- friends --------------------------------------------------------
00295 
00296     friend void    coh_thread_cleanup(Thread*);
00297     friend Thread* coh_thread_current();
00298     };
00299 
00300 COH_CLOSE_NAMESPACE2
00301 
00302 #endif // COH_THREAD_HPP
Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.