Skip to content

Commit

Permalink
Emscripten: re-enable the -pthread compiler flag (#9426)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-derevenetz authored Jan 11, 2025
1 parent 92395ac commit baebb96
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 61 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/make.yml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,46 @@ jobs:
run: |
sudo apt-get -y update
sudo apt-get -y install gettext p7zip-full
- name: Apply temporary patch
run: |
patch -d / -p0 -f <<EOF
--- emsdk/upstream/emscripten/tools/ports/sdl2_mixer.py
+++ emsdk/upstream/emscripten/tools/ports/sdl2_mixer.py
@@ -10,8 +10,10 @@
deps = ['sdl2']
variants = {
- 'sdl2_mixer_mp3': {'SDL2_MIXER_FORMATS': ["mp3"]},
- 'sdl2_mixer_none': {'SDL2_MIXER_FORMATS': []},
+ 'sdl2_mixer-mp3': {'SDL2_MIXER_FORMATS': ['mp3']},
+ 'sdl2_mixer-none': {'SDL2_MIXER_FORMATS': []},
+ 'sdl2_mixer-mp3-mt': {'SDL2_MIXER_FORMATS': ['mp3'], 'PTHREADS': 1},
+ 'sdl2_mixer-none-mt': {'SDL2_MIXER_FORMATS': [], 'PTHREADS': 1},
}
@@ -25,7 +27,9 @@ def get_lib_name(settings):
libname = 'libSDL2_mixer'
if formats != '':
- libname += '_' + formats
+ libname += '-' + formats
+ if settings.PTHREADS:
+ libname += '-mt'
libname += '.a'
return libname
@@ -68,6 +72,9 @@ def create(final):
'-DMUSIC_MID_TIMIDITY',
]
+ if settings.PTHREADS:
+ flags.append('-pthread')
+
build_dir = ports.clear_project_build('sdl2_mixer')
include_path = os.path.join(source_path, 'include')
includes = [
EOF
- name: Build
run: |
emmake make -f Makefile.emscripten -j "$(nproc)"
Expand Down
18 changes: 11 additions & 7 deletions src/dist/Makefile.emscripten
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@

TARGET := fheroes2.js

CCFLAGS := $(filter-out -pthread,$(CCFLAGS)) \
SDL2_MIXER_FORMATS := mid,mp3

CCFLAGS := $(CCFLAGS) \
--use-port=sdl2 \
--use-port=sdl2_mixer \
--use-port=zlib \
-sSDL2_MIXER_FORMATS=mid,mp3,ogg
-sSDL2_MIXER_FORMATS=$(SDL2_MIXER_FORMATS)

LDFLAGS := $(filter-out -pthread,$(LDFLAGS)) \
LDFLAGS := $(LDFLAGS) \
--preload-file ../../../files/data/resurrection.h2d@/files/data/resurrection.h2d \
--preload-file ../../../files/lang/@/files/lang/ \
--preload-file ../../../files/timidity/@/etc/ \
-sALLOW_MEMORY_GROWTH \
-sASYNCIFY \
-sASYNCIFY_STACK_SIZE=20480 \
-sENVIRONMENT=web \
-sASYNCIFY_STACK_SIZE=32768 \
-sENVIRONMENT=web,worker \
-sINITIAL_MEMORY=128mb \
-sNO_DISABLE_EXCEPTION_CATCHING \
-sSDL2_MIXER_FORMATS=mid,mp3,ogg \
-sSTACK_SIZE=262144 \
-sPTHREAD_POOL_SIZE=8 \
-sSDL2_MIXER_FORMATS=$(SDL2_MIXER_FORMATS) \
-sSTACK_SIZE=256kb \
-lidbfs.js

ifdef FHEROES2_WITH_DEBUG
Expand Down
54 changes: 0 additions & 54 deletions src/engine/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,10 @@
#include <cassert>
#include <memory>

#ifdef __EMSCRIPTEN__
namespace
{
class MutexUnlocker
{
public:
explicit MutexUnlocker( std::mutex & mutex )
: _mutex( mutex )
{
_mutex.unlock();
}

MutexUnlocker( const MutexUnlocker & ) = delete;

~MutexUnlocker()
{
_mutex.lock();
}

MutexUnlocker & operator=( const MutexUnlocker & ) = delete;

private:
std::mutex & _mutex;
};
}
#endif

namespace MultiThreading
{
void AsyncManager::createWorker()
{
// Emscripten has no pthread support. The worker thread should not be started.
#ifndef __EMSCRIPTEN__
if ( !_worker ) {
_runFlag = true;
_worker = std::make_unique<std::thread>( AsyncManager::_workerThread, this );
Expand All @@ -66,15 +37,10 @@ namespace MultiThreading
_masterNotification.wait( lock, [this] { return !_runFlag; } );
}
}
#endif
}

void AsyncManager::stopWorker()
{
#ifdef __EMSCRIPTEN__
// Emscripten has no pthread support. The worker thread should not be running here.
assert( !_worker );
#else
if ( _worker ) {
{
const std::scoped_lock<std::mutex> lock( _mutex );
Expand All @@ -88,33 +54,13 @@ namespace MultiThreading
_worker->join();
_worker.reset();
}
#endif
}

void AsyncManager::notifyWorker()
{
_runFlag = true;

// Emscripten has no pthread support, so instead of passing tasks to the worker thread, we will process them immediately in the current thread.
#ifdef __EMSCRIPTEN__
assert( !_exitFlag );

while ( _runFlag ) {
const bool moreTasks = prepareTask();
if ( !moreTasks ) {
_runFlag = false;
}

{
// In accordance with the contract, the _mutex should NOT be acquired while calling the executeTask().
MutexUnlocker unlocker( _mutex );

executeTask();
}
}
#else
_workerNotification.notify_all();
#endif
}

void AsyncManager::_workerThread( AsyncManager * manager )
Expand Down

0 comments on commit baebb96

Please sign in to comment.