windows/SysSemaphore.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /*****************************************************************************/
39 /* REXX Windows Support */
40 /* */
41 /* Semaphore support for Windows systems */
42 /* */
43 /*****************************************************************************/
44 
45 #ifndef Included_SysSemaphore
46 #define Included_SysSemaphore
47 
48 #include "rexx.h"
49 #include "SysDebug.hpp"
50 #include "Utilities.hpp"
51 
52 #include <stdlib.h>
53 #include <stdio.h>
54 
55 inline void waitHandle(HANDLE s);
56 
57 class SysSemaphore {
58 public:
59  SysSemaphore(const char *variable) : semVariable(variable), sem(0) { ; }
60  SysSemaphore(const char *variable, bool);
61  ~SysSemaphore() { ; }
62  void create();
63  inline void open() { ; }
64  void close();
65  void post() { SetEvent(sem); };
66  inline void wait(const char *ds, int di)
67  {
68 #if CONCURRENCY_DEBUG
70  {
71  struct ConcurrencyInfos concurrencyInfos;
72  Utilities::GetConcurrencyInfos(concurrencyInfos);
73  dbgprintf(CONCURRENCY_TRACE "...... ... (SysSemaphore)%s.wait : before waitHandle(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, semVariable, sem, ds, di);
74  }
75 #endif
76  waitHandle(sem);
77 #if CONCURRENCY_DEBUG
79  {
80  struct ConcurrencyInfos concurrencyInfos;
81  Utilities::GetConcurrencyInfos(concurrencyInfos);
82  dbgprintf(CONCURRENCY_TRACE "...... ... (SysSemaphore)%s.wait : after waitHandle(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, semVariable, sem, ds, di);
83  }
84 #endif
85  }
86 
87  inline bool wait(const char *ds, int di, uint32_t timeout)
88  {
89 #ifdef CONCURRENCY_DEBUG
91  {
92  struct ConcurrencyInfos concurrencyInfos;
93  Utilities::GetConcurrencyInfos(concurrencyInfos);
94  dbgprintf(CONCURRENCY_TRACE "...... ... (SysSemaphore)%s.wait : before WaitForSingleObject(0x%x, timemout) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, semVariable, sem, timeout, ds, di);
95  }
96 #endif
97  bool result = WaitForSingleObject(sem, timeout) != WAIT_TIMEOUT;
98 #ifdef CONCURRENCY_DEBUG
100  {
101  struct ConcurrencyInfos concurrencyInfos;
102  Utilities::GetConcurrencyInfos(concurrencyInfos);
103  dbgprintf(CONCURRENCY_TRACE "...... ... (SysSemaphore)%s.wait : after WaitForSingleObject(0x%x, timemout) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, semVariable, sem, timeout, ds, di);
104  }
105 #endif
106  return result;
107  }
108 
109  inline void reset() { ResetEvent(sem); }
110  inline bool posted() { return WaitForSingleObject(sem, 0) != 0; }
111 
112  inline void setSemVariable(const char *variable) { semVariable = variable; } // See RexxActivity::RexxActivity, must reassign, so public setter needed.
113 
114  static inline bool allocTlsIndex()
115  {
116  tlsNoMessageLoopIndex = TlsAlloc();
117  usingTls = (tlsNoMessageLoopIndex != TLS_OUT_OF_INDEXES);
118  return usingTls;
119  }
120  static inline void deallocTlsIndex()
121  {
122  TlsFree(tlsNoMessageLoopIndex);
123  usingTls = false;
124  }
125 
126  static inline void setNoMessageLoop() { TlsSetValue(tlsNoMessageLoopIndex, (LPVOID)1); }
127  static inline bool noMessageLoop() { return usingTls && ((DWORD_PTR)TlsGetValue(tlsNoMessageLoopIndex) == 1); }
128 
129 protected:
130  const char *semVariable;
131  HANDLE sem;
132 
133 private:
134  static bool usingTls;
135  static DWORD tlsNoMessageLoopIndex;
136 
137 };
138 
139 
140 class SysMutex {
141 public:
142  SysMutex(const char *variable) : mutexVariable(variable), mutexMutex(0) { }
143  SysMutex(const char *, bool);
144  ~SysMutex() { ; }
145  void create();
146  void close();
147  inline void request(const char *ds, int di)
148  {
149 #ifdef CONCURRENCY_DEBUG
151  {
152  struct ConcurrencyInfos concurrencyInfos;
153  Utilities::GetConcurrencyInfos(concurrencyInfos);
154  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.request : before waitHandle(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, mutexMutex, ds, di);
155  }
156 #endif
158 #ifdef CONCURRENCY_DEBUG
160  {
161  struct ConcurrencyInfos concurrencyInfos;
162  Utilities::GetConcurrencyInfos(concurrencyInfos);
163  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.request : after waitHandle(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, mutexMutex, ds, di);
164  }
165 #endif
166  }
167 
168  inline void release(const char *ds, int di)
169  {
170 #ifdef CONCURRENCY_DEBUG
172  {
173  struct ConcurrencyInfos concurrencyInfos;
174  Utilities::GetConcurrencyInfos(concurrencyInfos);
175  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.release : before ReleaseMutex(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, mutexMutex, ds, di);
176  }
177 #endif
178  ReleaseMutex(mutexMutex);
179 #ifdef CONCURRENCY_DEBUG
181  {
182  struct ConcurrencyInfos concurrencyInfos;
183  Utilities::GetConcurrencyInfos(concurrencyInfos);
184  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.release : after ReleaseMutex(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, mutexMutex, ds, di);
185  }
186 #endif
187  }
188 
189  inline bool requestImmediate(const char *ds, int di)
190  {
191 #ifdef CONCURRENCY_DEBUG
193  {
194  struct ConcurrencyInfos concurrencyInfos;
195  Utilities::GetConcurrencyInfos(concurrencyInfos);
196  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.requestImmediate : before WaitForSingleObject(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, mutexMutex, ds, di);
197  }
198 #endif
199  bool result = WaitForSingleObject(mutexMutex, 0) != WAIT_TIMEOUT;
200 #ifdef CONCURRENCY_DEBUG
202  {
203  struct ConcurrencyInfos concurrencyInfos;
204  Utilities::GetConcurrencyInfos(concurrencyInfos);
205  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.requestImmediate : after WaitForSingleObject(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, mutexMutex, ds, di);
206  }
207 #endif
208  return result;
209  }
210 
211 protected:
212  const char *mutexVariable;
213  HANDLE mutexMutex; // the actual mutex
214 };
215 
216 
217 /**
218  * Wait for a synchronization object to be in the signaled state.
219  *
220  * Any thread that creates windows must process messages. A thread that
221  * calls WaitForSingelObject with an infinite timeout risks deadlocking the
222  * system. MS's solution for this is to use MsgWaitForMultipleObjects to
223  * wait on the object, or a new message arriving in the message queue. Some
224  * threads create windows indirectly, an example is COM with CoInitialize.
225  * Since we can't know if the current thread has a message queue that needs
226  * processing, we use MsgWaitForMultipleObjects.
227  *
228  * However, with the introduction of the C++ native API in ooRexx 4.0.0, it
229  * became possible for external native libraries to attach a thread with an
230  * active window procedure to the interpreter. If a wait is done on that
231  * thread, here in waitHandle(), PeekMessage() causes non-queued messages to be
232  * dispatched, and the window procedure is reentered. This can cause the Rexx
233  * program to hang. In addition, in the one known extension where this problem
234  * happens, ooDialog, the messages need to be passed to the dialog manager
235  * rather than dispatched directly to the window.
236  *
237  * For this special case, the thread can explicity ask, through
238  * RexxSetProcessMessages(), that messages are *not* processed during this
239  * wait. Thread local storage is used to keep track of a flag signalling this
240  * case on a per-thread basis.
241  *
242  * Note that MsgWaitForMultipleObjects only returns if a new message is
243  * placed in the queue. PeekMessage alters the state of all messages in
244  * the queue so that they are no longer 'new.' Once PeekMessage is called,
245  * all the messages on the queue need to be processed.
246  */
247 inline void waitHandle(HANDLE s)
248 {
249  // If already signaled, return.
250  if ( WaitForSingleObject(s, 0) == WAIT_OBJECT_0 )
251  {
252  return;
253  }
254 
255  // If no message loop is flagged, then use WaitForSingleobject()
257  {
258  WaitForSingleObject(s, INFINITE);
259  return;
260  }
261 
262  MSG msg = {0};
263 
264  do
265  {
266  while ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
267  {
268  TranslateMessage(&msg);
269  DispatchMessage(&msg);
270 
271  // Check to see if signaled.
272  if ( WaitForSingleObject(s, 0) == WAIT_OBJECT_0 )
273  {
274  return;
275  }
276  }
277  } while ( MsgWaitForMultipleObjects(1, &s, FALSE, INFINITE, QS_ALLINPUT) == WAIT_OBJECT_0 + 1 );
278 }
279 
280 
281 #endif
#define CONCURRENCY_TRACE
Definition: Utilities.hpp:50
void close()
void release(const char *ds, int di)
pthread_mutex_t mutexMutex
SysMutex(const char *variable)
bool requestImmediate(const char *ds, int di)
SysMutex(const char *, bool)
const char * mutexVariable
void create()
void request(const char *ds, int di)
SysSemaphore(const char *variable)
static void setNoMessageLoop()
bool wait(const char *ds, int di, uint32_t timeout)
const char * semVariable
SysSemaphore(const char *variable, bool)
void wait(const char *ds, int di)
void setSemVariable(const char *variable)
static void deallocTlsIndex()
static bool noMessageLoop()
static DWORD tlsNoMessageLoopIndex
static bool allocTlsIndex()
static void GetConcurrencyInfos(struct ConcurrencyInfos &concurrencyInfos)
Definition: Utilities.cpp:61
static bool traceConcurrency()
RexxVariableDictionary * variableDictionary
Definition: Utilities.hpp:65
RexxActivation * activation
Definition: Utilities.hpp:64
unsigned short reserveCount
Definition: Utilities.hpp:66
wholenumber_t threadId
Definition: Utilities.hpp:62
void dbgprintf(const char *format,...)
void waitHandle(HANDLE s)
unsigned int uint32_t