Commit graph

43 commits

Author SHA1 Message Date
yorickhardy
71e5aa2dd6
Improve garbage collection for terminated threads (#550)
* gc: add a function to force the collector to run

This requires adding a "forced" stage for the collector,
which is the initial stage for a forced collection.
Thereafter, the collector continues to the usual stages
of collection.

* runtime: force the garbage collector to run when a thread exits

This is a first attempt to improve the memory usage reported in
issue #534.

* srfi-18: call Cyc_end_thread on thread exits

This ensures that the collector has a chance to run whenever
a thread exits. Attempts to partially address issue #534.

* gc: free unused parts of the heap before merging

When a thread exits, the heap is merged into the main thread.
Before doing so, free any unused parts of the heap to reduce
memory usage. Attempts to partially address issue #534.

* srfi-18: thread-terminate! takes a thread as argument

* gc: revert adding STAGE_FORCING

Use gc_start_major_collection() instead. Partial work towards
addressing issue #534.

* gc: free empty pages in gc_heap_merge()

Moving the code from gc_merge_all_heaps to gc_heap_merge removes
special handling of the start of the list and is (hopefully)
easier to read.

Partial work towards addressing issue #534.

* gc: oops, forgot the "freed" count

Partial work towards addressing issue #534.

* gc: oops, forgot the "freed" count (again)

Partial work towards addressing issue #534.

* types: update forward declaration of gc_heap_merge()

Partial work towards addressing issue #534.

* gc: remove accidental double counting

* runtime: small (cosmetic) simplification

* srfi-18: add a slot for thread context in the thread object

Partial work towards addressing issue #534.

* srfi-18: do a minor gc when terminating a thread

This ensures that any objects which are part of the
thread context are transferred to the heap.

Partial work towards addressing issue #534.

* types.h: make gc_alloc_pair public

This will be used to create the thread context.
Partial work towards addressing issue #534.

* gc: prepare heap objects for sweeping

Also introduce a global variable to track whether merged
heaps need to be swept.

Partial work towards addressing issue #534.

* gc: create a context for terminated thread objects

The context ensures that parametrised objects, continuations
and exception handlers can still be traced but are no longer
root objects (after thread terminations) and can be GCd eventually.

Partial work towards addressing issue #534.

* gc: sweep and free empty heaps for the primordial thread

The primordial thread may not have an opportunity to sweep
heap pages which have been merged from terminated threads.
So sweep any unswept pages during the cooperation phase.

Partial work towards addressing issue #534.

* srfi-18: revert thread-terminate! changes

These changes need to be revisited, and are not suitable for
the threads garbage collection pull request.
2025-01-20 21:10:49 -05:00
yorickhardy
3db92dc3c2
pthread-terminate! takes a thread object as argument (#553)
* srfi-18: define all of the components of the *primordial-thread* thread object

* srfi-18: pthread-terminate! takes a thread object as argument

Handle this by checking if the argument is the primordial thread,
current thread or another thread.

The first two cases remain almost identical to the previous implementation.
To terminate a thread (which is not the caller) we use a pthread key
which contains the thread data. The destructor is set to Cyc_end_thread
and will terminate the thread when pthread_cancel is called. This ensures
that Cyc_end_thread is called with the correct thread data by the thread
which will be terminated.

* runtime: cast to the required type for pthread_key_create

* runtime: clear the thread_key before exiting the thread

* runtime: handle cancelled threads separately

We probably don't want to call pthread_exit in the destructor.
Similarly, we don't want to perform a longjmp (i.e. GC(...))
in the desctructor.

* runtime: do a minor GC for cancelled threads

The main idea is to avoid a longjmp and return to the destructor
for the cancelled thread. So, adjust GC and gc_minor to allow
for a NULL continuation.
2025-01-20 10:55:36 -05:00
Justin Ethier
2a9d0ea604 Issue #279 - Support end-result and return from thread-join 2021-07-25 23:02:31 -04:00
Justin Ethier
df5438c9f6 Fix (thread-start!) to return thread obj, per SRFI 18 2021-07-23 16:31:04 -04:00
Justin Ethier
8f9698b1fc Avoid C compiler warning 2021-03-18 13:24:39 -04:00
Justin Ethier
738877f225 Issue #370 - Added current-thread-data
This allows returning the current thread data structure in a Opaque object.
2021-01-24 21:58:58 -05:00
Justin Ethier
10630b4907 Allow optional thread cont arg 2020-08-12 18:49:02 -04:00
Justin Ethier
34ece856d8 Fix compiler warnings 2019-12-23 19:24:17 -05:00
Justin Ethier
59bb0b4f41 Fix compiler error 2019-12-23 19:15:56 -05:00
Justin Ethier
e96ed6266a Avoid compiler warning on OSX 2019-12-23 19:06:48 -05:00
Justin Ethier
a14d3efe80 Cleanup 2019-06-24 19:02:30 -04:00
Justin Ethier
5cb5540d2e WIP: timed mutex lock 2019-06-24 17:55:01 -04:00
Justin Ethier
d1dbd7fae9 Issue #320 - Track threads that have not yet run
This allows (thread-join!) to be able to wait on these new threads
2019-06-10 13:16:39 -04:00
Justin Ethier
22ca4ff85a Issue #320 - Ensure caller has ref to child thread 2019-06-07 16:12:31 -04:00
Justin Ethier
f5a36697df Updated comments 2017-06-26 17:18:03 -04:00
Justin Ethier
c550b15f3a Issue #150 - Inefficient (but working) thread-join! 2017-06-17 01:36:47 -04:00
Justin Ethier
75b9e7bf8a Explicit inlines 2017-04-29 01:18:39 +00:00
Justin Ethier
a964bb17ae WIP 2017-03-13 12:15:43 +00:00
Justin Ethier
ce0bffb31a WIP 2017-01-26 23:45:46 -05:00
Justin Ethier
07500ce6d6 Properly store thread ID in thread objects
Also sketched out thread-join support but there are larger issues to consider to get that working.
2017-01-22 18:58:37 -05:00
Justin Ethier
2bee1575c0 Issue #118 - Added current-thread 2016-11-21 16:54:34 -05:00
Justin Ethier
97fa0c2dbd WIP 2016-11-19 05:35:25 +00:00
Justin Ethier
51748fb957 Let collector know mutator is blocking 2016-02-23 20:09:51 -05:00
Justin Ethier
c6bd33bfe9 Cleanup 2016-02-22 22:13:58 -05:00
Justin Ethier
67de922d9d Unlock thread after waiting for cv 2016-02-19 19:37:22 -05:00
Justin Ethier
4fdbea5c1d Cleanup 2016-02-18 21:09:58 -05:00
Justin Ethier
d5ea9ae2a3 Added TODO 2016-02-18 23:21:57 -05:00
Justin Ethier
b9978248b8 Added thread-terminate! 2016-02-18 23:03:01 -05:00
Justin Ethier
1ec3608d8d Integrate cond-variable wait with mutex-unlock! 2016-02-17 02:59:19 -05:00
Justin Ethier
c98e186172 Relocated mutex primitives to srfi 18 lib 2016-02-16 23:51:28 -05:00
Justin Ethier
214f8de4c0 Added signal and broadcast 2016-02-15 22:19:12 -05:00
Justin Ethier
bb4433b9ed WIP 2016-02-16 02:49:37 -05:00
Justin Ethier
730434bb14 Added header comment block. 2016-02-14 22:35:04 -05:00
Justin Ethier
1a3d88f999 Formatting 2016-01-18 22:43:30 -05:00
Justin Ethier
76bbca68e5 Cyc-minor-gc is now part of (srfi 18)
It makes more sense here than as a primitive, since threading is the only place you would need to trigger this from application code.
2016-01-18 22:40:59 -05:00
Justin Ethier
92561abc85 Added status of all SRFI 18 functions 2016-01-18 22:28:55 -05:00
Justin Ethier
1e71ff3fd3 Move thread-sleep! to 18.sld 2016-01-18 22:18:39 -05:00
Justin Ethier
40bcf2d5dc Added comments 2016-01-17 00:09:01 -05:00
Justin Ethier
ea45546c62 Experimenting with explicit heap copying 2016-01-15 22:43:21 -05:00
Justin Ethier
04d1655a6a Prevent passing stack objects to a thread's closure
Modified thread-start! to initiate a GC prior to running the thread, in case thunk contains any closures on the "parent" thread's stack. Otherwise when the parent thread goes to collect them, the contents will be corrupted when the spawned thread attempts to access them.
2016-01-14 23:30:53 -05:00
Justin Ethier
9402805af3 Mutex stubs 2016-01-02 22:13:45 -05:00
Justin Ethier
151a09386e Added import base 2015-12-26 23:49:41 -05:00
Justin Ethier
7db26c7d8c Relocated threading functions 2015-12-26 23:26:07 -05:00