CPN
Computational Process Networks
PthreadCondition.cc
Go to the documentation of this file.
1 //=============================================================================
2 // PthreadCondition class
3 //-----------------------------------------------------------------------------
4 // POSIX Pthread class library
5 // Copyright (C) 1997-1999 The University of Texas
6 //
7 // This library is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU Library General Public License as published
9 // by the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
16 //
17 // The GNU Public License is available in the file LICENSE, or you
18 // can write to the Free Software Foundation, Inc., 59 Temple Place -
19 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
20 // World Wide Web at http://www.fsf.org.
21 //=============================================================================
22 
23 
26 #include <sys/time.h>
27 #include <errno.h>
28 #ifdef _POSIX_THREADS
29 
30 
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
34 //: theCond(PTHREAD_COND_INITIALIZER)
35 {
36  TrapError(pthread_cond_init(&theCond, 0));
37 }
38 
39 
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 {
44  // It should simply be:
45 // TrapError(pthread_cond_init(&theCond, cAttr));
46 
47  // This is necessary because
48  // SGI's pthread_cond_init is not const correct!!!
49 
50  pthread_condattr_t* myAttr = (PthreadConditionAttr&)cAttr;
51  TrapError(pthread_cond_init(&theCond, myAttr));
52 }
53 
54 
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 {
59  try {
60  TrapError(pthread_cond_destroy(&theCond));
61  } catch (...) {
62  std::terminate();
63  }
64 }
65 
66 
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
70 { TrapError(pthread_cond_signal(&theCond));
71  return *this;
72 }
73 
74 
75 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
78 { TrapError(pthread_cond_broadcast(&theCond));
79  return *this;
80 }
81 
82 
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
86 { TrapError(pthread_cond_wait(&theCond, mutex));
87  return *this;
88 }
89 
90 //-----------------------------------------------------------------------------
91 int PthreadCondition::TimedWait(PthreadMutex& mutex, const timespec* abstime)
92 //-----------------------------------------------------------------------------
93 {
94  int err = pthread_cond_timedwait(&theCond, mutex, abstime);
95  if (err != 0) {
96  if (err == ETIMEDOUT) { return true; }
97  TrapError(err);
98  }
99  return false;
100 }
101 
102 //-----------------------------------------------------------------------------
103 int PthreadCondition::TimedWait(PthreadMutex& mutex, double reltime)
104 //-----------------------------------------------------------------------------
105 {
106  timespec ts;
107  timeval tv;
108  int ret = gettimeofday(&tv, 0);
109  if (ret != 0) throw ErrnoException();
110  ts.tv_sec = tv.tv_sec;
111  ts.tv_nsec = tv.tv_usec * 1000;
112  unsigned sec = (unsigned)reltime;
113  if (sec > 0) {
114  ts.tv_sec += sec;
115  unsigned nsec = (unsigned)((reltime - sec)*1e9);
116  if (nsec > 0) ts.tv_nsec += nsec;
117  }
118  int err = pthread_cond_timedwait(&theCond, mutex, &ts);
119  if (err != 0) {
120  if (err == ETIMEDOUT) { return true; }
121  TrapError(err);
122  }
123  return false;
124 }
125 
126 
127 
128 #endif
PthreadCondition & Wait(PthreadMutex &mutex)
PthreadCondition & Broadcast(void)
pthread_cond_t theCond
int TimedWait(PthreadMutex &mutex, const timespec *abstime)
void TrapError(int result)
PthreadCondition & Signal(void)