|
| 1 | +use std::{ |
| 2 | + pin::Pin, |
| 3 | + sync::atomic::{AtomicBool, Ordering}, |
| 4 | +}; |
| 5 | + |
| 6 | +pub static THREAD_POOL: forte::ThreadPool = const { forte::ThreadPool::new() }; |
| 7 | + |
| 8 | +pub static STARTED: AtomicBool = const { AtomicBool::new(false) }; |
| 9 | + |
| 10 | +#[inline(always)] |
| 11 | +fn ensure_started() { |
| 12 | + if !STARTED.load(Ordering::Relaxed) { |
| 13 | + if !STARTED.swap(true, Ordering::Relaxed) { |
| 14 | + THREAD_POOL.resize_to_available(); |
| 15 | + } |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +// ----------------------------------------------------------------------------- |
| 20 | +// Join |
| 21 | + |
| 22 | +#[derive(Debug)] |
| 23 | +pub struct FnContext { |
| 24 | + /// True if the task was migrated. |
| 25 | + migrated: bool, |
| 26 | +} |
| 27 | + |
| 28 | +impl FnContext { |
| 29 | + #[inline(always)] |
| 30 | + pub fn migrated(&self) -> bool { |
| 31 | + self.migrated |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[inline(always)] |
| 36 | +pub fn join_context<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB) |
| 37 | +where |
| 38 | + A: FnOnce(FnContext) -> RA + Send, |
| 39 | + B: FnOnce(FnContext) -> RB + Send, |
| 40 | + RA: Send, |
| 41 | + RB: Send, |
| 42 | +{ |
| 43 | + ensure_started(); |
| 44 | + THREAD_POOL.join( |
| 45 | + |worker| { |
| 46 | + let ctx = FnContext { |
| 47 | + migrated: worker.migrated(), |
| 48 | + }; |
| 49 | + oper_a(ctx) |
| 50 | + }, |
| 51 | + |worker| { |
| 52 | + let ctx = FnContext { |
| 53 | + migrated: worker.migrated(), |
| 54 | + }; |
| 55 | + oper_b(ctx) |
| 56 | + }, |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +#[inline(always)] |
| 61 | +pub fn join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB) |
| 62 | +where |
| 63 | + A: FnOnce() -> RA + Send, |
| 64 | + B: FnOnce() -> RB + Send, |
| 65 | + RA: Send, |
| 66 | + RB: Send, |
| 67 | +{ |
| 68 | + ensure_started(); |
| 69 | + THREAD_POOL.join(|_| oper_a(), |_| oper_b()) |
| 70 | +} |
| 71 | + |
| 72 | +#[inline(always)] |
| 73 | +pub fn current_num_threads() -> usize { |
| 74 | + 64 // Forte prefers smaller tasks, so it's better to lie to rayon about the size of the pool |
| 75 | +} |
| 76 | + |
| 77 | +#[inline(always)] |
| 78 | +pub fn current_thread_index() -> Option<usize> { |
| 79 | + forte::Worker::map_current(|worker| worker.index()) |
| 80 | +} |
| 81 | + |
| 82 | +// ----------------------------------------------------------------------------- |
| 83 | +// Scope |
| 84 | + |
| 85 | +pub struct Scope<'r, 'scope: 'r> { |
| 86 | + inner: Pin<&'r forte::Scope<'scope>>, |
| 87 | +} |
| 88 | + |
| 89 | +impl<'scope> Scope<'_, 'scope> { |
| 90 | + #[inline(always)] |
| 91 | + pub fn spawn<BODY>(&self, body: BODY) |
| 92 | + where |
| 93 | + BODY: FnOnce(&Scope) + Send + 'scope, |
| 94 | + { |
| 95 | + self.inner.spawn(|inner| { |
| 96 | + let scope = Scope { inner }; |
| 97 | + body(&scope) |
| 98 | + }); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[inline(always)] |
| 103 | +pub fn scope<'scope, OP, R>(op: OP) -> R |
| 104 | +where |
| 105 | + OP: FnOnce(&Scope<'_, 'scope>) -> R + Send, |
| 106 | + R: Send, |
| 107 | +{ |
| 108 | + THREAD_POOL.scope(|inner| { |
| 109 | + let scope = Scope { inner }; |
| 110 | + op(&scope) |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +#[inline(always)] |
| 115 | +pub fn in_place_scope<'scope, OP, R>(op: OP) -> R |
| 116 | +where |
| 117 | + OP: FnOnce(&Scope<'_, 'scope>) -> R, |
| 118 | +{ |
| 119 | + THREAD_POOL.scope(|inner| { |
| 120 | + let scope = Scope { inner }; |
| 121 | + op(&scope) |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +// ----------------------------------------------------------------------------- |
| 126 | +// Spawn |
| 127 | + |
| 128 | +#[inline(always)] |
| 129 | +pub fn spawn<F>(func: F) |
| 130 | +where |
| 131 | + F: FnOnce() + Send + 'static, |
| 132 | +{ |
| 133 | + THREAD_POOL.spawn(|_| func()) |
| 134 | +} |
| 135 | + |
| 136 | +// ----------------------------------------------------------------------------- |
| 137 | +// Fake stuff that dosn't work |
| 138 | + |
| 139 | +pub struct ThreadBuilder; |
| 140 | + |
| 141 | +pub struct ThreadPool; |
| 142 | + |
| 143 | +pub struct ThreadPoolBuildError; |
| 144 | + |
| 145 | +pub struct ThreadPoolBuilder; |
| 146 | + |
| 147 | +pub struct BroadcastContext; |
| 148 | + |
| 149 | +pub struct ScopeFifo; |
| 150 | + |
| 151 | +pub struct Yield; |
| 152 | + |
| 153 | +pub fn broadcast() { |
| 154 | + unimplemented!() |
| 155 | +} |
| 156 | + |
| 157 | +pub fn spawn_broadcast() { |
| 158 | + unimplemented!() |
| 159 | +} |
| 160 | + |
| 161 | +pub fn max_num_threads() { |
| 162 | + unimplemented!() |
| 163 | +} |
| 164 | + |
| 165 | +pub fn scope_fifo() { |
| 166 | + unimplemented!() |
| 167 | +} |
| 168 | + |
| 169 | +pub fn in_place_scope_fifo() { |
| 170 | + unimplemented!() |
| 171 | +} |
| 172 | + |
| 173 | +pub fn spawn_fifo() { |
| 174 | + unimplemented!() |
| 175 | +} |
| 176 | + |
| 177 | +pub fn yield_local() { |
| 178 | + unimplemented!() |
| 179 | +} |
| 180 | + |
| 181 | +pub fn yield_now() { |
| 182 | + unimplemented!() |
| 183 | +} |
0 commit comments