//---------------------------------------------------------------------
void DefaultWorkQueue::startup(bool forceRestart)
{
    if (mIsRunning)
    {
        if (forceRestart)
            shutdown();
        else
            return;
    }

    mShuttingDown = false;

    mWorkerFunc = OGRE_NEW_T(WorkerFunc(this), MEMCATEGORY_GENERAL);

    LogManager::getSingleton().stream() <<
                                        "DefaultWorkQueue('" << mName << "') initialising on thread " <<
#if OGRE_THREAD_SUPPORT
                                        OGRE_THREAD_CURRENT_ID
#else
                                        "main"
#endif
                                        << ".";

#if OGRE_THREAD_SUPPORT
    if (mWorkerRenderSystemAccess)
        Root::getSingleton().getRenderSystem()->preExtraThreadsStarted();

    mNumThreadsRegisteredWithRS = 0;
    for (uint8 i = 0; i < mWorkerThreadCount; ++i)
    {
        OGRE_THREAD_CREATE(t, *mWorkerFunc);
        mWorkers.push_back(t);
    }

    if (mWorkerRenderSystemAccess)
    {
        OGRE_LOCK_MUTEX_NAMED(mInitMutex, initLock);
        // have to wait until all threads are registered with the render system
        while (mNumThreadsRegisteredWithRS < mWorkerThreadCount)
            OGRE_THREAD_WAIT(mInitSync, mInitMutex, initLock);

        Root::getSingleton().getRenderSystem()->postExtraThreadsStarted();

    }
#endif

    mIsRunning = true;
}
	//------------------------------------------------------------------------
	void ResourceBackgroundQueue::initialise(void)
	{
#if OGRE_THREAD_SUPPORT
		if (mStartThread)
		{
			{
				OGRE_LOCK_AUTO_MUTEX
				mShuttingDown = false;
			}

#if OGRE_THREAD_SUPPORT == 1
			RenderSystem* rs = Root::getSingleton().getRenderSystem();
#endif

			LogManager::getSingleton().logMessage(
				"ResourceBackgroundQueue - threading enabled, starting own thread");
			{
				OGRE_LOCK_MUTEX_NAMED(initMutex, initLock)

#if OGRE_THREAD_SUPPORT == 1
				// Call thread creation pre-hook
				rs->preExtraThreadsStarted();
#endif

				mThread = OGRE_NEW_T(boost::thread, MEMCATEGORY_RESOURCE)(
					boost::function0<void>(&ResourceBackgroundQueue::threadFunc));
				// Wait for init to finish before allowing main thread to continue
				// this releases the initMutex until notified
				OGRE_THREAD_WAIT(initSync, initLock)

#if OGRE_THREAD_SUPPORT == 1
				// Call thread creation post-hook
				rs->postExtraThreadsStarted();
#endif
			}

		}
		else
		{
			LogManager::getSingleton().logMessage(
				"ResourceBackgroundQueue - threading enabled, user thread");
		}
#else
		LogManager::getSingleton().logMessage(
			"ResourceBackgroundQueue - threading disabled");	
#endif
	}
	//---------------------------------------------------------------------
	void DefaultWorkQueue::waitForNextRequest()
	{
#if OGRE_THREAD_SUPPORT
		// Lock; note that OGRE_THREAD_WAIT will free the lock
		OGRE_LOCK_MUTEX_NAMED(mRequestMutex, queueLock);
		if (mRequestQueue.empty())
		{
			// frees lock and suspends the thread
			OGRE_THREAD_WAIT(mRequestCondition, mRequestMutex, queueLock);
		}
		// When we get back here, it's because we've been notified 
		// and thus the thread has been woken up. Lock has also been
		// re-acquired, but we won't use it. It's safe to try processing and fail
		// if another thread has got in first and grabbed the request
#endif

	}