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 <Main/globals.hxx>
28 #include "FGTileLoader.hxx"
29 #include "tileentry.hxx"
30 #include "tilemgr.hxx"
32 extern ssgBranch *terrain;
33 extern ssgBranch *ground;
38 FGTileLoader::FGTileLoader()
41 // Create and start the loader threads.
42 for (int i = 0; i < MAX_THREADS; ++i)
44 threads[i] = new LoaderThread(this);
47 #endif // ENABLE_THREADS
51 * Terminate all threads.
53 FGTileLoader::~FGTileLoader()
56 // Wake up its time to die.
57 // queue_cond.broadcast();
59 for (int i = 0; i < MAX_THREADS; ++i)
64 #endif // ENABLE_THREADS
71 FGTileLoader::add( FGTileEntry* tile )
74 * Initialise tile_path here and not in ctor to avoid problems
75 * with the initialastion order of global objects.
77 static bool beenhere = false;
80 if ( globals->get_fg_scenery() != (string)"" ) {
81 tile_path.set( globals->get_fg_scenery() );
83 tile_path.set( globals->get_fg_root() );
84 tile_path.append( "Scenery" );
89 tile_load_queue.push( tile );
92 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
97 FGTileLoader::remove( FGTileEntry* tile )
99 tile_free_queue.push( tile );
107 FGTileLoader::update()
109 #ifdef ENABLE_THREADS
110 // send a signal to the pager thread that it is allowed to load
116 if ( !tile_load_queue.empty() ) {
117 cout << "loading next tile ..." << endl;
118 // load the next tile in the queue
119 FGTileEntry* tile = tile_load_queue.front();
120 tile_load_queue.pop();
121 tile->load( tile_path, true );
122 FGTileMgr::loaded( tile );
125 if ( !tile_free_queue.empty() ) {
126 cout << "freeing next tile ..." << endl;
127 // free the next tile in the queue
128 FGTileEntry* tile = tile_free_queue.front();
129 tile_free_queue.pop();
134 #endif // ENABLE_THREADS
138 #ifdef ENABLE_THREADS
143 FGTileLoader::LoaderThread::run()
145 pthread_cleanup_push( cleanup_handler, loader );
147 // Wait for a load request to be placed in the queue.
148 FGTileEntry* tile = loader->tile_load_queue.pop();
150 // Wait for the next frame signal before we load a tile from the queue
151 loader->mutex.lock();
152 loader->frame_cond.wait( loader->mutex );
153 loader->mutex.unlock();
155 set_cancel( SGThread::CANCEL_DISABLE );
156 tile->load( loader->tile_path, true );
157 set_cancel( SGThread::CANCEL_DEFERRED );
159 FGTileMgr::ready_to_attach( tile );
161 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
162 // Handle and pending removals
163 while ( !loader->tile_free_queue.empty() ) {
164 cout << "freeing next tile ..." << endl;
165 // free the next tile in the queue
166 FGTileEntry* tile = loader->tile_free_queue.pop();
173 pthread_cleanup_pop(1);
177 * Ensure mutex is unlocked.
180 cleanup_handler( void* arg )
182 FGTileLoader* loader = (FGTileLoader*) arg;
183 loader->mutex.unlock();
185 #endif // ENABLE_THREADS