Commit graph

170 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
Yorick Hardy
bb3df95d13 Define *ai-v4mapped* to zero on platforms where AI_V4MAPPED is undefined.
This change defines *ai-v4mapped* to zero when AI_V4MAPPED is undefined
and similarly for *ai-all* (similar to other patches). This allows
(srfi 106) to be available on NetBSD and other platforms without
AI_V4MAPPED and is the recommended behaviour by the author of SRFI-106:

https://srfi-email.schemers.org/srfi-106/msg/2762553/
2024-03-05 22:18:44 +02:00
Justin Ethier
e7725a2a36 Issue #519 - Fix fxlength 2024-01-08 19:00:39 -08:00
Jeronimo Pellegrini
b60bfc266b SRFI 143: fx-{width,least,greatest} are not procedures
In the SRFI text, these are constants:

fx-width
fx-greatest
fx-least

But they were implemented as procedures in Cyclone.

This patch changes that, so they now are implemented
as constants.
2023-03-04 06:53:53 -03:00
Justin Ethier
fa58b9d538 fxbit-set? properly handles negative i 2021-08-08 21:47:31 -04: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
6a59ab2a5d Issue #428 - Removed obsolete (srfi 9) 2020-12-29 21:30:51 -05:00
Justin Ethier
5978e98e37 Issue #414 - Include SRFI 162 defs 2020-09-30 13:06:59 -04:00
Justin Ethier
768e07139b Initial file 2020-09-30 13:01:31 -04:00
Justin Ethier
cf078d9d6c Add additional SRFI 132 functions 2020-09-03 19:02:41 -04: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
c9921ce69a Fix libtommath warnings 2019-10-23 13:24:13 -04:00
Justin Ethier
85751cda2b Issue #326 - Use timer to seed pseudorandom numbers 2019-07-24 13:19:38 -04: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
1a1f707a73 Relocated test library 2019-06-19 18:29:42 -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
78d5265442 New file 2019-05-17 12:37:16 -04:00
Justin Ethier
f69a458625 Sync up with latest changes to 132
There may be more fixes, this is more of a temporary band-aid.
2019-04-18 17:07:31 -04:00
Justin Ethier
f5b68ee8d2 Handle hashing of complex nums with a 0i component 2019-02-19 13:20:29 -05:00
Justin Ethier
74148a2f16 Remove dependency on (scheme complex) 2019-02-14 13:55:56 -05:00
Justin Ethier
4632efc71d Added Cyc-memoize 2019-02-14 12:44:48 -05:00
Justin Ethier
a42b5acdc8 Issue #176 - Improve hash-by-identity 2018-05-01 13:27:59 -04:00
Justin Ethier
20379a264c Issue #249 - Improve hash table performance
Hardcode bound such that it is not a bignum (reducing bignum allocations).

Replace symbol hash with a more efficient algorithm, since symbols are uniquely identified by memory location as well as name.
2018-04-30 13:59:22 -04:00
Justin Ethier
da51147b68 Issue #81 - allow for large recv sizes 2018-01-03 17:25:07 -05:00
Justin Ethier
8ec41e34bf Issue #218 - Fix startup issue 2017-09-04 18:57:23 -04:00
Justin Ethier
8f4e0ccf75 Issue #189 - Reimplementation of bitwise-if 2017-08-29 18:41:18 -04:00
Justin Ethier
05fb1cdb7b Use new make_input_port macro 2017-08-19 18:12:06 -04:00
Justin Ethier
9643d8742b Cleanup 2017-08-08 18:09:54 -04:00
Justin Ethier
cb2e2a7e59 Fixup header comment 2017-08-08 16:58:31 +00:00
Justin Ethier
1028fc566a WIP 2017-08-08 16:57:02 +00:00
Justin Ethier
444f27a8a6 Added additional defs from ref impl 2017-08-08 15:33:21 +00:00
Justin Ethier
be11ed674f Added bit-set 2017-08-08 15:27:23 +00:00
Justin Ethier
a247ba03ad Check for divide by zero 2017-08-08 15:09:37 +00:00
Justin Ethier
a6d2cc0b7a Added TODO 2017-08-08 13:54:15 +00:00
Justin Ethier
0cb8f0f591 Added fxif 2017-08-08 13:51:16 +00:00
Justin Ethier
1a3f60711f Added fxarithmetic-shift 2017-08-08 13:19:38 +00:00
Justin Ethier
390192db2a Added shifts 2017-08-08 13:17:09 +00:00
Justin Ethier
5b5e88a61a Adding bitwise ops 2017-08-08 13:05:17 +00:00
Justin Ethier
c46f3991d3 WIP 2017-08-08 12:52:44 +00:00
Justin Ethier
64b1afa5e2 WIP 2017-08-07 18:51:59 -04:00
Justin Ethier
59fc9ebba3 Added TODO 2017-08-07 17:41:32 +00:00