Home C-Bits Package Documentation Project Page

Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Thread.h

Go to the documentation of this file.
00001 /* ====================================================================
00002  *              The CBITS Software License, Version 1.0
00003  *
00004  *               Copyright (c) 2002-2003 Bruce Lowery
00005  *                       All rights reserved
00006  *
00007  * Redistribution and use of this software, in source and binary forms, 
00008  * with or without modification, are permitted provided that the above 
00009  * copyright notice, this paragraph, and the following paragraph are 
00010  * retained in each source code file.
00011  *
00012  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00013  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00014  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00015  * DISCLAIMED.  IN NO EVENT SHALL BRUCE LOWERY OR OTHER CONTRIBUTORS 
00016  * TO THE CBITS LIBRARY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00017  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00018  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00019  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00020  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00021  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00022  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00023  * SUCH DAMAGE.
00024  * ====================================================================
00025  */
00026 #ifndef __THREAD_H__
00027 #define __THREAD_H__
00028 
00029 #include <cbits/Runnable.h>
00030 #include <string>
00031 
00032 
00033 #ifdef BUILD_DLL
00034 #define EXPORT __declspec(dllexport)
00035 #else
00036 #define EXPORT
00037 #endif
00038 
00039 
00066 namespace cbits 
00067 {
00068 
00126 class EXPORT Thread
00127 {
00128 
00129 public:
00130 
00132     enum State
00133     {
00134         READY = 1,
00136         ALIVE = 2,
00138         DEAD  = 3
00140     };
00141 
00142 
00144     enum Priority
00145     {
00146         PRIORITY_1       = 1,
00147         PRIORITY_2       = 2,
00148         PRIORITY_3       = 3,
00149         PRIORITY_4       = 4,
00150         PRIORITY_5       = 5,
00151         PRIORITY_6       = 6,
00152         PRIORITY_7       = 7,
00153         PRIORITY_8       = 8,
00154         PRIORITY_9       = 9,
00155         MIN_PRIORITY     = PRIORITY_1,
00156         DEFAULT_PRIORITY = PRIORITY_5,
00157         MAX_PRIORITY     = PRIORITY_9
00158     };
00159 
00160 
00167     static Thread* create
00168     ( 
00169         Runnable* r,
00171         const Priority p = DEFAULT_PRIORITY 
00173     );
00174 
00175 
00182     static Thread* getCurrent();
00183 
00184 
00191     static void    yield();
00192 
00193 
00203     void start();
00204 
00205 
00212     const long getThreadID() const;
00213 
00214 
00223     void interrupt();
00224 
00225 
00236     static const bool isInterrupted();
00237 
00238 
00244     const bool join
00245     (
00251         const long msec=0
00252     );
00253 
00254 
00258     const State getState
00259     (
00260         const bool lockit = true
00261             /*
00262              * true if object should be locked 
00263              * while reading state.
00264              */
00265      ) const;
00266 
00267 
00271     void  setData
00272     ( 
00276         void* 
00277     ) const;
00278 
00279 
00286     void* getData() const;
00287 
00288 
00293     static void sleep
00294     ( 
00300         const long msec 
00301     );
00302 
00306     virtual ~Thread();
00307 
00308 
00309 protected:
00310 
00315     Thread
00316     ( 
00318         Priority p = DEFAULT_PRIORITY 
00319     );
00320 
00321 
00326     Thread
00327     ( 
00329         Runnable* r, 
00330 
00332         Priority p = DEFAULT_PRIORITY 
00333     );
00334 
00335 
00342     Runnable* getTarget() const { return _target; }
00343 
00344 
00354     const State setState
00355     ( 
00357         State, 
00358 
00363         const bool lockit = true
00364     );
00365 
00366 
00374     void setError
00375     (
00377         const char* e
00378     ) const 
00379     { 
00380         _error = e; 
00381     }
00382 
00383 
00395     const std::string& getError() const 
00396     { 
00397         return _error;
00398     }
00399 
00400 
00411     const Priority getPriority() const 
00412     { 
00413         return _priority; 
00414     }
00415 
00416 
00428     virtual const bool impl_create_thread
00429     ( 
00430         const Priority 
00431     )  = 0;
00432 
00433 
00446     virtual const bool impl_start_thread() = 0;
00447 
00448 
00458     virtual const long impl_get_threadid() const = 0;
00459 
00460 
00473     virtual void impl_lock_obj() const = 0;
00474 
00475 
00489     virtual void impl_unlock_obj() const = 0;
00490 
00491 
00501     virtual void impl_interrupt() = 0;
00502 
00503 
00517     virtual const bool impl_join( const long msec ) = 0;
00518 
00519 
00527     virtual void impl_sleep( const long msec ) = 0;
00528 
00529 
00530 private:
00531 
00533     mutable std::string  _error;
00534 
00536     Runnable* _target;
00537 
00539     Priority  _priority;
00540 
00542     State _state;
00543 
00545     mutable void*  _data;
00546 
00548     mutable bool _interrupted;
00549 };
00550 
00551 };
00552 
00553 #endif
00554 
00555 /*
00556  * $Id: Thread.h,v 1.10 2002/10/09 22:43:03 brulow Exp $
00557  *
00558  * History: (Add nothing manually below)
00559  * -----------------------------------------------------------------------
00560  *
00561  * $Log: Thread.h,v $
00562  * Revision 1.10  2002/10/09 22:43:03  brulow
00563  * Improve documentation
00564  *
00565  * Revision 1.9  2002/10/09 02:02:26  brulow
00566  * Improve package documentation
00567  *
00568  * Revision 1.8  2002/10/07 21:07:44  brulow
00569  * Add/improve documentation
00570  *
00571  * Revision 1.7  2002/10/06 04:13:01  brulow
00572  * Add unit thread pkg unit tests and fix thread pkg bugs.
00573  *
00574  * Revision 1.6  2002/10/05 15:48:50  brulow
00575  * Add 'sleep' to Thread interface;
00576  * Add synchronization between creator and new thread.
00577  *
00578  * Revision 1.5  2002/10/05 03:38:37  brulow
00579  * Fix bug in Thread package and unit test
00580  *
00581  * Revision 1.4  2002/10/04 23:51:14  brulow
00582  * Fix bug in property package unit test 7
00583  *
00584  * Revision 1.3  2002/10/04 03:02:17  brulow
00585  * Fix 'undefined virtual table' problem in ResourceLock_XX classes
00586  *
00587  * Revision 1.2  2002/10/03 15:09:23  brulow
00588  * Add footer
00589  *
00590  *
00591  */
00592 

Generated by
doxygen
Hosted by
SourceForge