]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/FGTileLoader.cxx
Modified FlightGear/src/Scenery. The tile loader thread no longer adds
[flightgear.git] / src / Scenery / FGTileLoader.cxx
1 // FGTileLoader - Queue scenery tiles for loading.
2 //
3 // Written by Bernie Bright, started March 2001.
4 //
5 // Copyright (C) 2001  Bernard Bright - bbright@bigpond.net.au
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <Main/globals.hxx>
28 #include "FGTileLoader.hxx"
29 #include "tileentry.hxx"
30 #include "tilemgr.hxx"
31
32 /**
33  * 
34  */
35 FGTileLoader::FGTileLoader()
36 {
37 #ifdef ENABLE_THREADS
38     // Create and start the loader threads.
39     for (int i = 0; i < MAX_THREADS; ++i)
40     {
41         threads[i] = new LoaderThread(this);
42         threads[i]->start();
43     }
44 #endif // ENABLE_THREADS
45 }
46
47 /**
48  * Terminate all threads.
49  */
50 FGTileLoader::~FGTileLoader()
51 {
52 #ifdef ENABLE_THREADS
53     // Wake up its time to die.
54     // queue_cond.broadcast();
55
56     for (int i = 0; i < MAX_THREADS; ++i)
57     {
58         threads[i]->cancel();
59         threads[i]->join();
60     }    
61 #endif // ENABLE_THREADS
62 }
63
64 /**
65  * 
66  */
67 void
68 FGTileLoader::add( FGTileEntry* tile )
69 {
70     /**
71      * Initialise tile_path here and not in ctor to avoid problems
72      * with the initialastion order of global objects.
73      */
74     static bool beenhere = false;
75     if (!beenhere)
76     {
77         if ( globals->get_fg_scenery() != (string)"" ) {
78             tile_path.set( globals->get_fg_scenery() );
79         } else {
80             tile_path.set( globals->get_fg_root() );
81             tile_path.append( "Scenery" );
82         }
83         beenhere = true;
84     }
85
86 #ifdef ENABLE_THREADS
87     tile_queue.push( tile );
88 #else
89     tile->load( tile_path, true );
90 #endif // ENABLE_THREADS
91 }
92
93 /**
94  * 
95  */
96 void
97 FGTileLoader::update()
98 {
99 #ifdef ENABLE_THREADS
100     mutex.lock();
101     frame_cond.signal();
102     mutex.unlock();
103 #endif // ENABLE_THREADS
104 }
105
106
107 #ifdef ENABLE_THREADS
108 /**
109  * 
110  */
111 void
112 FGTileLoader::LoaderThread::run()
113 {
114     pthread_cleanup_push( cleanup_handler, loader );
115     while ( true ) {
116         // Wait for a load request to be placed in the queue.
117         FGTileEntry* tile = loader->tile_queue.pop();
118
119         // Wait for the next frame signal before we load a tile from the queue
120         // loader->mutex.lock();
121         // loader->frame_cond.wait( loader->mutex );
122         // loader->mutex.unlock();
123
124         set_cancel( SGThread::CANCEL_DISABLE );
125         tile->load( loader->tile_path, true );
126         set_cancel( SGThread::CANCEL_DEFERRED );
127
128         FGTileMgr::loaded( tile );
129     }
130     pthread_cleanup_pop(1);
131 }
132
133 /**
134  * Ensure mutex is unlocked.
135  */
136 void 
137 cleanup_handler( void* arg )
138 {
139     FGTileLoader* loader = (FGTileLoader*) arg;
140     loader->mutex.unlock();
141 }
142 #endif // ENABLE_THREADS