Skip to content

bugfix: update structure One to be thread-safe#175

Merged
shwestrick merged 1 commit into
MPLLang:prep-v0.4-releasefrom
shwestrick:prep-v0.4-release
May 5, 2023
Merged

bugfix: update structure One to be thread-safe#175
shwestrick merged 1 commit into
MPLLang:prep-v0.4-releasefrom
shwestrick:prep-v0.4-release

Conversation

@shwestrick

Copy link
Copy Markdown
Collaborator

basis-library/util/one.sml defines a structure called One which is used in the basis library to optimize memory usage of a few functions, including:

  • Int.{fmt,toString}
  • Word.{fmt,toString}
  • Real.{split,toManExp}

This patch fixes a buggy race condition in the implementation of One. With this patch, the above functions should be safe for parallelism and concurrency.

The idea behind One is straightforward: a static buffer or mutable cell is allocated to be shared across calls. When a call is made, if the shared buffer is not in use, then the shared buffer can be claimed and used for the duration of that call, and then released.

The mechanism for claiming the buffer (inherited from MLton) was previously not thread-safe, because it was not atomic at the hardware level. For MLton, it didn't need to be. But for MPL this is no longer correct, hence the bug.

This patch switches to using an atomic compare-and-swap (CAS) to claim the buffer.

basis-library/util/one.sml defines a structure called `One` which
is used in the basis library to optimize memory usage of a few
functions, including:
  * Int.{fmt,toString}
  * Word.{fmt,toString}
  * Real.{split,toManExp}

This patch fixes a buggy race condition in the implementation of
`One`. With this patch, the above functions should be safe for
parallelism and concurrency.

The idea behind `One` is straightforward: a static buffer or mutable
cell is allocated to be shared across calls. When a call is made, if
the shared buffer is not in use, then the shared buffer can be claimed
and used for the duration of that call, and then released.

The mechanism for claiming the buffer (inherited from MLton) was
previously not thread-safe, because it was not atomic at the
hardware level. For MLton, it didn't need to be. But for MPL
this is no longer correct, hence the bug.

This patch switches to using an atomic compare-and-swap (CAS) to
claim the buffer.
@shwestrick shwestrick merged commit 09b59c9 into MPLLang:prep-v0.4-release May 5, 2023
@shwestrick shwestrick mentioned this pull request Nov 21, 2025
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant