1 // FGTileLoader - Queue scenery tiles for loading.
3 // Written by Bernie Bright, started March 2001.
5 // Copyright (C) 2001 Bernard Bright - bbright@bigpond.net.au
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.
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.
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.
27 #include <simgear/compiler.h>
29 #include <Main/globals.hxx>
30 #include "FGTileLoader.hxx"
31 #include "tileentry.hxx"
32 #include "tilemgr.hxx"
34 extern ssgBranch *terrain;
35 extern ssgBranch *ground;
41 FGTileLoader::FGTileLoader()
43 #if defined(ENABLE_THREADS) && ENABLE_THREADS
44 // Create and start the loader threads.
45 for (int i = 0; i < MAX_THREADS; ++i)
47 threads[i] = new LoaderThread(this);
50 #endif // ENABLE_THREADS
54 * Terminate all threads.
56 FGTileLoader::~FGTileLoader()
58 #if defined(ENABLE_THREADS) && ENABLE_THREADS
59 // Wake up its time to die.
60 // queue_cond.broadcast();
62 for (int i = 0; i < MAX_THREADS; ++i)
67 #endif // ENABLE_THREADS
71 #if 0 // we don't ever want to do this I don't think
75 void FGTileLoader::reinit() {
76 while ( !tile_load_queue.empty() ) {
77 tile_load_queue.pop();
87 FGTileLoader::add( FGTileEntry* tile )
90 * Initialise tile_path here and not in ctor to avoid problems
91 * with the initialastion order of global objects.
93 static bool beenhere = false;
96 if ( !globals->get_fg_scenery().empty() ) {
97 tile_path = globals->get_fg_scenery();
100 tmp.set( globals->get_fg_root() );
101 tmp.append( "Scenery/Terrain" );
107 tmp.append(globals->get_fg_root() );
108 tmp.append( "Scenery/Objects" );
109 tile_path = tmp.str();
114 tile_load_queue.push( tile );
117 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
122 FGTileLoader::remove( FGTileEntry* tile )
124 tile_free_queue.push( tile );
132 FGTileLoader::update()
135 #if defined(ENABLE_THREADS) && ENABLE_THREADS
136 // send a signal to the pager thread that it is allowed to load
142 if ( !tile_load_queue.empty() ) {
143 // cout << "loading next tile ..." << endl;
144 // load the next tile in the queue
145 FGTileEntry* tile = tile_load_queue.front();
146 tile_load_queue.pop();
148 tile->load( tile_path, true );
150 FGTileMgr::ready_to_attach( tile );
153 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
154 if ( !tile_free_queue.empty() ) {
155 // cout << "freeing next tile ..." << endl;
156 // free the next tile in the queue
157 FGTileEntry* tile = tile_free_queue.front();
158 tile_free_queue.pop();
164 #endif // ENABLE_THREADS
169 #if defined(ENABLE_THREADS) && ENABLE_THREADS
174 FGTileLoader::LoaderThread::run()
176 pthread_cleanup_push( cleanup_handler, loader );
178 // Wait for a load request to be placed in the queue.
179 FGTileEntry* tile = loader->tile_load_queue.pop();
181 // Wait for the next frame signal before we load a tile from the queue
182 loader->mutex.lock();
183 loader->frame_cond.wait( loader->mutex );
184 loader->mutex.unlock();
186 set_cancel( SGThread::CANCEL_DISABLE );
187 tile->load( loader->tile_path, true );
188 set_cancel( SGThread::CANCEL_DEFERRED );
190 FGTileMgr::ready_to_attach( tile );
192 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
193 // Handle and pending removals
194 while ( !loader->tile_free_queue.empty() ) {
195 // cout << "freeing next tile ..." << endl;
196 // free the next tile in the queue
197 FGTileEntry* tile = loader->tile_free_queue.pop();
204 pthread_cleanup_pop(1);
208 * Ensure mutex is unlocked.
211 cleanup_handler( void* arg )
213 FGTileLoader* loader = (FGTileLoader*) arg;
214 loader->mutex.unlock();
216 #endif // ENABLE_THREADS