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"
34 FGTileLoader::FGTileLoader()
37 // Create and start the loader threads.
38 for (int i = 0; i < MAX_THREADS; ++i)
40 threads[i] = new LoaderThread(this);
43 #endif // ENABLE_THREADS
47 * Terminate all threads.
49 FGTileLoader::~FGTileLoader()
52 // Wake up its time to die.
55 for (int i = 0; i < MAX_THREADS; ++i)
60 #endif // ENABLE_THREADS
67 FGTileLoader::add( FGTileEntry* tile )
70 * Initialise tile_path here and not in ctor to avoid problems
71 * with the initialastion order of global objects.
73 static bool beenhere = false;
76 if ( globals->get_fg_scenery() != (string)"" ) {
77 tile_path.set( globals->get_fg_scenery() );
79 tile_path.set( globals->get_fg_root() );
80 tile_path.append( "Scenery" );
87 tile_queue.push( tile );
88 // Signal waiting working threads.
92 tile->load( tile_path, true );
93 #endif // ENABLE_THREADS
101 FGTileLoader::LoaderThread::run()
103 pthread_cleanup_push( cleanup_handler, loader );
105 // Wait for a load request to be placed in the queue.
106 loader->mutex.lock();
107 while (loader->empty())
109 loader->cond.wait( loader->mutex );
112 // Have we been canceled - exits if yes.
113 //pthread_testcancel();
116 loader->mutex.unlock();
117 pthread_exit( PTHREAD_CANCELED );
120 // Grab the tile to load and release the mutex.
121 FGTileEntry* tile = loader->tile_queue.front();
122 loader->tile_queue.pop();
123 loader->mutex.unlock();
125 set_cancel( SGThread::CANCEL_DISABLE );
126 tile->load( loader->tile_path, true );
127 set_cancel( SGThread::CANCEL_DEFERRED );
129 pthread_cleanup_pop(1);
133 * Ensure mutex is unlocked.
136 cleanup_handler( void* arg )
138 FGTileLoader* loader = (FGTileLoader*) arg;
139 loader->mutex.unlock();
141 #endif // ENABLE_THREADS