VirtualBox

Changeset 92653 in vbox for trunk/src/VBox/Additions


Ignore:
Timestamp:
Nov 30, 2021 5:28:01 PM (3 years ago)
Author:
vboxsync
Message:

Additions: Linux: VBoxDRMClient: run resize task in a thread, bugref:10134.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/x11/VBoxClient/display-drm.cpp

    r92623 r92653  
    4848#include <iprt/initterm.h>
    4949#include <iprt/message.h>
     50#include <iprt/thread.h>
     51#include <iprt/asm.h>
    5052#include <unistd.h>
    5153#include <stdio.h>
    5254#include <limits.h>
     55#include <signal.h>
    5356
    5457#ifdef RT_OS_LINUX
     
    7780 *  context structure. */
    7881#define VMW_MAX_HEADS                   (32)
     82
     83/** Name of DRM resize thread. */
     84#define DRM_RESIZE_THREAD_NAME          "DrmResizeThread"
     85
     86/* Time in milliseconds to wait for host events. */
     87#define DRM_HOST_EVENT_RX_TIMEOUT_MS    (500)
    7988
    8089/** DRM version structure. */
     
    119128/** Path to the PID file. */
    120129static const char g_szPidFile[RTPATH_MAX] = "/var/run/VBoxDRMClient";
     130
     131/** Global flag which is triggered when service requested to shutdown. */
     132static bool volatile g_fShutdown;
    121133
    122134/**
     
    415427}
    416428
    417 
    418 static void drmMainLoop(RTFILE hDevice)
     429/** Worker thread for resize task. */
     430static DECLCALLBACK(int) drmResizeWorker(RTTHREAD ThreadSelf, void *pvUser)
    419431{
    420432    int rc;
     433    RTFILE hDevice = (RTFILE)pvUser;
     434
     435    RT_NOREF1(ThreadSelf);
     436
     437    AssertReturn(hDevice, VERR_INVALID_PARAMETER);
    421438
    422439    for (;;)
     
    472489        do
    473490        {
    474             rc = VbglR3WaitEvent(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, RT_INDEFINITE_WAIT, &events);
    475         } while (rc == VERR_INTERRUPTED);
    476         if (RT_FAILURE(rc))
     491            rc = VbglR3WaitEvent(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, DRM_HOST_EVENT_RX_TIMEOUT_MS, &events);
     492        } while (rc == VERR_TIMEOUT && !ASMAtomicReadBool(&g_fShutdown));
     493
     494        if (ASMAtomicReadBool(&g_fShutdown))
     495        {
     496            VBClLogInfo("VBoxDRMClient: exitting resize thread: shutdown requested\n");
     497            break;
     498        }
     499        else if (RT_FAILURE(rc))
     500        {
    477501            VBClLogFatalError("Failure waiting for event, rc=%Rrc\n", rc);
    478     }
     502        }
     503    }
     504
     505    return 0;
     506}
     507
     508static void drmRequestShutdown(int sig)
     509{
     510    RT_NOREF1(sig);
     511
     512    ASMAtomicWriteBool(&g_fShutdown, true);
    479513}
    480514
     
    483517    RTFILE hDevice = NIL_RTFILE;
    484518    RTFILE hPidFile;
     519
     520    RTTHREAD drmResizeThread;
     521    int rcDrmResizeThread = 0;
    485522
    486523    int rc = RTR3InitExe(argc, &argv, 0);
     
    523560    hDevice = drmOpenVmwgfx();
    524561    if (hDevice == NIL_RTFILE)
    525         return VERR_OPEN_FAILED;
     562        return RTEXITCODE_FAILURE;
    526563
    527564    rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0);
     
    529566    {
    530567        VBClLogFatalError("Failed to request display change events, rc=%Rrc\n", rc);
    531         return VERR_INVALID_HANDLE;
     568        return RTEXITCODE_FAILURE;
    532569    }
    533570    rc = VbglR3AcquireGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0, false);
    534571    if (rc == VERR_RESOURCE_BUSY)  /* Someone else has already acquired it. */
    535572    {
    536         return VERR_RESOURCE_BUSY;
     573        return RTEXITCODE_FAILURE;
    537574    }
    538575    if (RT_FAILURE(rc))
    539576    {
    540577        VBClLogFatalError("Failed to register resizing support, rc=%Rrc\n", rc);
    541         return VERR_INVALID_HANDLE;
    542     }
    543 
    544     drmMainLoop(hDevice);
    545 
    546     /** @todo this code never executed since we do not have yet a clean way to exit
    547      * main event loop above. */
     578        return RTEXITCODE_FAILURE;
     579    }
     580
     581    /* Setup signals: gracefully terminate on SIGINT, SIGTERM. */
     582    if (   signal(SIGINT, drmRequestShutdown) == SIG_ERR
     583        || signal(SIGTERM, drmRequestShutdown) == SIG_ERR)
     584    {
     585        VBClLogError("VBoxDRMClient: unable to setup signals\n");
     586        return RTEXITCODE_FAILURE;
     587    }
     588
     589    /* Attempt to start DRM resize task. */
     590    rc = RTThreadCreate(&drmResizeThread, drmResizeWorker, (void *)hDevice, 0,
     591                        RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, DRM_RESIZE_THREAD_NAME);
     592    if (RT_SUCCESS(rc))
     593    {
     594        rc = RTThreadWait(drmResizeThread, RT_INDEFINITE_WAIT, &rcDrmResizeThread);
     595        VBClLogInfo("VBoxDRMClient: %s thread exitted with status %Rrc\n", DRM_RESIZE_THREAD_NAME, rcDrmResizeThread);
     596        rc |= rcDrmResizeThread;
     597    }
    548598
    549599    RTFileClose(hDevice);
     
    552602    VbglR3ClosePidFile(g_szPidFile, hPidFile);
    553603
    554     return 0;
    555 }
     604    return rc == 0 ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
     605}
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