core/frame_hooks.cpp
changeset 2476 c21fd6e89ff5
parent 2289 44045ead61cd
child 2726 c96f3fab8211
     1.1 --- a/core/frame_hooks.cpp	Sat Dec 27 19:50:13 2008 -0500
     1.2 +++ b/core/frame_hooks.cpp	Sun Dec 28 01:02:05 2008 -0500
     1.3 @@ -36,13 +36,64 @@
     1.4  #include "MenuStyle_Radio.h"
     1.5  #include "PlayerManager.h"
     1.6  #include "CoreConfig.h"
     1.7 +#include <sm_queue.h>
     1.8 +#include "ThreadSupport.h"
     1.9  
    1.10 -float g_LastMenuTime = 0.0f;
    1.11 -float g_LastAuthCheck = 0.0f;
    1.12 +static IMutex *frame_mutex;
    1.13 +static Queue<FrameAction> *frame_queue;
    1.14 +static Queue<FrameAction> *frame_actions;
    1.15 +static float g_LastMenuTime = 0.0f;
    1.16 +static float g_LastAuthCheck = 0.0f;
    1.17  bool g_PendingInternalPush = false;
    1.18  
    1.19 +class FrameActionInit : public SMGlobalClass
    1.20 +{
    1.21 +public:
    1.22 +	void OnSourceModAllInitialized()
    1.23 +	{
    1.24 +		frame_queue = new Queue<FrameAction>();
    1.25 +		frame_actions = new Queue<FrameAction>();
    1.26 +		frame_mutex = g_pThreader->MakeMutex();
    1.27 +	}
    1.28 +
    1.29 +	void OnSourceModShutdown()
    1.30 +	{
    1.31 +		delete frame_queue;
    1.32 +		delete frame_actions;
    1.33 +		frame_mutex->DestroyThis();
    1.34 +	}
    1.35 +} s_FrameActionInit;
    1.36 +
    1.37 +void AddFrameAction(const FrameAction & action)
    1.38 +{
    1.39 +	frame_mutex->Lock();
    1.40 +	frame_queue->push(action);
    1.41 +	frame_mutex->Unlock();
    1.42 +}
    1.43 +
    1.44  void RunFrameHooks(bool simulating)
    1.45  {
    1.46 +	/* It's okay if this check races. */
    1.47 +	if (frame_queue->size())
    1.48 +	{
    1.49 +		Queue<FrameAction> *temp;
    1.50 +
    1.51 +		/* Very quick lock to move queue/actions back and forth */
    1.52 +		frame_mutex->Lock();
    1.53 +		temp = frame_queue;
    1.54 +		frame_queue = frame_actions;
    1.55 +		frame_actions = temp;
    1.56 +		frame_mutex->Unlock();
    1.57 +
    1.58 +		/* The server will now be adding to the other queue, so we can process events. */
    1.59 +		while (!frame_actions->empty())
    1.60 +		{
    1.61 +			FrameAction &item = frame_actions->first();
    1.62 +			frame_actions->pop();
    1.63 +			item.action(item.data);
    1.64 +		}
    1.65 +	}
    1.66 +
    1.67  	/* Frame based hooks */
    1.68  	g_DBMan.RunFrame();
    1.69  	g_HL2.ProcessFakeCliCmdQueue();