NeoPZ
TPZSemaphore.cpp
Go to the documentation of this file.
1 
6 #include "TPZSemaphore.h"
7 
8 #include <fstream>
9 
10 #include "pzlog.h"
11 
12 #ifdef LOG4CXX
13 static LoggerPtr logger(Logger::getLogger("pz.util.semaphore"));
14 #endif
15 
17 {
18  fCounter = 0;
19  PZ_PTHREAD_MUTEX_INIT(&fMutex, NULL, "TPZSemaphore::TPZSemaphore()");
20  PZ_PTHREAD_COND_INIT(&fCond, NULL, "TPZSemaphore::TPZSemaphore()");
21 }
22 
24 {
25  PZ_PTHREAD_MUTEX_DESTROY(&fMutex, "TPZSemaphore::~TPZSemaphore()");
26  PZ_PTHREAD_COND_DESTROY(&fCond, "TPZSemaphore::~TPZSemaphore()");
27 }
28 
30 {
31  fCounter = initCount;
32  PZ_PTHREAD_MUTEX_INIT(&fMutex, NULL, "TPZSemaphore::TPZSemaphore(int)");
33  PZ_PTHREAD_COND_INIT(&fCond, NULL, "TPZSemaphore::TPZSemaphore(int)");
34 }
35 
37 {
38  PZ_PTHREAD_MUTEX_LOCK(&fMutex, "TPZSemaphore::Wait()");
39  if (fCounter > 0)
40  {
41  fCounter--;
42 #ifdef LOG4CXX
43  if(logger->isDebugEnabled())
44  {
45  std::stringstream sout;
46 #ifdef VC
47  sout << "THREAD IN SEMAPHORE WAIT: " << pthread_self().x <<" " <<__LINE__ << std::endl;
48 #else
49  sout << "THREAD IN SEMAPHORE WAIT: " << pthread_self() <<" " <<__LINE__ << std::endl;
50 #endif
51  sout << "FCOUNTER VALUE : " << fCounter << std::endl;
52  LOGPZ_DEBUG(logger,sout.str())
53  }
54 #endif
55  PZ_PTHREAD_MUTEX_UNLOCK(&fMutex, "TPZSemaphore::Wait()");
56  return;
57  }
58  while (fCounter == 0) {
59  PZ_PTHREAD_COND_WAIT(&fCond, &fMutex, "TPZSemaphore::Wait()");
60  if (fCounter > 0) {
61  fCounter--;
62 #ifdef LOG4CXX
63  if(logger->isDebugEnabled())
64  {
65  std::stringstream sout;
66 #ifdef VC
67  sout << "THREAD IN SEMAPHORE AFTER WAIT: " << pthread_self().x <<" " << __LINE__ << std::endl;
68 #else
69  sout << "THREAD IN SEMAPHORE AFTER WAIT: " << pthread_self() <<" " << __LINE__ << std::endl;
70 #endif
71  sout << "FCOUNTER VALUE : " << fCounter << std::endl;
72  LOGPZ_DEBUG(logger,sout.str())
73  }
74 #endif
75  PZ_PTHREAD_MUTEX_UNLOCK(&fMutex, "TPZSemaphore::Wait()");
76  return;
77  }
78  }
79 
80 }
81 
83 {
84  PZ_PTHREAD_MUTEX_LOCK(&fMutex, "TPZSemaphore::Post()");
85  fCounter++;
86 #ifdef LOG4CXX
87  if(logger->isDebugEnabled())
88  {
89  std::stringstream sout;
90 #ifdef VC
91  sout << "THREAD IN SEMAPHORE POST: " << pthread_self().x <<" " << __LINE__ << std::endl;
92 #else
93  sout << "THREAD IN SEMAPHORE POST: " << pthread_self() <<" " << __LINE__ << std::endl;
94 #endif
95  sout << "FCOUNTER VALUE : " << fCounter << std::endl;
96  LOGPZ_DEBUG(logger,sout.str())
97  }
98 #endif
99  PZ_PTHREAD_COND_SIGNAL(&fCond, "TPZSemaphore::Post()");
100  PZ_PTHREAD_MUTEX_UNLOCK(&fMutex, "TPZSemaphore::Post()");
101  return;
102 }
#define PZ_PTHREAD_MUTEX_UNLOCK(mutex, fn)
Definition: pz_pthread.h:79
Contains declaration of the TPZSemaphore class which implements semaphore to threads.
#define PZ_PTHREAD_COND_SIGNAL(cond, fn)
Definition: pz_pthread.h:130
pthread_cond_t fCond
Condition for the thread must to be waiting.
Definition: TPZSemaphore.h:23
Contains definitions to LOGPZ_DEBUG, LOGPZ_INFO, LOGPZ_WARN, LOGPZ_ERROR and LOGPZ_FATAL, and the implementation of the inline InitializePZLOG(string) function using log4cxx library or not. It must to be called out of "#ifdef LOG4CXX" scope.
#define PZ_PTHREAD_COND_WAIT(cond, mutex, fn)
Definition: pz_pthread.h:127
#define PZ_PTHREAD_COND_INIT(cond, attr, fn)
Definition: pz_pthread.h:121
int fCounter
Counter of the times the semaphore is locked.
Definition: TPZSemaphore.h:19
#define PZ_PTHREAD_MUTEX_LOCK(mutex, fn)
Definition: pz_pthread.h:76
#define LOGPZ_DEBUG(A, B)
Define log for debug info.
Definition: pzlog.h:87
#define PZ_PTHREAD_MUTEX_DESTROY(mutex, fn)
Definition: pz_pthread.h:73
#define PZ_PTHREAD_COND_DESTROY(cond, fn)
Definition: pz_pthread.h:124
~TPZSemaphore(void)
Default destructor.
pthread_mutex_t fMutex
Mutex for the thread.
Definition: TPZSemaphore.h:21
TPZSemaphore(void)
Default constructor.
#define PZ_PTHREAD_MUTEX_INIT(mutex, attr, fn)
Definition: pz_pthread.h:70