Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed game threads falling behind the schedule.
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
1 parent
f3272be
commit cffc21e
Showing
6 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters