Skip to content

Commit f7698da

Browse files
committed
threading works
1 parent 17f9c85 commit f7698da

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

gamemodes/empty/gamemode.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ def OnPlayerSelectObject(playerid, type, objectid, modelid, fX, fY, fZ):
167167

168168
def OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, fX, fY, fZ):
169169
return False
170-
170+
171171
def OnProcessTick():
172172
return None
173+
174+
""" you can initialize your own additional threads here, note that OnThreadingInit isn't supposed to be a worker thread but gives you the ability to start them. Otherwise it would block the plugin initialization """
175+
def OnThreadingInit():
176+
return None
177+
178+
""" your initialized threads must be shutdown during this callbac """
179+
def OnThreadingStopSignal():
180+
return None

src/main.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define VERSION "1.1.0-100"
12

23
#include <stdio.h>
34
#include <string.h>
@@ -13,7 +14,7 @@ bool threadingInitialized = false;
1314

1415
PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
1516
{
16-
return sampgdk::Supports() | SUPPORTS_PROCESS_TICK;
17+
return sampgdk::Supports() | SUPPORTS_PROCESS_TICK;
1718
}
1819

1920
PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
@@ -25,23 +26,34 @@ PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
2526
}
2627

2728
bool sgdk = sampgdk::Load(ppData);
28-
if (sgdk) {
29-
PySAMP::callback("OnThreadingInit", NULL);
30-
threading = std::thread([] { PySAMP::callback("OnThreadingInit", NULL); });
29+
if (sgdk)
30+
{
31+
sampgdk::logprintf("PySAMP %s", VERSION);
3132

33+
threadingInitialized = true;
34+
threading = std::thread([] {
35+
PyGILState_STATE state = PyGILState_Ensure();
36+
PySAMP::callback("OnThreadingInit", NULL);
37+
PyGILState_Release(state);
38+
});
39+
40+
3241
}
33-
return false;
42+
return sgdk;
3443
}
3544

3645
PLUGIN_EXPORT void PLUGIN_CALL Unload()
3746
{
38-
sampgdk::Unload();
47+
if(threadingInitialized)
48+
{
49+
PySAMP::callback("OnThreadingStopSignal", NULL);
50+
threading.join();
51+
}
52+
sampgdk::Unload();
3953
}
4054

4155
PLUGIN_EXPORT void PLUGIN_CALL ProcessTick()
4256
{
43-
PySAMP::callback("OnThreadingStopSignal", NULL);
44-
threading.join();
4557
sampgdk::ProcessTick();
4658
PySAMP::callback("OnProcessTick", NULL);
4759
}

src/pysamp/pygamemode.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ PyGamemode::PyGamemode(const char * path)
2525
GetCurrentDir(cCurrentPath, sizeof(cCurrentPath));
2626
char* absolute = strcat(cCurrentPath, path);
2727

28-
sampgdk::logprintf("Setting python workspace to %s", absolute);
2928
PyObject* sysPath = PySys_GetObject("path");
3029

3130
if(!sysPath)
@@ -36,11 +35,7 @@ PyGamemode::PyGamemode(const char * path)
3635
pName = PyUnicode_DecodeFSDefault("gamemode");
3736
pModule = PyImport_Import(pName);
3837

39-
if (pModule)
40-
{
41-
sampgdk::logprintf("PyGamemode loaded!");
42-
}
43-
else
38+
if (!pModule)
4439
{
4540
PyErr_Print();
4641
sampgdk::logprintf("PyGamemode::PyGamemode(%s) failed!", "gamemode.py");

src/pysamp/pysamp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ void PySAMP::load()
77
{
88
PySAMP::gamemode = new PyGamemode(PYTHON_PATH);
99

10-
if (!PyEval_ThreadsInitialized()) {
10+
if (!PyEval_ThreadsInitialized())
11+
{
1112
PyEval_InitThreads();
1213
}
1314
}

0 commit comments

Comments
 (0)