Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ libhxcpp 6.0.3 (unreleased)
* Fixed sys.ssl.Certificate.loadFile and loadPath reporting certificates that did not parse as an unrelated mbedTLS error: mbedTLS counts them, and the count was read as an error code, so one threw "ERROR - Generic error" and two a BIGNUM file error. They throw "Could not parse 2 certificates in <file>" now, and for a directory "certificates or files", as mbedTLS also counts a file that holds none (HaxeFoundation/hxcpp@45adede6)
* Fixed cppia's interpreter running a var statement with no declarations, which a malformed file can hold, from the first element of its empty list: undefined behaviour, on which a standard library that checks indexing stops the program (HaxeFoundation/hxcpp@a8a5724f)
* A cppia script reads and writes a host class's static object fields, such as a singleton, a static Array or a static function, directly again below API level 500, which includes Haxe 5.0.0-preview.1, instead of looking each up by name: a loop that reads one host static Array and stores another runs 1.8 times as fast interpreted and 12 times as fast under the JIT. Host statics of other types and object fields already were direct; API level 500 keeps the lookup, which a typed function needs (HaxeFoundation/hxcpp#1365)
* Fixed the process ending with std::terminate where the platform cannot detach a thread, as on Switch with devkitA64, whose pthread_detach returns ENOSYS: std::thread::detach threw and the std::thread, still joinable, was destroyed, at the first Thread.create or when the collector, the file closer, the process reaper, the profiler or telemetry started a thread. The thread is kept and runs to its end as it would detached, and the runtime says once on stderr that finished threads will not be reclaimed, which only the platform can do (libhxcpp #502)

libhxcpp 6.0.2
------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/hx/Profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mutex>
#include <thread>
#include <chrono>
#include "thread/Detach.hpp"


#ifdef HX_WINRT
Expand Down Expand Up @@ -51,7 +52,7 @@ class Profiler

if (gThreadRefCount == 1) {
std::thread thread(ProfileMainLoop);
thread.detach();
hx::DetachThread(thread);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/hx/Telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <mutex>
#include <thread>
#include <chrono>
#include "thread/Detach.hpp"

namespace hx
{
Expand Down Expand Up @@ -70,7 +71,7 @@ class Telemetry
if (gThreadRefCount == 1) {
std::thread thread(ProfileMainLoop);

thread.detach();
hx::DetachThread(thread);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/hx/gc/Immix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <hx/thread/Thread.hpp>
#include "../Hash.h"
#include "../FastMutex.h"
#include "../thread/Detach.hpp"
#include "GcRegCapture.h"
#include <unordered_set>
#include <unordered_map>
Expand Down Expand Up @@ -4316,7 +4317,7 @@ class GlobalAllocator
try
{
std::thread thread(SReleaseLoop, this);
thread.detach();
hx::DetachThread(thread);
mReleaseStarted = true;
}
catch(...)
Expand Down Expand Up @@ -5739,7 +5740,7 @@ class GlobalAllocator

std::thread thread(SThreadLoop, info);

thread.detach();
hx::DetachThread(thread);
#endif
}

Expand Down
4 changes: 3 additions & 1 deletion src/hx/libs/std/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <thread>
#include <mutex>
#include <condition_variable>
#include "../../thread/Detach.hpp"
#endif

#include <hx/OS.h>
Expand Down Expand Up @@ -109,7 +110,8 @@ struct LateCloser
{
try
{
std::thread([this]() { run(); }).detach();
std::thread thread([this]() { run(); });
hx::DetachThread(thread);
}
catch(...)
{
Expand Down
4 changes: 3 additions & 1 deletion src/hx/libs/std/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#if !defined(HX_WINRT) && !defined(EPPC) && !defined(NEKO_WINDOWS) && !defined(__EMSCRIPTEN__) && !defined(HXCPP_SINGLE_THREADED_APP)
#define HX_PROCESS_REAPER
#include <thread>
#include "../../thread/Detach.hpp"
#include <mutex>
#include <chrono>
#endif
Expand Down Expand Up @@ -174,7 +175,8 @@ namespace
{
try
{
std::thread(reaperLoop).detach();
std::thread thread(reaperLoop);
hx::DetachThread(thread);
sReaper.running = true;
}
catch(...)
Expand Down
32 changes: 32 additions & 0 deletions src/hx/thread/Detach.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <thread>
#include <atomic>
#include <exception>
#include <utility>
#include <stdio.h>

namespace hx
{
// Detaches a std::thread the runtime will never join. Where the platform
// cannot detach, as devkitA64's newlib on Switch, whose pthread_detach
// returns ENOSYS, std::thread::detach throws, and the joinable std::thread
// then ended the process with std::terminate when it was destroyed, even
// inside a catch. The thread is kept instead: it runs to its end as it
// would detached, and what the platform holds for it is not reclaimed.
inline void DetachThread(std::thread &ioThread)
{
try
{
ioThread.detach();
}
catch (const std::exception &e)
{
static std::atomic<bool> sReported(false);
if (!sReported.exchange(true))
fprintf(stderr, "Could not detach a thread (%s): finished threads will not be reclaimed\n", e.what());
// Never destroyed, as destroying a joinable std::thread terminates
new std::thread(std::move(ioThread));
}
}
}
5 changes: 3 additions & 2 deletions src/hx/thread/ThreadImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <system_error>
#include <hx/thread/CountingSemaphore.hpp>
#include "ThreadImpl.hpp"
#include "Detach.hpp"

namespace
{
Expand Down Expand Up @@ -206,7 +207,7 @@ hx::thread::ThreadImpl_obj::Native::Native(Thread_obj::CreateFunction _job, Thre
// This does feel like a bit of a bodge and something which will actually need to be addressed to add name setting on Linux.

#ifndef HX_WINDOWS
thread.detach();
hx::DetachThread(thread);
#endif
}

Expand Down Expand Up @@ -269,7 +270,7 @@ void hx::thread::ThreadImpl_obj::finalise(hx::Object* obj)

if (native->thread.joinable())
{
native->thread.detach();
hx::DetachThread(native->thread);
}
}

Expand Down
63 changes: 63 additions & 0 deletions test/regression/ThreadDetachFailure/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import sys.thread.Lock;
import sys.thread.Thread;

// Threads where the platform cannot detach one. devkitA64's newlib on Switch
// has no pthread_detach hook, so it returns ENOSYS, std::thread::detach
// throws, and the joinable std::thread being destroyed ended the process
// with std::terminate at the first Thread.create, or when the collector
// started a worker. On glibc this program replaces pthread_detach with one
// that does the same. Elsewhere it cannot, and prints what is expected.
@:cppFileCode('
#if defined(__linux__)
#include <pthread.h>
#include <errno.h>
#endif
#include <atomic>

#if defined(__linux__) && defined(__GLIBC__)
static std::atomic<int> refusedDetaches(0);

extern "C" __attribute__((visibility("default"))) int pthread_detach(pthread_t) __THROW
{
refusedDetaches++;
return ENOSYS;
}

static bool detachRefused() { return refusedDetaches > 0; }
#else
static bool detachRefused() { return true; }
#endif
')
class Main {
static function main() {
final count = 32;
final done = new Lock();
var ran = 0;
final mutex = new sys.thread.Mutex();
for (i in 0...count) {
Thread.create(() -> {
// Allocates enough for collections to run while threads come and go
var keep = [];
for (j in 0...2000) {
keep.push([j, i]);
}
cpp.vm.Gc.run(false);
mutex.acquire();
ran++;
mutex.release();
done.release();
});
}
var all = true;
for (i in 0...count) {
if (!done.wait(30)) {
all = false;
break;
}
}
cpp.vm.Gc.run(true);
final refused:Bool = untyped __cpp__("detachRefused()");
Sys.println("threads created after a refused detach run to their end: " + (all && ran == count));
Sys.println("the platform refused to detach them: " + refused);
}
}
2 changes: 2 additions & 0 deletions test/regression/ThreadDetachFailure/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-m Main
--cpp bin
2 changes: 2 additions & 0 deletions test/regression/ThreadDetachFailure/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
threads created after a refused detach run to their end: true
the platform refused to detach them: true
1 change: 1 addition & 0 deletions test/regression/emscripten-skip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ SslNativeMemoryPressure: threads (std::thread, which needs a pthreads build)
SslPeerCertificateLifetime: sockets
SslSniLifetime: sockets
SslSniName: sockets
ThreadDetachFailure: threads
ThreadEndsAtExit: threads
ThreadEndsAtExitDebugger: threads
ThreadEndsWhileCollecting: threads
Expand Down
1 change: 1 addition & 0 deletions toolchain/haxe-target-threads.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<xml>
<depend name="${HXCPP}/src/hx/thread/ThreadImpl.hpp"/>
<depend name="${HXCPP}/src/hx/thread/Detach.hpp"/>

<file name="src/hx/thread/RecursiveMutex.cpp"/>
<file name="src/hx/thread/ConditionVariable.cpp"/>
Expand Down
1 change: 1 addition & 0 deletions toolchain/haxe-target.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
<depend name="${HXCPP}/src/hx/gc/Incremental.h"/>
<depend name="${HXCPP}/src/hx/gc/IncrementalApi.h"/>
<depend name="${HXCPP}/src/hx/thread/ThreadImpl.hpp"/>
<depend name="${HXCPP}/src/hx/thread/Detach.hpp"/>
<depend name="${HXCPP}/include/hx/DynamicImpl.h"/>
<depend name="${HXCPP}/include/hx/Memory.h"/>
<depend name="${HXCPP}/include/hx/NekoFunc.h"/>
Expand Down
Loading