CPN
Computational Process Networks
StatusHandler.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
25 #ifndef SYNC_STATUSHANDLER_H
26 #define SYNC_STATUSHANDLER_H
27 #pragma once
28 #include <cpn/common.h>
31 
32 namespace Sync {
33 
34  namespace Internal {
35  class ScopeMutex {
36  public:
37  ScopeMutex(pthread_mutex_t &l) : lock(l) { ENSURE_ABORT(!pthread_mutex_lock(&lock)); }
38  ~ScopeMutex() { ENSURE_ABORT(!pthread_mutex_unlock(&lock)); }
39  pthread_mutex_t &lock;
40  };
41  }
42 
57  template<class Status_t>
58  class StatusHandler {
59  public:
60  StatusHandler(Status_t initialStatus)
61  : status(initialStatus) {
62  ENSURE(!pthread_mutex_init(&lock, 0));
63  ENSURE(!pthread_cond_init(&cond, 0));
64  }
65 
67  ENSURE_ABORT(!pthread_cond_destroy(&cond));
68  ENSURE_ABORT(!pthread_mutex_destroy(&lock));
69  }
70 
75  void Post(Status_t newStatus) {
77  status = newStatus;
78  pthread_cond_broadcast(&cond);
79  }
80 
84  Status_t Get() const {
86  return status;
87  }
88 
96  bool CompareAndPost(Status_t oldStatus, Status_t newStatus) {
98  if (oldStatus == status) {
99  status = newStatus;
100  pthread_cond_broadcast(&cond);
101  return true;
102  }
103  return false;
104  }
105 
106  template <class Comparator>
107  bool CompareAndPost(Status_t oldStatus, Status_t newStatus, Comparator comp) {
109  if (comp(oldStatus, status)) {
110  status = newStatus;
111  pthread_cond_broadcast(&cond);
112  return true;
113  }
114  return false;
115  }
116 
124  Status_t CompareAndWait(Status_t oldStatus) const {
126  while (oldStatus == status) { pthread_cond_wait(&cond, &lock); }
127  return status;
128  }
129 
130  template<class Comparator>
131  Status_t CompareAndWait(Status_t oldStatus, Comparator comp) const {
133  while (comp(oldStatus, status)) { pthread_cond_wait(&cond, &lock); }
134  return status;
135  }
136 
145  Status_t ComparePostAndWait(Status_t oldStatus, Status_t newStatus) {
147  if (oldStatus == status) {
148  status = newStatus;
149  pthread_cond_broadcast(&cond);
150  while (status == newStatus) { pthread_cond_wait(&cond, &lock); }
151  }
152  return status;
153  }
154 
155  template<class Comparator>
156  Status_t ComparePostAndWait(Status_t oldStatus, Status_t newStatus, Comparator comp) {
158  if (comp(oldStatus, status)) {
159  status = newStatus;
160  pthread_cond_broadcast(&cond);
161  while (newStatus == status) { pthread_cond_wait(&cond, &lock); }
162  }
163  return status;
164  }
165 
169  void CompareWaitAndPost(Status_t theStatus, Status_t newStatus) {
171  while (theStatus != status) { pthread_cond_wait(&cond, &lock); }
172  status = newStatus;
173  pthread_cond_broadcast(&cond);
174  }
175 
176  private:
177  Status_t status;
178  mutable pthread_mutex_t lock;
179  mutable pthread_cond_t cond;
180  };
181 }
182 #endif
183 
#define ENSURE_ABORT(exp,...)
ScopeMutex(pthread_mutex_t &l)
Definition: StatusHandler.h:37
pthread_cond_t cond
Status_t Get() const
Definition: StatusHandler.h:84
Status_t CompareAndWait(Status_t oldStatus) const
Status_t ComparePostAndWait(Status_t oldStatus, Status_t newStatus)
void CompareWaitAndPost(Status_t theStatus, Status_t newStatus)
bool CompareAndPost(Status_t oldStatus, Status_t newStatus, Comparator comp)
#define ENSURE(exp,...)
StatusHandler(Status_t initialStatus)
Definition: StatusHandler.h:60
Status_t ComparePostAndWait(Status_t oldStatus, Status_t newStatus, Comparator comp)
bool CompareAndPost(Status_t oldStatus, Status_t newStatus)
Definition: StatusHandler.h:96
Status_t CompareAndWait(Status_t oldStatus, Comparator comp) const
pthread_mutex_t & lock
Definition: StatusHandler.h:39
pthread_mutex_t lock
A reentrant lock implementation.
void Post(Status_t newStatus)
Definition: StatusHandler.h:75