|
| 1 | +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; |
| 2 | + |
| 3 | +use mmtk::util::VMMutatorThread; |
| 4 | + |
| 5 | +/// Ruby's GC-disable state, owned by the binding instead of mmtk-core. |
| 6 | +/// |
| 7 | +/// mmtk-core cannot run a GC while collection is disabled, and every pause ends in `NotInGC`. |
| 8 | +/// A forced `GC.start` therefore has to enable collection in mmtk-core, and mmtk-core alone |
| 9 | +/// would lose the disabled state across that GC. The binding keeps the state here and mirrors |
| 10 | +/// it into mmtk-core, where it holds at most one `disable_collection` once all callers return. |
| 11 | +/// |
| 12 | +/// CRuby's fatal-signal handlers call `rb_gc_impl_gc_disable` and `rb_gc_impl_gc_enable`, and a |
| 13 | +/// handler can interrupt a thread anywhere in this module. The state is therefore an atomic word. |
| 14 | +/// `apply` runs one caller at a time. A caller that read an old state corrects mmtk-core before |
| 15 | +/// it returns, so a stale step never undoes the result of an `apply` that already returned. A |
| 16 | +/// handler that interrupts the applying thread does not wait for it, because it would wait |
| 17 | +/// forever. |
| 18 | +#[derive(Default)] |
| 19 | +pub struct GcDisableState { |
| 20 | + state: AtomicU64, |
| 21 | + /// The thread that runs `apply`, as returned by `current_thread_id`, or 0. |
| 22 | + applier: AtomicUsize, |
| 23 | +} |
| 24 | + |
| 25 | +/// A nonzero value that is unique to the calling thread. It must be safe to call in a signal |
| 26 | +/// handler. A Rust thread-local is not: this library is loaded with `dlopen`, so the first access |
| 27 | +/// on a thread can allocate in `__tls_get_addr`. `pthread_self` only reads the thread pointer. |
| 28 | +fn current_thread_id() -> usize { |
| 29 | + unsafe { libc::pthread_self() as usize } |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Clone, Copy)] |
| 33 | +struct State { |
| 34 | + /// Set by `rb_gc_impl_gc_disable` and cleared by `rb_gc_impl_gc_enable`. |
| 35 | + internal_disabled: bool, |
| 36 | + /// Set and cleared by `GC.disable` and `GC.enable`. |
| 37 | + user_disabled: bool, |
| 38 | + /// Forced `GC.start` calls that enabled collection and wait for a pause. |
| 39 | + forced_gc_requests: u32, |
| 40 | + /// Incremented when a pause ends. A pause serves every forced request made before it ends. |
| 41 | + pause_epoch: u32, |
| 42 | +} |
| 43 | + |
| 44 | +impl State { |
| 45 | + const INTERNAL_DISABLED: u64 = 1 << 0; |
| 46 | + const USER_DISABLED: u64 = 1 << 1; |
| 47 | + const FORCED_SHIFT: u32 = 2; |
| 48 | + const FORCED_MAX: u32 = (1 << 30) - 1; |
| 49 | + const EPOCH_SHIFT: u32 = 32; |
| 50 | + |
| 51 | + fn decode(bits: u64) -> Self { |
| 52 | + Self { |
| 53 | + internal_disabled: bits & Self::INTERNAL_DISABLED != 0, |
| 54 | + user_disabled: bits & Self::USER_DISABLED != 0, |
| 55 | + forced_gc_requests: (bits >> Self::FORCED_SHIFT) as u32 & Self::FORCED_MAX, |
| 56 | + pause_epoch: (bits >> Self::EPOCH_SHIFT) as u32, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fn encode(self) -> u64 { |
| 61 | + debug_assert!(self.forced_gc_requests <= Self::FORCED_MAX); |
| 62 | + (self.internal_disabled as u64) |
| 63 | + | (self.user_disabled as u64) << 1 |
| 64 | + | (self.forced_gc_requests as u64) << Self::FORCED_SHIFT |
| 65 | + | (self.pause_epoch as u64) << Self::EPOCH_SHIFT |
| 66 | + } |
| 67 | + |
| 68 | + fn want_disabled(self) -> bool { |
| 69 | + (self.internal_disabled || self.user_disabled) && self.forced_gc_requests == 0 |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl GcDisableState { |
| 74 | + fn load(&self) -> State { |
| 75 | + State::decode(self.state.load(Ordering::SeqCst)) |
| 76 | + } |
| 77 | + |
| 78 | + /// Returns the state before the update. |
| 79 | + fn update(&self, f: impl Fn(&mut State)) -> State { |
| 80 | + let bits = self |
| 81 | + .state |
| 82 | + .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |bits| { |
| 83 | + let mut state = State::decode(bits); |
| 84 | + f(&mut state); |
| 85 | + Some(state.encode()) |
| 86 | + }) |
| 87 | + .unwrap(); |
| 88 | + State::decode(bits) |
| 89 | + } |
| 90 | + |
| 91 | + fn apply(&self) { |
| 92 | + let me = current_thread_id(); |
| 93 | + |
| 94 | + // A signal handler interrupted this thread in `apply`. CRuby's handlers never return to |
| 95 | + // the interrupted code: they abort or longjmp. Apply the state and release `applier`, |
| 96 | + // so that a longjmp does not leave it held. |
| 97 | + if self.applier.load(Ordering::SeqCst) == me { |
| 98 | + self.mirror_into_mmtk(); |
| 99 | + let _ = self |
| 100 | + .applier |
| 101 | + .compare_exchange(me, 0, Ordering::SeqCst, Ordering::SeqCst); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + while self |
| 106 | + .applier |
| 107 | + .compare_exchange_weak(0, me, Ordering::SeqCst, Ordering::SeqCst) |
| 108 | + .is_err() |
| 109 | + { |
| 110 | + std::thread::yield_now(); |
| 111 | + } |
| 112 | + self.mirror_into_mmtk(); |
| 113 | + let _ = self |
| 114 | + .applier |
| 115 | + .compare_exchange(me, 0, Ordering::SeqCst, Ordering::SeqCst); |
| 116 | + } |
| 117 | + |
| 118 | + /// Brings mmtk-core to the state that `want_disabled` asks for. The binding is the only |
| 119 | + /// caller of `disable_collection` and `enable_collection`, so mmtk-core's status is the |
| 120 | + /// mirror. |
| 121 | + fn mirror_into_mmtk(&self) { |
| 122 | + let mmtk = crate::mmtk(); |
| 123 | + loop { |
| 124 | + let want_disabled = self.load().want_disabled(); |
| 125 | + let disabled = !mmtk.is_collection_enabled(); |
| 126 | + if want_disabled == disabled { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + if want_disabled { |
| 131 | + match mmtk.disable_collection() { |
| 132 | + Ok(true) => {} |
| 133 | + // Only if a signal handler ran `apply` on this thread and then returned. |
| 134 | + // Remove the extra nesting level. |
| 135 | + Ok(false) => { |
| 136 | + mmtk.enable_collection(); |
| 137 | + } |
| 138 | + // A pause is requested or running, or collection is not initialized. The end |
| 139 | + // of that pause, or the initialization of collection, applies the state again. |
| 140 | + Err(_) => return, |
| 141 | + } |
| 142 | + } else { |
| 143 | + mmtk.enable_collection(); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + pub fn set_internal_disabled(&self, disabled: bool) { |
| 149 | + self.update(|state| state.internal_disabled = disabled); |
| 150 | + self.apply(); |
| 151 | + } |
| 152 | + |
| 153 | + pub fn internal_disabled(&self) -> bool { |
| 154 | + self.load().internal_disabled |
| 155 | + } |
| 156 | + |
| 157 | + /// Returns the previous value. |
| 158 | + pub fn set_user_disabled(&self, disabled: bool) -> bool { |
| 159 | + let was = self |
| 160 | + .update(|state| state.user_disabled = disabled) |
| 161 | + .user_disabled; |
| 162 | + self.apply(); |
| 163 | + was |
| 164 | + } |
| 165 | + |
| 166 | + pub fn user_disabled(&self) -> bool { |
| 167 | + self.load().user_disabled |
| 168 | + } |
| 169 | + |
| 170 | + /// `GC.start` (`force`) runs even when GC is disabled. `GC.stress` does not. |
| 171 | + pub fn handle_user_collection_request( |
| 172 | + &self, |
| 173 | + tls: VMMutatorThread, |
| 174 | + force: bool, |
| 175 | + exhaustive: bool, |
| 176 | + ) { |
| 177 | + let mmtk = crate::mmtk(); |
| 178 | + |
| 179 | + if !force { |
| 180 | + mmtk.handle_user_collection_request(tls, false, exhaustive); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + let epoch = self |
| 185 | + .update(|state| state.forced_gc_requests += 1) |
| 186 | + .pause_epoch; |
| 187 | + |
| 188 | + self.apply(); |
| 189 | + mmtk.handle_user_collection_request(tls, true, exhaustive); |
| 190 | + |
| 191 | + self.update(|state| { |
| 192 | + if state.pause_epoch == epoch { |
| 193 | + state.forced_gc_requests -= 1; |
| 194 | + } |
| 195 | + }); |
| 196 | + self.apply(); |
| 197 | + } |
| 198 | + |
| 199 | + /// Called after mmtk-core sets `NotInGC` at the end of a pause and before mutators resume, |
| 200 | + /// so no mutator can request another pause before the disabled state is applied. |
| 201 | + pub fn on_pause_end(&self) { |
| 202 | + self.update(|state| { |
| 203 | + state.forced_gc_requests = 0; |
| 204 | + state.pause_epoch = state.pause_epoch.wrapping_add(1); |
| 205 | + }); |
| 206 | + self.apply(); |
| 207 | + } |
| 208 | + |
| 209 | + pub fn on_collection_initialized(&self) { |
| 210 | + self.apply(); |
| 211 | + } |
| 212 | +} |
0 commit comments