You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-atomic-access.md
+12-12Lines changed: 12 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,22 @@
1
-
- Feature Name: atomic_access
1
+
q- Feature Name: atomic_access
2
2
- Start Date: 2016-06-15
3
3
- RFC PR: (leave this empty)
4
4
- Rust Issue: (leave this empty)
5
5
6
6
# Summary
7
7
[summary]: #summary
8
8
9
-
Add the following methods to atomic types:
9
+
This RFC adds the following methods to atomic types:
10
10
11
11
```rust
12
12
implAtomicT {
13
13
fnget_mut(&mutself) ->&mutT;
14
14
fninto_inner(self) ->T;
15
-
fnas_raw(&self) ->*mutT;
16
-
unsafefnfrom_raw(ptr:*mutT) ->&AtomicT;
17
15
}
18
16
```
19
17
18
+
It also specifies that the layout of an `AtomicT` type is always the same as the underlying `T` type. So, for example, `AtomicI32` is guaranteed to be transmutable to and from `i32`.
19
+
20
20
# Motivation
21
21
[motivation]: #motivation
22
22
@@ -28,33 +28,33 @@ A normal load/store is different from a `load(Relaxed)` or `store(Relaxed)` beca
28
28
29
29
`get_mut` in particular is expected to be useful in `Drop` implementations where you have a `&mut self` and need to read the value of an atomic. `into_inner` somewhat overlaps in functionality with `get_mut`, but it is included to allow extracting the value without requiring the atomic object to be mutable. These methods mirror `Mutex::get_mut` and `Mutex::into_inner`.
30
30
31
-
## `as_raw` and `from_raw`
31
+
## Atomic type layout
32
32
33
-
These methods are mainly intended to be used for FFI, where a variable of a non-atomic type needs to be modified atomically. The most common example of this is the Linux `futex` system call which takes an `int*` parameter pointing to an integer that is atomically modified by both userspace and the kernel.
33
+
The layout guarantee is mainly intended to be used for FFI, where a variable of a non-atomic type needs to be modified atomically. The most common example of this is the Linux `futex` system call which takes an `int*` parameter pointing to an integer that is atomically modified by both userspace and the kernel.
34
34
35
-
Rust code invoking the `futex` system call so far has simply passed the address of the atomic object directly to the system call. However this makes the assumption that the atomic type has the same layout as the underlying integer type. Using `as_raw` instead makes it clear that the resulting pointer will point to the integer value inside the atomic object.
35
+
Rust code invoking the `futex` system call so far has simply passed the address of the atomic object directly to the system call. However this makes the assumption that the atomic type has the same layout as the underlying integer type, which is not currently guaranteed by the documentation.
36
36
37
-
`from_raw` provides the reverse operation: it allows Rust code to atomically modify a value that was not declared as a atomic type. This is useful when dealing with FFI structs that are shared with a thread managed by a C library. Another example would be to atomically modify a value in a memory mapped file that is shared with another process.
37
+
This also allows the reverse operation by casting a pointer: it allows Rust code to atomically modify a value that was not declared as a atomic type. This is useful when dealing with FFI structs that are shared with a thread managed by a C library. Another example would be to atomically modify a value in a memory mapped file that is shared with another process.
38
38
39
39
# Detailed design
40
40
[design]: #detailed-design
41
41
42
-
The actual implementations of these functions are mostly trivial since they are based on `UnsafeCell::get`. The only exception is `from_raw` which will cast the given pointer to a different type, but that should also be fine.
42
+
The actual implementations of these functions are mostly trivial since they are based on `UnsafeCell::get`.
43
+
44
+
The existing implementations of atomic types already have the same layout as the underlying types (even `AtomicBool` and `bool`), so no change is needed here apart from the documentation.
43
45
44
46
# Drawbacks
45
47
[drawbacks]: #drawbacks
46
48
47
49
The functionality of `into_inner` somewhat overlaps with `get_mut`.
48
50
49
-
`from_raw` returns an unbounded lifetime.
51
+
We lose the ability to change the layout of atomic types, but this shouldn't be necessary since these types map directly to hardware primitives.
50
52
51
53
# Alternatives
52
54
[alternatives]: #alternatives
53
55
54
56
The functionality of `get_mut` and `into_inner` can be implemented using `load(Relaxed)`, however the latter can result in worse code because it is poorly handled by the optimizer.
55
57
56
-
The functionality of `as_raw` and `from_raw` could be achieved using transmutes instead, however this requires making assumptions about the internal layout of the atomic types.
0 commit comments