Skip to content

Commit

Permalink
Use SDL threads (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
deveee committed Feb 26, 2023
1 parent d4880b5 commit 2452209
Show file tree
Hide file tree
Showing 19 changed files with 487 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Android/native/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ LOCAL_SRC_FILES := \
../../src/script/lua_api/l_util.cpp \
../../src/script/lua_api/l_vmanip.cpp \
$(wildcard ../../src/server/*.cpp) \
$(wildcard ../../src/threading/*.cpp) \
../../src/threading/event.cpp \
../../src/threading/sdl_semaphore.cpp \
../../src/threading/sdl_thread.cpp \
$(wildcard ../../src/util/*.c) \
$(wildcard ../../src/util/*.cpp) \
../../src/ban.cpp \
Expand Down
24 changes: 12 additions & 12 deletions Apple/MultiCraft/MultiCraft.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@
846F883B27A59704007B6210 /* libluajit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 846F883A27A596F9007B6210 /* libluajit.a */; };
847C6D4B25D6F483008F5FC8 /* lutf8lib.c in Sources */ = {isa = PBXBuildFile; fileRef = 847C6D4A25D6F483008F5FC8 /* lutf8lib.c */; };
8488539F28DA4857004AD90F /* libjpeg.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8488539E28DA4851004AD90F /* libjpeg.a */; };
848920FD29984994001CB194 /* sdl_semaphore.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 848920FA29984994001CB194 /* sdl_semaphore.cpp */; };
848920FE29984994001CB194 /* sdl_thread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 848920FB29984994001CB194 /* sdl_thread.cpp */; };
848ADEF427BD68AE001C60F3 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 848ADEF627BD68AE001C60F3 /* Localizable.strings */; };
848CE6E527778E38001D3E0F /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 848CE6E427778E30001D3E0F /* libz.tbd */; };
8495BD1628B80B14009FCF4D /* libSDL2.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8495BD1528B80B00009FCF4D /* libSDL2.a */; };
Expand All @@ -138,8 +140,6 @@
8495BD2A28B81170009FCF4D /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8495BD2928B81170009FCF4D /* Carbon.framework */; };
849D0847278AC1B200471354 /* lchacha.c in Sources */ = {isa = PBXBuildFile; fileRef = 849D0845278AC1B200471354 /* lchacha.c */; };
849D0848278AC1B200471354 /* chacha.c in Sources */ = {isa = PBXBuildFile; fileRef = 849D0846278AC1B200471354 /* chacha.c */; };
84A1F9B2252E616B00000717 /* semaphore.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 84A1F9AC252E616B00000717 /* semaphore.cpp */; };
84A1F9B3252E616B00000717 /* thread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 84A1F9AF252E616B00000717 /* thread.cpp */; };
84A1F9BB252E617D00000717 /* scripting_mainmenu.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 84A1F9B5252E617D00000717 /* scripting_mainmenu.cpp */; };
84A1F9BC252E617D00000717 /* scripting_client.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 84A1F9B8252E617D00000717 /* scripting_client.cpp */; };
84A1F9BD252E617D00000717 /* scripting_server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 84A1F9B9252E617D00000717 /* scripting_server.cpp */; };
Expand Down Expand Up @@ -547,6 +547,10 @@
847C6D4925D6F483008F5FC8 /* unidata.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = unidata.h; path = ../../../lib/luautf8/unidata.h; sourceTree = "<group>"; };
847C6D4A25D6F483008F5FC8 /* lutf8lib.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lutf8lib.c; path = ../../../lib/luautf8/lutf8lib.c; sourceTree = "<group>"; };
8488539E28DA4851004AD90F /* libjpeg.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjpeg.a; path = ../deps/libjpeg/libjpeg.a; sourceTree = "<group>"; };
848920F929984994001CB194 /* sdl_semaphore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = sdl_semaphore.h; path = ../../../src/threading/sdl_semaphore.h; sourceTree = "<group>"; };
848920FA29984994001CB194 /* sdl_semaphore.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = sdl_semaphore.cpp; path = ../../../src/threading/sdl_semaphore.cpp; sourceTree = "<group>"; };
848920FB29984994001CB194 /* sdl_thread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = sdl_thread.cpp; path = ../../../src/threading/sdl_thread.cpp; sourceTree = "<group>"; };
848920FC29984994001CB194 /* sdl_thread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = sdl_thread.h; path = ../../../src/threading/sdl_thread.h; sourceTree = "<group>"; };
848ADEF527BD68AE001C60F3 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
848ADEF827BD6904001C60F3 /* bg */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = bg; path = bg.lproj/Localizable.strings; sourceTree = "<group>"; };
848ADEF927BD695E001C60F3 /* cs */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = cs; path = cs.lproj/Localizable.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -578,13 +582,9 @@
849D0844278AC1B200471354 /* ecrypt-machine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "ecrypt-machine.h"; path = "../../../lib/luachacha/ecrypt-machine.h"; sourceTree = "<group>"; };
849D0845278AC1B200471354 /* lchacha.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lchacha.c; path = ../../../lib/luachacha/lchacha.c; sourceTree = "<group>"; };
849D0846278AC1B200471354 /* chacha.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = chacha.c; path = ../../../lib/luachacha/chacha.c; sourceTree = "<group>"; };
84A1F9A6252E616A00000717 /* thread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = thread.h; path = ../../../src/threading/thread.h; sourceTree = "<group>"; };
84A1F9A7252E616A00000717 /* mutex_auto_lock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = mutex_auto_lock.h; path = ../../../src/threading/mutex_auto_lock.h; sourceTree = "<group>"; };
84A1F9A8252E616A00000717 /* event.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = event.cpp; path = ../../../src/threading/event.cpp; sourceTree = "<group>"; };
84A1F9AC252E616B00000717 /* semaphore.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = semaphore.cpp; path = ../../../src/threading/semaphore.cpp; sourceTree = "<group>"; };
84A1F9AD252E616B00000717 /* semaphore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = semaphore.h; path = ../../../src/threading/semaphore.h; sourceTree = "<group>"; };
84A1F9AE252E616B00000717 /* event.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = event.h; path = ../../../src/threading/event.h; sourceTree = "<group>"; };
84A1F9AF252E616B00000717 /* thread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = thread.cpp; path = ../../../src/threading/thread.cpp; sourceTree = "<group>"; };
84A1F9B5252E617D00000717 /* scripting_mainmenu.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = scripting_mainmenu.cpp; path = ../../../src/script/scripting_mainmenu.cpp; sourceTree = "<group>"; };
84A1F9B6252E617D00000717 /* scripting_client.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = scripting_client.h; path = ../../../src/script/scripting_client.h; sourceTree = "<group>"; };
84A1F9B7252E617D00000717 /* scripting_mainmenu.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = scripting_mainmenu.h; path = ../../../src/script/scripting_mainmenu.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1544,10 +1544,10 @@
84A1F9A8252E616A00000717 /* event.cpp */,
84A1F9AE252E616B00000717 /* event.h */,
84A1F9A7252E616A00000717 /* mutex_auto_lock.h */,
84A1F9AC252E616B00000717 /* semaphore.cpp */,
84A1F9AD252E616B00000717 /* semaphore.h */,
84A1F9AF252E616B00000717 /* thread.cpp */,
84A1F9A6252E616A00000717 /* thread.h */,
848920FA29984994001CB194 /* sdl_semaphore.cpp */,
848920F929984994001CB194 /* sdl_semaphore.h */,
848920FB29984994001CB194 /* sdl_thread.cpp */,
848920FC29984994001CB194 /* sdl_thread.h */,
);
name = threading;
sourceTree = "<group>";
Expand Down Expand Up @@ -1819,7 +1819,6 @@
84135C2125D526D700CA4DCF /* sound.cpp in Sources */,
84135B8B25D5264C00CA4DCF /* noise.cpp in Sources */,
84F20E2A25D5282A009562A9 /* l_minimap.cpp in Sources */,
84A1F9B2252E616B00000717 /* semaphore.cpp in Sources */,
84F20F1825D52958009562A9 /* guiScrollContainer.cpp in Sources */,
84135B6425D5264B00CA4DCF /* map_settings_manager.cpp in Sources */,
84F20F4425D52975009562A9 /* dungeongen.cpp in Sources */,
Expand Down Expand Up @@ -1871,6 +1870,7 @@
84F20F4D25D52975009562A9 /* mapgen_v7.cpp in Sources */,
84F20E8F25D52868009562A9 /* directiontables.cpp in Sources */,
84F20E5625D5283F009562A9 /* player_sao.cpp in Sources */,
848920FD29984994001CB194 /* sdl_semaphore.cpp in Sources */,
84135B8C25D5264C00CA4DCF /* serverenvironment.cpp in Sources */,
84F20E5325D5283F009562A9 /* luaentity_sao.cpp in Sources */,
84F20F1425D52958009562A9 /* guiKeyChangeMenu.cpp in Sources */,
Expand All @@ -1887,6 +1887,7 @@
84135C1425D526D700CA4DCF /* inputhandler.cpp in Sources */,
84F20E3E25D5282A009562A9 /* l_vmanip.cpp in Sources */,
84F20E3325D5282A009562A9 /* l_areastore.cpp in Sources */,
848920FE29984994001CB194 /* sdl_thread.cpp in Sources */,
84F20DB725D527D8009562A9 /* c_converter.cpp in Sources */,
84135B7125D5264B00CA4DCF /* tool.cpp in Sources */,
84135BA125D5264C00CA4DCF /* serialization.cpp in Sources */,
Expand Down Expand Up @@ -1979,7 +1980,6 @@
84135B7F25D5264C00CA4DCF /* version.cpp in Sources */,
84F20F0C25D52958009562A9 /* guiAnimatedImage.cpp in Sources */,
84135C1A25D526D700CA4DCF /* clientmap.cpp in Sources */,
84A1F9B3252E616B00000717 /* thread.cpp in Sources */,
84F20F0125D52958009562A9 /* guiInventoryList.cpp in Sources */,
84F20EA525D528C5009562A9 /* factory.cpp in Sources */,
84F20E2425D5282A009562A9 /* l_env.cpp in Sources */,
Expand Down
6 changes: 6 additions & 0 deletions src/ban.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#pragma once

#include "IrrCompileConfig.h"

#include "util/string.h"
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
#include "threading/sdl_thread.h"
#else
#include "threading/thread.h"
#endif
#include "exceptions.h"
#include <map>
#include <string>
Expand Down
6 changes: 6 additions & 0 deletions src/network/connectionthreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#pragma once

#include "IrrCompileConfig.h"

#include <cassert>
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
#include "threading/sdl_thread.h"
#else
#include "threading/thread.h"
#endif
#include "connection.h"

namespace con
Expand Down
8 changes: 6 additions & 2 deletions src/porting_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#error This file may only be compiled for android!
#endif

#include "IrrCompileConfig.h"

#include "util/numeric.h"
#include "porting.h"
#include "porting_android.h"
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
#include "threading/sdl_thread.h"
#else
#include "threading/thread.h"
#endif
#include "config.h"
#include "filesys.h"
#include "log.h"
Expand Down Expand Up @@ -54,8 +60,6 @@ extern "C" int SDL_main(int argc, char *argv[])
return 0;
}

Thread::setName("Main");

try {
char *argv[] = {strdup(PROJECT_NAME), nullptr};
real_main(ARRLEN(argv) - 1, argv);
Expand Down
7 changes: 7 additions & 0 deletions src/script/cpp_api/s_async.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#pragma once

#include "IrrCompileConfig.h"

#include <vector>
#include <deque>
#include <map>

#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
#include "threading/sdl_semaphore.h"
#include "threading/sdl_thread.h"
#else
#include "threading/semaphore.h"
#include "threading/thread.h"
#endif
#include "lua.h"
#include "cpp_api/s_base.h"

Expand Down
6 changes: 6 additions & 0 deletions src/terminal_chat_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#pragma once

#include "IrrCompileConfig.h"

#include "chat.h"
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
#include "threading/sdl_thread.h"
#else
#include "threading/thread.h"
#endif
#include "util/container.h"
#include "log.h"
#include <sstream>
Expand Down
2 changes: 2 additions & 0 deletions src/threading/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ set(JTHREAD_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/event.cpp
${CMAKE_CURRENT_SOURCE_DIR}/thread.cpp
${CMAKE_CURRENT_SOURCE_DIR}/semaphore.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sdl_semaphore.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sdl_thread.cpp
PARENT_SCOPE)

71 changes: 71 additions & 0 deletions src/threading/sdl_semaphore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
MultiCraft
Copyright (C) 2013 sapier <sapier AT gmx DOT net>
Copyright (C) 2023 Dawid Gan <deveee@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3.0 of the License, or
(at your option) any later version.
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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "threading/sdl_semaphore.h"

#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_

#include <cassert>

#define UNUSED(expr) \
do { \
(void)(expr); \
} while (0)

Semaphore::Semaphore(int val)
{
semaphore = SDL_CreateSemaphore(val);
}

Semaphore::~Semaphore()
{
SDL_DestroySemaphore(semaphore);
}

void Semaphore::post(unsigned int num)
{
assert(num > 0);
for (unsigned i = 0; i < num; i++) {
int ret = SDL_SemPost(semaphore);
assert(!ret);
UNUSED(ret);
}
}

void Semaphore::wait()
{
int ret = SDL_SemWait(semaphore);
assert(!ret);
UNUSED(ret);
}

bool Semaphore::wait(unsigned int time_ms)
{
int ret;
if (time_ms > 0) {
ret = SDL_SemWaitTimeout(semaphore, time_ms);
} else {
ret = SDL_SemTryWait(semaphore);
}
assert(!ret || ret == SDL_MUTEX_TIMEDOUT);
return !ret;
}

#endif
48 changes: 48 additions & 0 deletions src/threading/sdl_semaphore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
MultiCraft
Copyright (C) 2013 sapier <sapier AT gmx DOT net>
Copyright (C) 2023 Dawid Gan <deveee@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3.0 of the License, or
(at your option) any later version.
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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include "IrrCompileConfig.h"

#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_

#include "porting.h"
#include "util/basic_macros.h"

#include <SDL.h>

class Semaphore
{
public:
Semaphore(int val = 0);
~Semaphore();

DISABLE_CLASS_COPY(Semaphore);

void post(unsigned int num = 1);
void wait();
bool wait(unsigned int time_ms);

private:
SDL_sem *semaphore;
};

#endif

0 comments on commit 2452209

Please sign in to comment.