NeoPZ
TPZThreadTools.cpp
Go to the documentation of this file.
1 #include "TPZThreadTools.h"
2 #include "pzerror.h"
3 
5 namespace tht
6 {
7 
9 
11  {
12 #ifdef using_pthread
13  pthread_mutexattr_t * nullPtr = NULL;
14  pthread_mutex_init(&cs, nullPtr);
15 #endif
16 #ifdef WINDOWS_THREADS
18 #endif
19 #ifdef EMBARCADERO_THREADS
20  cs = new TCriticalSection();
21 #endif
22  }
23 
25  {
26 #ifdef using_pthread
27  pthread_mutex_destroy(&cs);
28 #endif
29 #ifdef WINDOWS_THREADS
31 #endif
32 #ifdef EMBARCADERO_THREADS
33  // ::DeleteCriticalSection( &cs );
35 #endif
36  }
37 
39  {
40 #ifdef using_pthread
41  pthread_mutex_lock(&cs);
42 #endif
43 #ifdef WINDOWS_THREADS
45 #endif
46 #ifdef EMBARCADERO_THREADS
47  // ::EnterCriticalSection(&cs);
48  cs->Enter();
49 #endif
50  }
51 
53  {
54 #ifdef using_pthread
55  pthread_mutex_unlock(&cs);
56 #endif
57 #ifdef WINDOWS_THREADS
59 #endif
60 #ifdef EMBARCADERO_THREADS
61  // ::LeaveCriticalSection(&cs);
62  cs->Leave();
63 #endif
64  }
65 
67 
69  {
70 #ifdef using_pthread
71  pthread_mutexattr_t * nullPtr = NULL;
72  pthread_mutex_init(&m, nullPtr);
73 #endif
74 #ifdef WINDOWS_THREADS
75  m = CreateMutex(NULL, false, NULL);
76  if(!m)
77  {
78  MessageBox(NULL, "Error", "Fail to create Windows Mutex",
79  MB_OK | MB_ICONERROR);
80  DebugStop();
81  }
82 #endif
83 #ifdef EMBARCADERO_THREADS
84  m = new TMutex(false);
85 #endif
86  }
87 
89  {
90 #ifdef using_pthread
91  pthread_mutex_destroy(&m);
92 #endif
93 #ifdef WINDOWS_THREADS
94  CloseHandle(m);
95 #endif
96 #ifdef EMBARCADERO_THREADS
97 #endif
99  }
100 
102  {
103 #ifdef using_pthread
104  pthread_mutex_lock(&m);
105 #endif
106 #ifdef WINDOWS_THREADS
107  WaitForSingleObject(m, INFINITE);
108 #endif
109 #ifdef EMBARCADERO_THREADS
110  m->Acquire();
111 #endif
112  }
113 
115  {
116 #ifdef using_pthread
117  pthread_mutex_unlock(&m);
118 #endif
119 #ifdef WINDOWS_THREADS
120  ::ReleaseMutex(m);
121 #endif
122 #ifdef EMBARCADERO_THREADS
123  m->Release();
124 #endif
125  }
126 
128 
130  {
131 #ifdef using_pthread
132 // int sem_result = sem_init(&s, 0, 0);
133 // return sem_result;
134  // In TPZSemaphore nothing needs to be done (remember to create an object TPZSemaphore and not a pointer)
135 #endif
136 #ifdef WINDOWS_THREADS
137  s = ::CreateSemaphore(NULL, 0, 1, NULL);
138  if(!s)
139  {
140  MessageBox(NULL, "Error", "Fail to create Windows Mutex",
141  MB_OK | MB_ICONERROR);
142  DebugStop();
143  return 1;
144  }
145  return 0;
146 #endif
147 #ifdef EMBARCADERO_THREADS
148  int max = INT_MAX;
149  System::UnicodeString name = "";
150  s = new TSemaphore(NULL, 0, max, name, false);
151  return 0;
152 #endif
153  return 0;
154  }
155 
157  {
158 #ifdef using_pthread
159 // sem_destroy(&s);
160  // as it is and object (and not a pointer) it will be destroied at the end of the scope
161 
162 #endif
163 #ifdef WINDOWS_THREADS
164  CloseHandle(s);
165 #endif
166 #ifdef EMBARCADERO_THREADS
167 #endif
169  }
170 
172  {
173 #ifdef using_pthread
174 // sem_post(&s);
175  s.Post();
176  //(remember to create an object TPZSemaphore and not a pointer)
177 #endif
178 #ifdef WINDOWS_THREADS
179  int64_t pCount = 0;
180  ::ReleaseSemaphore(s, 1, &pCount);
181 #endif
182 #ifdef EMBARCADERO_THREADS
183  s->Release();
184 #endif
185  }
186 
188  {
189 #ifdef using_pthread
190  //sem_wait(&s);
191  s.Wait();
192  //(remember to create an object TPZSemaphore and not a pointer)
193 #endif
194 #ifdef WINDOWS_THREADS
195  WaitForSingleObject(s, INFINITE);
196 #endif
197 #ifdef EMBARCADERO_THREADS
198  s->Acquire();
199 #endif
200  }
201 
203 
204  void CreateThread(pz_thread_t & thread, void* (*function)(void* param),
205  void* data)
206  {
207 #ifdef using_pthread
208  pthread_attr_t * attr = NULL;
209  int res = pthread_create(&thread, attr, function, data);
210  if (res)
211  DebugStop();
212 #endif
213 #ifdef WINDOWS_THREADS
214  thread = ::CreateThread(NULL, 0, (uint64_t (__stdcall* )(void *))function, data, 0, NULL);
215 #endif
216 #ifdef EMBARCADERO_THREADS
217  thread = new TSWXEmbarcaderoThread(function, data);
218 #endif
219  }
220 
221  void ThreadWaitFor(pz_thread_t & thread)
222  {
223 #ifdef using_pthread
224  pthread_join(thread, NULL);
225 #endif
226 #ifdef WINDOWS_THREADS
227  WaitForSingleObject(thread, INFINITE);
228 #endif
229 #ifdef EMBARCADERO_THREADS
230  thread->WaitFor();
231 #endif
232  }
233 
234 }
void DeleteSemaphore(pz_semaphore_t &s)
Implements semaphore to threads. Utility.
Definition: TPZSemaphore.h:15
int InitializeSemaphore(pz_semaphore_t &s)
Semaforos.
pthread_mutex_t pz_critical_section_t
void MutexUnlock(pz_mutex_t &m)
Defines PZError.
void LeaveCriticalSection(pz_critical_section_t &cs)
pthread_mutex_t pz_mutex_t
void EnterCriticalSection(pz_critical_section_t &cs)
void CreateThread(pz_thread_t &thread, void *(*function)(void *param), void *data)
Threads.
void InitializeCriticalSection(pz_critical_section_t &cs)
Critical sections.
void ThreadWaitFor(pz_thread_t &thread)
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
string res
Definition: test.py:151
void MutexLock(pz_mutex_t &m)
void SemaphoreWait(pz_semaphore_t &s)
void SemaphorePost(pz_semaphore_t &s)
pthread_t pz_thread_t
clarg::argString m("-m", "input matrix file name (text format)", "matrix.txt")
void DeleteMutex(pz_mutex_t &m)
namespace tht means "ThreadTools"
void InitializeMutex(pz_mutex_t &m)
Mutexes.
void DeleteCriticalSection(pz_critical_section_t &cs)