Skip to content

Commit 9d0ca38

Browse files
authored
Rollup merge of #73925 - eduardosm:improve-pr72617-comments, r=RalfJung
Improve comments from #72617, as suggested by RalfJung r? @RalfJung
2 parents d46de10 + 0f1adc8 commit 9d0ca38

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/libstd/panicking.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@ pub mod panic_count {
229229
thread_local! { static LOCAL_PANIC_COUNT: Cell<usize> = Cell::new(0) }
230230

231231
// Sum of panic counts from all threads. The purpose of this is to have
232-
// a fast path in `is_zero` (which is used by `panicking`). Access to
233-
// this variable can be always be done with relaxed ordering because
234-
// it is always guaranteed that, if `GLOBAL_PANIC_COUNT` is zero,
235-
// `LOCAL_PANIC_COUNT` will be zero.
232+
// a fast path in `is_zero` (which is used by `panicking`). In any particular
233+
// thread, if that thread currently views `GLOBAL_PANIC_COUNT` as being zero,
234+
// then `LOCAL_PANIC_COUNT` in that thread is zero. This invariant holds before
235+
// and after increase and decrease, but not necessarily during their execution.
236236
static GLOBAL_PANIC_COUNT: AtomicUsize = AtomicUsize::new(0);
237237

238238
pub fn increase() -> usize {
@@ -263,6 +263,12 @@ pub mod panic_count {
263263
// Fast path: if `GLOBAL_PANIC_COUNT` is zero, all threads
264264
// (including the current one) will have `LOCAL_PANIC_COUNT`
265265
// equal to zero, so TLS access can be avoided.
266+
//
267+
// In terms of performance, a relaxed atomic load is similar to a normal
268+
// aligned memory read (e.g., a mov instruction in x86), but with some
269+
// compiler optimization restrictions. On the other hand, a TLS access
270+
// might require calling a non-inlinable function (such as `__tls_get_addr`
271+
// when using the GD TLS model).
266272
true
267273
} else {
268274
is_zero_slow_path()

0 commit comments

Comments
 (0)