STXXL  1.4-dev
 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 
27 
28 class onoff_switch : private noncopyable
29 {
30  //! mutex for condition variable
32 
33  //! condition variable
35 
36  //! the switch's state
37  bool m_on;
38 
39 public:
40  //! construct switch
41  onoff_switch(bool flag = false)
42  : m_on(flag)
43  { }
44  //! turn switch ON and notify one waiter
45  void on()
46  {
47  scoped_mutex_lock lock(m_mutex);
48  m_on = true;
49  lock.unlock();
50  m_cond.notify_one();
51  }
52  //! turn switch OFF and notify one waiter
53  void off()
54  {
55  scoped_mutex_lock lock(m_mutex);
56  m_on = false;
57  lock.unlock();
58  m_cond.notify_one();
59  }
60  //! wait for switch to turn ON
61  void wait_for_on()
62  {
63  scoped_mutex_lock lock(m_mutex);
64  if (!m_on)
65  m_cond.wait(lock);
66  }
67  //! wait for switch to turn OFF
68  void wait_for_off()
69  {
70  scoped_mutex_lock lock(m_mutex);
71  if (m_on)
72  m_cond.wait(lock);
73  }
74  //! return true if switch is ON
75  bool is_on()
76  {
77  scoped_mutex_lock lock(m_mutex);
78  return m_on;
79  }
80 };
81 
83 
84 #endif // !STXXL_COMMON_ONOFF_SWITCH_HEADER
onoff_switch(bool flag=false)
construct switch
Definition: onoff_switch.h:41
void off()
turn switch OFF and notify one waiter
Definition: onoff_switch.h:53
void unlock()
unlock mutex hold prematurely
Definition: mutex.h:144
void wait_for_off()
wait for switch to turn OFF
Definition: onoff_switch.h:68
condition_variable m_cond
condition variable
Definition: onoff_switch.h:34
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:123
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
mutex m_mutex
mutex for condition variable
Definition: onoff_switch.h:31
bool is_on()
return true if switch is ON
Definition: onoff_switch.h:75
void on()
turn switch ON and notify one waiter
Definition: onoff_switch.h:45
void wait_for_on()
wait for switch to turn ON
Definition: onoff_switch.h:61
bool m_on
the switch&#39;s state
Definition: onoff_switch.h:37
#define STXXL_END_NAMESPACE
Definition: namespace.h:17