Skip to content

Commit

Permalink
Fixed game threads falling behind the schedule.
Browse files Browse the repository at this point in the history
When the interpreter is not actively performing locking tasks in the main thread, it goes into a relaxed state and only work on threaded calls every 5000µs. Normally, this should be frequent enough. However, these intervals fall behind because the server also sleeps it off between game frames. This effectively result into pending work only being performed every ~1250000µs instead.

By poking the system every frame, we ensure the interpreter attempts a switch if it's not busy with something else deemed more important at that time.
  • Loading branch information
jordanbriere committed Jul 23, 2023
1 parent f3272be commit cffc21e
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 0 deletions.
13 changes: 13 additions & 0 deletions addons/source-python/packages/source-python/listeners/tick.py
Expand Up @@ -23,6 +23,14 @@
)


# =============================================================================
# >> FORWARD IMPORTS
# =============================================================================
# Source.Python Imports
# Listeners
from _listeners._tick import ThreadPoker


# =============================================================================
# >> ALL DECLARATION
# =============================================================================
Expand All @@ -31,6 +39,7 @@
'GameThread',
'Repeat',
'RepeatStatus',
'ThreadPoker',
)


Expand Down Expand Up @@ -60,6 +69,10 @@ def _unload_instance(self):
f'Thread "{self.name}" ({self.ident}) from "{self._caller}" '
f'is running even though its plugin has been unloaded!')

def run(self, *args, **kwargs):
with ThreadPoker():
super().run(*args, **kwargs)


# =============================================================================
# >> DELAY CLASSES
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Expand Up @@ -314,11 +314,14 @@ Set(SOURCEPYTHON_KEYVALUES_MODULE_SOURCES
# ------------------------------------------------------------------
Set(SOURCEPYTHON_LISTENERS_MODULE_HEADERS
core/modules/listeners/listeners_manager.h
core/modules/listeners/listeners_tick.h
)

Set(SOURCEPYTHON_LISTENERS_MODULE_SOURCES
core/modules/listeners/listeners_manager.cpp
core/modules/listeners/listeners_wrap.cpp
core/modules/listeners/listeners_tick.cpp
core/modules/listeners/listeners_tick_wrap.cpp
)

# ------------------------------------------------------------------
Expand Down
68 changes: 68 additions & 0 deletions src/core/modules/listeners/listeners_tick.cpp
@@ -0,0 +1,68 @@
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2023 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/

//-----------------------------------------------------------------------------
// Includes.
//-----------------------------------------------------------------------------
#include "listeners_tick.h"


//-----------------------------------------------------------------------------
// CThreadPoker initialization.
//-----------------------------------------------------------------------------
unsigned long CThreadPoker::s_nRefCount = 0;


//-----------------------------------------------------------------------------
// CThreadPoker class.
//-----------------------------------------------------------------------------
PyObject *CThreadPoker::__enter__(PyObject *pSelf)
{
++s_nRefCount;

Py_INCREF(pSelf);
return pSelf;
}

void CThreadPoker::__exit__(PyObject *pSelf, PyObject *, PyObject *, PyObject *)
{
if (!s_nRefCount) {
return;
}

--s_nRefCount;
}

void CThreadPoker::Poke()
{
if (!s_nRefCount) {
return;
}

PyThreadState *pState = PyEval_SaveThread();
system(";");
PyEval_RestoreThread(pState);
}
52 changes: 52 additions & 0 deletions src/core/modules/listeners/listeners_tick.h
@@ -0,0 +1,52 @@
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2023 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/

#ifndef _LISTENERS_TICK_H
#define _LISTENERS_TICK_H

//-----------------------------------------------------------------------------
// Includes.
//-----------------------------------------------------------------------------
#include "boost/python.hpp"


//-----------------------------------------------------------------------------
// CThreadPoker class.
//-----------------------------------------------------------------------------
class CThreadPoker
{
private:
static unsigned long s_nRefCount;

public:
static PyObject *__enter__(PyObject *pSelf);
static void __exit__(PyObject *pSelf, PyObject *, PyObject *, PyObject *);

static void Poke();
};


#endif // _LISTENERS_TICK_H
72 changes: 72 additions & 0 deletions src/core/modules/listeners/listeners_tick_wrap.cpp
@@ -0,0 +1,72 @@
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2023 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/

//-----------------------------------------------------------------------------
// Includes.
//-----------------------------------------------------------------------------
#include "export_main.h"
#include "listeners_tick.h"
#include "modules/memory/memory_tools.h"


//-----------------------------------------------------------------------------
// Namespaces.
//-----------------------------------------------------------------------------
using namespace boost::python;


//-----------------------------------------------------------------------------
// Forward declarations.
//-----------------------------------------------------------------------------
static void export_thread_poker(scope);


//-----------------------------------------------------------------------------
// Declare the _listeners._tick module.
//-----------------------------------------------------------------------------
DECLARE_SP_SUBMODULE(_listeners, _tick)
{
export_thread_poker(_tick);
}


//-----------------------------------------------------------------------------
// Exports CThreadPoker.
//-----------------------------------------------------------------------------
void export_thread_poker(scope _tick)
{
class_<CThreadPoker> ThreadPoker("ThreadPoker");

// Special methods...
ThreadPoker.def("__enter__", &CThreadPoker::__enter__);
ThreadPoker.def("__exit__", &CThreadPoker::__exit__);

// Methods...
ThreadPoker.def("poke", &CThreadPoker::Poke).staticmethod("poke");

// Add memory tools...
ThreadPoker ADD_MEM_TOOLS(CThreadPoker);
}
4 changes: 4 additions & 0 deletions src/core/sp_main.cpp
Expand Up @@ -58,6 +58,7 @@
#include "manager.h"

#include "modules/listeners/listeners_manager.h"
#include "modules/listeners/listeners_tick.h"
#include "utilities/conversions.h"
#include "modules/entities/entities.h"
#include "modules/entities/entities_entity.h"
Expand Down Expand Up @@ -381,6 +382,9 @@ void CSourcePython::ServerActivate( edict_t *pEdictList, int edictCount, int cli
void CSourcePython::GameFrame( bool simulating )
{
CALL_LISTENERS(OnTick);

// Poke active game threads
CThreadPoker::Poke();
}

//-----------------------------------------------------------------------------
Expand Down

0 comments on commit cffc21e

Please sign in to comment.