Commit graph

912 commits

Author SHA1 Message Date
yorickhardy
2b0f0601a6
Raise errors and objects with different tags (#557)
* tests/base.scm: add two tests for issue #556

The two tests are adapted from issue #556 (originally from r7rs).
The tests currently fail because errors and raised objects are
treated in the same way.

* Use different tags for raised objects and raised errors

The behaviour for raising an error (error message irritants) and
objects (raise object) are different in r7rs. So tag error objects
differently, and adjust the raised object handling to handle
the raised object instead of a list containing the raised object.

This should resolve issue #556.

* runtime: use the correct string length for comparison

Fix for the pull request adressing issue #556.

* runtime: distinguish exceptions and errors in default handler

* repl: use error-object? to decide whether an error or an exception was raised

This makes error messages a bit more informative.  Also, if error objects become
a distinct type, then the repl implementation will continue to be correct. The
(deleted) second cond clause seemed to be bit redundant - I am not sure what the
original intent was.

* tests/base.scm: revert accidental deletion of else clause

* Display exceptions as errors for consistency
2025-02-23 16:23:16 -05:00
Justin Ethier
f59ce9999e Formatting 2025-01-20 18:57:04 -08:00
Justin Ethier
62250cf5d7 Run formatting job 2025-01-20 18:54:18 -08:00
Justin Ethier
674a6373b6 Avoid compiler warning 2025-01-20 18:17:56 -08:00
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
8e74c0409e Add code change back 2024-04-22 18:32:06 -07:00
Justin Ethier
29b4c77922 Cleanup 2024-03-13 19:32:15 -07:00
Yorick Hardy
4bbceeb4d6 round half-integers to even instead of away from zero
This changes the behaviour to match r7rs (round x) instead of C round(x).

An answer to https://stackoverflow.com/questions/32746523/ieee-754-compliant-round-half-to-even
suggests using remainder(). The following will work if FE_TONEAREST is defined, but C11
requires FE_TONEAREST to be defined if and only if the implemenetation supports it in
fegetround() and fesetround() [Draft N1570]. On the other hand, remainder() must be defined.
C23 will have roundeven(), but this is not yet available on all platforms.
The behaviour of remainder is described in Draft N1570, page 254, footnote 239.

Alternative implementation:

  double round_to_nearest_even(double x)
  {
  #pragma STDC FENV_ACCESS ON
    int mode;
    double nearest;
    mode = fegetround();
    fesetround(FE_TONEAREST);
    nearest = nearbyint(x);
    fesetround(mode);
  #pragma STDC FENV_ACCESS OFF
    return nearest;
  }
2024-02-01 22:25:47 +02:00
Justin Ethier
3b921e7389 Re-format code 2024-01-17 19:43:47 -08:00
Justin Ethier
88fb4b909f Issue #519 - fix bignum TODO's in Cyc_remainder 2024-01-07 19:40:32 -08:00
Justin Ethier
749d4b6a0c Issue #519 - Properly handle doubles in remainder 2024-01-07 19:29:20 -08:00
Justin Ethier
d048b3d4f0 Issue #519 - numerator/denominator
Return fixnum or bignum values from this function when they are passed as arg, per R7RS.
2024-01-07 07:44:04 -08:00
Justin Ethier
1a0f42386b Issue #519 - Return fixnum from div if possible 2024-01-06 14:37:15 -08:00
Justin Ethier
8875c534dc Issue #519 - allow fixnum results from Cyc_div_op
Need to extend this to fast_div, but this is another good edge case.
2024-01-06 09:14:01 -08:00
Justin Ethier
8bf60e9239 Free memory 2023-11-27 19:43:38 -08:00
Justin Ethier
132c745330 Cleanup 2023-11-27 19:33:19 -08:00
Justin Ethier
08bd333701 Cleanup 2023-11-27 18:59:42 -08:00
Justin Ethier
4d902f9a77 WIP - support for bignums in rational parsing 2023-11-20 19:44:17 -08:00
Justin Ethier
bb4e176e42 WIP, read rationals as inexact nums 2023-11-16 19:37:36 -08:00
Justin Ethier
c599dbb62a WIP 2023-11-15 19:43:25 -08:00
Justin Ethier
9c197965b3 Cleanup, add TODO 2023-11-14 19:48:02 -08:00
Justin Ethier
abaed9f6f2 Issue #510 - Implement exact using runtime functions 2023-09-12 19:20:38 -07:00
Justin Ethier
29a27098a8 Issue #510 - Stage macos compat fix
Apparently mp_set_double does not compile on OSX. Need to fix up this portion of the code.
2023-09-11 19:53:43 -07:00
Justin Ethier
0533d3eab0 Merge branch 'issue-509' 2023-09-04 18:31:58 -07:00
Justin Ethier
20fe02d9d9 Issue #509 - Raise error on invalid complex ops
Error on more types of complex comparison operations.
2023-08-23 20:00:44 -07:00
Justin Ethier
cc24c6be6d Issue #511 - Properly recognize +inf.0 / -inf.0 2023-08-21 19:11:59 -07:00
Justin Ethier
ca931300e3 WIP 2023-08-17 19:56:41 -07:00
Justin Ethier
ad1ac3a135 Issue #490 - Proper assv and memv implementations
Both were previously implemented in terms of `assq` and `memq`, respectively.
2022-07-24 10:56:41 -04:00
Justin Ethier
6ffd229dcd Add memory-streams to list of features 2022-05-28 08:21:11 -07:00
Justin Ethier
5101de1547 Issue #143 - Track recursion depth of equalp
This prevents the possibility of segfaulting when traversing arbitrarily complex circular structures.
2022-05-26 13:02:24 -04:00
Justin Ethier
d3f7262414 Issue #143 - Max recursion depth for printing
Enforce a maximum C recursion depth when printing data structures. This protects against cases where a circular data structure may produce infinite output, blowing the stack. The recursive limit is sufficiently large such that a non-circular structure should not be impacted.
2022-05-25 22:28:08 -04:00
Justin Ethier
43267e2939 WIP 2022-05-24 23:02:18 -04:00
Justin Ethier
2e84aaac9c Issue #484 - Improve handling of exporting prims
To fixes:
- Prevent segfault setting a global variable to itself
- Do not throw an error when exporting a primitive that is not defined in the current module, as built-ins are always available in any context.
2021-12-13 19:05:18 -08:00
Justin Ethier
d2915abe6a Issue #477 - Clean up 2021-08-12 23:01:04 -04:00
Justin Ethier
4ff0bca100 Issue #477 - Added CYC_PTHREAD_SET_STACK_SIZE 2021-08-12 22:46:13 -04:00
Justin Ethier
14d4c27eac Issue #472 - Avoid races with tracing GC when allocating large vectors 2021-07-28 22:26:33 -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
d99d4a9459 Cleanup 2021-07-23 15:45:08 -04:00
Justin Ethier
efece6a413 Handle complex numbers better for numerator/denominator
Peel off real part if there is no imaginary part
2021-07-22 21:45:08 -04:00
Justin Ethier
48a7958c33 Initial numerator/denominator complex num support 2021-07-22 17:21:52 -04:00
Justin Ethier
826e7895ae Issue #433 - Special case, denominator of 0 is 1 2021-07-22 17:16:53 -04:00
Justin Ethier
f17102178b Continue building-out new numerator/denominator 2021-07-21 19:47:42 -07:00
Justin Ethier
63b7c12ede TODO: numerator/denominator support 2021-07-21 15:47:06 -04:00
Justin Ethier
d9d1b35a62 Issue #351 - Prevent compiled warnings on clang
Cleaned up code to prevent compiler warnings with respect to comparing uint8 with EOF (IE, -1).
2021-07-15 19:20:47 -07:00
Justin Ethier
71cca38b44 Issue #379 - Fix read-line to be compatible with other I/O functions
The function now uses the same port buffer as our other I/O functions.
2021-07-14 23:00:42 -04:00
Justin Ethier
215552cfe7 Working version of Cyc_io_read_line_slow 2021-07-14 17:28:59 -04:00
Justin Ethier
5a50814a61 WIP for read_line slow 2021-07-14 13:40:32 -04:00
Justin Ethier
92bc2364fe WIP 2021-07-13 20:00:36 -07:00
Justin Ethier
8aec6c4c83 Stub out approach for fully-integrated read-line 2021-07-13 19:45:10 -07:00