OpenWalnut  1.4.0
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 "WConditionSet.h"
26 
28  WCondition(),
29  m_resetable( false ),
30  m_autoReset( false ),
31  m_fired( false ),
32  m_notifier( boost::bind( &WConditionSet::conditionFired, this ) )
33 {
34 }
35 
37 {
38  // get write lock
39  boost::unique_lock<boost::shared_mutex> lock = boost::unique_lock<boost::shared_mutex>( m_conditionSetLock );
40 
41  // clean conditions list
42  // NOTE: we need to disconnect here.
43  for( ConditionConnectionMap::iterator it = m_conditionSet.begin(); it != m_conditionSet.end(); ++it )
44  {
45  ( *it ).second.disconnect();
46  }
47 
48  m_conditionSet.clear();
49  lock.unlock();
50 }
51 
52 void WConditionSet::add( boost::shared_ptr< WCondition > condition )
53 {
54  // get write lock
55  boost::unique_lock<boost::shared_mutex> lock = boost::unique_lock<boost::shared_mutex>( m_conditionSetLock );
56 
57  if( !m_conditionSet.count( condition ) )
58  {
59  // create a new pair, the condition and its connection object.
60  // this is needed since remove needs the connection to disconnect the notifier again
61  m_conditionSet.insert( ConditionConnectionPair( condition, condition->subscribeSignal( m_notifier ) ) );
62  }
63 
64  lock.unlock();
65 }
66 
67 void WConditionSet::remove( boost::shared_ptr< WCondition > condition )
68 {
69  // get write lock
70  boost::unique_lock<boost::shared_mutex> lock = boost::unique_lock<boost::shared_mutex>( m_conditionSetLock );
71 
72  // get the element
73  ConditionConnectionMap::iterator it = m_conditionSet.find( condition );
74  if( it != m_conditionSet.end() )
75  {
76  ( *it ).second.disconnect();
77  m_conditionSet.erase( it );
78  }
79 
80  lock.unlock();
81 }
82 
84 {
85  m_fired = true;
86  notify();
87 }
88 
89 void WConditionSet::wait() const
90 {
91  if( !m_resetable || !m_fired )
92  {
94  }
95 
96  if( m_autoReset )
97  {
98  reset();
99  }
100 }
101 
103 {
104  m_fired = false;
105 }
106 
107 void WConditionSet::setResetable( bool resetable, bool autoReset )
108 {
109  m_autoReset = autoReset;
110  m_resetable = resetable;
111 }
112 
114 {
115  return m_resetable;
116 }
117 
WConditionSet()
Default constructor.
virtual void remove(boost::shared_ptr< WCondition > condition)
Removes the specified condition.
virtual void wait() const
Wait for the condition.
bool m_autoReset
Flag which shows whether the wait() call should reset the state m_fired when it returns.
virtual void add(boost::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
virtual ~WConditionSet()
Destructor.
std::pair< boost::shared_ptr< WCondition >, boost::signals2::connection > ConditionConnectionPair
Each condition has a connection.
virtual void reset() const
Resets the internal fire state.
boost::shared_mutex m_conditionSetLock
Lock used for thread-safe writing to the condition set.
Class allowing multiple conditions to be used for one waiting cycle.
Definition: WConditionSet.h:42
virtual void conditionFired()
Notifier function getting notified whenever a condition got fired.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
bool m_fired
Flag denoting whether one condition fired in the past.
bool isResetable()
Returns whether the condition set acts like a one shot condition.
ConditionConnectionMap m_conditionSet
Set of conditions to be waited for.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:39
virtual void notify()
Notifies all waiting threads.
Definition: WCondition.cpp:44
bool m_resetable
Flag denoting whether the condition set should act like a one shot condition.
virtual void wait() const
Wait for the condition.
Definition: WCondition.cpp:37
WCondition::t_ConditionNotifierType m_notifier
The notifier which gets called by all conditions if they fire.