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
69  if (Utilities::traceConcurrency()) dbgprintf(CONCURRENCY_TRACE "...... ... ", Utilities::currentThreadId(), NULL, NULL, 0, ' ');
70  dbgprintf("(SysSemaphore)%s.wait : before waitHandle(0x%x) from %s (0x%x)\n", semVariable, sem, ds, di);
71 #endif
72  waitHandle(sem);
73 #if CONCURRENCY_DEBUG
74  if (Utilities::traceConcurrency()) dbgprintf(CONCURRENCY_TRACE "...... ... ", Utilities::currentThreadId(), NULL, NULL, 0, ' ');
75  dbgprintf("(SysSemaphore)%s.wait : after waitHandle(0x%x) from %s (0x%x)\n", semVariable, sem, ds, di);
76 #endif
77  }
78 
79  inline bool wait(const char *ds, int di, uint32_t timeout)
80  {
81 #ifdef CONCURRENCY_DEBUG
82  if (Utilities::traceConcurrency()) dbgprintf(CONCURRENCY_TRACE "...... ... ", Utilities::currentThreadId(), NULL, NULL, 0, ' ');
83  dbgprintf("(SysSemaphore)%s.wait : before WaitForSingleObject(0x%x, timemout) from %s (0x%x)\n", semVariable, sem, timeout, ds, di);
84 #endif
85  bool result = WaitForSingleObject(sem, timeout) != WAIT_TIMEOUT;
86 #ifdef CONCURRENCY_DEBUG
87  if (Utilities::traceConcurrency()) dbgprintf(CONCURRENCY_TRACE "...... ... ", Utilities::currentThreadId(), NULL, NULL, 0, ' ');
88  dbgprintf("(SysSemaphore)%s.wait : after WaitForSingleObject(0x%x, timemout) from %s (0x%x)\n", semVariable, sem, timeout, ds, di);
89 #endif
90  return result;
91  }
92 
93  inline void reset() { ResetEvent(sem); }
94  inline bool posted() { return WaitForSingleObject(sem, 0) != 0; }
95 
96  inline void setSemVariable(const char *variable) { semVariable = variable; } // See RexxActivity::RexxActivity, must reassign, so public setter needed.
97 
98  static inline bool allocTlsIndex()
99  {
100  tlsNoMessageLoopIndex = TlsAlloc();
101  usingTls = (tlsNoMessageLoopIndex != TLS_OUT_OF_INDEXES);
102  return usingTls;
103  }
104  static inline void deallocTlsIndex()
105  {
106  TlsFree(tlsNoMessageLoopIndex);
107  usingTls = false;
108  }
109 
110  static inline void setNoMessageLoop() { TlsSetValue(tlsNoMessageLoopIndex, (LPVOID)1); }
111  static inline bool noMessageLoop() { return usingTls && ((DWORD_PTR)TlsGetValue(tlsNoMessageLoopIndex) == 1); }
112 
113 protected:
114  const char *semVariable;
115  HANDLE sem;
116 
117 private:
118  static bool usingTls;
119  static DWORD tlsNoMessageLoopIndex;
120 
121 };
122 
123 
124 class SysMutex {
125 public:
126  SysMutex(const char *variable) : mutexVariable(variable), mutexMutex(0) { }
127  SysMutex(const char *, bool);
128  ~SysMutex() { ; }
129  void create();
130  void close();
131  inline void request(const char *ds, int di)
132  {
133 #ifdef CONCURRENCY_DEBUG
134  if (Utilities::traceConcurrency()) dbgprintf(CONCURRENCY_TRACE "...... ... ", Utilities::currentThreadId(), NULL, NULL, 0, ' ');
135  dbgprintf("(SysMutex)%s.request : before waitHandle(0x%x) from %s (0x%x)\n", mutexVariable, mutexMutex, ds, di);
136 #endif
138 #ifdef CONCURRENCY_DEBUG
139  if (Utilities::traceConcurrency()) dbgprintf(CONCURRENCY_TRACE "...... ... ", Utilities::currentThreadId(), NULL, NULL, 0, ' ');
140  dbgprintf("(SysMutex)%s.request : after waitHandle(0x%x) from %s (0x%x)\n", mutexVariable, mutexMutex, ds, di);
141 #endif
142  }
143 
144  inline void release(const char *ds, int di)
145  {
146 #ifdef CONCURRENCY_DEBUG
147  if (Utilities::traceConcurrency()) dbgprintf(CONCURRENCY_TRACE "...... ... ", Utilities::currentThreadId(), NULL, NULL, 0, ' ');
148  dbgprintf("(SysMutex)%s.release : before ReleaseMutex(0x%x) from %s (0x%x)\n", mutexVariable, mutexMutex, ds, di);
149 #endif
150  ReleaseMutex(mutexMutex);
151 #ifdef CONCURRENCY_DEBUG
152  if (Utilities::traceConcurrency()) dbgprintf(CONCURRENCY_TRACE "...... ... ", Utilities::currentThreadId(), NULL, NULL, 0, ' ');
153  dbgprintf("(SysMutex)%s.release : after ReleaseMutex(0x%x) from %s (0x%x)\n", mutexVariable, mutexMutex, ds, di);
154 #endif
155  }
156 
157  inline bool requestImmediate(const char *ds, int di)
158  {
159 #ifdef CONCURRENCY_DEBUG
160  if (Utilities::traceConcurrency()) dbgprintf(CONCURRENCY_TRACE "...... ... ", Utilities::currentThreadId(), NULL, NULL, 0, ' ');
161  dbgprintf("(SysMutex)%s.requestImmediate : before WaitForSingleObject(0x%x) from %s (0x%x)\n", mutexVariable, mutexMutex, ds, di);
162 #endif
163  bool result = WaitForSingleObject(mutexMutex, 0) != WAIT_TIMEOUT;
164 #ifdef CONCURRENCY_DEBUG
165  if (Utilities::traceConcurrency()) dbgprintf(CONCURRENCY_TRACE "...... ... ", Utilities::currentThreadId(), NULL, NULL, 0, ' ');
166  dbgprintf("(SysMutex)%s.requestImmediate : after WaitForSingleObject(0x%x) from %s (0x%x)\n", mutexVariable, mutexMutex, ds, di);
167 #endif
168  return result;
169  }
170 
171 protected:
172  const char *mutexVariable;
173  HANDLE mutexMutex; // the actual mutex
174 };
175 
176 
177 /**
178  * Wait for a synchronization object to be in the signaled state.
179  *
180  * Any thread that creates windows must process messages. A thread that
181  * calls WaitForSingelObject with an infinite timeout risks deadlocking the
182  * system. MS's solution for this is to use MsgWaitForMultipleObjects to
183  * wait on the object, or a new message arriving in the message queue. Some
184  * threads create windows indirectly, an example is COM with CoInitialize.
185  * Since we can't know if the current thread has a message queue that needs
186  * processing, we use MsgWaitForMultipleObjects.
187  *
188  * However, with the introduction of the C++ native API in ooRexx 4.0.0, it
189  * became possible for external native libraries to attach a thread with an
190  * active window procedure to the interpreter. If a wait is done on that
191  * thread, here in waitHandle(), PeekMessage() causes non-queued messages to be
192  * dispatched, and the window procedure is reentered. This can cause the Rexx
193  * program to hang. In addition, in the one known extension where this problem
194  * happens, ooDialog, the messages need to be passed to the dialog manager
195  * rather than dispatched directly to the window.
196  *
197  * For this special case, the thread can explicity ask, through
198  * RexxSetProcessMessages(), that messages are *not* processed during this
199  * wait. Thread local storage is used to keep track of a flag signalling this
200  * case on a per-thread basis.
201  *
202  * Note that MsgWaitForMultipleObjects only returns if a new message is
203  * placed in the queue. PeekMessage alters the state of all messages in
204  * the queue so that they are no longer 'new.' Once PeekMessage is called,
205  * all the messages on the queue need to be processed.
206  */
207 inline void waitHandle(HANDLE s)
208 {
209  // If already signaled, return.
210  if ( WaitForSingleObject(s, 0) == WAIT_OBJECT_0 )
211  {
212  return;
213  }
214 
215  // If no message loop is flagged, then use WaitForSingleobject()
217  {
218  WaitForSingleObject(s, INFINITE);
219  return;
220  }
221 
222  MSG msg = {0};
223 
224  do
225  {
226  while ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
227  {
228  TranslateMessage(&msg);
229  DispatchMessage(&msg);
230 
231  // Check to see if signaled.
232  if ( WaitForSingleObject(s, 0) == WAIT_OBJECT_0 )
233  {
234  return;
235  }
236  }
237  } while ( MsgWaitForMultipleObjects(1, &s, FALSE, INFINITE, QS_ALLINPUT) == WAIT_OBJECT_0 + 1 );
238 }
239 
240 
241 #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 wholenumber_t currentThreadId()
static bool traceConcurrency()
void dbgprintf(const char *format,...)
void waitHandle(HANDLE s)
unsigned int uint32_t