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 = globals->get_fg_scenery();
99 tmp.set( globals->get_fg_root() );
100 tmp.append( "Scenery" );
101 tile_path = tmp.str();
106 tile_load_queue.push( tile );
109 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
114 FGTileLoader::remove( FGTileEntry* tile )
116 tile_free_queue.push( tile );
124 FGTileLoader::update()
127 #ifdef ENABLE_THREADS
128 // send a signal to the pager thread that it is allowed to load
134 if ( !tile_load_queue.empty() ) {
135 // cout << "loading next tile ..." << endl;
136 // load the next tile in the queue
137 FGTileEntry* tile = tile_load_queue.front();
138 tile_load_queue.pop();
140 tile->load( tile_path, true );
142 FGTileMgr::ready_to_attach( tile );
145 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
146 if ( !tile_free_queue.empty() ) {
147 // cout << "freeing next tile ..." << endl;
148 // free the next tile in the queue
149 FGTileEntry* tile = tile_free_queue.front();
150 tile_free_queue.pop();
156 #endif // ENABLE_THREADS
161 #ifdef ENABLE_THREADS
166 FGTileLoader::LoaderThread::run()
168 pthread_cleanup_push( cleanup_handler, loader );
170 // Wait for a load request to be placed in the queue.
171 FGTileEntry* tile = loader->tile_load_queue.pop();
173 // Wait for the next frame signal before we load a tile from the queue
174 loader->mutex.lock();
175 loader->frame_cond.wait( loader->mutex );
176 loader->mutex.unlock();
178 set_cancel( SGThread::CANCEL_DISABLE );
179 tile->load( loader->tile_path, true );
180 set_cancel( SGThread::CANCEL_DEFERRED );
182 FGTileMgr::ready_to_attach( tile );
184 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
185 // Handle and pending removals
186 while ( !loader->tile_free_queue.empty() ) {
187 // cout << "freeing next tile ..." << endl;
188 // free the next tile in the queue
189 FGTileEntry* tile = loader->tile_free_queue.pop();
196 pthread_cleanup_pop(1);
200 * Ensure mutex is unlocked.
203 cleanup_handler( void* arg )
205 FGTileLoader* loader = (FGTileLoader*) arg;
206 loader->mutex.unlock();
208 #endif // ENABLE_THREADS