2 // This file is part of the aMule Project.
4 // Copyright (c) 2005-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2011 Marcelo Roberto Jimenez ( phoenix@amule.org )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "StateMachine.h"
28 #include <common/StringFunctions.h>
30 CStateMachine::CStateMachine(
32 const unsigned int maxStates
,
33 const t_sm_state initialState
)
35 m_stateMutex(wxMUTEX_RECURSIVE
),
36 m_queueMutex(wxMUTEX_RECURSIVE
),
38 m_maxStates(maxStates
)
40 m_state
= initialState
;
42 m_clocksInCurrentState
= 0;
45 CStateMachine::~CStateMachine()
49 void CStateMachine::Clock()
57 /* Process state change acording to event */
58 if (!m_queue
.empty()) {
59 event
= m_queue
.front();
65 /* State changes can only happen here */
66 wxMutexLocker
lock(m_stateMutex
);
67 m_state
= next_state( event
);
70 state_entry
= ( m_state
!= old_state
) || ( m_clockCounter
== 1 );
73 m_clocksInCurrentState
= 0;
74 // Uncomment here to debug the state machine.
75 // State changes will be printed to stdout.
76 //printf("%s(%04d): %d -> %d\n", (const char *)unicode2char(m_name), m_clockCounter, old_state, m_state);
78 ++m_clocksInCurrentState
;
80 /* Process new state entry */
81 if( m_state
< m_maxStates
)
83 /* It should be ok to call Clock() recursively inside this
84 * procedure because state change has already happened. Also
85 * the m_state mutex is recursive. */
86 process_state(m_state
, state_entry
);
90 /* In multithreaded implementations, this must be locked */
91 void CStateMachine::Schedule(t_sm_event event
)
93 wxMutexLocker
lock(m_queueMutex
);
97 void CStateMachine::flush_queue()
99 while (!m_queue
.empty()) {
103 // File_checked_for_headers