234 Commits

Author SHA1 Message Date
gingerBill
fc2cb8fb39 Remove #no_copy 2025-11-05 13:44:14 +00:00
Jeroen van Rijn
7a9ea3ee6d Further overhaul of package line comments. 2025-10-09 23:05:29 +02:00
gingerBill
9b8223dd69 Remove use of .no_copy 2025-07-30 23:19:14 +01:00
gingerBill
6a10cfdc30 Fix typo 2025-07-30 23:15:14 +01:00
gingerBill
aa6a749804 Improve atomic logic for sync.Wait_Group 2025-07-30 23:11:18 +01:00
Harold Brenes
219b0fe535 Replace system:System.framework imports with system:System
This makes the linker work for both macOS and iOS targets
2025-07-13 15:45:21 -04:00
Jack Mordaunt
17927729dd core/sync/chan: (unbuffered) ack reads
This fixes an issue where a call to close could intercept the dance
between send and recv, causing send to report incorrectly that a value
was not transmitted (when it actually was).
2025-06-13 18:08:44 -03:00
Jack Mordaunt
760d8c1cdd core/sync/chan.send: return false if channel is closed while blocked
This commit makes send behave the same as recv: that the call will
return false if the channel is closed while a thread is waiting on the
blocking operation.

Prior logic would have send return true even if the channel was actually
closed rather than read from.

Docs adjusted to make this clear.
Tests added to lock in this behaviour.
2025-06-12 17:35:49 -03:00
Jack Mordaunt
c29168f76f core/sync/chan.try_send: avoid blocking if no reader is available
This changes the semantics of try_send to be consistently non-blocking.

That is, if the buffered is full OR there are no readers it returns
false.

The previous behaviour was such that it would block in the latter case
of no reader, and it would wait for a reader. That is problematic
because it produces inconsistent behaviour between buffered and
unbuffered channels which is astonishing and adds complexity to the
caller.

To illustrate the problem with the old behaviour, consider the
try_select operation: if a send-channel happens to be unbuffered the
try_select (which wants to never block) can now block, that unbuffered
send channel is selected (at random) and there is no reader on the other
side. Thus we have unpredictable blocking behaviour, which breaks the
guarantee that try_select never blocks.

If you want a blocking send you can just call "send" (the blocking
variant).

In addition, there is some reader/writer math done inside
can_{send,recv} such that they only report true if there is sufficient
reader/writer capacity. If there is contention we need to ensure that
each reader is paired to exactly one writer.

Consider try_send: if there is a single reader we can send. If there is
a single reader and a single writer, then we cannot send, as that reader
will be paired with the existing writer. Therefore can_send is only true
if there are more readers than writers at the time of check.

NOTE: The original tests don't need to use wait-looping with thread.yield()
or heuristic sleep. Instead we can just use blocking channel operations
rather than non-blocking operations.
2025-06-12 17:35:48 -03:00
Jack Mordaunt
c1cd525d9d core/sync/chan.select_raw: call try_select_raw with deprecation warning
Eventually select_raw should be a blocking select operation, but for now
we need to migrate people away.
2025-06-12 16:14:52 -03:00
Jack Mordaunt
96b91849a9 core/sync/chan.try_select_raw: fix doc comment typo
Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>
2025-06-12 16:14:52 -03:00
Jack Mordaunt
faae81ba61 core/sync/chan.try_select_raw: test hook for testing the toctou
This is necessary because we need to allow the test guarantee against a
rare condition: where a third-party thread steals a value between the
validity checks can_{send,recv} and the channel operation
try_{send,recv}.
2025-06-12 16:14:52 -03:00
Jack Mordaunt
4043be8567 core/sync/chan.try_select_raw: skip nil input messages
This makes the proc easier and safer to call by letting the caller nil
out messages to skip sends.
2025-06-12 16:14:52 -03:00
Jack Mordaunt
fb39e5a2f8 core/sync/chan.try_select_raw: clarify loop control flow
Use a label to clarify the continue statements.
2025-06-12 16:14:52 -03:00
Jack Mordaunt
d5b7302ac0 core/sync.try_select_raw: fix TOCTOU
Fixes a TOCTOU where the channel could be used between the call to
can_{recv,send} and {recv,send} causing an unexpected blocking
operation.

To do this we use the non-blocking try_{recv,send} and retry the check
in a loop. This guarantees non-blocking select behaviour, at the cost of
spinning if the input channels are highly contended.

Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>
2025-06-12 16:14:52 -03:00
Jack Mordaunt
be873af003 core/sync.select_raw: rename to try_select_raw
This follows the convention where non-blocking operations are prefixed
with "try" to indicate as much.

Since select_raw in it's current form doesn't block, it should be
try_select_raw, and allow select_raw to name a blocking implementation.
2025-06-12 16:14:52 -03:00
Jack Mordaunt
7f9589922d core/sync.select_raw: return a useful index
This fixes a flaw in the original implementation: the returned index is
actually useless to the caller.

This is because the index returned refers to the internal "candidate"
list. This list is dynamic, and may not have all of the input channels
(if they weren't ready according to chan.can_{recv,send}). That means
the index is not guaranteed to mean anything to the caller.

The fix introduced here is to return the index into the input slice
(recvs,sends) and an enum to specify which input slice that is.

If no selection was made, then (-1, .None) is returned to communicate as
much.

Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>
2025-06-12 16:14:51 -03:00
Feoramund
8cde9dce47 Rewrite Atomic_RW_Mutex
This patch simplifies the implementation and fixes #5254.

Previously, the mutex was set up as if there could be multiple writers,
and there seemed to be some confusion as to which `Writer` bits to
check, as not all were checked or set at the same time.

This could also result in the mutex being left in a non-zero state even
after unlocking all locks.

All unneeded state has been removed and extra checks have been put in
place.
2025-06-03 09:07:38 -04:00
fusion32
bd4afafc66 make once_do_without_data_contextless actually contextless 2025-04-16 12:44:00 -03:00
Robin Bergewski
1c6958d443 core:sync/chan: maintainance and package clarity
this deprecates all procedures around 'Raw_Queue' for it to
become package private.
2025-04-13 22:56:40 +02:00
Robin Bergewski
4ec93ffe39 fix indent 2025-04-13 16:40:08 +02:00
Robin Bergewski
ec71a5afb1 fix missing imports 2025-04-13 16:33:25 +02:00
Robin Bergewski
19eb53c321 changes due to review 2025-04-13 15:34:14 +02:00
Robin Bergewski
ca72aba9eb core:sync/chan: add package documentation 2025-04-13 15:07:06 +02:00
flysand7
9da144157e [sync]: Fix typos in comments and remove my note. 2025-01-16 19:33:09 +03:00
avanspector
a6dccf5e98 Haiku: fix futex bug 2025-01-10 06:28:57 +01:00
avanspector
5376d2a20b fix haiku 2024-12-20 17:19:04 +01:00
Laytan Laats
f1cae8d844 fix #4496 - allow unlock of unlocked mutex (making it consistent with windows behaviour) 2024-12-06 23:08:47 +01:00
Laytan Laats
cf53404f5f sync: fix no new values 2024-12-05 20:05:19 +01:00
Laytan Laats
ac3a87c2cf sync: fix require results 2024-12-05 19:04:45 +01:00
Laytan Laats
ad438f418d sync: fix futexes on wasm 2024-12-05 19:00:45 +01:00
Karl Zylinski
093ade0504 Merge branch 'master' into file-tags-without-comments 2024-09-17 19:36:17 +02:00
Laytan Laats
0975820c48 fix wrong ulock timeout calculation, add version check for ios 2024-09-17 15:52:35 +02:00
pkova
6e0f1cc866 Pass microseconds instead of nanoseconds to __ulock_wait 2024-09-17 02:35:00 +03:00
Pyry Kovanen
4d6f7dcac0 Fix code alignment in futex_darwin.odin
Co-authored-by: Feoramund <161657516+Feoramund@users.noreply.github.com>
2024-09-17 02:21:00 +03:00
pkova
fff99c726e Fix core sync test deadlock on darwin 2024-09-17 01:52:51 +03:00
Feoramund
d38f5ffb49 Remove unneeded synchronizations in Chan
Everything was already guarded by `c.mutex`.
2024-09-15 22:59:30 -04:00
Karl Zylinski
19f0127e55 Moved all packages in core, base, vendor, tests and examples to use new #+ file tag syntax. 2024-09-14 18:27:49 +02:00
Feoramund
16cd16b91e Fix comments 2024-09-11 08:39:23 -04:00
Feoramund
b1db33b519 Add cpu_relax to sync.auto_reset_event_signal 2024-09-11 08:39:23 -04:00
Feoramund
a1435a6a90 Fix deadlock in Auto_Reset_Event 2024-09-11 08:39:23 -04:00
Feoramund
fec1ccd7a3 Fix data races in sync.Recursive_Benaphore 2024-09-11 08:39:23 -04:00
Feoramund
b2c2235e58 Fix recursive_benaphore_try_lock
Previously, if the owner called this, it would fail.
2024-09-10 14:52:20 -04:00
Feoramund
8a14a656fb Fix chan.can_send for unbuffered channels
`w_waiting` is the signal that says a caller is waiting to be able to
send something. It is incremented upon send and - in the case of an
unbuffered channel - it can only hold one message.

Therefore, check that `w_waiting` is zero instead.
2024-09-10 14:52:20 -04:00
Feoramund
e9a6a34480 Forbid chan.try_send on closed buffered channels 2024-09-10 14:52:20 -04:00
Feoramund
026aef69e3 Fix deadlock on sending to full, buffered, closed Chan
This will also keep messages from being sent to closed, buffered
channels in general.
2024-09-10 14:52:20 -04:00
Feoramund
73f5ab473c Keep chan.can_recv from deadlocking 2024-09-10 14:52:20 -04:00
Feoramund
0a594147af Use contextless procs in core:sync instead 2024-09-10 14:52:20 -04:00
Feoramund
dbb783fbf2 Fix atomic memory order for sync.ticket_mutex_unlock 2024-09-09 16:19:15 -04:00
Feoramund
cbd4d5e765 Fix data race in atomic_sema_wait_with_timeout 2024-09-09 16:19:14 -04:00