OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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