2001-04-11 02:47:15 +00:00
|
|
|
// FGTileLoader - Queue scenery tiles for loading.
|
|
|
|
//
|
|
|
|
// Written by Bernie Bright, started March 2001.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2001 Bernard Bright - bbright@bigpond.net.au
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2001-04-11 02:47:15 +00:00
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2002-03-16 00:18:38 +00:00
|
|
|
#include <simgear/compiler.h>
|
2005-01-17 22:10:53 +00:00
|
|
|
#include <simgear/structure/exception.hxx>
|
2002-03-16 00:18:38 +00:00
|
|
|
|
2001-04-11 02:47:15 +00:00
|
|
|
#include <Main/globals.hxx>
|
|
|
|
#include "FGTileLoader.hxx"
|
|
|
|
#include "tileentry.hxx"
|
2001-04-16 20:03:52 +00:00
|
|
|
#include "tilemgr.hxx"
|
2001-04-11 02:47:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
FGTileLoader::FGTileLoader()
|
|
|
|
{
|
2005-11-22 17:02:31 +00:00
|
|
|
#if defined(ENABLE_THREADS)
|
2001-04-11 02:47:15 +00:00
|
|
|
// Create and start the loader threads.
|
|
|
|
for (int i = 0; i < MAX_THREADS; ++i)
|
|
|
|
{
|
|
|
|
threads[i] = new LoaderThread(this);
|
2005-01-09 10:27:01 +00:00
|
|
|
threads[i]->start( 1 );
|
2001-04-11 02:47:15 +00:00
|
|
|
}
|
|
|
|
#endif // ENABLE_THREADS
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Terminate all threads.
|
|
|
|
*/
|
|
|
|
FGTileLoader::~FGTileLoader()
|
|
|
|
{
|
2005-11-22 17:02:31 +00:00
|
|
|
#if defined(ENABLE_THREADS)
|
2001-04-11 02:47:15 +00:00
|
|
|
// Wake up its time to die.
|
2001-04-16 20:03:52 +00:00
|
|
|
// queue_cond.broadcast();
|
2001-04-11 02:47:15 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < MAX_THREADS; ++i)
|
|
|
|
{
|
|
|
|
threads[i]->cancel();
|
|
|
|
threads[i]->join();
|
|
|
|
}
|
|
|
|
#endif // ENABLE_THREADS
|
|
|
|
}
|
|
|
|
|
2001-05-21 20:44:59 +00:00
|
|
|
|
2001-05-30 18:21:03 +00:00
|
|
|
#if 0 // we don't ever want to do this I don't think
|
2001-05-21 20:44:59 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void FGTileLoader::reinit() {
|
|
|
|
while ( !tile_load_queue.empty() ) {
|
|
|
|
tile_load_queue.pop();
|
|
|
|
}
|
|
|
|
}
|
2001-05-30 18:21:03 +00:00
|
|
|
#endif
|
2001-05-21 20:44:59 +00:00
|
|
|
|
|
|
|
|
2001-04-11 02:47:15 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
FGTileLoader::add( FGTileEntry* tile )
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Initialise tile_path here and not in ctor to avoid problems
|
Melchior FRANZ:
Wouldn't it be better to prepare the whole list of paths (or two
separate ones for Terrain/Objects if necessary) in FGGlobals::set_fg_scenery,
and to pass the vector<string>s to FGTileEntry::load? It doesn't seem to make
a lot of sense to split the path up, modify it, mount it together to one string
again, and then let FGTileEntry::load split it up again.
Here we go:
Main/globals.cxx
================
As fg_scenery is now a string_list, we don't need initialization. Furthermore,
this list is cleared with every set_fg_scenery() call.
ctor: create default dir from fg_root if necessary. Otherwise check all paths
of --fg-scenery/FG_SCENERY: If the path doesn't exist, ignore it. If it contains
a dir Terrain and/or Objects, then only add that to the list. If it contains
neither, then use the path as is.
Scenery/tileentry.cxx
=====================
Trivial: don't split a "base path", but use the given path_list as is.
(I considered a variable name "path_list" better suited than "search".)
Scenery/FGTileLoader.cxx
========================
No more fiddling with sub-paths. This has to be delivered by get_fg_scenery
already.
2004-06-08 15:32:09 +00:00
|
|
|
* with the initialisation order of global objects.
|
2001-04-11 02:47:15 +00:00
|
|
|
*/
|
|
|
|
static bool beenhere = false;
|
Melchior FRANZ:
Wouldn't it be better to prepare the whole list of paths (or two
separate ones for Terrain/Objects if necessary) in FGGlobals::set_fg_scenery,
and to pass the vector<string>s to FGTileEntry::load? It doesn't seem to make
a lot of sense to split the path up, modify it, mount it together to one string
again, and then let FGTileEntry::load split it up again.
Here we go:
Main/globals.cxx
================
As fg_scenery is now a string_list, we don't need initialization. Furthermore,
this list is cleared with every set_fg_scenery() call.
ctor: create default dir from fg_root if necessary. Otherwise check all paths
of --fg-scenery/FG_SCENERY: If the path doesn't exist, ignore it. If it contains
a dir Terrain and/or Objects, then only add that to the list. If it contains
neither, then use the path as is.
Scenery/tileentry.cxx
=====================
Trivial: don't split a "base path", but use the given path_list as is.
(I considered a variable name "path_list" better suited than "search".)
Scenery/FGTileLoader.cxx
========================
No more fiddling with sub-paths. This has to be delivered by get_fg_scenery
already.
2004-06-08 15:32:09 +00:00
|
|
|
if (!beenhere) {
|
|
|
|
tile_path = globals->get_fg_scenery();
|
2005-01-17 22:10:53 +00:00
|
|
|
if (!tile_path.size())
|
|
|
|
throw sg_throwable(string("No valid scenery path defined!"));
|
|
|
|
|
Melchior FRANZ:
Wouldn't it be better to prepare the whole list of paths (or two
separate ones for Terrain/Objects if necessary) in FGGlobals::set_fg_scenery,
and to pass the vector<string>s to FGTileEntry::load? It doesn't seem to make
a lot of sense to split the path up, modify it, mount it together to one string
again, and then let FGTileEntry::load split it up again.
Here we go:
Main/globals.cxx
================
As fg_scenery is now a string_list, we don't need initialization. Furthermore,
this list is cleared with every set_fg_scenery() call.
ctor: create default dir from fg_root if necessary. Otherwise check all paths
of --fg-scenery/FG_SCENERY: If the path doesn't exist, ignore it. If it contains
a dir Terrain and/or Objects, then only add that to the list. If it contains
neither, then use the path as is.
Scenery/tileentry.cxx
=====================
Trivial: don't split a "base path", but use the given path_list as is.
(I considered a variable name "path_list" better suited than "search".)
Scenery/FGTileLoader.cxx
========================
No more fiddling with sub-paths. This has to be delivered by get_fg_scenery
already.
2004-06-08 15:32:09 +00:00
|
|
|
beenhere = true;
|
2001-04-11 02:47:15 +00:00
|
|
|
}
|
|
|
|
|
2001-05-18 20:31:23 +00:00
|
|
|
tile_load_queue.push( tile );
|
2001-04-11 02:47:15 +00:00
|
|
|
}
|
|
|
|
|
2001-05-20 06:49:06 +00:00
|
|
|
#ifdef WISH_PLIB_WAS_THREADED // but it isn't
|
2001-05-19 16:59:43 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
FGTileLoader::remove( FGTileEntry* tile )
|
|
|
|
{
|
|
|
|
tile_free_queue.push( tile );
|
|
|
|
}
|
2001-05-20 06:49:06 +00:00
|
|
|
#endif
|
2001-05-19 16:59:43 +00:00
|
|
|
|
2001-04-14 03:11:39 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
FGTileLoader::update()
|
|
|
|
{
|
2002-02-11 23:33:20 +00:00
|
|
|
|
2005-11-22 17:02:31 +00:00
|
|
|
#if defined(ENABLE_THREADS)
|
2001-05-18 20:31:23 +00:00
|
|
|
// send a signal to the pager thread that it is allowed to load
|
|
|
|
// another tile
|
2001-04-14 03:11:39 +00:00
|
|
|
mutex.lock();
|
|
|
|
frame_cond.signal();
|
|
|
|
mutex.unlock();
|
2001-05-18 20:31:23 +00:00
|
|
|
#else
|
|
|
|
if ( !tile_load_queue.empty() ) {
|
2001-05-30 18:21:03 +00:00
|
|
|
// cout << "loading next tile ..." << endl;
|
2001-05-18 20:31:23 +00:00
|
|
|
// load the next tile in the queue
|
|
|
|
FGTileEntry* tile = tile_load_queue.front();
|
|
|
|
tile_load_queue.pop();
|
2002-02-11 23:33:20 +00:00
|
|
|
|
2001-05-18 20:31:23 +00:00
|
|
|
tile->load( tile_path, true );
|
2002-02-11 23:33:20 +00:00
|
|
|
|
2001-05-21 20:44:59 +00:00
|
|
|
FGTileMgr::ready_to_attach( tile );
|
2001-05-18 20:31:23 +00:00
|
|
|
}
|
2001-05-19 16:59:43 +00:00
|
|
|
|
2001-05-21 20:44:59 +00:00
|
|
|
#ifdef WISH_PLIB_WAS_THREADED // but it isn't
|
2001-05-19 16:59:43 +00:00
|
|
|
if ( !tile_free_queue.empty() ) {
|
2001-05-30 18:21:03 +00:00
|
|
|
// cout << "freeing next tile ..." << endl;
|
2001-05-19 16:59:43 +00:00
|
|
|
// free the next tile in the queue
|
|
|
|
FGTileEntry* tile = tile_free_queue.front();
|
|
|
|
tile_free_queue.pop();
|
|
|
|
tile->free_tile();
|
|
|
|
delete tile;
|
|
|
|
}
|
2001-05-21 20:44:59 +00:00
|
|
|
#endif
|
2001-05-19 16:59:43 +00:00
|
|
|
|
2001-04-14 03:11:39 +00:00
|
|
|
#endif // ENABLE_THREADS
|
2002-02-11 23:33:20 +00:00
|
|
|
|
2001-04-14 03:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-22 17:02:31 +00:00
|
|
|
#if defined(ENABLE_THREADS)
|
2005-12-29 16:22:38 +00:00
|
|
|
/**
|
|
|
|
* Ensure mutex is unlocked.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cleanup_handler( void* arg )
|
|
|
|
{
|
|
|
|
FGTileLoader* loader = (FGTileLoader*) arg;
|
|
|
|
loader->mutex.unlock();
|
|
|
|
}
|
|
|
|
|
2001-04-11 02:47:15 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
FGTileLoader::LoaderThread::run()
|
|
|
|
{
|
|
|
|
pthread_cleanup_push( cleanup_handler, loader );
|
|
|
|
while ( true ) {
|
|
|
|
// Wait for a load request to be placed in the queue.
|
2001-05-18 20:31:23 +00:00
|
|
|
FGTileEntry* tile = loader->tile_load_queue.pop();
|
2001-04-11 02:47:15 +00:00
|
|
|
|
2001-04-14 03:11:39 +00:00
|
|
|
// Wait for the next frame signal before we load a tile from the queue
|
2001-05-18 20:31:23 +00:00
|
|
|
loader->mutex.lock();
|
|
|
|
loader->frame_cond.wait( loader->mutex );
|
|
|
|
loader->mutex.unlock();
|
2001-04-11 02:47:15 +00:00
|
|
|
|
|
|
|
set_cancel( SGThread::CANCEL_DISABLE );
|
|
|
|
tile->load( loader->tile_path, true );
|
|
|
|
set_cancel( SGThread::CANCEL_DEFERRED );
|
2001-04-16 20:03:52 +00:00
|
|
|
|
2001-05-19 16:59:43 +00:00
|
|
|
FGTileMgr::ready_to_attach( tile );
|
|
|
|
|
2001-05-20 06:49:06 +00:00
|
|
|
#ifdef WISH_PLIB_WAS_THREADED // but it isn't
|
2001-05-19 16:59:43 +00:00
|
|
|
// Handle and pending removals
|
|
|
|
while ( !loader->tile_free_queue.empty() ) {
|
2001-05-30 18:21:03 +00:00
|
|
|
// cout << "freeing next tile ..." << endl;
|
2001-05-19 16:59:43 +00:00
|
|
|
// free the next tile in the queue
|
|
|
|
FGTileEntry* tile = loader->tile_free_queue.pop();
|
|
|
|
tile->free_tile();
|
|
|
|
delete tile;
|
|
|
|
}
|
2001-05-20 06:49:06 +00:00
|
|
|
#endif
|
|
|
|
|
2001-04-11 02:47:15 +00:00
|
|
|
}
|
|
|
|
pthread_cleanup_pop(1);
|
|
|
|
}
|
|
|
|
#endif // ENABLE_THREADS
|