Mobile API Reference  MicroStrategy 2019
InprocessSemaphore.h
Go to the documentation of this file.
1 //==============================================================================================
2 // FILENAME : InprocessSemaphore.h
3 // AUTHOR : yuwen
4 // CREATION : 2004-06-17
5 // Copyright (C) MicroStrategy Incorporated 2004
6 //==============================================================================================
7 #ifndef MSynch_InprocessSemaphore_h
8 #define MSynch_InprocessSemaphore_h
9 
10 #include "PDCHeader/PDCpthread.h"
11 #include "AbsoluteTimeout.h"
13 
14 #include "../../Defines/SmartIncrementor.h"
15 
16 namespace MSynch
17 {
19  {
20  public:
21  // @param ipname: Used for named semaphore. It is not supported/used here.
22  // @param p: Argument to be passed in. It is not supported/used here. The SemaphoreImpl will simply
23  // pass NULL to p.
24  inline InprocessSemaphore(unsigned Int32 iInitialAvailableResourceCount,
25  unsigned Int32 iMaximumResourceCount = 0x7fffffff);
26 
27  inline ~InprocessSemaphore() throw();
28 
29  inline bool WaitForResource(unsigned Int32 iMilliseconds);
30  inline void WaitForeverForResource();
31  inline void ReleaseResource();
32 
33  private:
34 
36  pthread_cond_t mConditionalVariable;
37 
38  const unsigned Int32 mMaxCount;
39 
40  unsigned Int32 mCount;
41  unsigned Int32 mWaiters;
42  };
43 }
44 
46 //
47 // MSynch::InprocessSemaphore
48 //
50 
51 inline MSynch::InprocessSemaphore::InprocessSemaphore(unsigned Int32 iInitialAvailableResourceCount, unsigned Int32 iMaximumResourceCount) :
52 
53  mMaxCount(iMaximumResourceCount),
54  mCount(iInitialAvailableResourceCount),
55  mWaiters(0)
56 
57  {
58 
59  const int lResult = ::pthread_cond_init(&mConditionalVariable, NULL);
60 
61  if (lResult != 0)
62  {
63  throw ("::pthread_cond_init failed");
64  }
65  }
66 
68 {
69 #ifndef WIN32
70  const int lResult = ::pthread_cond_destroy(&mConditionalVariable);
71  _ASSERT(lResult == 0);
72 #endif
73 }
74 
75 inline bool MSynch::InprocessSemaphore::WaitForResource(unsigned Int32 iMilliseconds)
76 {
78 
79  // Keep track of the number of waiters so that we can signal them
80  SmartIncrementor<unsigned Int32> lInc(mWaiters);
81 
82  struct timespec lAbsoluteTimeout;
83  GetAbsoluteTimeout(lAbsoluteTimeout, iMilliseconds);
84 
85  while (mCount == 0)
86  {
87  if (lLock.WaitUntilSpuriouslyWokenUpOrTimeoutExpired(mConditionalVariable, lAbsoluteTimeout) == false)
88  {
89  return false; //time out
90  }
91  }
92 
93  _ASSERT(mCount > 0);
94  --mCount;
95 
96  return true; //success
97 
98 }
99 
100 
102 {
104 
105  SmartIncrementor<unsigned Int32> lInc(mWaiters);
106 
107  while (mCount == 0)
108  {
109  lLock.WaitUntilSpuriouslyWokenUp(mConditionalVariable);
110  }
111 
112  _ASSERT(mCount > 0);
113  --mCount;
114 }
115 
117 {
119 
120  _ASSERT(mCount <= mMaxCount);
121  if (mCount == mMaxCount)
122  {
123  throw ("MSynch::InprocessSemaphore::ReleaseResource: exceeded the maximum.");
124  }
125 
126  // Always allow a waiter to continue if there is one.
127  if (mWaiters > 0)
128  {
129  const int lResult = ::pthread_cond_signal(&mConditionalVariable);
130  // On success, pthread_cond_signal() returns 0
131  if (lResult != 0)
132  {
133  throw ("::pthread_cond_signal failed");
134  }
135  }
136 
137  ++mCount;
138 }
139 
140 #endif // MSynch_InprocessSemaphore_h
bool WaitUntilSpuriouslyWokenUpOrTimeoutExpired(pthread_cond_t &irConditionalVariable, const struct timespec &irAbsoluteTimeout) const
Definition: InprocessRecursiveMutex.h:200
#define _ASSERT(x)
Definition: Asserte.h:34
Definition: InprocessRecursiveMutex.h:25
Definition: ReferenceCountedImpl.h:18
void WaitUntilSpuriouslyWokenUp(pthread_cond_t &irConditionalVariable) const
this method is here only for the ManualEvent
Definition: InprocessRecursiveMutex.h:184
Definition: InprocessRecursiveMutex.h:31
void GetAbsoluteTimeout(struct timespec &oAbsoluteTimeout, unsigned Int32 iMilliseconds)
Definition: AbsoluteTimeout.h:19
#define Int32
Definition: BasicTypes.h:20
InprocessSemaphore(unsigned Int32 iInitialAvailableResourceCount, unsigned Int32 iMaximumResourceCount=0x7fffffff)
Definition: InprocessSemaphore.h:51
bool WaitForResource(unsigned Int32 iMilliseconds)
Definition: InprocessSemaphore.h:75
Definition: SmartIncrementor.h:13
void WaitForeverForResource()
Definition: InprocessSemaphore.h:101
Definition: InprocessSemaphore.h:18
~InprocessSemaphore()
Definition: InprocessSemaphore.h:67
void ReleaseResource()
Definition: InprocessSemaphore.h:116
#define NULL
Definition: Null.h:10