CLAM-Development  1.4.0
Thread.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef __THREAD__
23 #define __THREAD__
24 
25 #include <pthread.h>
26 #include "CBL.hxx"
27 #include "Condition.hxx"
28 
29 // there is a Yield method defined in a Windows API which is included by windows.h
30 // this undefines the Windows Yield method
31 #ifdef Yield
32 #undef Yield
33 #endif
34 
35 namespace CLAM
36 {
37  class Condition;
38 
39 class Thread
40 {
41 public:
42 
43  Thread(bool realtime = false);
44 
45  virtual ~Thread();
46 
47  virtual void Start();
48 
49  virtual void Stop();
50 
51  void Sleep();
52 
53  void Sleep( unsigned int milliseconds );
54 
55  void WakeUp();
56 
57  void Yield();
58 
59  void SetThreadCode( const CBL::Functor0& thread_code );
60 
61  void SetCleanupCode( const CBL::Functor0& cleanup_code );
62 
63  bool operator==( const Thread& other ) const;
64 
65  inline bool IsCancelled() const
66  {
67  return mIsCancelled;
68  }
69 
70  inline bool IsRunning() const
71  {
72  return mRunning;
73  }
74 
75 protected:
76  virtual void SetupPriorityPolicy();
77 
78  bool mRealtime;
79  bool mHasCode;
81  pthread_t mThreadID;
83  bool mRunning;
84  CBL::Functor0 mThreadCode;
85  CBL::Functor0 mCleanUpCode;
87 
88 private:
89 
90  void SleepDetail( unsigned int millisecs );
91 
92  // Pthreads needed static functions for launching and cleaning
93 
94  static void* LaunchThread( void* pSelf );
95 
96  static void LaunchThreadCleanup( void* pSelf );
97 };
98 
99 }
100 
101 #endif // Thread.hxx
102