thread.h revision b72771e8c6ba3b3d9ebdd7977730325131ae0f98
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/** @file
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * VirtualBox additions client application: thread class.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * available from http://www.virtualbox.org. This file is free software;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * you can redistribute it and/or modify it under the terms of the GNU
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * General Public License (GPL) as published by the Free Software
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * additional information or have any questions.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#ifndef __Additions_client_thread_h
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# define __Additions_client_thread_h
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <iprt/thread.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <iprt/err.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <VBox/log.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncclass VBoxGuestThread;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/** Virtual parent class for thread functions for the VBoxGuestThread class. */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncclass VBoxGuestThreadFunction
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncpublic:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync // VBoxGuestThreadFunction(void);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync virtual ~VBoxGuestThreadFunction(void) {}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /**
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The actual thread function.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * @returns iprt status code as thread return value
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * @param pParent the VBoxGuestThread running this thread function
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync virtual int threadFunction(VBoxGuestThread *pPThread) = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /**
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Send a signal to the thread function that it should exit. This should not block.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync virtual void stop(void) = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync};
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/** C++ wrapper for VBox runtime threads. */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncclass VBoxGuestThread
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncprivate:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync // Private member variables
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /** The thread function for this thread */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync VBoxGuestThreadFunction *mFunction;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /** The size of the stack for the new thread. Use 0 for the default stack size. */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync size_t mStack;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /** The thread type. Used for deciding scheduling attributes of the thread. */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync RTTHREADTYPE mType;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /** Flags of the RTTHREADFLAGS type (ORed together). */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync unsigned mFlags;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /** Thread name */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync const char *mName;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /** The VBox runtime thread handle. */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync RTTHREAD mSelf;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /** Is the thread currently running? */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync volatile bool mRunning;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /** Should the thread be stopped? */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync volatile bool mExit;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync // Typedefs
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /** Ourselves, for use in the thread function. */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync typedef VBoxGuestThread *PSELF;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncpublic:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /**
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Initialise the class.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * @param pFunction the thread function for this thread
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * @param cbStack The size of the stack for the new thread.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Use 0 for the default stack size.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * @param enmType The thread type. Used for deciding scheduling attributes
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * of the thread.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * @param fFlags Flags of the RTTHREADFLAGS type (ORed together).
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * @param pszName Thread name.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync VBoxGuestThread(VBoxGuestThreadFunction *pFunction, size_t cbStack, RTTHREADTYPE enmType,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync unsigned fFlags, const char *pszName)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync {
mFunction = pFunction;
mStack = cbStack;
mType = enmType;
mFlags = fFlags;
mName = pszName;
mSelf = NIL_RTTHREAD;
mRunning = false;
mExit = false;
}
/** Stop the thread using its stop method and get the exit value.
* @returns iprt status code
* @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
* an indefinite wait. Only relevant if the thread is
* waitable.
* @param prc Where to store the return code of the thread. Optional.
*/
int stop(unsigned cMillies, int *prc);
/** Destroy the class, stopping the thread if necessary. */
~VBoxGuestThread(void);
/** Return the VBox runtime thread handle. */
RTTHREAD getSelf(void) { return mSelf; }
/** Start the thread. */
int start(void);
/** Yield the CPU */
bool yield(void);
/** Is the thread running? */
bool isRunning(void) { return mRunning; }
/** Should the thread function exit? */
bool isStopping(void) { return mExit; }
private:
// Copying or assigning a thread object is not sensible
VBoxGuestThread(const VBoxGuestThread&);
VBoxGuestThread& operator=(const VBoxGuestThread&);
// Member functions
/** The "real" thread function for the VBox runtime. */
static DECLCALLBACK(int) threadFunction(RTTHREAD self, void *pvUser);
};
#endif /* __Additions_client_thread_h not defined */