irondash_run_loop 0.6.0

Consistent, platform-independent interface to system run loop.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use std::{
    cell::Cell,
    collections::HashMap,
    ffi::c_void,
    mem::ManuallyDrop,
    sync::{Arc, Mutex},
    time::{Duration, Instant},
};

use core_foundation::{
    base::{kCFAllocatorDefault, CFRelease, CFRetain, TCFType},
    date::CFAbsoluteTimeGetCurrent,
    runloop::{
        kCFRunLoopCommonModes, kCFRunLoopDefaultMode, kCFRunLoopRunFinished, kCFRunLoopRunStopped,
        CFRunLoopAddSource, CFRunLoopAddTimer, CFRunLoopGetCurrent, CFRunLoopGetMain, CFRunLoopRef,
        CFRunLoopRemoveTimer, CFRunLoopRunInMode, CFRunLoopSource, CFRunLoopSourceContext,
        CFRunLoopSourceCreate, CFRunLoopSourceSignal, CFRunLoopStop, CFRunLoopTimer,
        CFRunLoopTimerContext, CFRunLoopTimerRef, CFRunLoopWakeUp,
    },
    string::CFStringRef,
};
use objc2::rc::{autoreleasepool, Id};
use objc2_foundation::NSString;

use self::sys::pthread_threadid_np;

mod sys;

pub type HandleType = usize;
pub const INVALID_HANDLE: HandleType = 0;

type Callback = Box<dyn FnOnce()>;

struct Timer {
    scheduled: Instant,
    callback: Callback,
}

struct State {
    callbacks: Vec<Callback>,
    timers: HashMap<HandleType, Timer>,
    timer: Option<CFRunLoopTimer>,
    source: Option<CFRunLoopSource>,
    run_loop: CFRunLoopRef,
    run_loop_mode: Id<NSString>,
}

// CFRunLoopTimer is thread safe
unsafe impl Send for State {}

struct StatePendingExecution {
    callbacks: Vec<Callback>,
    timers: Vec<Timer>,
}

impl State {
    fn new() -> Self {
        let run_loop = unsafe { CFRunLoopGetCurrent() };
        let run_loop: CFRunLoopRef = unsafe { CFRetain(run_loop as *mut _) } as *mut _;
        Self {
            callbacks: Vec::new(),
            timers: HashMap::new(),
            timer: None,
            source: None,
            run_loop_mode: NSString::from_str("IrondashRunLoopMode"),
            run_loop,
        }
    }

    fn get_pending_execution(&mut self) -> StatePendingExecution {
        let now = Instant::now();
        let pending: Vec<HandleType> = self
            .timers
            .iter()
            .filter(|v| v.1.scheduled <= now)
            .map(|v| *v.0)
            .collect();

        StatePendingExecution {
            callbacks: self.callbacks.drain(0..).collect(),
            timers: pending
                .iter()
                .map(|h| self.timers.remove(h).unwrap())
                .collect(),
        }
    }

    fn remove_timer(&mut self) {
        if let Some(timer) = self.timer.take() {
            unsafe {
                CFRunLoopRemoveTimer(
                    self.run_loop,
                    timer.as_concrete_TypeRef(),
                    kCFRunLoopCommonModes,
                );
                CFRunLoopRemoveTimer(
                    self.run_loop,
                    timer.as_concrete_TypeRef(),
                    Id::as_ptr(&self.run_loop_mode) as CFStringRef,
                );
            };
        }
    }

    fn create_source(&mut self, state: Arc<Mutex<State>>) {
        let mutex = Arc::as_ptr(&state);
        let mut context = CFRunLoopSourceContext {
            version: 0,
            info: mutex as *mut c_void,
            retain: Some(Self::retain),
            release: Some(Self::release),
            copyDescription: None,
            equal: None,
            hash: None,
            schedule: None,
            cancel: None,
            perform: Self::on_source,
        };
        let source: CFRunLoopSource = unsafe {
            let source_ref = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &mut context as *mut _);
            TCFType::wrap_under_create_rule(source_ref)
        };
        unsafe {
            CFRunLoopAddSource(
                self.run_loop,
                source.as_concrete_TypeRef(),
                kCFRunLoopCommonModes,
            );
            // Register source with custom RunLoopMode. This lets clients to only process
            // events scheduled through RunLoopSender (which also includes MessageChannel).
            CFRunLoopAddSource(
                self.run_loop,
                source.as_concrete_TypeRef(),
                Id::as_ptr(&self.run_loop_mode) as CFStringRef,
            );
        }
        self.source = Some(source);
    }

    fn remove_source(&mut self) {
        use core_foundation::runloop::CFRunLoopRemoveSource;
        if let Some(source) = self.source.take() {
            unsafe {
                CFRunLoopRemoveSource(
                    self.run_loop,
                    source.as_concrete_TypeRef(),
                    kCFRunLoopCommonModes,
                );
                CFRunLoopRemoveSource(
                    self.run_loop,
                    source.as_concrete_TypeRef(),
                    Id::as_ptr(&self.run_loop_mode) as CFStringRef,
                )
            };
        }
    }

    fn next_instant(&self) -> Instant {
        if !self.callbacks.is_empty() {
            Instant::now()
        } else {
            let min = self.timers.values().map(|x| x.scheduled).min();
            min.unwrap_or_else(|| Instant::now() + Duration::from_secs(60 * 60))
        }
    }

    fn schedule(&mut self, state: Arc<Mutex<State>>) {
        self.remove_timer();

        if !self.callbacks.is_empty() {
            if self.source.is_none() {
                self.create_source(state);
            }
            unsafe {
                CFRunLoopSourceSignal(
                    self.source
                        .as_ref()
                        .expect("Failed to create source")
                        .as_concrete_TypeRef(),
                );
                CFRunLoopWakeUp(self.run_loop);
            }
        } else {
            let mutex = Arc::as_ptr(&state);
            let next = self.next_instant();
            let pending = next.saturating_duration_since(Instant::now());
            let fire_date = unsafe { CFAbsoluteTimeGetCurrent() } + pending.as_secs_f64();

            let mut context = CFRunLoopTimerContext {
                version: 0,
                info: mutex as *mut c_void,
                retain: Some(Self::retain),
                release: Some(Self::release),
                copyDescription: None,
            };

            let timer =
                CFRunLoopTimer::new(fire_date, 0.0, 0, 0, Self::on_timer, &mut context as *mut _);
            self.timer = Some(timer.clone());
            unsafe {
                CFRunLoopAddTimer(
                    self.run_loop,
                    timer.as_concrete_TypeRef(),
                    kCFRunLoopCommonModes,
                );
                CFRunLoopAddTimer(
                    self.run_loop,
                    timer.as_concrete_TypeRef(),
                    Id::as_ptr(&self.run_loop_mode) as CFStringRef,
                );
                CFRunLoopWakeUp(self.run_loop);
            };
        }
    }

    extern "C" fn retain(data: *const c_void) -> *const c_void {
        let state = data as *const Mutex<State>;
        unsafe { Arc::increment_strong_count(state) }
        data
    }

    extern "C" fn release(data: *const c_void) {
        let state = data as *const Mutex<State>;
        unsafe { Arc::decrement_strong_count(state) };
    }

    extern "C" fn on_timer(_timer: CFRunLoopTimerRef, data: *mut c_void) {
        let state = data as *const Mutex<State>;
        let state = unsafe { Arc::from_raw(state) };
        Self::poll(&state);
        let _ = ManuallyDrop::new(state);
    }

    extern "C" fn on_source(data: *const c_void) {
        let state = data as *const Mutex<State>;
        let state = unsafe { Arc::from_raw(state) };
        Self::poll(&state);
        let _ = ManuallyDrop::new(state);
    }

    fn drain(state: &Arc<Mutex<State>>) {
        autoreleasepool(|_| {
            let execution = state.lock().unwrap().get_pending_execution();
            for c in execution.callbacks {
                c();
            }
            for t in execution.timers {
                (t.callback)();
            }
        });
    }

    fn poll(state: &Arc<Mutex<State>>) {
        Self::drain(state);
        if !state.lock().unwrap().timers.is_empty() {
            let state_clone = state.clone();
            state.lock().unwrap().schedule(state_clone);
        }
    }
}

impl Drop for State {
    fn drop(&mut self) {
        unsafe {
            CFRelease(self.run_loop as *mut _);
        }
    }
}

pub struct PlatformRunLoop {
    next_handle: Cell<HandleType>,
    state: Arc<Mutex<State>>,
    running: Cell<bool>,
}

impl Drop for PlatformRunLoop {
    fn drop(&mut self) {
        // This needs to be done to unref State
        self.state.lock().unwrap().remove_source();
        self.state.lock().unwrap().remove_timer();
    }
}

pub struct PollSession {
    start: Instant,
    timed_out: bool,
}

impl PollSession {
    pub fn new() -> Self {
        Self {
            start: Instant::now(),
            timed_out: false,
        }
    }
}

impl PlatformRunLoop {
    pub fn new() -> Self {
        Self {
            next_handle: Cell::new(INVALID_HANDLE + 1),
            state: Arc::new(Mutex::new(State::new())),
            running: Cell::new(false),
        }
    }

    fn next_handle(&self) -> HandleType {
        let r = self.next_handle.get();
        self.next_handle.replace(r + 1);
        r
    }

    pub fn unschedule(&self, handle: HandleType) {
        let state_clone = self.state.clone();
        let mut state = self.state.lock().unwrap();
        state.timers.remove(&handle);
        state.schedule(state_clone);
    }

    pub fn schedule<F>(&self, in_time: Duration, callback: F) -> HandleType
    where
        F: FnOnce() + 'static,
    {
        let handle = self.next_handle();

        let state_clone = self.state.clone();
        let mut state = self.state.lock().unwrap();

        state.timers.insert(
            handle,
            Timer {
                scheduled: Instant::now() + in_time,
                callback: Box::new(callback),
            },
        );

        state.schedule(state_clone);

        handle
    }

    pub fn is_main_thread() -> bool {
        unsafe { CFRunLoopGetCurrent() == CFRunLoopGetMain() }
    }

    pub fn run(&self) {
        self.running.set(true);
        // Run-loop will exit immediately if it has no sources, but that's not what we
        // expect from run(). To workaround it schedule a very distant timer.
        let distant_duration = 1.0e10;
        let distant_timer = self.schedule(Duration::from_secs_f64(distant_duration), || {});
        while self.running.get() {
            let result = unsafe { CFRunLoopRunInMode(kCFRunLoopDefaultMode, distant_duration, 0) };
            if result == kCFRunLoopRunStopped || result == kCFRunLoopRunFinished {
                State::drain(&self.state);
                self.running.set(false);
            }
        }
        self.unschedule(distant_timer);
    }

    #[cfg(target_os = "macos")]
    pub fn run_app(&self) {
        use objc2_app_kit::NSApplication;
        use objc2_foundation::MainThreadMarker;

        unsafe {
            let mtm = MainThreadMarker::new().unwrap();
            let app = NSApplication::sharedApplication(mtm);
            // TODO(knopp): Replace with `activate` once macOS 14 is minimum deployment target
            #[allow(deprecated)]
            app.activateIgnoringOtherApps(true);
            app.run();
        }
    }

    pub fn stop(&self) {
        self.running.set(false);
        unsafe {
            let run_loop: CFRunLoopRef =
                CFRetain(self.state.lock().unwrap().run_loop as *mut _) as *mut _;
            CFRunLoopStop(run_loop);
            CFRelease(run_loop as *mut _);
        }
    }

    #[cfg(target_os = "macos")]
    pub fn stop_app(&self) {
        use objc2_app_kit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType};
        use objc2_foundation::{CGPoint, MainThreadMarker};

        unsafe {
            let mtm = MainThreadMarker::new().unwrap();
            let app = NSApplication::sharedApplication(mtm);
            app.stop(None);

            // To stop event loop immediately, we need to post event.
            let dummy_event = NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2(NSEventType::ApplicationDefined, CGPoint::ZERO, NSEventModifierFlags::empty(), 0.0, 0, None, 0,0,0).unwrap();
            app.postEvent_atStart(&dummy_event, true);
        }
    }

    pub fn poll_once(&self, poll_session: &mut PollSession) {
        let mode = self.state.lock().unwrap().run_loop_mode.clone();
        if !poll_session.timed_out {
            // We try to drain only tasks scheduled by run_loop. However in some
            // circumstances the UI thread may be waiting for RasterThread (i.e. await toImage)
            // and raster thread might might be waiting to schedule things on UI thread
            // (i.e. ResizeSynchronizer) - in which case we must drain the run loop fully.
            unsafe { CFRunLoopRunInMode(Id::as_ptr(&mode) as CFStringRef, 0.006, 1) };
            poll_session.timed_out = poll_session.start.elapsed() >= Duration::from_millis(6);
        } else {
            unsafe { CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, 1) };
        }
    }

    pub fn new_sender(&self) -> PlatformRunLoopSender {
        PlatformRunLoopSender {
            state: Arc::downgrade(&self.state),
        }
    }
}

#[derive(Clone)]
pub struct PlatformRunLoopSender {
    state: std::sync::Weak<Mutex<State>>,
}

impl PlatformRunLoopSender {
    pub fn send<F>(&self, callback: F) -> bool
    where
        F: FnOnce() + 'static + Send,
    {
        if let Some(state) = self.state.upgrade() {
            let state_clone = state.clone();
            let mut state = state.lock().unwrap();
            state.callbacks.push(Box::new(callback));
            state.schedule(state_clone);
            true
        } else {
            false
        }
    }
}

pub(crate) type PlatformThreadId = u64;

pub(crate) fn get_system_thread_id() -> PlatformThreadId {
    let mut id = 0u64;
    unsafe { pthread_threadid_np(std::ptr::null_mut(), &mut id as *mut _) };
    id
}