]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/FGTileLoader.cxx
a27d79dd769781a496b52115bb77c0e882eee7f4
[flightgear.git] / src / Scenery / FGTileLoader.cxx
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 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <Main/globals.hxx>
28 #include "FGTileLoader.hxx"
29 #include "tileentry.hxx"
30 #include "tilemgr.hxx"
31
32 extern ssgBranch *terrain;
33 extern ssgBranch *ground;
34
35 /**
36  * 
37  */
38 FGTileLoader::FGTileLoader()
39 {
40 #ifdef ENABLE_THREADS
41     // Create and start the loader threads.
42     for (int i = 0; i < MAX_THREADS; ++i)
43     {
44         threads[i] = new LoaderThread(this);
45         threads[i]->start();
46     }
47 #endif // ENABLE_THREADS
48 }
49
50 /**
51  * Terminate all threads.
52  */
53 FGTileLoader::~FGTileLoader()
54 {
55 #ifdef ENABLE_THREADS
56     // Wake up its time to die.
57     // queue_cond.broadcast();
58
59     for (int i = 0; i < MAX_THREADS; ++i)
60     {
61         threads[i]->cancel();
62         threads[i]->join();
63     }    
64 #endif // ENABLE_THREADS
65 }
66
67
68 /**
69  * 
70  */
71 void FGTileLoader::reinit() {
72     while ( !tile_load_queue.empty() ) {
73         tile_load_queue.pop();
74     }
75 }
76
77
78 /**
79  * 
80  */
81 void
82 FGTileLoader::add( FGTileEntry* tile )
83 {
84     /**
85      * Initialise tile_path here and not in ctor to avoid problems
86      * with the initialastion order of global objects.
87      */
88     static bool beenhere = false;
89     if (!beenhere)
90     {
91         if ( globals->get_fg_scenery() != (string)"" ) {
92             tile_path.set( globals->get_fg_scenery() );
93         } else {
94             tile_path.set( globals->get_fg_root() );
95             tile_path.append( "Scenery" );
96         }
97         beenhere = true;
98     }
99
100     tile_load_queue.push( tile );
101 }
102
103 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
104 /**
105  * 
106  */
107 void
108 FGTileLoader::remove( FGTileEntry* tile )
109 {
110     tile_free_queue.push( tile );
111 }
112 #endif
113
114 /**
115  * 
116  */
117 void
118 FGTileLoader::update()
119 {
120 #ifdef ENABLE_THREADS
121     // send a signal to the pager thread that it is allowed to load
122     // another tile
123     mutex.lock();
124     frame_cond.signal();
125     mutex.unlock();
126 #else
127     if ( !tile_load_queue.empty() ) {
128         cout << "loading next tile ..." << endl;
129         // load the next tile in the queue
130         FGTileEntry* tile = tile_load_queue.front();
131         tile_load_queue.pop();
132         tile->load( tile_path, true );
133         FGTileMgr::ready_to_attach( tile );
134     }
135
136 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
137     if ( !tile_free_queue.empty() ) {
138         cout << "freeing next tile ..." << endl;
139         // free the next tile in the queue
140         FGTileEntry* tile = tile_free_queue.front();
141         tile_free_queue.pop();
142         tile->free_tile();
143         delete tile;
144     }
145 #endif
146
147 #endif // ENABLE_THREADS
148 }
149
150
151 #ifdef ENABLE_THREADS
152 /**
153  * 
154  */
155 void
156 FGTileLoader::LoaderThread::run()
157 {
158     pthread_cleanup_push( cleanup_handler, loader );
159     while ( true ) {
160         // Wait for a load request to be placed in the queue.
161         FGTileEntry* tile = loader->tile_load_queue.pop();
162
163         // Wait for the next frame signal before we load a tile from the queue
164         loader->mutex.lock();
165         loader->frame_cond.wait( loader->mutex );
166         loader->mutex.unlock();
167
168         set_cancel( SGThread::CANCEL_DISABLE );
169         tile->load( loader->tile_path, true );
170         set_cancel( SGThread::CANCEL_DEFERRED );
171
172         FGTileMgr::ready_to_attach( tile );
173
174 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
175         // Handle and pending removals
176         while ( !loader->tile_free_queue.empty() ) {
177             cout << "freeing next tile ..." << endl;
178             // free the next tile in the queue
179             FGTileEntry* tile = loader->tile_free_queue.pop();
180             tile->free_tile();
181             delete tile;
182         }
183 #endif
184
185     }
186     pthread_cleanup_pop(1);
187 }
188
189 /**
190  * Ensure mutex is unlocked.
191  */
192 void 
193 cleanup_handler( void* arg )
194 {
195     FGTileLoader* loader = (FGTileLoader*) arg;
196     loader->mutex.unlock();
197 }
198 #endif // ENABLE_THREADS