Skip to content

Commit

Permalink
[External] [stdlib] Make atomics sharable (#53867)
Browse files Browse the repository at this point in the history
[External] [stdlib] Make atomics sharable

Prior to this, most atomic operations required a mutable reference,
which meant you had statically guarenteed mutual exclusion, which means
you don't need atomics. This fixes those operations to only require an
immutable reference, allowing them to be used in multiple threads.

Co-authored-by: Owen Hilyard <[email protected]>
Closes modular#3943
MODULAR_ORIG_COMMIT_REV_ID: ba2460225e7243f65d80ea397c97c9c0a77f26f6

Signed-off-by: Joshua James Venter <[email protected]>
  • Loading branch information
owenhilyard authored and jjvraw committed Jan 16, 2025
1 parent 06a2534 commit 76bc768
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions stdlib/src/os/atomic.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct Atomic[type: DType, *, scope: StringLiteral = ""]:
self.value = value

@always_inline
fn load(mut self) -> Scalar[type]:
fn load(self) -> Scalar[type]:
"""Loads the current value from the atomic.
Returns:
Expand Down Expand Up @@ -139,7 +139,7 @@ struct Atomic[type: DType, *, scope: StringLiteral = ""]:
)

@always_inline
fn fetch_add(mut self, rhs: Scalar[type]) -> Scalar[type]:
fn fetch_add(self, rhs: Scalar[type]) -> Scalar[type]:
"""Performs atomic in-place add.
Atomically replaces the current value with the result of arithmetic
Expand All @@ -158,7 +158,7 @@ struct Atomic[type: DType, *, scope: StringLiteral = ""]:
return Self._fetch_add(value_addr, rhs)

@always_inline
fn __iadd__(mut self, rhs: Scalar[type]):
fn __iadd__(self, rhs: Scalar[type]):
"""Performs atomic in-place add.
Atomically replaces the current value with the result of arithmetic
Expand All @@ -173,7 +173,7 @@ struct Atomic[type: DType, *, scope: StringLiteral = ""]:
_ = self.fetch_add(rhs)

@always_inline
fn fetch_sub(mut self, rhs: Scalar[type]) -> Scalar[type]:
fn fetch_sub(self, rhs: Scalar[type]) -> Scalar[type]:
"""Performs atomic in-place sub.
Atomically replaces the current value with the result of arithmetic
Expand All @@ -197,7 +197,7 @@ struct Atomic[type: DType, *, scope: StringLiteral = ""]:
](value_addr.address, rhs.value)

@always_inline
fn __isub__(mut self, rhs: Scalar[type]):
fn __isub__(self, rhs: Scalar[type]):
"""Performs atomic in-place sub.
Atomically replaces the current value with the result of arithmetic
Expand All @@ -213,7 +213,7 @@ struct Atomic[type: DType, *, scope: StringLiteral = ""]:

@always_inline
fn compare_exchange_weak(
mut self, mut expected: Scalar[type], desired: Scalar[type]
self, mut expected: Scalar[type], desired: Scalar[type]
) -> Bool:
"""Atomically compares the self value with that of the expected value.
If the values are equal, then the self value is replaced with the
Expand Down Expand Up @@ -271,7 +271,7 @@ struct Atomic[type: DType, *, scope: StringLiteral = ""]:
_max_impl[scope=scope](ptr, rhs)

@always_inline
fn max(mut self, rhs: Scalar[type]):
fn max(self, rhs: Scalar[type]):
"""Performs atomic in-place max.
Atomically replaces the current value with the result of max of the
Expand Down Expand Up @@ -311,7 +311,7 @@ struct Atomic[type: DType, *, scope: StringLiteral = ""]:
_min_impl[scope=scope](ptr, rhs)

@always_inline
fn min(mut self, rhs: Scalar[type]):
fn min(self, rhs: Scalar[type]):
"""Performs atomic in-place min.
Atomically replaces the current value with the result of min of the
Expand Down

0 comments on commit 76bc768

Please sign in to comment.