Author: dmeyer
Date: Tue Apr 10 14:26:16 2007
New Revision: 9430
Modified:
trunk/core/src/resources.py
trunk/ui/src/application/base.py
trunk/ui/src/audio/player.py
trunk/ui/src/games/player.py
trunk/ui/src/video/player.py
Log:
merge suspend_all and resume_all into get and free resources functions.
Modified: trunk/core/src/resources.py
==============================================================================
--- trunk/core/src/resources.py (original)
+++ trunk/core/src/resources.py Tue Apr 10 14:26:16 2007
@@ -41,61 +41,74 @@
__suspended = []
__resources = {}
- def get_resources(self, *resources):
+ @yield_execution(lock=True)
+ def get_resources(self, *resources, **kwargs):
"""
Reserve a list of resources. If one or more resources can not be
reserved, the whole operation fails. The function will return the
list of failed resources with this instance having this resource.
+ If the keyword force=True is set, the function will try to suspend
+ all modules holding a needed ressource. In this case the function
+ returns False on failure (not possible), True when all resources
+ are reserved or InProgress if it is possible and takes a while.
"""
blocked = {}
+ blocking = []
for r in resources:
- if r in ResourceHandler.__resources:
- if ResourceHandler.__resources[r].can_suspend():
- blocked[r] = ResourceHandler.__resources[r]
- else:
- return False
- if blocked:
- # failed to reserve
- return blocked
- # reserve all resources
- for r in resources:
- ResourceHandler.__resources[r] = self
- return {}
-
-
- def free_resources(self):
- """
- Free all resources blocked by this instance.
- """
- for res, app in ResourceHandler.__resources.items()[:]:
- if app == self:
- del ResourceHandler.__resources[res]
-
+ module = ResourceHandler.__resources.get(r)
+ if module is None or module == self:
+ continue
+ if module.can_suspend():
+ blocked[r] = module
+ if not module in blocking:
+ blocking.append(module)
+ else:
+ # TODO: return bad ressources so the app could
+ # try to reserve resources without that bad one.
+ yield False
+
+ if blocked and not kwargs.get('force'):
+ # failed to reserve but it would be possible, return list
+ # of blocking resources
+ yield blocked
- @yield_execution()
- def suspend_all(self, blocked):
- """
- Suspend all handler that block a resource. Can return an InProgress
- object if this takes a while.
- """
- # create list of modules to suspend
- modules = []
- for m in blocked.values():
- if not m in modules:
- modules.append(m)
- # suspend modules
- for module in modules:
+ # suspend modules (if any)
+ for module in blocking:
if not module in self.__suspended:
self.__suspended.append(module)
answer = module.suspend()
if isinstance(answer, InProgress):
yield answer
+ # since get_resources is decorated with the locking
+ # keyword, no other application or plugin could have
+ # gotten the ressources we need and we are free to
+ # take them.
+
+ # FIXME: what happens if a resumed application will
+ # try to get the ressources and can't do that? We
+ # need a way for application/plugin x to connect to
+ # the __suspended list of y.
+
+ # reserve all resources
+ for r in resources:
+ ResourceHandler.__resources[r] = self
+ if kwargs.get('force'):
+ yield True
+ yield {}
- def resume_all(self):
+
+ def free_resources(self, resume=False):
"""
- Resume all handler suspended by this handler. Clear list at the end.
+ Free all resources blocked by this instance. If resume is True, this
+ function will also resume all modules this object has suspended.
"""
+ for res, app in ResourceHandler.__resources.items()[:]:
+ if app == self:
+ del ResourceHandler.__resources[res]
+ if not resume:
+ # done here
+ return True
# resume suspended modules
for handler in self.__suspended:
handler.resume()
@@ -122,4 +135,3 @@
Resume the plugin acquire all the resources again.
"""
pass
-
Modified: trunk/ui/src/application/base.py
==============================================================================
--- trunk/ui/src/application/base.py (original)
+++ trunk/ui/src/application/base.py Tue Apr 10 14:26:16 2007
@@ -104,7 +104,7 @@
self.signals['start'].emit()
elif status == STATUS_IDLE:
handler.hide_application(self)
- self.resume_all()
+ self.free_resources(resume=True)
self._status = status
self.signals['stop'].emit()
else:
Modified: trunk/ui/src/audio/player.py
==============================================================================
--- trunk/ui/src/audio/player.py (original)
+++ trunk/ui/src/audio/player.py Tue Apr 10 14:26:16 2007
@@ -84,16 +84,12 @@
# Try to get AUDIO resource. The ressouce will be freed by the system
# when the application switches to STATUS_STOPPED or STATUS_IDLE.
- blocked = self.get_resources('AUDIO')
+ blocked = self.get_resources('AUDIO', force=True)
if blocked == False:
log.error("Can't get Audio resource.")
return False
- if len(blocked) != 0:
- status = self.suspend_all(blocked)
- if isinstance(status, kaa.notifier.InProgress):
- status.connect(retry)
- return True
- retry()
+ if isinstance(blocked, kaa.notifier.InProgress):
+ blocked.connect(retry)
return True
# Store item and playlist. We need to keep the playlist object
@@ -237,8 +233,8 @@
# FIXME: what to do in this case?
log.error('unable to get AUDIO ressource')
yield False
- elif len(blocked) > 0:
- yield self.suspend_all(blocked)
+ if isinstance(blocked, kaa.notifier.InProgress):
+ yield blocked
self.player.resume()
yield kaa.notifier.YieldCallback(self.player.signals['play'])
Modified: trunk/ui/src/games/player.py
==============================================================================
--- trunk/ui/src/games/player.py (original)
+++ trunk/ui/src/games/player.py Tue Apr 10 14:26:16 2007
@@ -84,16 +84,12 @@
# Try to get VIDEO and AUDIO resources. The ressouces will be freed
# by the system when the application switches to STATUS_STOPPED or
# STATUS_IDLE.
- blocked = self.get_resources('AUDIO', 'VIDEO', 'JOYSTICK')
+ blocked = self.get_resources('AUDIO', 'VIDEO', 'JOYSTICK', force=True)
if blocked == False:
log.error("Can't get resource AUDIO, VIDEO, JOYSTICK")
return False
- if len(blocked) > 0:
- status = self.suspend_all(blocked)
- if isinstance(status, kaa.notifier.InProgress):
- status.connect(retry)
- return True
- retry()
+ if isinstance(blocked, kaa.notifier.InProgress):
+ blocked.connect(retry)
return True
# store item
Modified: trunk/ui/src/video/player.py
==============================================================================
--- trunk/ui/src/video/player.py (original)
+++ trunk/ui/src/video/player.py Tue Apr 10 14:26:16 2007
@@ -87,16 +87,12 @@
# Try to get VIDEO and AUDIO resources. The ressouces will be freed
# by the system when the application switches to STATUS_STOPPED or
# STATUS_IDLE.
- blocked = self.get_resources('AUDIO', 'VIDEO')
+ blocked = self.get_resources('AUDIO', 'VIDEO', force=True)
if blocked == False:
log.error("Can't get resource AUDIO, VIDEO")
return False
- if len(blocked) > 0:
- status = self.suspend_all(blocked)
- if isinstance(status, kaa.notifier.InProgress):
- status.connect(retry)
- return True
- retry()
+ if isinstance(blocked, kaa.notifier.InProgress):
+ blocked.connect(retry)
return True
# store item and playlist
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog