Move idle wrappers to baseclass

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2011-04-18 11:12:36 -04:00
parent b7de618897
commit de8c901d7b
13 changed files with 50 additions and 54 deletions

View File

@ -390,7 +390,7 @@ class vmmAddHardware(vmmGObjectUI):
label_widget = self.window.get_widget("phys-hd-label")
label_widget.set_markup("")
if not self.host_storage_timer:
self.host_storage_timer = util.safe_timeout_add(3 * 1000,
self.host_storage_timer = self.safe_timeout_add(3 * 1000,
uihelpers.host_space_tick,
self.conn,
label_widget)

View File

@ -27,7 +27,6 @@ import gobject
import libvirt
from virtManager import util
from virtManager.baseclass import vmmGObjectUI
# This thin wrapper only exists so we can put debugging
@ -136,7 +135,7 @@ class vmmAsyncJob(vmmGObjectUI):
self.topwin.set_title(title)
def run(self):
timer = util.safe_timeout_add(100, self.exit_if_necessary)
timer = self.safe_timeout_add(100, self.exit_if_necessary)
if self.show_progress:
self.topwin.present()

View File

@ -27,6 +27,13 @@ import gobject
import virtManager.config
def _safe_wrapper(func, *args):
gtk.gdk.threads_enter()
try:
return func(*args)
finally:
gtk.gdk.threads_leave()
class vmmGObject(gobject.GObject):
@staticmethod
@ -122,6 +129,28 @@ class vmmGObject(gobject.GObject):
return conn_id
def idle_emit(self, signal, *args):
"""
Safe wrapper for using 'self.emit' with gobject.idle_add
"""
def emitwrap(_s, *_a):
self.emit(_s, *_a)
return False
self.safe_idle_add(emitwrap, signal, *args)
def safe_idle_add(self, func, *args):
"""
Make sure idle functions are run thread safe
"""
return gobject.idle_add(_safe_wrapper, func, *args)
def safe_timeout_add(self, timeout, func, *args):
"""
Make sure timeout functions are run thread safe
"""
return gobject.timeout_add(timeout, _safe_wrapper, func, *args)
def __del__(self):
if hasattr(gobject.GObject, "__del__"):
getattr(gobject.GObject, "__del__")(self)

View File

@ -1152,13 +1152,13 @@ class vmmConnection(vmmGObject):
# We want to kill off this thread asap, so schedule a gobject
# idle even to inform the UI of result
logging.debug("Background open thread complete, scheduling notify")
util.safe_idle_add(self._open_notify)
self.safe_idle_add(self._open_notify)
self.connectThread = None
def _open_notify(self):
logging.debug("Notifying open result")
util.safe_idle_add(util.idle_emit, self, "state-changed")
self.idle_emit("state-changed")
if self.state == self.STATE_ACTIVE:
caps = self.get_capabilities_xml()
@ -1173,8 +1173,7 @@ class vmmConnection(vmmGObject):
self.vms.keys())
if self.state == self.STATE_DISCONNECTED:
util.safe_idle_add(util.idle_emit, self, "connect-error",
self.connectError)
self.idle_emit("connect-error", self.connectError)
self.connectError = None
@ -1473,7 +1472,7 @@ class vmmConnection(vmmGObject):
for name in newNodedevs:
self.emit("nodedev-added", self.uri, name)
util.safe_idle_add(tick_send_signals)
self.safe_idle_add(tick_send_signals)
# Finally, we sample each domain
now = time()
@ -1496,7 +1495,7 @@ class vmmConnection(vmmGObject):
if not noStatsUpdate:
self._recalculate_stats(now, updateVMs)
util.safe_idle_add(util.idle_emit, self, "resources-sampled")
self.idle_emit("resources-sampled")
return 1

View File

@ -37,7 +37,6 @@ import signal
import socket
import logging
from virtManager import util
from virtManager.baseclass import vmmGObjectUI
from virtManager.error import vmmErrorDialog
@ -875,7 +874,7 @@ class vmmConsolePages(vmmGObjectUI):
logging.error("Too many connection failures, not retrying again")
return
util.safe_timeout_add(self.viewerRetryDelay, self.try_login)
self.safe_timeout_add(self.viewerRetryDelay, self.try_login)
if self.viewerRetryDelay < 2000:
self.viewerRetryDelay = self.viewerRetryDelay * 2
@ -1021,7 +1020,7 @@ class vmmConsolePages(vmmGObjectUI):
def unset_cb(src):
src.queue_resize_no_redraw()
util.safe_idle_add(restore_scroll, src)
self.safe_idle_add(restore_scroll, src)
return False
def request_cb(src, req):
@ -1031,7 +1030,7 @@ class vmmConsolePages(vmmGObjectUI):
src.disconnect(signal_id)
util.safe_idle_add(unset_cb, widget)
self.safe_idle_add(unset_cb, widget)
return False
# Disable scroll bars while we resize, since resizing to the VM's

View File

@ -384,7 +384,7 @@ class vmmCreate(vmmGObjectUI):
label_widget = self.window.get_widget("phys-hd-label")
label_widget.set_markup("")
if not self.host_storage_timer:
self.host_storage_timer = util.safe_timeout_add(3 * 1000,
self.host_storage_timer = self.safe_timeout_add(3 * 1000,
uihelpers.host_space_tick,
self.conn,
label_widget)
@ -1841,7 +1841,7 @@ class vmmCreate(vmmGObjectUI):
self.mediaDetected = True
logging.debug("Leaving OS detection thread.")
if forward:
util.safe_idle_add(self.forward, ())
self.safe_idle_add(self.forward, ())
return

View File

@ -1372,8 +1372,7 @@ class vmmDomain(vmmDomainBase):
# are operating with fresh XML
self.refresh_xml()
util.safe_idle_add(util.idle_emit, self, "status-changed",
oldstatus, status)
self.idle_emit("status-changed", oldstatus, status)
##################
# Stats handling #
@ -1538,7 +1537,7 @@ class vmmDomain(vmmDomainBase):
self.record.insert(0, newStats)
self._update_status(info[0])
util.safe_idle_add(util.idle_emit, self, "resources-sampled")
self.idle_emit("resources-sampled")
class vmmDomainVirtinst(vmmDomainBase):
@ -1577,7 +1576,7 @@ class vmmDomainVirtinst(vmmDomainBase):
def _define(self, newxml):
ignore = newxml
self._orig_xml = None
util.safe_idle_add(util.idle_emit, self, "config-changed")
self.idle_emit("config-changed")
def _redefine_xml(self, newxml):
# We need to cache origxml in order to have something to diff against
@ -1614,7 +1613,7 @@ class vmmDomainVirtinst(vmmDomainBase):
# Device/XML hotplug implementations
def set_autostart(self, val):
self._backend.autostart = bool(val)
util.safe_idle_add(util.idle_emit, self, "config-changed")
self.idle_emit("config-changed")
def define_name(self, newname):
def change(guest):

View File

@ -440,7 +440,7 @@ class vmmEngine(vmmGObject):
logging.exception("Could not refresh connection %s." % uri)
logging.debug("Closing connection since libvirtd "
"appears to have stopped.")
util.safe_idle_add(conn.close)
self.safe_idle_add(conn.close)
else:
raise
return 1

View File

@ -25,7 +25,6 @@ import logging
import libxml2
from virtManager import util
from virtManager.baseclass import vmmGObject
def _sanitize_xml(xml):
@ -111,7 +110,7 @@ class vmmLibvirtObject(vmmGObject):
self._is_xml_valid = True
if origxml != self._xml or forcesignal:
util.safe_idle_add(util.idle_emit, self, "config-changed")
self.idle_emit("config-changed")
######################################
# Internal XML cache/update routines #

View File

@ -207,7 +207,7 @@ class vmmManager(vmmGObjectUI):
vmlist.get_model().get_iter_first())
# Queue up the default connection detector
util.safe_idle_add(self.engine.add_default_connection)
self.safe_idle_add(self.engine.add_default_connection)
##################
# Common methods #

View File

@ -23,7 +23,6 @@ import logging
import virtinst
from virtManager import util
from virtManager.baseclass import vmmGObject
MEDIA_FLOPPY = "floppy"
@ -140,7 +139,7 @@ class vmmMediaDevice(vmmGObject):
if self.poll_signal:
return
self.poll_signal = util.safe_timeout_add(MEDIA_TIMEOUT * 1000,
self.poll_signal = self.safe_timeout_add(MEDIA_TIMEOUT * 1000,
self._poll_for_media)
self.add_gobject_timeout(self.poll_signal)

View File

@ -531,7 +531,7 @@ class vmmMigrateDialog(vmmGObjectUI):
# 0 means that the spin box migrate-max-downtime does not
# be enabled.
current_thread = threading.currentThread()
timer = util.safe_timeout_add(100,
timer = self.safe_timeout_add(100,
self._async_set_max_downtime,
vm, max_downtime,
current_thread)

View File

@ -19,7 +19,6 @@
#
import gtk
import gobject
import libvirt
import libxml2
@ -305,32 +304,6 @@ def pretty_hv(gtype, domtype):
return label
def idle_emit(self, signal, *args):
"""
Safe wrapper for using 'self.emit' with gobject.idle_add
"""
self.emit(signal, *args)
return False
def _safe_wrapper(func, *args):
gtk.gdk.threads_enter()
try:
return func(*args)
finally:
gtk.gdk.threads_leave()
def safe_idle_add(func, *args):
"""
Make sure idle functions are run thread safe
"""
return gobject.idle_add(_safe_wrapper, func, *args)
def safe_timeout_add(timeout, func, *args):
"""
Make sure timeout functions are run thread safe
"""
return gobject.timeout_add(timeout, _safe_wrapper, func, *args)
def uuidstr(rawuuid):
hx = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f']