Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
background_scanner.hpp
Go to the documentation of this file.
1#pragma once
2
3// Background plugin scanning.
4//
5// Wraps any scan function in a worker-thread driver with cooperative
6// cancellation and progress callbacks. Settings-panel UIs subscribe to
7// progress events, run a scan without blocking the UI thread, and cancel
8// mid-flight when the user closes the dialog or adds/removes a search
9// path.
10//
11// Not tied to PluginScanner concretely — accepts any callable of shape
12// std::vector<PluginInfo>(const CancelToken&, ProgressSink).
13// This keeps tests fast (fake scanners) and lets consumers swap in
14// cached-scan paths later.
15
16#include <pulp/host/scanner.hpp>
17
18#include <atomic>
19#include <functional>
20#include <mutex>
21#include <string>
22#include <thread>
23#include <vector>
24
25namespace pulp::host {
26
30public:
31 void request() { flag_.store(true, std::memory_order_release); }
32 bool requested() const { return flag_.load(std::memory_order_acquire); }
35 void reset() { flag_.store(false, std::memory_order_release); }
36
37private:
38 std::atomic<bool> flag_{false};
39};
40
43using ScanProgressCallback = std::function<void(
44 const std::string& current_path, int scanned, int total)>;
45
48using ScanCompletionCallback = std::function<void(
49 std::vector<PluginInfo> results, bool cancelled)>;
50
54using ScanWorkerFn = std::function<std::vector<PluginInfo>(
55 const CancelToken& token, const ScanProgressCallback& progress)>;
56
58public:
59 BackgroundScanner() = default;
60 ~BackgroundScanner() { stop_and_join(); }
61
64
66 bool start(ScanWorkerFn worker,
67 ScanProgressCallback progress,
68 ScanCompletionCallback completion);
69
72 void cancel();
73
75 bool is_running() const {
76 return running_.load(std::memory_order_acquire);
77 }
78
80 void join();
81
82private:
83 void stop_and_join();
84
85 std::thread worker_;
86 CancelToken token_;
87 std::atomic<bool> running_{false};
88 std::mutex start_mutex_; // serializes start() vs stop_and_join()
89};
90
91} // namespace pulp::host
BackgroundScanner & operator=(const BackgroundScanner &)=delete
void join()
Block until the worker thread finishes. Safe to call when idle.
void cancel()
Request the running scan cancel.
bool start(ScanWorkerFn worker, ScanProgressCallback progress, ScanCompletionCallback completion)
Start a scan. Returns false if a scan is already running.
~BackgroundScanner()
Definition background_scanner.hpp:60
bool is_running() const
True while the worker thread is active.
Definition background_scanner.hpp:75
BackgroundScanner(const BackgroundScanner &)=delete
Shared cancel flag.
Definition background_scanner.hpp:29
bool requested() const
Definition background_scanner.hpp:32
void reset()
Clear the flag for a fresh run.
Definition background_scanner.hpp:35
void request()
Definition background_scanner.hpp:31
Bounded event contracts shared by host ingress and graph-authored nodes.
Definition anticipation_eligibility.hpp:9
std::function< void( std::vector< PluginInfo > results, bool cancelled)> ScanCompletionCallback
Completion callback invoked when the scan finishes (or cancel lands).
Definition background_scanner.hpp:48
std::function< std::vector< PluginInfo >( const CancelToken &token, const ScanProgressCallback &progress)> ScanWorkerFn
Worker signature: runs on the background thread.
Definition background_scanner.hpp:54
std::function< void( const std::string &current_path, int scanned, int total)> ScanProgressCallback
Progress callback invoked from the worker thread.
Definition background_scanner.hpp:43