STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
state.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/state.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2013 Timo Bingmann <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_COMMON_STATE_HEADER
16 #define STXXL_COMMON_STATE_HEADER
17 
18 #include <stxxl/bits/noncopyable.h>
21 
23 
24 template <typename ValueType = int>
25 class state : private noncopyable
26 {
27  typedef ValueType value_type;
28 
29  //! mutex for condition variable
31 
32  //! condition variable
34 
35  //! current state
37 
38 public:
39  state(const value_type& s)
40  : m_state(s)
41  { }
42 
43  void set_to(const value_type& new_state)
44  {
45  scoped_mutex_lock lock(m_mutex);
46  m_state = new_state;
47  lock.unlock();
48  m_cond.notify_all();
49  }
50 
51  void wait_for(const value_type& needed_state)
52  {
53  scoped_mutex_lock lock(m_mutex);
54  while (needed_state != m_state)
55  m_cond.wait(lock);
56  }
57 
58  value_type operator () ()
59  {
60  scoped_mutex_lock lock(m_mutex);
61  return m_state;
62  }
63 };
64 
66 
67 #endif // !STXXL_COMMON_STATE_HEADER
state(const value_type &s)
Definition: state.h:39
void set_to(const value_type &new_state)
Definition: state.h:43
void unlock()
unlock mutex hold prematurely
Definition: mutex.h:126
value_type m_state
current state
Definition: state.h:36
ValueType value_type
Definition: state.h:27
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:105
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void wait_for(const value_type &needed_state)
Definition: state.h:51
mutex m_mutex
mutex for condition variable
Definition: state.h:30
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
condition_variable m_cond
condition variable
Definition: state.h:33