bool useSnapshotOld; /* cannot use virDomainSnapshotGetParent or
virDomainSnapshotNumChildren */
virThread eventLoop;
+ virMutex lock;
bool eventLoopStarted;
bool quit;
} __vshControl;
{
vshControl *ctl = opaque;
- while (!ctl->quit) {
- if (virEventRunDefaultImpl() < 0) {
+ while (1) {
+ bool quit;
+ virMutexLock(&ctl->lock);
+ quit = ctl->quit;
+ virMutexUnlock(&ctl->lock);
+
+ if (quit)
+ break;
+
+ if (virEventRunDefaultImpl() < 0)
virshReportError(ctl);
- }
}
}
#endif /* !USE_READLINE */
+static void
+vshDeinitTimer(int timer ATTRIBUTE_UNUSED, void *opaque ATTRIBUTE_UNUSED)
+{
+ /* nothing to be done here */
+}
+
/*
* Deinitialize virsh
*/
static bool
vshDeinit(vshControl *ctl)
{
- ctl->quit = true;
vshReadlineDeinit(ctl);
vshCloseLogFile(ctl);
VIR_FREE(ctl->name);
virResetLastError();
if (ctl->eventLoopStarted) {
+ int timer;
+
+ virMutexLock(&ctl->lock);
+ ctl->quit = true;
/* HACK: Add a dummy timeout to break event loop */
- int timer = virEventAddTimeout(-1, NULL, NULL, NULL);
+ timer = virEventAddTimeout(0, vshDeinitTimer, NULL, NULL);
+ virMutexUnlock(&ctl->lock);
+
+ virThreadJoin(&ctl->eventLoop);
+
if (timer != -1)
virEventRemoveTimeout(timer);
- virThreadJoin(&ctl->eventLoop);
ctl->eventLoopStarted = false;
}
+ virMutexDestroy(&ctl->lock);
+
return true;
}
return EXIT_FAILURE;
}
+ if (virMutexInit(&ctl->lock) < 0) {
+ vshError(ctl, "%s", _("Failed to initialize mutex"));
+ return EXIT_FAILURE;
+ }
+
if (virInitialize() < 0) {
vshError(ctl, "%s", _("Failed to initialize libvirt"));
return EXIT_FAILURE;