os.args is never freed, while this is an insignificant leak, it is a bit
annoying as it makes valgrind complain:
==234270== Memcheck, a memory error detector
==234270== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==234270== Using Valgrind-3.24.0 and LibVEX; rerun with -h for copyright info
==234270== Command: ./wc /tmp/mulumulu
==234270==
1 8 58 /tmp/mulumulu
==234270==
==234270== HEAP SUMMARY:
==234270== in use at exit: 47 bytes in 1 blocks
==234270== total heap usage: 5 allocs, 4 frees, 4,195,875 bytes allocated
==234270==
==234270== 47 bytes in 1 blocks are possibly lost in loss record 1 of 1
==234270== at 0x484BC13: calloc (vg_replace_malloc.c:1675)
==234270== by 0x402E49: runtime._heap_alloc-769 (in /d/learn-odin/wc/wc)
==234270== by 0x40A8D7: runtime.heap_alloc (in /d/learn-odin/wc/wc)
==234270== by 0x436E9D: runtime.heap_allocator_proc.aligned_alloc-0 (in /d/learn-odin/wc/wc)
==234270== by 0x4022DC: runtime.heap_allocator_proc (in /d/learn-odin/wc/wc)
==234270== by 0x4165E0: runtime.make_aligned-22560 (in /d/learn-odin/wc/wc)
==234270== by 0x41F6D0: runtime.make_slice-22340 (in /d/learn-odin/wc/wc)
==234270== by 0x40156B: os._alloc_command_line_arguments-4679 (in /d/learn-odin/wc/wc)
==234270== by 0x4011AF: __$startup_runtime (in /d/learn-odin/wc/wc)
==234270== by 0x406F17: main (in /d/learn-odin/wc/wc)
==234270==
==234270== LEAK SUMMARY:
==234270== definitely lost: 0 bytes in 0 blocks
==234270== indirectly lost: 0 bytes in 0 blocks
==234270== possibly lost: 47 bytes in 1 blocks
==234270== still reachable: 0 bytes in 0 blocks
==234270== suppressed: 0 bytes in 0 blocks
==234270==
==234270== For lists of detected and suppressed errors, rerun with: -s
==234270== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
With the fix the leak is gone, tested on linux only.
While here, also make _alloc_command_line_arguments() private.
Also do some cleanup and refactoring of the thread, sync and time APIs.
- remove 'semaphore_release' because 'post' and 'wait' is easier to understand
- change 'semaphore_wait' to '*_wait_for' to match Condition
- pthreads can be given a stack, but doing so requires the user to set up the guard
pages manually. BE WARNED. The alignment requirements of the stack are also
platform-dependant; it may need to be page size aligned on some systems.
Unclear which systems, however. See 'os.get_page_size', and 'mem.make_aligned'.
HOWEVER: I was unable to get custom stacks with guard pages working reliably,
so while you can do it, the API does not support it.
- add 'os.get_page_size', 'mem.make_aligned', and 'mem.new_aligned'.
- removed thread return values because windows and linux are not consistent; windows returns 'i32'
and pthreads return 'void*'; besides which, if you really wanted to communicate how the
thread exited, you probably wouldn't do it with the thread's exit code.
- fixed 'thread.is_done' on Windows; it didn't report true immediately after calling 'thread.join'.
- moved time related stuff out of 'core:os' to 'core:time'.
- add 'mem.align_backward'
- fixed default allocator alignment
The heap on Windows, and calloc on Linux, both have no facility to request alignment.
It's a bit of hack, but the heap_allocator now overallocates; `size + alignment` bytes,
and aligns things to at least 2.
It does both of these things to ensure that there is at least two bytes before the payload,
which it uses to store how much padding it needed to insert in order to fulfil the alignment
requested.
- make conditions more sane by matching the Windows behaviour.
The fact that they were signalled now lingers until a thread tries to wait,
causing them to just pass by uninterrupted, without sleeping or locking the
underlying mutex, as it would otherwise need to do.
This means that a thread no longer has to be waiting in order to be signalled, which
avoids timing bugs that causes deadlocks that are hard to debug and fix.
See the comment on the `sync.Condition.flag` field.
- add thread priority: `thread.create(worker_proc, .High)`