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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <simgear/compiler.h>
28 #include <simgear/structure/exception.hxx>
30 #include <Main/globals.hxx>
31 #include "FGTileLoader.hxx"
32 #include "tileentry.hxx"
33 #include "tilemgr.hxx"
35 extern ssgBranch *terrain;
36 extern ssgBranch *ground;
42 FGTileLoader::FGTileLoader()
44 #if defined(ENABLE_THREADS)
45 // Create and start the loader threads.
46 for (int i = 0; i < MAX_THREADS; ++i)
48 threads[i] = new LoaderThread(this);
49 threads[i]->start( 1 );
51 #endif // ENABLE_THREADS
55 * Terminate all threads.
57 FGTileLoader::~FGTileLoader()
59 #if defined(ENABLE_THREADS)
60 // Wake up its time to die.
61 // queue_cond.broadcast();
63 for (int i = 0; i < MAX_THREADS; ++i)
68 #endif // ENABLE_THREADS
72 #if 0 // we don't ever want to do this I don't think
76 void FGTileLoader::reinit() {
77 while ( !tile_load_queue.empty() ) {
78 tile_load_queue.pop();
88 FGTileLoader::add( FGTileEntry* tile )
91 * Initialise tile_path here and not in ctor to avoid problems
92 * with the initialisation order of global objects.
94 static bool beenhere = false;
96 tile_path = globals->get_fg_scenery();
97 if (!tile_path.size())
98 throw sg_throwable(string("No valid scenery path defined!"));
103 tile_load_queue.push( tile );
106 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
111 FGTileLoader::remove( FGTileEntry* tile )
113 tile_free_queue.push( tile );
121 FGTileLoader::update()
124 #if defined(ENABLE_THREADS)
125 // send a signal to the pager thread that it is allowed to load
131 if ( !tile_load_queue.empty() ) {
132 // cout << "loading next tile ..." << endl;
133 // load the next tile in the queue
134 FGTileEntry* tile = tile_load_queue.front();
135 tile_load_queue.pop();
137 tile->load( tile_path, true );
139 FGTileMgr::ready_to_attach( tile );
142 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
143 if ( !tile_free_queue.empty() ) {
144 // cout << "freeing next tile ..." << endl;
145 // free the next tile in the queue
146 FGTileEntry* tile = tile_free_queue.front();
147 tile_free_queue.pop();
153 #endif // ENABLE_THREADS
158 #if defined(ENABLE_THREADS)
160 * Ensure mutex is unlocked.
163 cleanup_handler( void* arg )
165 FGTileLoader* loader = (FGTileLoader*) arg;
166 loader->mutex.unlock();
173 FGTileLoader::LoaderThread::run()
175 pthread_cleanup_push( cleanup_handler, loader );
177 // Wait for a load request to be placed in the queue.
178 FGTileEntry* tile = loader->tile_load_queue.pop();
180 // Wait for the next frame signal before we load a tile from the queue
181 loader->mutex.lock();
182 loader->frame_cond.wait( loader->mutex );
183 loader->mutex.unlock();
185 set_cancel( SGThread::CANCEL_DISABLE );
186 tile->load( loader->tile_path, true );
187 set_cancel( SGThread::CANCEL_DEFERRED );
189 FGTileMgr::ready_to_attach( tile );
191 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
192 // Handle and pending removals
193 while ( !loader->tile_free_queue.empty() ) {
194 // cout << "freeing next tile ..." << endl;
195 // free the next tile in the queue
196 FGTileEntry* tile = loader->tile_free_queue.pop();
203 pthread_cleanup_pop(1);
205 #endif // ENABLE_THREADS