unix/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 Unix Support */
40 /* */
41 /* Semaphore support for Unix systems */
42 /* */
43 /*****************************************************************************/
44 
45 #ifndef SysSemaphore_DEFINED
46 #define SysSemaphore_DEFINED
47 
48 #include <pthread.h>
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include "rexx.h"
52 #include "SysDebug.hpp"
53 #include "Utilities.hpp"
54 
55 class SysSemaphore {
56 public:
57  SysSemaphore(const char *variable) : semVariable(variable), postedCount(0), created(false) { }
58  SysSemaphore(const char *variable, bool);
59  ~SysSemaphore() { ; }
60  void create();
61  inline void open() { ; }
62  void close();
63  void post();
64  void wait(const char *ds, int di);
65  bool wait(const char *ds, int di, uint32_t);
66  void reset();
67  inline bool posted() { return postedCount != 0; }
68  inline void setSemVariable(const char *variable) { semVariable = variable; } // See RexxActivity::RexxActivity, must reassign, so public setter needed.
69 
70 protected:
71  const char *semVariable;
72  pthread_cond_t semCond;
73  pthread_mutex_t semMutex;
75  bool created;
76 };
77 
78 class SysMutex {
79 public:
80  SysMutex(const char *variable) : mutexVariable(variable), created(false) { ; }
81  SysMutex(const char *, bool);
82  ~SysMutex() { ; }
83  void create();
84  inline void open() { ; }
85  void close();
86  inline void request(const char *ds, int di)
87  {
88 #ifdef CONCURRENCY_DEBUG
90  {
91  struct ConcurrencyInfos concurrencyInfos;
92  Utilities::GetConcurrencyInfos(concurrencyInfos);
93  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.request : before pthread_mutex_lock(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, &mutexMutex, ds, di);
94  }
95 #endif
96  pthread_mutex_lock(&mutexMutex);
97 #ifdef CONCURRENCY_DEBUG
99  {
100  struct ConcurrencyInfos concurrencyInfos;
101  Utilities::GetConcurrencyInfos(concurrencyInfos);
102  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.request : after pthread_mutex_lock(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, &mutexMutex, ds, di);
103  }
104 #endif
105  }
106  inline void release(const char *ds, int di)
107  {
108 #ifdef CONCURRENCY_DEBUG
110  {
111  struct ConcurrencyInfos concurrencyInfos;
112  Utilities::GetConcurrencyInfos(concurrencyInfos);
113  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.release : before pthread_mutex_unlock(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, &mutexMutex, ds, di);
114  }
115 #endif
116  pthread_mutex_unlock(&mutexMutex);
117 #ifdef CONCURRENCY_DEBUG
119  {
120  struct ConcurrencyInfos concurrencyInfos;
121  Utilities::GetConcurrencyInfos(concurrencyInfos);
122  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.release : after pthread_mutex_unlock(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, &mutexMutex, ds, di);
123  }
124 #endif
125  }
126  inline bool requestImmediate(const char *ds, int di)
127  {
128 #ifdef CONCURRENCY_DEBUG
130  {
131  struct ConcurrencyInfos concurrencyInfos;
132  Utilities::GetConcurrencyInfos(concurrencyInfos);
133  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.requestImmediate : before pthread_mutex_trylock(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, &mutexMutex, ds, di);
134  }
135 #endif
136  bool result = pthread_mutex_trylock(&mutexMutex) == 0;
137 #ifdef CONCURRENCY_DEBUG
139  {
140  struct ConcurrencyInfos concurrencyInfos;
141  Utilities::GetConcurrencyInfos(concurrencyInfos);
142  dbgprintf(CONCURRENCY_TRACE "...... ... (SysMutex)%s.requestImmediate : after pthread_mutex_trylock(0x%x) from %s (0x%x)\n", concurrencyInfos.threadId, concurrencyInfos.activation, concurrencyInfos.variableDictionary, concurrencyInfos.reserveCount, concurrencyInfos.lock, mutexVariable, &mutexMutex, ds, di);
143  }
144 #endif
145  return result;
146  }
147 
148 protected:
149  const char *mutexVariable;
150  pthread_mutex_t mutexMutex;
151  bool created;
152 };
153 #endif
#define CONCURRENCY_TRACE
Definition: Utilities.hpp:50
void release(const char *ds, int di)
pthread_mutex_t mutexMutex
SysMutex(const char *variable)
bool requestImmediate(const char *ds, int di)
const char * mutexVariable
void request(const char *ds, int di)
SysSemaphore(const char *variable)
pthread_mutex_t semMutex
const char * semVariable
void wait(const char *ds, int di)
void setSemVariable(const char *variable)
pthread_cond_t semCond
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,...)
unsigned int uint32_t