]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/FGTileLoader.hxx
Initial stab at a threaded tile loader contributed by Bernie Bright.
[flightgear.git] / src / Scenery / FGTileLoader.hxx
1 // FGTileLoader - Queue scenery tiles for loading.
2 //
3 // Written by Bernie Bright, started March 2001.
4 //
5 // Copyright (C) 2001  Bernard Bright - bbright@bigpond.net.au
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef FG_TILE_LOADER_HXX
25 #define FG_TILE_LOADER_HXX
26
27 #include <queue>
28 #include <pthread.h>
29
30 #include <simgear/bucket/newbucket.hxx>
31 #include <simgear/misc/sg_path.hxx>
32
33 #ifdef ENABLE_THREADS
34 #  include <simgear/threads/SGThread.hxx>
35 #endif
36
37 // Forward reference.
38 class FGTileEntry;
39
40 /**
41  * Queues tiles for loading, possibly by a separate thread.
42  */
43 class FGTileLoader
44 {
45 public:
46
47     /**
48      * Constructor.
49      */
50     FGTileLoader();
51
52     /**
53      * Destructor.
54      */
55     ~FGTileLoader();
56
57     /**
58      * Add a tile to the end of the load queue.
59      * @param tile The tile to be loaded from disk.
60      * @param vis Current visibilty (in feet?) (see FGTileMgr::vis).
61      */
62     void add( FGTileEntry* tile );
63
64     /**
65      * Returns whether the load queue is empty (contains no elements).
66      * @return true if load queue is empty otherwise returns false.
67      */
68     bool empty() const { return tile_queue.empty(); }
69
70 private:
71
72 private:
73
74     /**
75      * FIFO queue of tiles to load from data files.
76      */
77     std::queue< FGTileEntry* > tile_queue;
78
79     /**
80      * Base name of directory containing tile data file.
81      */
82     SGPath tile_path;
83
84 #ifdef ENABLE_THREADS
85     /**
86      * Maximum number of threads to create for loading tiles.
87      */
88     enum { MAX_THREADS = 1 };
89
90     /**
91      * This class represents the thread of execution responsible for
92      * loading a tile.
93      */
94     class LoaderThread : public SGThread
95     {
96     public:
97         LoaderThread( FGTileLoader* l ) : loader(l) {}
98         ~LoaderThread() {}
99
100         /**
101          * Reads the tile from disk.
102          */
103         void run();
104
105     private:
106         FGTileLoader* loader;
107
108     private:
109         // not implemented.
110         LoaderThread();
111         LoaderThread( const LoaderThread& );
112         LoaderThread& operator=( const LoaderThread& );
113     };
114
115     friend class LoaderThread;
116
117     /**
118      * Array of loading threads.
119      */
120     LoaderThread* threads[ MAX_THREADS ];
121     
122     /**
123      * Lock and synchronize access to tile queue.
124      */
125     SGMutex mutex;
126     SGCondition cond;
127
128     /**
129      * Thread cleanup handler.
130      */
131     friend void cleanup_handler( void* );
132 #endif // ENABLE_THREADS
133 };
134
135 #endif // FG_TILE_LOADER_HXX