Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

threadutils.h

Go to the documentation of this file.
00001 /********************************************************************
00002         Copyright (c) 2001, Lee Patterson & Ant Works Software
00003         http://ssobjects.sourceforge.net
00004 
00005         created  :  2/15/2000
00006         filename :  threadutils.h
00007         author   :  Lee Patterson (lee@antws.com)
00008         
00009         purpose  :  Wrapper class for threads
00010 
00011 *********************************************************************/
00012 
00013 /*
00014 impliments:
00015         ThreadHandler
00016         RWLock
00017         Lock
00018         AutoLock
00019         Event
00020 */
00021 
00022 #ifndef THREADUTILS_H
00023 #define THREADUTILS_H
00024 
00025 #include "mcl.h"
00026 #include "defs.h"
00027 
00028 namespace ssobjects
00029 {
00030 
00031 //TODO: need a way to wait for this thread to finish. using detach currently, so I can't join
00032 //TODO  to do synchronizing. Should be able to just call something like wait() and have a time-
00033 //TODO  out
00034 class ThreadHandler : public CMclThreadHandler
00035 {
00036   public:
00037     ThreadHandler(bool bStart=false);
00038     void start();
00039     void stop();
00040     void setRunning(bool bRunning) {m_bRun=bRunning;}
00041     bool running() {return m_bRun;}
00042     threadReturn ThreadHandlerProc(void)=0;
00043     virtual ~ThreadHandler();
00044 
00045   protected:
00046     bool m_bRun;
00047     CMclThread* m_pThread;
00048 
00049   protected:
00050     //unused overloads
00051     ThreadHandler(const ThreadHandler&);
00052     ThreadHandler& operator=(const ThreadHandler&);
00053 };
00054 
00055 class Lock : public CMclCritSec
00056 {
00057   public:
00058     void lock() {Enter();}
00059     void unlock() {Leave();}
00060 };
00061 
00062 class RWLock
00063 {
00064   protected:
00065     unsigned32 m_nReadersReading;
00066     unsigned32 m_nWriterWriting;
00067     pthread_mutex_t m_mutex;
00068     pthread_cond_t m_condLockFree;
00069 
00070   public:
00071     int  isLocked(){return m_nReadersReading||m_nWriterWriting;};
00072     int  isRLocked(){return m_nReadersReading;};
00073     int  isWLocked(){return m_nWriterWriting;};
00074     RWLock();
00075     ~RWLock();
00076 
00077     void rlock();
00078     void wlock();
00079     void wunlock();
00080     void runlock();
00081 };
00082 
00083 class AutoLock
00084 {
00085   public:
00086     AutoLock(Lock& lock);
00087     ~AutoLock();
00088   protected:
00089     Lock* m_pcLock;
00090   private:
00091     //unused overloads
00092     AutoLock(const AutoLock&);
00093     AutoLock& operator=(const AutoLock&);
00094 };
00095 
00096 class Event : public CMclEvent
00097 {
00098         public:
00099                 Event(bool bAutoSignal=true) : CMclEvent(bAutoSignal){};
00100     virtual ~Event(){return;};
00101                 bool signal() {return Set() ? true:false;}
00102                 bool reset() {return Reset() ? true:false;}
00103                 bool wait(unsigned int nMilliSeconds=INFINITE) {return Wait(nMilliSeconds)?true:false;}
00104 };
00105 
00106 };
00107 
00108 #endif //THREADUTILS_H

Generated on Thu Nov 8 12:39:26 2001 for SimpleServerObjects by doxygen1.2.11 written by Dimitri van Heesch, © 1997-2001