GCS  0.2.3
gu_cond.hpp
1 /*
2  * Copyright (C) 2009 Codership Oy <info@codership.com>
3  */
4 
5 #ifndef __GU_COND__
6 #define __GU_COND__
7 
8 #include <pthread.h>
9 #include <unistd.h>
10 #include <cerrno>
11 
12 #include "gu_macros.h"
13 #include "gu_exception.hpp"
14 
15 // TODO: make exceptions more verbose
16 
17 namespace gu
18 {
19  class Cond
20  {
21 
22  friend class Lock;
23  // non-copyable
24  Cond(const Cond&);
25  void operator=(const Cond&);
26  protected:
27 
28  pthread_cond_t mutable cond;
29  long mutable ref_count;
30 
31  public:
32 
33  Cond () : cond(), ref_count(0)
34  {
35  pthread_cond_init (&cond, NULL);
36  }
37 
38  ~Cond ()
39  {
40  int ret;
41  while (EBUSY == (ret = pthread_cond_destroy(&cond)))
42  { usleep (100); }
43  if (gu_unlikely(ret != 0))
44  {
45  log_fatal << "pthread_cond_destroy() failed: " << ret
46  << " (" << strerror(ret) << ". Aborting.";
47  ::abort();
48  }
49  }
50 
51  inline void signal () const
52  {
53  if (ref_count > 0) {
54  int ret = pthread_cond_signal (&cond);
55  if (gu_unlikely(ret != 0))
56  throw Exception("pthread_cond_signal() failed", ret);
57  }
58  }
59 
60  inline void broadcast () const
61  {
62  if (ref_count > 0) {
63  int ret = pthread_cond_broadcast (&cond);
64  if (gu_unlikely(ret != 0))
65  throw Exception("pthread_cond_broadcast() failed", ret);
66  }
67  }
68 
69  };
70 }
71 
72 #endif // __GU_COND__
Definition: gu_lock.hpp:20
Definition: gu_cond.hpp:19
Definition: gu_exception.hpp:19