Zero  0.1.0
condex.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-kits -- Benchmark implementations for Shore-MT
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
24 #ifndef __CONDEX_H
25 #define __CONDEX_H
26 
27 #include <cstdlib>
28 
29 /********************************************************************
30  *
31  * @struct: condex
32  *
33  * @brief: Struct of a cond var with a flag indicating if it was fired
34  *
35  ********************************************************************/
36 
37 struct condex {
38  pthread_cond_t _cond;
39 
40  pthread_mutex_t _lock;
41 
42  long _signals;
43 
44  long _waits;
45 
46  condex() : _signals(0),
47  _waits(0) {
48  if (pthread_cond_init(&_cond, nullptr)) {
49  assert (0); // failed to init cond var
50  }
51  if (pthread_mutex_init(&_lock, nullptr)) {
52  assert (0); // failed to init mutex
53  }
54  }
55 
56  ~condex() {
57  pthread_cond_destroy(&_cond);
58  pthread_mutex_destroy(&_lock);
59  }
60 
61  void signal() {
62  CRITICAL_SECTION(cs, _lock);
63  _signals++;
64  pthread_cond_signal(&_cond);
65  }
66 
67  void wait() {
68  CRITICAL_SECTION(cs, _lock);
69  _waits++;
70  while (_waits > _signals) {
71  pthread_cond_wait(&_cond, &_lock);
72  }
73  }
74 }; // EOF: condex
75 
76 
77 
78 /********************************************************************
79  *
80  * @struct: condex_pair
81  *
82  * @brief: Struct of two condexes with a ready flag and an pointer to
83  * the active condex
84  *
85  ********************************************************************/
86 
87 struct condex_pair {
88  condex _wait[2];
89 
90  long _requested;
91 
92  long _wanted;
93 
94  long _waited;
95 
96  condex_pair() : _requested(0),
97  _wanted(0),
98  _waited(0) {}
99 
101 
103  ++_wanted;
104  }
105 
107  if (_wanted > _requested) {
108  return &_wait[_requested++ % 2];
109  }
110  return nullptr;
111  }
112 
113  void wait() {
114  w_assert1(_waited < _requested);
115  _wait[_waited++ % 2].wait();
116  }
117 }; // EOF: condex_pair
118 
119 
120 // my_condex = {
121 // {
122 // {PTHREAD_COND_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, false},
123 // {PTHREAD_COND_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, false},
124 // },
125 // 0, false
126 // };
127 
128 
129 
130 #endif // __CONDEX_H
#define w_assert1(x)
Level 1 should not add significant extra time.
Definition: w_base.h:198
long _waited
Definition: condex.h:94
void wait()
Definition: condex.h:113
long _requested
Definition: condex.h:90
condex * take_one()
Definition: condex.h:106
~condex_pair()
Definition: condex.h:100
pthread_mutex_t _lock
Definition: condex.h:40
long _signals
Definition: condex.h:42
void wait()
Definition: condex.h:67
pthread_cond_t _cond
Definition: condex.h:38
Definition: condex.h:87
void signal()
Definition: condex.h:61
condex_pair()
Definition: condex.h:96
condex()
Definition: condex.h:46
Definition: condex.h:37
long _waits
Definition: condex.h:44
~condex()
Definition: condex.h:56
long _wanted
Definition: condex.h:92
void please_take_one()
Definition: condex.h:102
#define CRITICAL_SECTION(name, lock)
Definition: critical_section.h:75