Structural deformable models
vuThread.cpp
Go to the documentation of this file.
1 #include "vuThread.h"
2 #include <stdlib.h>
3 #include <iostream>
4 #include <errno.h>
5 #include <string.h>
6 #include <fstream>
7 
8 using namespace std;
9 
11 
12 vuMutex& getLogMut() {return logmut;}
13 
14 #ifndef WIN32
15 
16 #include <sys/time.h>
17 #include <pthread.h>
18 #include <unistd.h>
19 
20 void* _kickoff(void* ptr)
21 {
22  vuThread *th = (vuThread*)ptr;
23  int whatsup = th->m_Whatsup;
24  void* data = th->m_AdditionalData;
25  th->m_WhatsupMutex.unlock();
26 
27  th->run(whatsup, data);
28 
29  th->stopThread();
30  return NULL;
31 }
32 
33 /*
34  void* _ret_kickoff(void *ptr)
35  {
36  vuThread *th = (vuThread*)ptr;
37  int whatsup = th->m_Whatsup;
38  th->m_WhatsupMutex.unlock();
39 
40  return th->retrun(whatsup);
41 
42 // return NULL;
43 }*/
44 
45 bool vuThread::startThread(int whatsup, void* data)
46 
47 {
48  pthread_t *thread = new pthread_t;
49 
50  m_WhatsupMutex.lock();
51  m_Whatsup = whatsup;
52  m_AdditionalData = data;
53 
54  pthread_attr_t tattr;
55 
56 // cout << "here" << endl;
57 
58  pthread_attr_setdetachstate (&tattr, PTHREAD_CREATE_DETACHED);
59 
60 // cout << "s1" << endl;
61 
62  int t = pthread_create(thread, NULL, &_kickoff, (void*) this);
63 
64  if (t != 0)
65 
66  {
67 // cout << "s2.5" << endl;
68  cout << strerror (errno) << endl;
69  if (t == EAGAIN)
70  cout << "eagain" << endl;
71 
72  return false;
73  }
74 
75  pthread_detach (*thread);
76 
77 // cout << "s3" << endl;
78 
79  return true;
80 }
81 
82 /*bool vuThread::retStartThread(int whatsup)
83 
84  {
85  pthread_t *thread = new pthread_t [1];
86 
87  m_WhatsupMutex.lock();
88  m_Whatsup = whatsup;
89 
90  int t = pthread_create(thread, NULL, &_ret_kickoff, (void*) this);
91 
92  if (t != 0)
93 
94  {
95  cout << strerror (errno) << endl;
96  if (t == EAGAIN)
97  cout << "eagain" << endl;
98 
99  return false;
100  }
101 
102  pthread_detach (*thread);
103 
104  return true;
105  }*/
106 
108 {
109  pthread_exit(NULL);
110 }
111 
112 void vuThread::usleep(unsigned long ms)
113 {
114  ::usleep(ms);
115 }
116 
117 vuMutex::vuMutex(bool recursive)
118 {
119  mutex = (void*) new pthread_mutex_t;
120  if(recursive) {
121  pthread_mutexattr_t attr;
122  pthread_mutexattr_init(&attr);
123  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
124  // other: PTHREAD_MUTEX_FAST_NP (default), PTHREAD_MUTEX_ERRORCHECK_NP
125  pthread_mutex_init((pthread_mutex_t*)mutex, &attr);
126  pthread_mutexattr_destroy(&attr);
127  } else {
128  pthread_mutex_init((pthread_mutex_t*)mutex, NULL);
129  }
130 }
131 
133 {
134  pthread_mutex_destroy((pthread_mutex_t*) mutex);
135  delete (pthread_mutex_t*)mutex;
136 }
137 
139 {
140  pthread_mutex_lock( (pthread_mutex_t*) mutex );
141 }
142 
144 {
145  return pthread_mutex_trylock( (pthread_mutex_t*) mutex ) != EBUSY;
146 }
147 
149 {
150  pthread_mutex_unlock( (pthread_mutex_t*) mutex );
151 }
152 
153 #else // WIN32 defined
154 
155 #include <windows.h>
156 #include <process.h>
157 #include <sys/time.h>
158 
159 vuMutex::vuMutex(bool recursive)
160 {
161  // one of these should be usable as recursive mutex, but right now,
162  // I don't know which... so we just have fast mutexes
163  //mutex = (void*)CreateMutex( NULL, FALSE, NULL );
164 
165  //mutex = (void*)new CRITICAL_SECTION;
166  //InitializeCriticalSection((CRITICAL_SECTION*)mutex);
167 
168  //mutex = (void*)CreateSemaphore(NULL, 1, 0x7fffffff, NULL);
169 
170  mutex = (void*)CreateEvent(NULL,FALSE,TRUE,NULL);
171 }
172 
174 {
175  if(mutex) {
176  //DeleteCriticalSection((CRITICAL_SECTION*)mutex);
177  //delete (CRITICAL_SECTION*)mutex;
178  CloseHandle((HANDLE)mutex);
179  mutex = NULL;
180  }
181 }
182 
183 void vuMutex::lock()
184 {
185  WaitForSingleObject( (HANDLE)mutex, INFINITE );
186  //EnterCriticalSection((CRITICAL_SECTION*)mutex);
187 }
188 
189 bool vuMutex::trylock()
190 {
191  return WaitForSingleObject( (HANDLE)mutex, 1) != WAIT_TIMEOUT;
192 // return false;
193 }
194 
195 void vuMutex::unlock()
196 {
197  //ReleaseSemaphore( (HANDLE)mutex, 1, NULL );
198  //LeaveCriticalSection((CRITICAL_SECTION*)mutex);
199  SetEvent((HANDLE)mutex);
200 }
201 
202 void _kickoff(void* ptr)
203 {
204  vuThread *th = reinterpret_cast<vuThread*>(ptr);
205  int whatsup = th->m_Whatsup;
206  void* data = th->m_AdditionalData;
207  th->m_Whatsup = -1;
208  th->m_WhatsupMutex.unlock();
209  th->run(whatsup, data);
210  return;
211 }
212 
213 bool vuThread::startThread(int whatsup, void* data)
214 {
215  m_WhatsupMutex.lock();
216  m_Whatsup = whatsup;
217  m_AdditionalData = data;
218 
219  unsigned long thread = _beginthread(_kickoff, 0, (void*)this);
220  //HANDLE thread = CreateThread(NULL, 0, _kickoff, this, 0, NULL);
221  while(m_Whatsup == -1);
222 
223  m_WhatsupMutex.lock();
224  m_WhatsupMutex.unlock();
225  if (thread == (unsigned long)(-1))
226  {
227 // cout << "s2.5" << endl;
228  cout << strerror (errno) << endl;
229  if (errno == EAGAIN)
230  cout << "eagain" << endl;
231 
232  return false;
233  }
234 
235  return true;
236 }
237 
239 {
240  //_endthread();
241 }
242 
243 void vuThread::usleep(unsigned long ms) {
244  if (ms > 500)
245  Sleep ((ms+500)/1000);
246  else if (ms > 0)
247  Sleep (1);
248  else
249  Sleep (0);
250 }
251 
252 #endif
vuMutex & getLogMut()
Definition: vuThread.cpp:12
#define NULL
Definition: simpletypes.h:9
bool trylock()
Definition: vuThread.cpp:143
void stopThread()
Definition: vuThread.cpp:107
void * _kickoff(void *ptr)
Definition: vuThread.cpp:20
STL namespace.
bool startThread(int whatsup, void *data=NULL)
Definition: vuThread.cpp:45
vuMutex logmut
Definition: vuThread.cpp:10
~vuMutex()
Definition: vuThread.cpp:132
virtual void run(int whatsup, void *data)=0
void unlock()
Definition: vuThread.cpp:148
vuMutex(bool recursive=false)
Definition: vuThread.cpp:117
static void usleep(unsigned long ms)
sleep for n microseconds (!!not yet implemented for win32)
Definition: vuThread.cpp:112
void lock()
Definition: vuThread.cpp:138