VirtualBox

Changeset 15762 in vbox for trunk/src/VBox/Main


Ignore:
Timestamp:
Dec 25, 2008 11:53:50 PM (16 years ago)
Author:
vboxsync
Message:

Main: Exmplicit machine state comparisons replaced with bool "IsMetastate"-like methods.

Location:
trunk/src/VBox/Main
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/ConsoleImpl.cpp

    r15708 r15762  
    3838
    3939#include "ConsoleImpl.h"
     40
     41#include "Global.h"
    4042#include "GuestImpl.h"
    4143#include "KeyboardImpl.h"
     
    13521354    AutoWriteLock alock (this);
    13531355
    1354     if (mMachineState != MachineState_Running &&
    1355         mMachineState != MachineState_Paused &&
    1356         mMachineState != MachineState_Stuck)
     1356    if (!Global::IsActive (mMachineState))
    13571357    {
    13581358        /* extra nice error message for a common case */
     
    13921392    AutoWriteLock alock (this);
    13931393
    1394     if (mMachineState != MachineState_Running &&
    1395         mMachineState != MachineState_Paused &&
    1396         mMachineState != MachineState_Stuck)
     1394    if (!Global::IsActive (mMachineState))
    13971395    {
    13981396        /* extra nice error message for a common case */
     
    14561454
    14571455    if (mMachineState != MachineState_Running)
    1458         return setError(VBOX_E_INVALID_VM_STATE,
    1459             tr ("Cannot reset the machine as it is not running "
    1460             "(machine state: %d)"), mMachineState);
     1456        return setError (VBOX_E_INVALID_VM_STATE,
     1457            tr ("Invalid machine state: %d)"), mMachineState);
    14611458
    14621459    /* protect mpVM */
     
    14881485    if (mMachineState != MachineState_Running)
    14891486        return setError (VBOX_E_INVALID_VM_STATE,
    1490             tr ("Cannot pause the machine as it is not running "
    1491             "(machine state: %d)"), mMachineState);
     1487            tr ("Invalid machine state: %d)"), mMachineState);
    14921488
    14931489    /* protect mpVM */
     
    15601556    if (mMachineState != MachineState_Running)
    15611557        return setError (VBOX_E_INVALID_VM_STATE,
    1562             tr ("Cannot power off the machine as it is not running "
    1563                 "(machine state: %d)"), mMachineState);
     1558            tr ("Invalid machine state: %d)"), mMachineState);
    15641559
    15651560    /* protect mpVM */
     
    15991594
    16001595    if (mMachineState != MachineState_Running)
    1601         return E_FAIL;
     1596        return setError (VBOX_E_INVALID_VM_STATE,
     1597            tr ("Invalid machine state: %d)"), mMachineState);
    16021598
    16031599    /* protect mpVM */
     
    16421638    if (mMachineState != MachineState_Running)
    16431639        return setError (VBOX_E_INVALID_VM_STATE,
    1644             tr ("Cannot get guest ACPI mode as it is "
    1645             "not running (machine state: %d)"), mMachineState);
     1640            tr ("Invalid machine state: %d)"), mMachineState);
    16461641
    16471642    /* protect mpVM */
     
    16771672    if (mMachineState != MachineState_Running)
    16781673        return setError (VBOX_E_INVALID_VM_STATE,
    1679             tr ("Cannot send the sleep button event as it is "
    1680             "not running (machine state: %d)"), mMachineState);
     1674            tr ("Invalid machine state: %d)"), mMachineState);
    16811675
    16821676    /* protect mpVM */
     
    17201714        return setError (VBOX_E_INVALID_VM_STATE,
    17211715            tr ("Cannot save the execution state as the machine "
    1722                 "is not running (machine state: %d)"), mMachineState);
     1716                "is not running or paused (machine state: %d)"), mMachineState);
    17231717    }
    17241718
     
    19871981    AutoWriteLock alock (this);
    19881982
    1989     /// @todo (r=dmik) is it legal to attach USB devices when the machine is
    1990     //  Paused, Starting, Saving, Stopping, etc? if not, we should make a
    1991     //  stricter check (mMachineState != MachineState_Running).
    1992     //
    1993     //  I'm changing it to the semi-strict check for the time being. We'll
    1994     //  consider the below later.
    1995     //
    1996     /* bird: It is not permitted to attach or detach while the VM is saving,
    1997      * is restoring or has stopped - definitely not.
    1998      *
    1999      * Attaching while starting, well, if you don't create any deadlock it
    2000      * should work...  Paused should work I guess, but we shouldn't push our
    2001      * luck if we're pausing because an runtime error condition was raised
    2002      * (which is one of the reasons there better be a separate state for that
    2003      * in the VMM).
    2004      */
    20051983    if (mMachineState != MachineState_Running &&
    20061984        mMachineState != MachineState_Paused)
    20071985        return setError (VBOX_E_INVALID_VM_STATE,
    2008             tr ("Cannot attach a USB device to the machine which is not running "
    2009                 "(machine state: %d)"), mMachineState);
     1986            tr ("Cannot attach a USB device to the machine which is not "
     1987                "running or paused (machine state: %d)"), mMachineState);
    20101988
    20111989    /* protect mpVM */
     
    22412219    AutoWriteLock alock (this);
    22422220
    2243     if (mMachineState > MachineState_Paused)
     2221    if (Global::IsTransient (mMachineState))
    22442222    {
    22452223        return setError (VBOX_E_INVALID_VM_STATE,
     
    24102388    AutoWriteLock alock (this);
    24112389
    2412     if (mMachineState >= MachineState_Running)
     2390    if (Global::IsOnlineOrTransient (mMachineState))
    24132391        return setError (VBOX_E_INVALID_VM_STATE,
    24142392            tr ("Cannot discard a snapshot of the running machine "
     
    24312409    AutoWriteLock alock (this);
    24322410
    2433     if (mMachineState >= MachineState_Running)
     2411    if (Global::IsOnlineOrTransient (mMachineState))
    24342412        return setError (VBOX_E_INVALID_VM_STATE,
    24352413            tr ("Cannot discard the current state of the running machine "
     
    24522430    AutoWriteLock alock (this);
    24532431
    2454     if (mMachineState >= MachineState_Running)
     2432    if (Global::IsOnlineOrTransient (mMachineState))
    24552433        return setError (VBOX_E_INVALID_VM_STATE,
    24562434            tr ("Cannot discard the current snapshot and state of the "
     
    42484226    AutoWriteLock alock (this);
    42494227
    4250     if (mMachineState >= MachineState_Running)
     4228    if (Global::IsOnlineOrTransient (mMachineState))
    42514229        return setError(VBOX_E_INVALID_VM_STATE,
    4252             tr ("Virtual machine is already running "
     4230            tr ("Virtual machine is already running or busy "
    42534231            "(machine state: %d)"), mMachineState);
    42544232
     
    49104888        if (aUpdateServer)
    49114889        {
    4912             /*
    4913              *  Server notification MUST be done from under the lock; otherwise
    4914              *  the machine state here and on the server might go out of sync, that
    4915              *  can lead to various unexpected results (like the machine state being
    4916              *  >= MachineState_Running on the server, while the session state is
    4917              *  already SessionState_Closed at the same time there).
     4890            /* Server notification MUST be done from under the lock; otherwise
     4891             * the machine state here and on the server might go out of sync
     4892             * whihc can lead to various unexpected results (like the machine
     4893             * state being >= MachineState_Running on the server, while the
     4894             * session state is already SessionState_Closed at the same time
     4895             * there).
    49184896             *
    4919              *  Cross-lock conditions should be carefully watched out: calling
    4920              *  UpdateState we will require Machine and SessionMachine locks
    4921              *  (remember that here we're holding the Console lock here, and
    4922              *  also all locks that have been entered by the thread before calling
    4923              *  this method).
     4897             * Cross-lock conditions should be carefully watched out: calling
     4898             * UpdateState we will require Machine and SessionMachine locks
     4899             * (remember that here we're holding the Console lock here, and also
     4900             * all locks that have been entered by the thread before calling
     4901             * this method).
    49244902             */
    49254903            LogFlowThisFunc (("Doing mControl->UpdateState()...\n"));
     
    54415419
    54425420            /* Guru respects only running VMs */
    5443             Assert ((that->mMachineState >= MachineState_Running));
     5421            Assert (Global::IsOnline (that->mMachineState));
    54445422
    54455423            that->setMachineState (MachineState_Stuck);
  • trunk/src/VBox/Main/DisplayImpl.cpp

    r15732 r15762  
    205205    if (machineState == MachineState_Running)
    206206    {
    207         LogFlowFunc (("Machine running\n"));
     207        LogFlowFunc (("Machine is running.\n"));
    208208
    209209        mfMachineRunning = true;
  • trunk/src/VBox/Main/MachineDebuggerImpl.cpp

    r15051 r15762  
    2323
    2424#include "MachineDebuggerImpl.h"
     25
     26#include "Global.h"
    2527#include "ConsoleImpl.h"
    2628#include "Logging.h"
     
    799801        MachineState_T machineState;
    800802        mParent->COMGETTER(State) (&machineState);
    801         if (    machineState != MachineState_Running
    802             &&  machineState != MachineState_Paused
    803             &&  machineState != MachineState_Stuck)
     803        if (!Global::IsActive (machineState))
    804804            // queue the request
    805805            return true;
  • trunk/src/VBox/Main/MachineImpl.cpp

    r15582 r15762  
    670670                             (SessionMachine *) mData->mSession.mMachine));
    671671
    672         if (mData->mMachineState >= MachineState_Running)
     672        if (Global::IsOnlineOrTransient (mData->mMachineState))
    673673        {
    674674            LogWarningThisFunc (("Setting state to Aborted!\n"));
     
    19451945    AssertReturn (mData->mMachineState != MachineState_Saved, E_FAIL);
    19461946
    1947     if (mData->mMachineState >= MachineState_Running)
     1947    if (Global::IsOnlineOrTransient (mData->mMachineState))
    19481948        return setError (VBOX_E_INVALID_VM_STATE,
    19491949            tr ("Invalid machine state: %d"), mData->mMachineState);
     
    22652265    AssertReturn (mData->mMachineState != MachineState_Saved, E_FAIL);
    22662266
    2267     if (mData->mMachineState >= MachineState_Running)
     2267    if (Global::IsOnlineOrTransient (mData->mMachineState))
    22682268        return setError (VBOX_E_INVALID_VM_STATE,
    22692269            tr ("Invalid machine state: %d"), mData->mMachineState);
     
    33963396            mUserData->mName.raw());
    33973397
    3398     /* may not be Running */
    3399     AssertReturn (mData->mMachineState < MachineState_Running, E_FAIL);
     3398    /* may not be busy */
     3399    AssertReturn (!Global::IsOnlineOrTransient (mData->mMachineState), E_FAIL);
    34003400
    34013401    /* get the session PID */
     
    36033603            mUserData->mName.raw());
    36043604
    3605     /* may not be Running */
    3606     AssertReturn (mData->mMachineState < MachineState_Running, E_FAIL);
     3605    /* may not be busy */
     3606    AssertReturn (!Global::IsOnlineOrTransient (mData->mMachineState), E_FAIL);
    36073607
    36083608    /* get the path to the executable */
     
    75537553    AssertReturnVoid (aThat->mType == IsSnapshotMachine);
    75547554
    7555     AssertReturnVoid (mData->mMachineState < MachineState_Running ||
    7556                       mData->mMachineState >= MachineState_Discarding);
     7555    AssertReturnVoid (!Global::IsOnline (mData->mMachineState));
    75577556
    75587557    mHWData.assignCopy (aThat->mHWData);
     
    79377936    if (aReason == Uninit::Abnormal)
    79387937    {
    7939         LogWarningThisFunc (("ABNORMAL client termination! (wasRunning=%d)\n",
    7940                              lastState >= MachineState_Running));
     7938        LogWarningThisFunc (("ABNORMAL client termination! (wasBusy=%d)\n",
     7939                             Global::IsOnlineOrTransient (lastState)));
    79417940
    79427941        /* reset the state to Aborted */
     
    79657964#ifdef VBOX_WITH_USB
    79667965    /* release all captured USB devices */
    7967     if (aReason == Uninit::Abnormal && lastState >= MachineState_Running)
     7966    if (aReason == Uninit::Abnormal && Global::IsOnline (lastState))
    79687967    {
    79697968        /* Console::captureUSBDevices() is called in the VM process only after
     
    84818480    AutoMultiWriteLock2 alock (mParent, this);
    84828481
    8483     AssertReturn ((mData->mMachineState < MachineState_Running ||
     8482    AssertReturn ((!Global::IsOnlineOrTransient (mData->mMachineState) ||
    84848483                   mData->mMachineState == MachineState_Paused) &&
    84858484                  mSnapshotData.mLastState == MachineState_Null &&
     
    86678666    AutoMultiWriteLock2 alock (mParent, this);
    86688667
    8669     ComAssertRet (mData->mMachineState < MachineState_Running, E_FAIL);
     8668    ComAssertRet (!Global::IsOnlineOrTransient (mData->mMachineState), E_FAIL);
    86708669
    86718670    ComObjPtr <Snapshot> snapshot;
     
    87498748    AutoWriteLock alock (this);
    87508749
    8751     ComAssertRet (mData->mMachineState < MachineState_Running, E_FAIL);
     8750    ComAssertRet (!Global::IsOnlineOrTransient (mData->mMachineState), E_FAIL);
    87528751
    87538752    if (mData->mCurrentSnapshot.isNull())
     
    88138812    AutoWriteLock alock (this);
    88148813
    8815     ComAssertRet (mData->mMachineState < MachineState_Running, E_FAIL);
     8814    ComAssertRet (!Global::IsOnlineOrTransient (mData->mMachineState), E_FAIL);
    88168815
    88178816    if (mData->mCurrentSnapshot.isNull())
     
    95099508
    95109509        int opFlags = SaveSS_AddOp | SaveSS_CurrentId;
    9511         if (mSnapshotData.mLastState < MachineState_Running)
     9510        if (!Global::IsOnline (mSnapshotData.mLastState))
    95129511        {
    95139512            /* the machine was powered off or saved when taking a snapshot, so
     
    95229521    if (aSuccess && SUCCEEDED (rc))
    95239522    {
    9524         bool online = mSnapshotData.mLastState >= MachineState_Running;
     9523        bool online = Global::IsOnline (mSnapshotData.mLastState);
    95259524
    95269525        /* associate old hard disks with the snapshot and do locking/unlocking*/
     
    95949593    HRESULT rc = S_OK;
    95959594
    9596     bool online = mSnapshotData.mLastState >= MachineState_Running;
     9595    bool online = Global::IsOnline (mSnapshotData.mLastState);
    95979596
    95989597    LogFlowThisFunc (("Creating differencing hard disks (online=%d)...\n",
  • trunk/src/VBox/Main/USBControllerImpl.cpp

    r15157 r15762  
    2020 */
    2121
    22 
    23 
    2422#include "USBControllerImpl.h"
     23
     24#include "Global.h"
    2525#include "MachineImpl.h"
    2626#include "VirtualBoxImpl.h"
     
    456456
    457457    /* notify the proxy (only when it makes sense) */
    458     if (filter->data().mActive && adep.machineState() >= MachineState_Running)
     458    if (filter->data().mActive && Global::IsOnline (adep.machineState()))
    459459    {
    460460        USBProxyService *service = mParent->virtualBox()->host()->usbProxyService();
     
    520520
    521521    /* notify the proxy (only when it makes sense) */
    522     if (filter->data().mActive && adep.machineState() >= MachineState_Running)
     522    if (filter->data().mActive && Global::IsOnline (adep.machineState()))
    523523    {
    524524        USBProxyService *service = mParent->virtualBox()->host()->usbProxyService();
     
    846846                /* notify the proxy (only when it makes sense) */
    847847                if ((*it)->data().mActive &&
    848                     adep.machineState() >= MachineState_Running &&
    849                     adep.machineState() < MachineState_Discarding)
     848                    Global::IsOnline (adep.machineState()))
    850849                {
    851850                    USBDeviceFilter *filter = *it;
     
    860859        }
    861860
    862         if (adep.machineState() >= MachineState_Running &&
    863             adep.machineState() < MachineState_Discarding)
     861        if (Global::IsOnline (adep.machineState()))
    864862        {
    865863            /* find all removed old filters (absent in the new list)
     
    10341032    AssertComRCReturnVoid (adep.rc());
    10351033    /* Machine::copyFrom() may not be called when the VM is running */
    1036     AssertReturnVoid (adep.machineState() < MachineState_Running ||
    1037                       adep.machineState() >= MachineState_Discarding);
     1034    AssertReturnVoid (!Global::IsOnline (adep.machineState()));
    10381035
    10391036    /* peer is not modified, lock it for reading (aThat is "master" so locked
     
    10831080
    10841081    /* nothing to do if the machine isn't running */
    1085     if (adep.machineState() < MachineState_Running)
     1082    if (!Global::IsOnline (adep.machineState()))
    10861083        return S_OK;
    10871084
  • trunk/src/VBox/Main/idl/VirtualBox.xidl

    r15732 r15762  
    482482      an offline state. This inconsistency may be removed in one of the future
    483483      versions of VirtualBox by adding a new state.
     484
     485      <note internal="yes">
     486        For whoever decides to touch this enum: In order to keep the
     487        comparisons involving FirstOnline and LastOnline pseudo-states valid,
     488        the numeric values of these states must be correspondingly updated if
     489        needed: for any online VM state, the condition
     490        <tt>FirstOnline &lt;= state &lt;= LastOnline</tt> must be
     491        <tt>true</tt>. The same relates to transient states for which
     492        the condition <tt>FirstOnline &lt;= state &lt;= LastOnline</tt> must be
     493        <tt>true</tt>.
     494      </note>
    484495    </desc>
    485496
     
    508519        <note internal="yes">
    509520          For whoever decides to touch this enum: In order to keep the
    510           comparisons in the source code valid, this state must immediately
     521          comparisons in the old source code valid, this state must immediately
    511522          precede the Paused state.
    512523        </note>
     
    518529        <note internal="yes">
    519530          For whoever decides to touch this enum: In order to keep the
    520           comparisons in the source code valid, this state must immediately
     531          comparisons in the old source code valid, this state must immediately
    521532          follow the Running state.
    522533        </note>
     
    567578      <desc>
    568579        Pseudo-state: first online state (for use in relational expressions).
    569         <note internal="yes">
    570           For whoever decides to touch this enum: In order to keep the
    571           comparisons involving FirstOnline and LastOnline pseudo-states valid,
    572           the numeric values of these states must be correspondingly updated if
    573           needed: for any online VM state, the condition
    574           <tt>FirstOnline &lt;= state &lt;= LastOnline</tt> must be
    575           <tt>true</tt>.
    576         </note>
    577580      </desc>
    578581    </const>
     
    580583      <desc>
    581584        Pseudo-state: last online state (for use in relational expressions).
    582         <note internal="yes">
    583           For whoever decides to touch this enum: In order to keep the
    584           comparisons involving FirstOnline and LastOnline pseudo-states valid,
    585           the numeric values of these states must be correspondingly updated if
    586           needed: for any online VM state, the condition
    587           <tt>FirstOnline &lt;= state &lt;= LastOnline</tt> must be
    588           <tt>true</tt>.
    589         </note>
     585      </desc>
     586    </const>
     587
     588    <const name="FirstTransient" value="7" wsmap="suppress"> <!-- Starting -->
     589      <desc>
     590        Pseudo-state: first transient state (for use in relational expressions).
     591      </desc>
     592    </const>
     593    <const name="LastTransient"  value="12" wsmap="suppress"> <!-- SettingUp -->
     594      <desc>
     595        Pseudo-state: last transient state (for use in relational expressions).
    590596      </desc>
    591597    </const>
  • trunk/src/VBox/Main/include/Global.h

    r15064 r15762  
    6666
    6767    static const char *OSTypeId (VBOXOSTYPE aOSType);
     68
     69    /**
     70     * Returns @c true if the given machine state is an online state. This is a
     71     * recommended way to detect if the VM is online (being executed in a
     72     * dedicated process) or not. Note that some online states are also
     73     * transitional states (see #IsTransitional()).
     74     */
     75    static bool IsOnline (MachineState_T aState)
     76    {
     77        return aState >= MachineState_FirstOnline &&
     78               aState <= MachineState_LastOnline;
     79    }
     80
     81    /**
     82     * Returns @c true if the given machine state is a transient state. This is
     83     * a recommended way to detect if the VM is performing some potentially
     84     * lengthy operation (such as starting, stopping, saving, discarding
     85     * snapshot, etc.). Note some (but not all) transitional states are also
     86     * online states (see #IsOnline()).
     87     */
     88    static bool IsTransient (MachineState_T aState)
     89    {
     90        return aState >= MachineState_FirstTransient &&
     91               aState <= MachineState_LastTransient;
     92    }
     93
     94    /**
     95     * Shortcut to <tt>IsOnline (aState) || IsTransient (aState)</tt>. When it
     96     * returns @false, the VM is turned off (no VM process) and not busy with
     97     * another exclusive operation.
     98     */
     99    static bool IsOnlineOrTransient (MachineState_T aState)
     100    {
     101        return IsOnline (aState) || IsTransient (aState);
     102    }
     103
     104    /**
     105     * Shortcut to <tt>IsOnline (aState) && !IsTransient (aState)</tt>. This is
     106     * a recommended way to detect if the VM emulation thread is in action
     107     * (either running, suspended, or stuck). When this method returns @false,
     108     * then either the VM is not online or the emulation thread is being started
     109     * or stopped, etc.
     110     */
     111    static bool IsActive (MachineState_T aState)
     112    {
     113        return IsOnline (aState) && !IsTransient (aState);
     114    }
    68115};
    69116
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette