vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vThread.h
Go to the documentation of this file.
1 /***************************************************************************
2  vThread.h - description
3  -------------------
4  begin : September 2004
5  copyright : (C) 2004 by Jan Flasar
6  email : flasar@fi.muni.cz
7  ***************************************************************************/
8 
9 
10 #ifndef VTHREAD_H
11 #define VTHREAD_H
12 
13 #include <vrecko/BaseClass.h>
14 
15 #ifdef WIN32
16 #include <windows.h>
17 #else
18 #include <unistd.h>
19 #endif
20 
21 #include <map>
22 #include <vector>
23 #include <osg/Timer>
24 
25 
26 namespace vrecko {
27 
28 class BaseClass;
29 
30 class VRECKO_EXPORT vThread {
31  public:
32  vThread(BaseClass *ent_ptr, float freq);
33  vThread(std::vector<BaseClass*> ent_vec, float freq);
34  vThread(void (*funcPtr)(void), float freq);
35  vThread(void (*funcPtr)(void), unsigned int step, float freq);
36  ~vThread();
37 
38  void start(void) {run = true;}
39  void stop(void) {run = false;}
40 
41  bool getDone(void) {return done;}
42  bool getRun(void) {return run;}
43  float getFrequency(void) {return frequency;}
44  osg::Timer_t getLastTick(void) {return last_tick;}
45  BaseClass *getEntityPtr(int pos) {return (*entity_vector)[pos];}
46  bool addEntity(BaseClass *ent);
47  bool removeEntity(BaseClass *ent);
48  void syncEntityVector(void);
49  std::vector<BaseClass*> *getEntityVec(void) {return entity_vector;}
50  void (*getFuncPtr(void))() {return voidFuncPtr;}
51  unsigned int getStepCounter(void) {return step_counter;}
52 
53 #ifdef WIN32
54  static HANDLE createMutex();
55  static void destroyMutex(HANDLE mutex);
56  HANDLE getMutex(void) {return hvT_Mutex;}
57 #else
58  static pthread_mutex_t *createMutex();
59  static void destroyMutex(pthread_mutex_t *mutex);
60  pthread_mutex_t *getMutex(void) {return hvT_Mutex;}
61 #endif
62 
63  void reportError(const std::string &error);
64  inline void reportError(char* error) { reportError(std::string(error)); }
65 
66  static unsigned long getDelay(osg::Timer_t last_t, float freq);
67  inline unsigned long getDelay(void) { return getDelay(last_tick, frequency); }
68 
69  static const osg::Timer *getTimerPtr(void) {return timer;}
70  osg::Timer_t last_tick;
71 
72  protected:
73 #ifndef WIN32
74  pthread_t th_read;
75 #else
76  DWORD dwThreadId, dwThrdParam;
77  HANDLE hThread;
78 #endif
79  static const osg::Timer *timer;
80  std::vector<BaseClass*> *entity_vector;
81  std::map<BaseClass*, bool> temp_entity_map;
82  void (*voidFuncPtr)(void);
83  float frequency;
84  bool done, run;
85  unsigned int step_counter;
86 
87 #ifdef WIN32
88  HANDLE hvT_Mutex;
89 #else
90  pthread_mutex_t *hvT_Mutex;
91 #endif
92 };
93 
94 }
95 
96 #endif
97