1 /* SPDX-FileCopyrightText: 2018 Robert Krawitz <rlk@alum.mit.edu>
2 
3    SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #ifndef IMAGESCOUT_H
7 #define IMAGESCOUT_H
8 
9 #include <kpabase/FileName.h>
10 
11 #include <QAtomicInt>
12 #include <QList>
13 #include <QMutex>
14 #include <QQueue>
15 
16 namespace DB
17 {
18 
19 typedef QQueue<DB::FileName> ImageScoutQueue;
20 typedef void (*PreloadFunc)(const DB::FileName &);
21 class ImageScoutThread;
22 
23 /**
24  * Scout thread for image loading: preload images from disk to have them in
25  * RAM to mask I/O latency.
26  */
27 class ImageScout
28 {
29 public:
30     // count is an atomic variable containing the number of images
31     // that have been loaded thus far.  Used to prevent the scout from
32     // getting too far ahead, if it's able to run faster than the
33     // loader.  Which usually it can't; with hard disks on any halfway
34     // decent system, the loader can run faster than the disk.
35     ImageScout(ImageScoutQueue &, QAtomicInt &count, int threads = 1);
36     ~ImageScout();
37     // Specify scout buffer size.  May not be called after starting the scout.
38     void setBufSize(int);
39     int getBufSize();
40     // Specify how far we're allowed to run ahead of the loader, in images.
41     // May not be called after starting the scout.
42     void setMaxSeekAhead(int);
43     int getMaxSeekAhead();
44     // Specify how many bytes we read before moving on.
45     // May not be called after starting the scout.
46     void setReadLimit(int);
47     int getReadLimit();
48     // Specify an alternate function to preload the file.
49     // This function may perform useful work.  Note that this is not
50     // guaranteed to be called, so anything using this must be
51     // prepared to do the work later.
52     void setPreloadFunc(PreloadFunc);
53     PreloadFunc getPreloadFunc();
54     // Start the scout running
55     void start();
56 
57 private:
58     QMutex m_mutex;
59     QList<ImageScoutThread *> m_scoutList;
60     QAtomicInt m_preloadedCount;
61     QAtomicInt m_skippedCount;
62     bool m_isStarted;
63     int m_scoutBufSize;
64     int m_maxSeekAhead;
65     int m_readLimit;
66     PreloadFunc m_preloadFunc;
67 };
68 }
69 
70 #endif /* IMAGESCOUT_H */
71 
72 // vi:expandtab:tabstop=4 shiftwidth=4:
73