STXXL  1.4.0
 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 
22 
24 
25 template <typename ValueType = int>
26 class state : private noncopyable
27 {
28  typedef ValueType value_type;
29 
30  //! mutex for condition variable
32 
33  //! condition variable
35 
36  //! current state
38 
39 public:
40  state(const value_type& s)
41  : m_state(s)
42  { }
43 
44  void set_to(const value_type& new_state)
45  {
46  scoped_mutex_lock lock(m_mutex);
47  m_state = new_state;
48  lock.unlock();
49  m_cond.notify_all();
50  }
51 
52  void wait_for(const value_type& needed_state)
53  {
54  scoped_mutex_lock lock(m_mutex);
55  while (needed_state != m_state)
56  m_cond.wait(lock);
57  }
58 
59  value_type operator () ()
60  {
61  scoped_mutex_lock lock(m_mutex);
62  return m_state;
63  }
64 };
65 
67 
68 #endif // !STXXL_COMMON_STATE_HEADER
state(const value_type &s)
Definition: state.h:40
void set_to(const value_type &new_state)
Definition: state.h:44
void unlock()
unlock mutex hold prematurely
Definition: mutex.h:127
value_type m_state
current state
Definition: state.h:37
ValueType value_type
Definition: state.h:28
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:106
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void wait_for(const value_type &needed_state)
Definition: state.h:52
mutex m_mutex
mutex for condition variable
Definition: state.h:31
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
condition_variable m_cond
condition variable
Definition: state.h:34