mln-rs-runtime 0.0.11

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! A runtime: the thread that its maps live on, and the file sources, cache
//! and offline regions that they share.

use std::cell::RefCell;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, mpsc};
use std::thread::ThreadId;

use mln_actor::{RunLoop, RunLoopHandle};
use mln_storage::http_header_transform::{HttpHeaderTransform, HttpHeaders};
use mln_storage::resource::Kind;
use mln_storage::resource_transform::{FinishedCallback, ResourceTransform};
use mln_storage::{DatabaseFileSource, FileSource, FileSourceManager, FileSourceType, ResourceOptions};
use mln_text::GlyphCache;
use mln_util::error::Error;
use mln_util::sync::MutexExt;

use crate::completion::{Completion, Resolver};
use crate::event::{EventHandler, RuntimeEvent};
use crate::map::{Applied, Map, MapEntry, MapId, MapOptions, MapShared, Published};
use crate::offline::Offline;
use crate::resources::{CacheDatabaseOperation, ProviderFileSource, ResourceHooks, ResourceProvider};

/// Where a runtime keeps its assets and its cache.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RuntimeOptions {
    /// The directory that `asset://` URLs load from. `None` uses the working
    /// directory, or on Android the app's assets, which
    /// `mln_storage::android::init` gives the library.
    pub asset_path: Option<PathBuf>,
    /// The cache database file, which holds the ambient cache and the offline
    /// regions. Its directory must be writable. `None` keeps the cache in
    /// memory.
    pub cache_path: Option<PathBuf>,
}

/// A runtime: a thread of its own that holds maps, and the resource loading,
/// cache and offline regions that they share.
///
/// Every call returns at once. Commands run on the runtime's thread in the
/// order they were sent, across the runtime and all its maps, and report
/// through a [`Completion`]. `Clone` is another handle to the same runtime,
/// and any thread may use one.
///
/// The runtime lives until [`release`](Self::release) or
/// [`dispose`](Self::dispose), or until its last handle and its last map
/// are dropped. A handler or provider that holds a handle keeps it alive
/// until then.
#[derive(Clone)]
pub struct Runtime {
    pub(crate) shared: Arc<RuntimeShared>,
}

impl std::fmt::Debug for Runtime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Runtime").field("thread", &self.shared.thread).finish()
    }
}

struct Lifecycle {
    released: bool,
    live_maps: usize,
    shut_down: bool,
}

pub(crate) struct RuntimeShared {
    run_loop: RunLoopHandle,
    /// The tasks that [`post`](Self::post) sent and the thread hasn't run.
    queued: Arc<AtomicUsize>,
    thread: ThreadId,
    lifecycle: Mutex<Lifecycle>,
    next_map_id: AtomicU64,
    /// The glyphs that the renderers of the runtime's maps parse, which they
    /// share.
    pub(crate) glyph_cache: Arc<GlyphCache>,
}

impl RuntimeShared {
    /// Runs `task` on the runtime's thread, after everything sent before it,
    /// in the burst of the tasks queued with it.
    pub(crate) fn post(&self, task: impl FnOnce(&mut RuntimeCore, &mut Outbox) + Send + 'static) {
        self.queued.fetch_add(1, Ordering::SeqCst);
        self.run_loop.invoke(move || run(task, true));
    }

    /// Runs `task` with `value` on the runtime's thread, after everything
    /// sent before it, or `instead` with `value` on this thread when the
    /// runtime has shut down, so that what must happen either way isn't lost
    /// with the runtime's loop.
    pub(crate) fn post_or<V: Send + 'static>(
        &self,
        value: V,
        task: impl FnOnce(&mut RuntimeCore, &mut Outbox, V) + Send + 'static,
        instead: impl FnOnce(V),
    ) {
        // Posting with the lock held orders the task before a shutdown.
        let lifecycle = self.lifecycle.lock_unpoisoned();
        if lifecycle.shut_down {
            drop(lifecycle);
            instead(value);
        } else {
            self.post(move |core, outbox| task(core, outbox, value));
        }
    }

    pub(crate) fn thread(&self) -> ThreadId {
        self.thread
    }

    /// A completion that the runtime's thread resolves.
    pub(crate) fn completion<T: Send + 'static>(&self) -> (Completion<T>, Resolver<T>) {
        Completion::new(Some(self.thread))
    }

    fn check_live(&self) -> Result<(), Error> {
        if self.lifecycle.lock_unpoisoned().released { Err(released()) } else { Ok(()) }
    }

    /// A map of the runtime was released or dropped. A disposed runtime shuts
    /// down with its last map.
    pub(crate) fn map_released(&self) {
        let shut_down = {
            let mut lifecycle = self.lifecycle.lock_unpoisoned();
            lifecycle.live_maps -= 1;
            let shut_down = lifecycle.released && lifecycle.live_maps == 0 && !lifecycle.shut_down;
            lifecycle.shut_down |= shut_down;
            shut_down
        };
        if shut_down {
            self.shut_down(None);
        }
    }

    /// Tears the runtime down on its thread, after the commands sent before.
    fn shut_down(&self, done: Option<Resolver<()>>) {
        let run_loop = self.run_loop.clone();
        self.run_loop.invoke(move || {
            let core = CORE.with(|core| core.borrow_mut().take());
            drop(core);
            if let Some(done) = done {
                done.resolve(Ok(()));
            }
            run_loop.stop();
        });
    }
}

impl Drop for RuntimeShared {
    fn drop(&mut self) {
        if !self.lifecycle.get_mut_unpoisoned().shut_down {
            self.shut_down(None);
        }
    }
}

fn released() -> Error {
    Error::invalid_state("the runtime was released")
}

thread_local! {
    /// The state of the runtime whose thread this is.
    static CORE: RefCell<Option<RuntimeCore>> = const { RefCell::new(None) };
}

/// Posts `task` to the runtime thread whose loop is `run_loop`.
pub(crate) fn post(run_loop: &RunLoopHandle, task: impl FnOnce(&mut RuntimeCore, &mut Outbox) + Send + 'static) {
    run_loop.invoke(move || run(task, false));
}

/// Runs `task` on the runtime's state, then delivers what it sends to the
/// host, with the state unborrowed. A task that comes after the runtime shut
/// down drops, which cancels its completions. A task that
/// [`RuntimeShared::post`] sent runs in its burst.
fn run(task: impl FnOnce(&mut RuntimeCore, &mut Outbox), in_burst: bool) {
    let mut outbox = Outbox::default();
    let unrun = CORE.with(|core| match core.borrow_mut().as_mut() {
        Some(core) => {
            if in_burst {
                core.begin_burst_task();
            }
            task(core, &mut outbox);
            if in_burst {
                core.end_burst_task(&mut outbox);
            }
            None
        }
        None => Some(task),
    });
    drop(unrun);
    outbox.deliver();
}

/// What a task sends to the host: event batches and results, in order. They
/// go out once the task is done with the runtime's state, so host code that
/// runs never finds it borrowed.
#[derive(Default)]
pub(crate) struct Outbox {
    deliveries: Vec<Box<dyn FnOnce()>>,
}

impl Outbox {
    pub(crate) fn push(&mut self, delivery: impl FnOnce() + 'static) {
        self.deliveries.push(Box::new(delivery));
    }

    pub(crate) fn resolve<T: 'static>(&mut self, resolver: Resolver<T>, result: Result<T, Error>) {
        self.push(move || resolver.resolve(result));
    }

    pub(crate) fn deliver(self) {
        for delivery in self.deliveries {
            delivery();
        }
    }
}

/// The runtime's state, on its thread.
pub(crate) struct RuntimeCore {
    /// Declared first, so the maps go before the file sources they use.
    pub(crate) maps: BTreeMap<MapId, MapEntry>,
    pub(crate) event_handler: Option<EventHandler>,
    hooks: Arc<ResourceHooks>,
    file_sources: FileSourceManager,
    resource_options: ResourceOptions,
    /// The runtime's file sources, held for its life so that each map, and
    /// each transform, finds the same ones.
    _loader: Option<Arc<dyn FileSource>>,
    database: Option<Arc<dyn FileSource>>,
    network: Option<Arc<dyn FileSource>>,
    pub(crate) run_loop: RunLoopHandle,
    /// [`RuntimeShared::queued`].
    queued: Arc<AtomicUsize>,
    /// The tasks of the burst in progress that haven't finished.
    burst_left: usize,
    /// Whether the maps hold their updates back for the burst.
    holding: bool,
}

impl RuntimeCore {
    fn new(resource_options: ResourceOptions, run_loop: RunLoopHandle, queued: Arc<AtomicUsize>) -> Self {
        let hooks = Arc::new(ResourceHooks::default());
        let file_sources = FileSourceManager::new();
        let network_hooks = hooks.clone();
        file_sources.register_file_source_factory(
            FileSourceType::Network,
            Arc::new(move |_: &FileSourceManager, options: &ResourceOptions| {
                Arc::new(ProviderFileSource::new(options, network_hooks.clone())) as Arc<dyn FileSource>
            }),
        );
        let network = file_sources.get_file_source(FileSourceType::Network, &resource_options);
        let database = file_sources.get_file_source(FileSourceType::Database, &resource_options);
        let loader = file_sources.get_file_source(FileSourceType::ResourceLoader, &resource_options);
        RuntimeCore {
            maps: BTreeMap::new(),
            event_handler: None,
            hooks,
            file_sources,
            resource_options,
            _loader: loader,
            database,
            network,
            run_loop,
            queued,
            burst_left: 0,
            holding: false,
        }
    }

    /// Starts a task that [`RuntimeShared::post`] sent. The tasks that were
    /// queued when a burst's first one starts make up the burst, and while a
    /// burst of more than one runs, the maps hold their updates back, so that
    /// a render session draws what the whole burst changed in one frame. A
    /// task sent during a burst is in the next one, so a host that keeps
    /// sending holds no update back for longer than a burst.
    fn begin_burst_task(&mut self) {
        let queued = self.queued.fetch_sub(1, Ordering::SeqCst);
        if self.burst_left == 0 {
            self.burst_left = queued;
            self.holding = queued > 1;
            if self.holding {
                self.maps.values_mut().for_each(|entry| entry.core.hold_updates());
            }
        }
    }

    /// Ends a task of the burst, and after its last, shows what the maps
    /// held back.
    fn end_burst_task(&mut self, outbox: &mut Outbox) {
        self.burst_left = self.burst_left.saturating_sub(1);
        if self.burst_left > 0 || !std::mem::take(&mut self.holding) {
            return;
        }
        let ids: Vec<MapId> = self.maps.keys().copied().collect();
        for id in ids {
            if let Some(entry) = self.maps.get_mut(&id) {
                entry.core.release_updates();
            }
            crate::map::flush(self, id, outbox);
        }
    }

    pub(crate) fn file_sources(&self) -> (&FileSourceManager, &ResourceOptions) {
        (&self.file_sources, &self.resource_options)
    }

    /// Sends `events` to the event handler.
    pub(crate) fn send_events(&self, events: Vec<RuntimeEvent>, outbox: &mut Outbox) {
        if events.is_empty() {
            return;
        }
        if let Some(handler) = self.event_handler.clone() {
            outbox.push(move || handler(&events));
        }
    }

    /// The cache database, which every runtime has.
    pub(crate) fn database(&self) -> Result<&DatabaseFileSource, Error> {
        self.database
            .as_ref()
            .and_then(|database| database.as_any())
            .and_then(|database| database.downcast_ref::<DatabaseFileSource>())
            .ok_or_else(|| Error::unsupported("the runtime has no cache database"))
    }

    fn network(&self) -> Result<&Arc<dyn FileSource>, Error> {
        self.network.as_ref().ok_or_else(|| Error::unsupported("the runtime has no network file source"))
    }
}

/// Delivers a database result on the runtime's thread, so it keeps its
/// place among the runtime's other results and events.
pub(crate) fn reply_on<T: Send + 'static, E: Into<Error>>(
    run_loop: RunLoopHandle,
    resolver: Resolver<T>,
) -> impl FnOnce(Result<T, E>) + Send + 'static {
    move |result| {
        let result = result.map_err(Into::into);
        post(&run_loop, move |_, outbox| outbox.resolve(resolver, result));
    }
}

/// The runtime's resource options, with the cache file created when there
/// is one, so that a path the runtime can't use fails here.
fn resource_options(options: &RuntimeOptions) -> Result<ResourceOptions, Error> {
    let mut resource_options = ResourceOptions::default();
    if let Some(asset_path) = &options.asset_path {
        resource_options = resource_options.with_asset_path(utf8(asset_path, "asset path")?);
    }
    #[cfg(target_os = "android")]
    if options.asset_path.is_none() {
        resource_options = resource_options.with_asset_path(mln_storage::android::APP_ASSETS);
    }
    if let Some(cache_path) = &options.cache_path {
        let path = utf8(cache_path, "cache path")?;
        std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(cache_path)
            .map_err(|error| Error::invalid_argument(format!("can't open the cache database {path}: {error}")))?;
        resource_options = resource_options.with_cache_path(path);
    }
    Ok(resource_options)
}

fn utf8<'a>(path: &'a Path, what: &str) -> Result<&'a str, Error> {
    path.to_str().ok_or_else(|| Error::invalid_argument(format!("the {what} {} isn't UTF-8", path.display())))
}

impl Runtime {
    /// Starts a runtime and its thread.
    ///
    /// Fails with [`ErrorKind::InvalidArgument`](mln_util::ErrorKind) when the
    /// cache database file can't be opened or created.
    pub fn new(options: &RuntimeOptions) -> Result<Runtime, Error> {
        let resource_options = resource_options(options)?;
        let (started, start) = mpsc::channel();
        let queued = Arc::new(AtomicUsize::new(0));
        let core_queued = Arc::clone(&queued);
        let thread = mln_util::thread::builder("Runtime")
            .spawn(move || {
                let run_loop = RunLoop::new();
                let core = RuntimeCore::new(resource_options, run_loop.handle(), core_queued);
                CORE.with(|cell| *cell.borrow_mut() = Some(core));
                let _ = started.send(run_loop.handle());
                run_loop.run();
                let core = CORE.with(|cell| cell.borrow_mut().take());
                drop(core);
            })
            .map_err(|error| Error::native(format!("can't start the runtime's thread: {error}")))?;
        let thread_id = thread.thread().id();
        // The thread ends by itself once the runtime shuts down, so nothing
        // joins it: a runtime may shut down from its own thread.
        drop(thread);
        let run_loop = start.recv().map_err(|_| Error::native("the runtime's thread stopped as it started"))?;
        Ok(Runtime {
            shared: Arc::new(RuntimeShared {
                run_loop,
                queued,
                thread: thread_id,
                lifecycle: Mutex::new(Lifecycle { released: false, live_maps: 0, shut_down: false }),
                next_map_id: AtomicU64::new(1),
                glyph_cache: Arc::default(),
            }),
        })
    }

    /// Sends a command to the runtime's thread.
    fn command<T: Send + 'static>(
        &self,
        command: impl FnOnce(&mut RuntimeCore, &mut Outbox, Resolver<T>) + Send + 'static,
    ) -> Result<Completion<T>, Error> {
        self.shared.check_live()?;
        let (completion, resolver) = self.shared.completion();
        self.shared.post(move |core, outbox| command(core, outbox, resolver));
        Ok(completion)
    }

    /// Sends a command that runs on the runtime's state and completes at once.
    fn apply<T: Send + 'static>(
        &self,
        apply: impl FnOnce(&mut RuntimeCore) -> Result<T, Error> + Send + 'static,
    ) -> Result<Completion<T>, Error> {
        self.command(move |core, outbox, resolver| {
            let result = apply(core);
            outbox.resolve(resolver, result);
        })
    }

    /// Creates a map on the runtime.
    ///
    /// The map's handle is ready at once, and the commands sent to it wait
    /// for the map to be built. If building fails, the completion reports
    /// why and those commands fail with
    /// [`ErrorKind::InvalidState`](mln_util::ErrorKind).
    ///
    /// Fails with [`ErrorKind::InvalidArgument`](mln_util::ErrorKind) for an
    /// empty size or a pixel ratio that isn't finite and positive.
    pub fn create_map(&self, options: MapOptions) -> Result<(Map, Completion<Applied>), Error> {
        let size = options.map.size();
        let pixel_ratio = options.map.pixel_ratio();
        if size.is_empty() {
            return Err(Error::invalid_argument("the map's size must not be empty"));
        }
        if !pixel_ratio.is_finite() || pixel_ratio <= 0.0 {
            return Err(Error::invalid_argument("the map's pixel ratio must be finite and positive"));
        }
        {
            let mut lifecycle = self.shared.lifecycle.lock_unpoisoned();
            if lifecycle.released {
                return Err(released());
            }
            lifecycle.live_maps += 1;
        }
        let id = MapId(self.shared.next_map_id.fetch_add(1, Ordering::Relaxed));
        let published = Arc::new(Published::new(options.event_mask));
        let map = Map::new(MapShared::new(id, self.shared.clone(), &options, published.clone()));
        let (completion, resolver) = self.shared.completion();
        self.shared.post(move |core, outbox| crate::map::create(core, id, &options, published, resolver, outbox));
        Ok((map, completion))
    }

    /// Sends the events of the runtime and its maps to `handler`, in batches
    /// on the runtime's thread. It replaces the handler before.
    pub fn set_event_handler(
        &self,
        handler: impl Fn(&[RuntimeEvent]) + Send + Sync + 'static,
    ) -> Result<Completion<()>, Error> {
        let handler: EventHandler = Arc::new(handler);
        self.command(move |core, outbox, resolver| {
            let previous = core.event_handler.replace(handler);
            outbox.push(move || drop(previous));
            outbox.resolve(resolver, Ok(()));
        })
    }

    /// Stops sending events to a handler.
    pub fn clear_event_handler(&self) -> Result<Completion<()>, Error> {
        self.command(move |core, outbox, resolver| {
            let previous = core.event_handler.take();
            outbox.push(move || drop(previous));
            outbox.resolve(resolver, Ok(()));
        })
    }

    /// Serves the runtime's network requests that `provider` serves through
    /// it, and the rest as usual. It replaces the provider before.
    pub fn set_resource_provider(&self, provider: impl ResourceProvider) -> Result<Completion<()>, Error> {
        self.replace_provider(Some(Box::new(provider)))
    }

    /// Removes the resource provider, so the library loads every request
    /// itself.
    pub fn clear_resource_provider(&self) -> Result<Completion<()>, Error> {
        self.replace_provider(None)
    }

    fn replace_provider(&self, provider: Option<Box<dyn ResourceProvider>>) -> Result<Completion<()>, Error> {
        self.command(move |core, outbox, resolver| {
            let previous = core.hooks.set_provider(provider);
            outbox.push(move || drop(previous));
            outbox.resolve(resolver, Ok(()));
        })
    }

    /// Rewrites the URLs of the runtime's network requests before they go
    /// out: `transform` gets the resource's kind and URL, and answers through
    /// the callback, now or later and on any thread, with the URL to load or
    /// the error to fail the request with. It replaces the transform before.
    pub fn set_resource_transform(
        &self,
        transform: impl Fn(Kind, &str, FinishedCallback) + Send + Sync + 'static,
    ) -> Result<Completion<()>, Error> {
        let transform = ResourceTransform::new(Some(Arc::new(transform)));
        self.apply(move |core| {
            core.network()?.set_resource_transform(transform);
            core.hooks.set_transform_set(true);
            Ok(())
        })
    }

    /// Stops rewriting request URLs.
    pub fn clear_resource_transform(&self) -> Result<Completion<()>, Error> {
        self.apply(move |core| {
            core.network()?.set_resource_transform(ResourceTransform::default());
            core.hooks.set_transform_set(false);
            Ok(())
        })
    }

    /// Adds the headers that `transform` returns to the runtime's HTTP
    /// requests: it gets the resource's kind and the URL after any resource
    /// transform, and runs for each attempt of a request, which waits for
    /// it. Names that the library manages, such as `Range` or `User-Agent`,
    /// are dropped, and a later value of a name replaces an earlier one. It
    /// replaces the transform before.
    ///
    /// Fails with [`ErrorKind::Unsupported`](mln_util::ErrorKind) in the
    /// browser, where the browser sends the requests.
    pub fn set_http_header_transform(
        &self,
        transform: impl Fn(Kind, &str) -> HttpHeaders + Send + Sync + 'static,
    ) -> Result<Completion<()>, Error> {
        if cfg!(target_family = "wasm") {
            return Err(Error::unsupported("the browser sends the requests and sets their headers"));
        }
        let transform = HttpHeaderTransform::new(Some(Arc::new(move |kind: Kind, url: &str| {
            crate::resources::sanitize_headers(transform(kind, url))
        })));
        self.apply(move |core| {
            core.network()?.set_http_header_transform(transform);
            Ok(())
        })
    }

    /// Stops adding headers to HTTP requests.
    pub fn clear_http_header_transform(&self) -> Result<Completion<()>, Error> {
        self.apply(move |core| {
            core.network()?.set_http_header_transform(HttpHeaderTransform::default());
            Ok(())
        })
    }

    /// Runs a maintenance action on the cache database.
    pub fn run_cache_database_operation(&self, operation: CacheDatabaseOperation) -> Result<Completion<()>, Error> {
        self.command(move |core, outbox, resolver| {
            let database = match core.database() {
                Ok(database) => database,
                Err(error) => return outbox.resolve(resolver, Err(error)),
            };
            let reply = reply_on(core.run_loop.clone(), resolver);
            match operation {
                CacheDatabaseOperation::Clear => database.clear_ambient_cache(reply),
                CacheDatabaseOperation::Invalidate => database.invalidate_ambient_cache(reply),
                CacheDatabaseOperation::Pack => database.pack_database(reply),
                CacheDatabaseOperation::Reset => database.reset_database(reply),
            }
        })
    }

    /// Limits the ambient cache to `size` bytes. Offline regions don't count
    /// toward it.
    pub fn set_maximum_ambient_cache_size(&self, size: u64) -> Result<Completion<()>, Error> {
        self.command(move |core, outbox, resolver| match core.database() {
            Ok(database) => database.set_maximum_ambient_cache_size(size, reply_on(core.run_loop.clone(), resolver)),
            Err(error) => outbox.resolve(resolver, Err(error)),
        })
    }

    /// The offline regions in the runtime's cache database.
    #[must_use]
    pub fn offline(&self) -> Offline<'_> {
        Offline::new(self)
    }

    /// Shuts the runtime down. The completion reports when its thread has
    /// let go of its maps' resources and its file sources.
    ///
    /// Fails with [`ErrorKind::InvalidState`](mln_util::ErrorKind) while the
    /// runtime has maps that aren't released, or when the runtime was
    /// released already. Every call on the runtime fails the same way after.
    pub fn release(&self) -> Result<Completion<()>, Error> {
        {
            let mut lifecycle = self.shared.lifecycle.lock_unpoisoned();
            if lifecycle.released {
                return Err(released());
            }
            if lifecycle.live_maps > 0 {
                return Err(Error::invalid_state("release the runtime's maps first"));
            }
            lifecycle.released = true;
            lifecycle.shut_down = true;
        }
        let (completion, resolver) = self.shared.completion();
        self.shared.shut_down(Some(resolver));
        Ok(completion)
    }

    /// Releases the runtime whatever its state, without waiting: it shuts
    /// down once its last map is released. Every call on the runtime fails
    /// with [`ErrorKind::InvalidState`](mln_util::ErrorKind) after.
    pub fn dispose(&self) -> Result<(), Error> {
        let shut_down = {
            let mut lifecycle = self.shared.lifecycle.lock_unpoisoned();
            if lifecycle.released {
                return Err(released());
            }
            lifecycle.released = true;
            let shut_down = lifecycle.live_maps == 0;
            lifecycle.shut_down |= shut_down;
            shut_down
        };
        if shut_down {
            self.shared.shut_down(None);
        }
        Ok(())
    }

    /// Sends a command for the offline API.
    pub(crate) fn offline_command<T: Send + 'static>(
        &self,
        command: impl FnOnce(&DatabaseFileSource, &RuntimeCore, Resolver<T>) + Send + 'static,
    ) -> Result<Completion<T>, Error> {
        self.command(move |core, outbox, resolver| match core.database() {
            Ok(database) => command(database, core, resolver),
            Err(error) => outbox.resolve(resolver, Err(error)),
        })
    }
}

// Handles cross threads; the core never leaves its own.
const _: () = {
    const fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<Runtime>();
    assert_send_sync::<Map>();
};