STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
onoff_switch.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/onoff_switch.h
3  *
4  * Kind of binary semaphore: initially OFF, then multiple waiters can attach
5  * to the switch, which get notified one-by-one when switched ON.
6  *
7  * Part of the STXXL. See http://stxxl.sourceforge.net
8  *
9  * Copyright (C) 2002 Roman Dementiev <[email protected]>
10  * Copyright (C) 2013 Timo Bingmann <[email protected]>
11  *
12  * Distributed under the Boost Software License, Version 1.0.
13  * (See accompanying file LICENSE_1_0.txt or copy at
14  * http://www.boost.org/LICENSE_1_0.txt)
15  **************************************************************************/
16 
17 #ifndef STXXL_COMMON_ONOFF_SWITCH_HEADER
18 #define STXXL_COMMON_ONOFF_SWITCH_HEADER
19 
20 #include <stxxl/bits/config.h>
21 
22 #include <stxxl/bits/noncopyable.h>
25 
26 
28 
29 class onoff_switch : private noncopyable
30 {
31  //! mutex for condition variable
33 
34  //! condition variable
36 
37  //! the switch's state
38  bool m_on;
39 
40 public:
41  //! construct switch
42  onoff_switch(bool flag = false)
43  : m_on(flag)
44  { }
45  //! turn switch ON and notify one waiter
46  void on()
47  {
48  scoped_mutex_lock lock(m_mutex);
49  m_on = true;
50  lock.unlock();
51  m_cond.notify_one();
52  }
53  //! turn switch OFF and notify one waiter
54  void off()
55  {
56  scoped_mutex_lock lock(m_mutex);
57  m_on = false;
58  lock.unlock();
59  m_cond.notify_one();
60  }
61  //! wait for switch to turn ON
62  void wait_for_on()
63  {
64  scoped_mutex_lock lock(m_mutex);
65  if (!m_on)
66  m_cond.wait(lock);
67  }
68  //! wait for switch to turn OFF
69  void wait_for_off()
70  {
71  scoped_mutex_lock lock(m_mutex);
72  if (m_on)
73  m_cond.wait(lock);
74  }
75  //! return true if switch is ON
76  bool is_on()
77  {
78  scoped_mutex_lock lock(m_mutex);
79  return m_on;
80  }
81 };
82 
84 
85 #endif // !STXXL_COMMON_ONOFF_SWITCH_HEADER
onoff_switch(bool flag=false)
construct switch
Definition: onoff_switch.h:42
void off()
turn switch OFF and notify one waiter
Definition: onoff_switch.h:54
void unlock()
unlock mutex hold prematurely
Definition: mutex.h:127
void wait_for_off()
wait for switch to turn OFF
Definition: onoff_switch.h:69
condition_variable m_cond
condition variable
Definition: onoff_switch.h:35
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:106
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
mutex m_mutex
mutex for condition variable
Definition: onoff_switch.h:32
bool is_on()
return true if switch is ON
Definition: onoff_switch.h:76
void on()
turn switch ON and notify one waiter
Definition: onoff_switch.h:46
void wait_for_on()
wait for switch to turn ON
Definition: onoff_switch.h:62
bool m_on
the switch&#39;s state
Definition: onoff_switch.h:38
#define STXXL_END_NAMESPACE
Definition: namespace.h:17