comparison engine/python/fife/extensions/fife_timer.py @ 606:a5c890f0e757

This is a temporary fix for the timer issue which causes random segfaults. Timers are no longer deleted prematurely. [t:483]
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 16 Sep 2010 16:46:17 +0000
parents c8820cc201db
children eab690c748a3
comparison
equal deleted inserted replaced
605:d242e6ce6f9f 606:a5c890f0e757
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 2
3 # #################################################################### 3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team 4 # Copyright (C) 2005-2010 by the FIFE team
5 # http://www.fifengine.de 5 # http://www.fifengine.net
6 # This file is part of FIFE. 6 # This file is part of FIFE.
7 # 7 #
8 # FIFE is free software; you can redistribute it and/or 8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public 9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either 10 # License as published by the Free Software Foundation; either
40 40
41 from fife import fife 41 from fife import fife
42 42
43 _manager = None 43 _manager = None
44 _alltimers = [] 44 _alltimers = []
45 _deadtimers = []
45 46
46 def init(timemanager): 47 def init(timemanager):
47 """ 48 """
48 Initialize timers. 49 Initialize timers.
49 50
64 @param delay: The delay in milliseconds to execute the callback 65 @param delay: The delay in milliseconds to execute the callback
65 @param callback: The function to execute 66 @param callback: The function to execute
66 @param repeat: The number of times to execute the callback. 1=once, 0=forever 67 @param repeat: The number of times to execute the callback. 1=once, 0=forever
67 """ 68 """
68 super(Timer,self).__init__(delay) 69 super(Timer,self).__init__(delay)
69 self._is_registered = False 70 self._active = False
70 self._callback = callback 71 self._callback = callback
71 self._manager = _manager 72 self._manager = _manager
72 self.setPeriod(delay) 73 self.setPeriod(delay)
73 self._repeat = repeat 74 self._repeat = repeat
74 self._executed = 0 75 self._executed = 0
75 76
76 def start(self): 77 def start(self):
77 """ 78 """
78 Must be called before the timer will start 79 Call this to start the timer.
80
81 This registers the timer with the time manager. The time manger then
82 calls the timers updateEvent() function when the delay time has passed.
79 """ 83 """
80 if self._is_registered: 84 if self._active:
81 return 85 return
82 self._is_registered = True 86 self._active = True
87
83 self._executed = 0 88 self._executed = 0
89
90 self.setLastUpdateTime(self._manager.getTime())
84 self._manager.registerEvent(self) 91 self._manager.registerEvent(self)
85 92
86 global _alltimers 93 global _alltimers
94 global _deadtimers
95
87 _alltimers.append(self) 96 _alltimers.append(self)
88 97
89 def stop(self): 98 def stop(self):
90 """ 99 """
91 Stops the timer 100 Stops the timer
101
102 This unregisters the timer from the time manager. The time manager does NOT
103 delete the timer so make sure you keep a reference to this timer and ensure
104 python doesnt delete the timer prematurely.
92 """ 105 """
93 if not self._is_registered: 106 if not self._active:
94 return 107 return
95 self._is_registered = False 108
109 self._active = False
96 self._manager.unregisterEvent(self) 110 self._manager.unregisterEvent(self)
97 111
112
113 #FIXME: See ticket #483 in trac. This is a temporary solution and needs
114 #to be reworked.
115
98 global _alltimers 116 global _alltimers
117 global _deadtimers
118
119 #clean up any dead timers
120 del _deadtimers[:]
121
122 #Ddd this timer to the dead timers list to be removed next time a timer
123 #is stopped.
124 _deadtimers.append(self)
125
126 #finally remove self from the global timer list
99 _alltimers.remove(self) 127 _alltimers.remove(self)
100 128
101 129
102 def updateEvent(self,delta): 130 def updateEvent(self,delta):
103 """ 131 """
104 Should not be called directly. This is called by FIFE. 132 This is called by FIFE::TimeManager when the delay has passed.
133
134 Should not be called directly.
105 """ 135 """
136
106 if self._repeat != 0: 137 if self._repeat != 0:
107 self._executed += 1 138 self._executed += 1
108 if self._executed >= self._repeat: 139 if self._executed >= self._repeat:
109 self.stop() 140 self.stop()
110 141
111 if callable(self._callback): 142 if callable(self._callback):
112 self._callback() 143 self._callback()
113 144
114 145
115 def delayCall(delay,callback): 146 def delayCall(delay,callback):
116 """ 147 """
117 Delay a function call by a number of milliseconds. 148 Delay a function call by a number of milliseconds.
118 149