nodesuspend: Use automatic mutex management
Signed-off-by: Tim Wiederhake <twiederh@redhat.com> Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
79e6bf3c45
commit
df8992c277
@ -53,17 +53,6 @@ static virMutex virNodeSuspendMutex = VIR_MUTEX_INITIALIZER;
|
|||||||
|
|
||||||
static bool aboutToSuspend;
|
static bool aboutToSuspend;
|
||||||
|
|
||||||
static void virNodeSuspendLock(void)
|
|
||||||
{
|
|
||||||
virMutexLock(&virNodeSuspendMutex);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void virNodeSuspendUnlock(void)
|
|
||||||
{
|
|
||||||
virMutexUnlock(&virNodeSuspendMutex);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virNodeSuspendSetNodeWakeup:
|
* virNodeSuspendSetNodeWakeup:
|
||||||
* @alarmTime: time in seconds from now, at which the RTC alarm has to be set.
|
* @alarmTime: time in seconds from now, at which the RTC alarm has to be set.
|
||||||
@ -116,9 +105,9 @@ static void virNodeSuspendHelper(void *cmdString)
|
|||||||
* Now that we have resumed from suspend or the suspend failed,
|
* Now that we have resumed from suspend or the suspend failed,
|
||||||
* reset 'aboutToSuspend' flag.
|
* reset 'aboutToSuspend' flag.
|
||||||
*/
|
*/
|
||||||
virNodeSuspendLock();
|
VIR_WITH_MUTEX_LOCK_GUARD(&virNodeSuspendMutex) {
|
||||||
aboutToSuspend = false;
|
aboutToSuspend = false;
|
||||||
virNodeSuspendUnlock();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -154,8 +143,8 @@ int virNodeSuspend(unsigned int target,
|
|||||||
{
|
{
|
||||||
static virThread thread;
|
static virThread thread;
|
||||||
const char *cmdString = NULL;
|
const char *cmdString = NULL;
|
||||||
int ret = -1;
|
|
||||||
unsigned int supported;
|
unsigned int supported;
|
||||||
|
VIR_LOCK_GUARD lock = { NULL };
|
||||||
|
|
||||||
virCheckFlags(0, -1);
|
virCheckFlags(0, -1);
|
||||||
|
|
||||||
@ -166,13 +155,13 @@ int virNodeSuspend(unsigned int target,
|
|||||||
* Ensure that we are the only ones trying to suspend.
|
* Ensure that we are the only ones trying to suspend.
|
||||||
* Fail if somebody has already initiated a suspend.
|
* Fail if somebody has already initiated a suspend.
|
||||||
*/
|
*/
|
||||||
virNodeSuspendLock();
|
lock = virLockGuardLock(&virNodeSuspendMutex);
|
||||||
|
|
||||||
if (aboutToSuspend) {
|
if (aboutToSuspend) {
|
||||||
/* A suspend operation is already in progress */
|
/* A suspend operation is already in progress */
|
||||||
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
||||||
_("Suspend operation already in progress"));
|
_("Suspend operation already in progress"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if the host supports the requested suspend target */
|
/* Check if the host supports the requested suspend target */
|
||||||
@ -180,7 +169,7 @@ int virNodeSuspend(unsigned int target,
|
|||||||
case VIR_NODE_SUSPEND_TARGET_MEM:
|
case VIR_NODE_SUSPEND_TARGET_MEM:
|
||||||
if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_MEM))) {
|
if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_MEM))) {
|
||||||
virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-to-RAM"));
|
virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-to-RAM"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
cmdString = "pm-suspend";
|
cmdString = "pm-suspend";
|
||||||
break;
|
break;
|
||||||
@ -188,7 +177,7 @@ int virNodeSuspend(unsigned int target,
|
|||||||
case VIR_NODE_SUSPEND_TARGET_DISK:
|
case VIR_NODE_SUSPEND_TARGET_DISK:
|
||||||
if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_DISK))) {
|
if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_DISK))) {
|
||||||
virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-to-Disk"));
|
virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-to-Disk"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
cmdString = "pm-hibernate";
|
cmdString = "pm-hibernate";
|
||||||
break;
|
break;
|
||||||
@ -196,19 +185,19 @@ int virNodeSuspend(unsigned int target,
|
|||||||
case VIR_NODE_SUSPEND_TARGET_HYBRID:
|
case VIR_NODE_SUSPEND_TARGET_HYBRID:
|
||||||
if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_HYBRID))) {
|
if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_HYBRID))) {
|
||||||
virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Hybrid-Suspend"));
|
virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Hybrid-Suspend"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
cmdString = "pm-suspend-hybrid";
|
cmdString = "pm-suspend-hybrid";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
virReportError(VIR_ERR_INVALID_ARG, "%s", _("Invalid suspend target"));
|
virReportError(VIR_ERR_INVALID_ARG, "%s", _("Invalid suspend target"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Just set the RTC alarm. Don't suspend yet. */
|
/* Just set the RTC alarm. Don't suspend yet. */
|
||||||
if (virNodeSuspendSetNodeWakeup(duration) < 0)
|
if (virNodeSuspendSetNodeWakeup(duration) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
|
|
||||||
if (virThreadCreateFull(&thread, false,
|
if (virThreadCreateFull(&thread, false,
|
||||||
virNodeSuspendHelper,
|
virNodeSuspendHelper,
|
||||||
@ -217,14 +206,11 @@ int virNodeSuspend(unsigned int target,
|
|||||||
(void *)cmdString) < 0) {
|
(void *)cmdString) < 0) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("Failed to create thread to suspend the host"));
|
_("Failed to create thread to suspend the host"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
aboutToSuspend = true;
|
aboutToSuspend = true;
|
||||||
ret = 0;
|
return 0;
|
||||||
cleanup:
|
|
||||||
virNodeSuspendUnlock();
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WITH_PM_UTILS
|
#ifdef WITH_PM_UTILS
|
||||||
@ -348,11 +334,10 @@ virNodeSuspendSupportsTarget(unsigned int target, bool *supported)
|
|||||||
int
|
int
|
||||||
virNodeSuspendGetTargetMask(unsigned int *bitmask)
|
virNodeSuspendGetTargetMask(unsigned int *bitmask)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
VIR_LOCK_GUARD lock = virLockGuardLock(&virNodeSuspendMutex);
|
||||||
|
|
||||||
*bitmask = 0;
|
*bitmask = 0;
|
||||||
|
|
||||||
virNodeSuspendLock();
|
|
||||||
/* Get the power management capabilities supported by the host */
|
/* Get the power management capabilities supported by the host */
|
||||||
if (!nodeSuspendTargetMaskInit) {
|
if (!nodeSuspendTargetMaskInit) {
|
||||||
bool supported;
|
bool supported;
|
||||||
@ -360,19 +345,19 @@ virNodeSuspendGetTargetMask(unsigned int *bitmask)
|
|||||||
|
|
||||||
/* Check support for Suspend-to-RAM (S3) */
|
/* Check support for Suspend-to-RAM (S3) */
|
||||||
if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_MEM, &supported) < 0)
|
if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_MEM, &supported) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
if (supported)
|
if (supported)
|
||||||
nodeSuspendTargetMask |= (1 << VIR_NODE_SUSPEND_TARGET_MEM);
|
nodeSuspendTargetMask |= (1 << VIR_NODE_SUSPEND_TARGET_MEM);
|
||||||
|
|
||||||
/* Check support for Suspend-to-Disk (S4) */
|
/* Check support for Suspend-to-Disk (S4) */
|
||||||
if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_DISK, &supported) < 0)
|
if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_DISK, &supported) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
if (supported)
|
if (supported)
|
||||||
nodeSuspendTargetMask |= (1 << VIR_NODE_SUSPEND_TARGET_DISK);
|
nodeSuspendTargetMask |= (1 << VIR_NODE_SUSPEND_TARGET_DISK);
|
||||||
|
|
||||||
/* Check support for Hybrid-Suspend */
|
/* Check support for Hybrid-Suspend */
|
||||||
if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_HYBRID, &supported) < 0)
|
if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_HYBRID, &supported) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
if (supported)
|
if (supported)
|
||||||
nodeSuspendTargetMask |= (1 << VIR_NODE_SUSPEND_TARGET_HYBRID);
|
nodeSuspendTargetMask |= (1 << VIR_NODE_SUSPEND_TARGET_HYBRID);
|
||||||
|
|
||||||
@ -380,8 +365,5 @@ virNodeSuspendGetTargetMask(unsigned int *bitmask)
|
|||||||
}
|
}
|
||||||
|
|
||||||
*bitmask = nodeSuspendTargetMask;
|
*bitmask = nodeSuspendTargetMask;
|
||||||
ret = 0;
|
return 0;
|
||||||
cleanup:
|
|
||||||
virNodeSuspendUnlock();
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user