• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/threading/Thread.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef JTHREAD_H
00018 #define JTHREAD_H
00019 
00020 #include <spl/types.h>
00021 #include <spl/Debug.h>
00022 
00023 #ifdef _WINDOWS
00024 #include <spl/configwin32.h>
00025 #else
00026 #include <spl/autoconf/config.h>
00027 #endif
00028 
00029 #include <spl/DateTime.h>
00030 #include <spl/RefCountPtr.h>
00031 
00032 #ifdef _WINDOWS
00033 #define _WINSOCKAPI_
00034 #include <spl/cleanwindows.h>
00035 #endif
00036 #if defined(HAVE_SPTHREAD_H) || defined(_TANDEM)
00037 #include <sys/types.h>
00038 #include <spt_types.h>
00039 #include <sys/time.h>
00040 #include <wchar.h>
00041 #include <signal.h>
00042 #define _tal
00043 #define _extensible
00044 #include <spthread.h>
00045 #include <spt_extensions.h>
00046 /* also need LDFLAGS="-spthread" */
00047 #endif
00048 #if defined(HAVE_PTHREAD_H) && !defined(_TANDEM)
00049 #include <pthread.h>
00050 #endif
00051 
00052 #include <spl/Debug.h>
00053 #include <spl/Memory.h>
00054 #include <spl/threading/Mutex.h>
00055 
00067 #define ERR_JTHREAD_CANTINITMUTEX                                               -1
00068 #define ERR_JTHREAD_CANTSTARTTHREAD                                             -2
00069 #define ERR_JTHREAD_THREADFUNCNOTSET                                            -3
00070 #define ERR_JTHREAD_NOTRUNNING                                                  -4
00071 #define ERR_JTHREAD_ALREADYRUNNING                                              -5
00072 
00073 class Thread;
00074 typedef RefCountPtr<Thread> ThreadPtr;
00075 typedef WeakReference<Thread, ThreadPtr> ThreadRef;
00076 
00077 REGISTER_TYPEOF( 478, ThreadPtr );
00078 REGISTER_TYPEOF( 480, ThreadRef );
00079 
00084 class Thread : public IMemoryValidate
00085 {
00086 private:
00087         // forbid copy constructor
00088         inline Thread(const Thread& t) {}
00089         inline void operator =(const Thread& t) {}
00090 
00091 private:
00092 
00093 #ifdef _WINDOWS
00094         static DWORD WINAPI TheThread(void *param);
00095         Thread(HANDLE threadhandle);
00096         
00097         HANDLE m_threadhandle;
00098         DWORD m_threadid;
00099 #else 
00100         // pthread type threads
00101         friend void *TheThread(void *param);
00102         Thread(pthread_t thread);
00103 
00104         pthread_t m_threadid;
00105 #endif
00106 
00107         volatile bool m_running;
00108         volatile bool m_hasrun;
00109 
00110         Mutex m_joinmutex;
00111 
00112 public:
00113         enum ThreadPriority
00114         {
00115                 PRIORITY_NORMAL = 0,
00116                 PRIORITY_HI = 1,
00117                 PRIORITY_LOW = 2
00118         };
00119 
00120         Thread();
00121         virtual ~Thread();
00122 
00123         void Start();
00124         void Kill();
00125         bool IsRunning() const;
00126         void Join();
00127         bool Join( int timeoutMs );
00128         void SetPriority( enum ThreadPriority prilv );
00129 
00130         virtual void Run() = 0;
00131         
00132         static void YYield();
00133         static void Sleep( long ms );
00134         
00135 #if defined(DEBUG)
00136         void CheckMem() const;
00137         void ValidateMem() const;
00138 #endif
00139 };
00140 
00141 REGISTER_TYPEOF( 482, Thread );
00142 
00146 #endif