Skip to content

Fix GCC 11 gthr-amigaos-native thread cleanup and I/O inheritance - #47

Open
derfsss wants to merge 1 commit into
AmigaLabs:masterfrom
derfsss:fix-gcc11-gthr-thread-cleanup
Open

Fix GCC 11 gthr-amigaos-native thread cleanup and I/O inheritance#47
derfsss wants to merge 1 commit into
AmigaLabs:masterfrom
derfsss:fix-gcc11-gthr-thread-cleanup

Conversation

@derfsss

@derfsss derfsss commented Apr 10, 2026

Copy link
Copy Markdown

Fix two issues in GCC's native AmigaOS thread implementation (libgcc/gthr-amigaos-native.c):

  1. Thread process cleanup on program exit: Add attribute((destructor)) that waits for all threads created via __gthread_create to finish before the program exits. A 5-second per-thread timeout prevents hanging if a thread is blocked. After the timeout, DOS's NP_Child mechanism provides a safety net.

    Without this fix, child processes linger indefinitely after exit, leaking memory and triggering "Parent process has tried to exit before all children have" warnings from the shell.

    Affects all C++ programs using std::thread, std::async, std::future.

  2. I/O handle inheritance: Change child thread processes to use NP_Input/NP_Output/NP_Error=ZERO instead of inheriting parent's file handles. Prevents "input fh buffer was changed" DOS warnings when parent exits first.

Tested: 6/6 thread tests pass on AmigaOS 4.1 (Pegasos II via QEMU).
Affected versions: GCC 8, 9, 10, 11 (all with amigaos thread model).

Fix two issues in GCC's native AmigaOS thread implementation
(libgcc/gthr-amigaos-native.c):

1. Thread process cleanup on program exit:
   Add __attribute__((destructor)) that waits for all threads created
   via __gthread_create to finish before the program exits. A 5-second
   per-thread timeout prevents hanging if a thread is blocked. After
   the timeout, DOS's NP_Child mechanism provides a safety net.

   Without this fix, child processes linger indefinitely after exit,
   leaking memory and triggering "Parent process has tried to exit
   before all children have" warnings from the shell.

   Affects all C++ programs using std::thread, std::async, std::future.

2. I/O handle inheritance:
   Change child thread processes to use NP_Input/NP_Output/NP_Error=ZERO
   instead of inheriting parent's file handles. Prevents "input fh
   buffer was changed" DOS warnings when parent exits first.

Tested: 6/6 thread tests pass on AmigaOS 4.1 (Pegasos II via QEMU).
Affected versions: GCC 8, 9, 10, 11 (all with amigaos thread model).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new GCC 11 patch intended to improve the AmigaOS native gthread implementation by ensuring child thread processes don’t linger on program exit and by preventing child threads from inheriting the parent’s I/O handles.

Changes:

  • Introduces a destructor-based cleanup routine to wait (with timeout) for threads created via __gthread_create before exit.
  • Changes CreateNewProcTags to use NP_Input/NP_Output/NP_Error = ZERO to avoid inheriting parent file handles.
  • Adds __gthread_once_unlock implementation (but this duplicates an existing patch in the current GCC 11 patch stack).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +46 to +59
@@ -194,6 +194,12 @@
return 0;
}

+void __gthread_once_unlock(__gthread_once_t *__once)
+{
+ __internal_gthread_once_t *once = (__internal_gthread_once_t *)__once;
+ __atomic_store_1(&once->u.i.started, 0, __ATOMIC_SEQ_CST);
+}
+
/******************************************************************************/

/* We keep the entries of each key organized as a single linked list */
@@ -541,6 +547,67 @@
Comment on lines +1 to +3
From: Richard Gibbs <rich_gibbs@hotmail.com>
Date: Thu, 10 Apr 2026 15:00:00 +0100
Subject: [PATCH] Fix gthr-amigaos-native thread cleanup and I/O inheritance
Comment on lines +1 to +5
From: Richard Gibbs <rich_gibbs@hotmail.com>
Date: Thu, 10 Apr 2026 15:00:00 +0100
Subject: [PATCH] Fix gthr-amigaos-native thread cleanup and I/O inheritance

Fix two issues in the native AmigaOS thread implementation:
@walkero-gr

Copy link
Copy Markdown

@derfsss How can this be tested? Do you have any failing example that can be used to test the before and after status?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants