ooRexx  4.2.0-source
About: ooRexx (Open Object Rexx) is a free implementation of Object Rexx. Object Rexx is an enhancement of the classic Rexx interpreter; a full-featured programming language with a human-oriented syntax.
  Fossies Dox: ooRexx-4.2.0-source.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

SysSemaphore.cpp
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 */
42 /* */
43 /*****************************************************************************/
44 /*****************************************************************************/
45 
46 #ifdef HAVE_CONFIG_H
47  #include "config.h"
48 #endif
49 
50 #include <pthread.h>
51 #include <memory.h>
52 #include <stdio.h>
53 #ifdef AIX
54  #include <sys/sched.h>
55  #include <time.h>
56 #endif
57 
58 #if defined(OPSYS_SUN)
59  #include <sched.h>
60 #endif
61 
62 #include <errno.h>
63 
64 #include "SysSemaphore.hpp"
65 
66 
67 /* ********************************************************************** */
68 /* *** SysSemaphore *** */
69 /* ********************************************************************** */
70 
78 {
79  postedCount = 0;
80  created = false;
81 
82  if (createSem)
83  {
84  create();
85  }
86 }
87 
88 
90 {
91  int iRC = 0;
92 
93  if (!created)
94  {
95  // Clear mutex/cond prior to init
96  // this->semMutex = NULL;
97  // this->semCond = NULL;
98 
99  /* The original settings for pthread_mutexattr_settype() were:
100  AIX : PTHREAD_MUTEX_RECURSIVE
101  SUNOS: PTHREAD_MUTEX_ERRORCHECK
102  LINUX: PTHREAD_MUTEX_RECURSIVE_NP
103  */
104 
105  #if defined( HAVE_PTHREAD_MUTEXATTR_SETTYPE )
106  pthread_mutexattr_t mutexattr;
107 
108  iRC = pthread_mutexattr_init(&mutexattr);
109  if ( iRC == 0 )
110  {
111  #if defined( HAVE_PTHREAD_MUTEX_RECURSIVE_NP ) /* Linux most likely */
112  iRC = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
113  #elif defined( HAVE_PTHREAD_MUTEX_RECURSIVE )
114  iRC = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
115  #elif defined( HAVE_PTHREAD_MUTEX_ERRORCHECK )
116  iRC = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK);
117  #else
118  fprintf(stderr," *** ERROR: Unknown 2nd argument to pthread_mutexattr_settype()!\n");
119  #endif
120  }
121  if ( iRC == 0 )
122  {
123  iRC = pthread_mutex_init(&(this->semMutex), &mutexattr);
124  }
125  if ( iRC == 0 )
126  {
127  iRC = pthread_mutexattr_destroy(&mutexattr); /* It does not affect */
128  }
129  if ( iRC == 0 ) /* mutexes created with it */
130  {
131  iRC = pthread_cond_init(&(this->semCond), NULL);
132  }
133  #else
134  iRC = pthread_mutex_init(&(this->semMutex), NULL);
135  if ( iRC == 0 )
136  {
137  iRC = pthread_cond_init(&(this->semCond), NULL);
138  }
139  #endif
140  if ( iRC != 0 )
141  {
142  fprintf(stderr," *** ERROR: At RexxSemaphore(), pthread_mutex_init - RC = %d !\n", iRC);
143  if ( iRC == EINVAL )
144  {
145  fprintf(stderr," *** ERROR: Application was not built thread safe!\n");
146  }
147  }
148  this->postedCount = 0;
149  created = true;
150  }
151 }
152 
154 {
155  if (created)
156  {
157  pthread_cond_destroy(&(this->semCond));
158  pthread_mutex_destroy(&(this->semMutex));
159  created = false;
160  }
161 }
162 
163 
165 {
166  int rc;
167 
168  rc = pthread_mutex_lock(&(this->semMutex)); //Lock the semaphores Mutex
169  postedCount++; //Increment post count
170  rc = pthread_cond_broadcast(&(this->semCond)); //allows any threads waiting to run
171  rc = pthread_mutex_unlock(&(this->semMutex)); // Unlock access to Semaphore mutex
172 }
173 
175 {
176  int rc;
177  int schedpolicy, i_prio;
178  struct sched_param schedparam;
179 
180  pthread_getschedparam(pthread_self(), &schedpolicy, &schedparam);
181  i_prio = schedparam.sched_priority;
182  schedparam.sched_priority = 100;
183  pthread_setschedparam(pthread_self(),SCHED_OTHER, &schedparam);
184  rc = pthread_mutex_lock(&(this->semMutex)); // Lock access to semaphore
185  if (this->postedCount == 0) // Has it been posted?
186  {
187  rc = pthread_cond_wait(&(this->semCond), &(this->semMutex)); // Nope, then wait on it.
188  }
189  pthread_mutex_unlock(&(this->semMutex)); // Release mutex lock
190  schedparam.sched_priority = i_prio;
191  pthread_setschedparam(pthread_self(),SCHED_OTHER, &schedparam);
192 }
193 
194 bool SysSemaphore::wait(uint32_t t) // takes a timeout in msecs
195 {
196  struct timespec timestruct;
197  time_t *Tpnt = NULL;
198 
199  int result = 0;
200  timestruct.tv_nsec = 0;
201  timestruct.tv_sec = t/1000+time(Tpnt); // convert to secs and abstime
202  pthread_mutex_lock(&(this->semMutex)); // Lock access to semaphore
203  if (!this->postedCount) // Has it been posted?
204  {
205  // wait with timeout
206  result = pthread_cond_timedwait(&(this->semCond),&(this->semMutex),&timestruct);
207  }
208  pthread_mutex_unlock(&(this->semMutex)); // Release mutex lock
209  // a false return means this timed out
210  return result != ETIMEDOUT;
211 }
212 
214 {
215  pthread_mutex_lock(&(this->semMutex)); // Lock access to semaphore
216  this->postedCount = 0; // Clear value
217  pthread_mutex_unlock(&(this->semMutex)); // unlock access to semaphore
218 }
219 
220 /* ********************************************************************** */
221 /* *** SysMutex *** */
222 /* ********************************************************************** */
223 
230 SysMutex::SysMutex(bool createSem)
231 {
232  if (createSem)
233  {
234  create();
235  }
236 }
237 
239 {
240  // don't create this multiple times
241  if (created)
242  {
243  return;
244  }
245  int iRC = 0;
246 
247 /* The original settings for pthread_mutexattr_settype() were:
248  SUNOS: PTHREAD_MUTEX_ERRORCHECK
249  LINUX: PTHREAD_MUTEX_RECURSIVE_NP
250 */
251 #if defined( HAVE_PTHREAD_MUTEXATTR_SETTYPE )
252  pthread_mutexattr_t mutexattr;
253 
254  iRC = pthread_mutexattr_init(&mutexattr);
255  if ( iRC == 0 )
256  {
257  #if defined( HAVE_PTHREAD_MUTEX_RECURSIVE_NP ) /* Linux most likely */
258  iRC = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
259  #elif defined( HAVE_PTHREAD_MUTEX_RECURSIVE )
260  iRC = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
261  #elif defined( HAVE_PTHREAD_MUTEX_ERRORCHECK )
262  iRC = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK);
263  #else
264  fprintf(stderr," *** ERROR: Unknown 2nd argument to pthread_mutexattr_settype()!\n");
265  #endif
266  }
267  if ( iRC == 0 )
268  {
269  iRC = pthread_mutex_init(&(this->mutexMutex), &mutexattr);
270  }
271  if ( iRC == 0 )
272  {
273  iRC = pthread_mutexattr_destroy(&mutexattr); /* It does not affect */
274  }
275 #else /* mutexes created with it */
276  iRC = pthread_mutex_init(&(this->mutexMutex), NULL);
277 #endif
278  if ( iRC != 0 )
279  {
280  fprintf(stderr," *** ERROR: At RexxMutex(), pthread_mutex_init - RC = %d !\n", iRC);
281  }
282 
283  created = true;
284 }
285 
286 
288 {
289  if (created)
290  {
291  pthread_mutex_destroy(&(this->mutexMutex));
292  created = false;
293  }
294 }
SysMutex::create
void create()
Definition: SysSemaphore.cpp:238
SysSemaphore::reset
void reset()
Definition: SysSemaphore.cpp:213
SysSemaphore::SysSemaphore
SysSemaphore()
Definition: SysSemaphore.hpp:54
SysMutex::mutexMutex
pthread_mutex_t mutexMutex
Definition: SysSemaphore.hpp:86
SysSemaphore::created
bool created
Definition: SysSemaphore.hpp:70
SysSemaphore.hpp
SysMutex::close
void close()
Definition: SysSemaphore.cpp:287
SysSemaphore::semMutex
pthread_mutex_t semMutex
Definition: SysSemaphore.hpp:68
SysSemaphore::close
void close()
Definition: SysSemaphore.cpp:153
SysSemaphore::create
void create()
Definition: SysSemaphore.cpp:89
SysMutex::SysMutex
SysMutex()
Definition: SysSemaphore.hpp:75
SysMutex::created
bool created
Definition: SysSemaphore.hpp:87
SysSemaphore::postedCount
int postedCount
Definition: SysSemaphore.hpp:69
SysSemaphore::semCond
pthread_cond_t semCond
Definition: SysSemaphore.hpp:67
SysSemaphore::post
void post()
Definition: SysSemaphore.cpp:164
SysSemaphore::wait
void wait()
Definition: SysSemaphore.cpp:174