libxlDomainDeathThread(void *opaque)
{
    struct libxlEventHandlerThreadInfo *death_info = opaque;
    virDomainObj *vm = NULL;
    libxl_event *ev = death_info->event;
    libxlDriverPrivate *driver = death_info->driver;
    virObjectEvent *dom_event = NULL;
    g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver);

    vm = virDomainObjListFindByID(driver->domains, ev->domid);
    if (!vm) {
        /* Nothing to do if we can't find the virDomainObj */
        goto cleanup;
    }

    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

    virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_DESTROYED);
    dom_event = virDomainEventLifecycleNewFromObj(vm,
                                                  VIR_DOMAIN_EVENT_STOPPED,
                                                  VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
    libxlDomainCleanup(driver, vm);
    if (!vm->persistent)
        virDomainObjListRemove(driver->domains, vm);
    libxlDomainObjEndJob(driver, vm);
    virObjectEventStateQueue(driver->domainEventState, dom_event);

 cleanup:
    virDomainObjEndAPI(&vm);
    libxl_event_free(cfg->ctx, ev);
    VIR_FREE(death_info);
}
libxlDomainDeathThread(void *opaque)
{
    struct libxlEventHandlerThreadInfo *death_info = opaque;
    virDomainObj *vm = NULL;
    libxl_event *ev = death_info->event;
    libxlDriverPrivate *driver = death_info->driver;
    virObjectEvent *dom_event = NULL;
    g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver);

    vm = virDomainObjListFindByID(driver->domains, ev->domid);
    if (!vm) {
         
        goto cleanup;
    }

    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

    virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_DESTROYED);
    dom_event = virDomainEventLifecycleNewFromObj(vm,
                                                  VIR_DOMAIN_EVENT_STOPPED,
                                                  VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
    libxlDomainCleanup(driver, vm);
    if (!vm->persistent)
        virDomainObjListRemove(driver->domains, vm);
    libxlDomainObjEndJob(driver, vm);
    virObjectEventStateQueue(driver->domainEventState, dom_event);

 cleanup:
    virDomainObjEndAPI(&vm);
    libxl_event_free(cfg->ctx, ev);
    VIR_FREE(death_info);
}