OpenWalnut  1.5.0dev
WConditionSet.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <memory>
26 
27 #include "WConditionSet.h"
28 
30  WCondition(),
31  m_resetable( false ),
32  m_autoReset( false ),
33  m_fired( false ),
34  m_notifier( boost::bind( &WConditionSet::conditionFired, this ) )
35 {
36 }
37 
39 {
40  // get write lock
41  std::unique_lock<std::shared_mutex> lock = std::unique_lock<std::shared_mutex>( m_conditionSetLock );
42 
43  // clean conditions list
44  // NOTE: we need to disconnect here.
45  for( ConditionConnectionMap::iterator it = m_conditionSet.begin(); it != m_conditionSet.end(); ++it )
46  {
47  ( *it ).second.disconnect();
48  }
49 
50  m_conditionSet.clear();
51  lock.unlock();
52 }
53 
54 void WConditionSet::add( std::shared_ptr< WCondition > condition )
55 {
56  // get write lock
57  std::unique_lock<std::shared_mutex> lock = std::unique_lock<std::shared_mutex>( m_conditionSetLock );
58 
59  if( !m_conditionSet.count( condition ) )
60  {
61  // create a new pair, the condition and its connection object.
62  // this is needed since remove needs the connection to disconnect the notifier again
63  m_conditionSet.insert( ConditionConnectionPair( condition, condition->subscribeSignal( m_notifier ) ) );
64  }
65 
66  lock.unlock();
67 }
68 
69 void WConditionSet::remove( std::shared_ptr< WCondition > condition )
70 {
71  // get write lock
72  std::unique_lock<std::shared_mutex> lock = std::unique_lock<std::shared_mutex>( m_conditionSetLock );
73 
74  // get the element
75  ConditionConnectionMap::iterator it = m_conditionSet.find( condition );
76  if( it != m_conditionSet.end() )
77  {
78  ( *it ).second.disconnect();
79  m_conditionSet.erase( it );
80  }
81 
82  lock.unlock();
83 }
84 
86 {
87  notify();
88 }
89 
91 {
92  m_fired = true;
94 }
95 
96 void WConditionSet::wait() const
97 {
98  if( !m_resetable || !m_fired )
99  {
101  }
102 
103  if( m_autoReset )
104  {
105  reset();
106  }
107 }
108 
110 {
111  m_fired = false;
112 }
113 
114 void WConditionSet::setResetable( bool resetable, bool autoReset )
115 {
116  m_autoReset = autoReset;
117  m_resetable = resetable;
118 }
119 
121 {
122  return m_resetable;
123 }
124 
Class allowing multiple conditions to be used for one waiting cycle.
Definition: WConditionSet.h:44
virtual void wait() const
Wait for the condition.
ConditionConnectionMap m_conditionSet
Set of conditions to be waited for.
bool m_fired
Flag denoting whether one condition fired in the past.
virtual ~WConditionSet()
Destructor.
virtual void reset() const
Resets the internal fire state.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
virtual void remove(std::shared_ptr< WCondition > condition)
Removes the specified condition.
bool m_resetable
Flag denoting whether the condition set should act like a one shot condition.
virtual void add(std::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
bool isResetable()
Returns whether the condition set acts like a one shot condition.
WCondition::t_ConditionNotifierType m_notifier
The notifier which gets called by all conditions if they fire.
bool m_autoReset
Flag which shows whether the wait() call should reset the state m_fired when it returns.
virtual void conditionFired()
Notifier function getting notified whenever a condition got fired.
std::shared_mutex m_conditionSetLock
Lock used for thread-safe writing to the condition set.
virtual void notify()
Notifies all waiting threads.
WConditionSet()
Default constructor.
std::pair< std::shared_ptr< WCondition >, boost::signals2::connection > ConditionConnectionPair
Each condition has a connection.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
virtual void notify()
Notifies all waiting threads.
Definition: WCondition.cpp:44
virtual void wait() const
Wait for the condition.
Definition: WCondition.cpp:37