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;
40 FGTileLoader::FGTileLoader()
43 // Create and start the loader threads.
44 for (int i = 0; i < MAX_THREADS; ++i)
46 threads[i] = new LoaderThread(this);
49 #endif // ENABLE_THREADS
53 * Terminate all threads.
55 FGTileLoader::~FGTileLoader()
58 // Wake up its time to die.
59 // queue_cond.broadcast();
61 for (int i = 0; i < MAX_THREADS; ++i)
66 #endif // ENABLE_THREADS
70 #if 0 // we don't ever want to do this I don't think
74 void FGTileLoader::reinit() {
75 while ( !tile_load_queue.empty() ) {
76 tile_load_queue.pop();
86 FGTileLoader::add( FGTileEntry* tile )
89 * Initialise tile_path here and not in ctor to avoid problems
90 * with the initialastion order of global objects.
92 static bool beenhere = false;
95 if ( !globals->get_fg_scenery().empty() ) {
96 tile_path.set( globals->get_fg_scenery() );
98 tile_path.set( globals->get_fg_root() );
99 tile_path.append( "Scenery" );
104 tile_load_queue.push( tile );
107 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
112 FGTileLoader::remove( FGTileEntry* tile )
114 tile_free_queue.push( tile );
122 FGTileLoader::update()
125 #ifdef ENABLE_THREADS
126 // send a signal to the pager thread that it is allowed to load
132 if ( !tile_load_queue.empty() ) {
133 // cout << "loading next tile ..." << endl;
134 // load the next tile in the queue
135 FGTileEntry* tile = tile_load_queue.front();
136 tile_load_queue.pop();
138 tile->load( tile_path, true );
140 FGTileMgr::ready_to_attach( tile );
143 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
144 if ( !tile_free_queue.empty() ) {
145 // cout << "freeing next tile ..." << endl;
146 // free the next tile in the queue
147 FGTileEntry* tile = tile_free_queue.front();
148 tile_free_queue.pop();
154 #endif // ENABLE_THREADS
159 #ifdef ENABLE_THREADS
164 FGTileLoader::LoaderThread::run()
166 pthread_cleanup_push( cleanup_handler, loader );
168 // Wait for a load request to be placed in the queue.
169 FGTileEntry* tile = loader->tile_load_queue.pop();
171 // Wait for the next frame signal before we load a tile from the queue
172 loader->mutex.lock();
173 loader->frame_cond.wait( loader->mutex );
174 loader->mutex.unlock();
176 set_cancel( SGThread::CANCEL_DISABLE );
177 tile->load( loader->tile_path, true );
178 set_cancel( SGThread::CANCEL_DEFERRED );
180 FGTileMgr::ready_to_attach( tile );
182 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
183 // Handle and pending removals
184 while ( !loader->tile_free_queue.empty() ) {
185 // cout << "freeing next tile ..." << endl;
186 // free the next tile in the queue
187 FGTileEntry* tile = loader->tile_free_queue.pop();
194 pthread_cleanup_pop(1);
198 * Ensure mutex is unlocked.
201 cleanup_handler( void* arg )
203 FGTileLoader* loader = (FGTileLoader*) arg;
204 loader->mutex.unlock();
206 #endif // ENABLE_THREADS