]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/FGTileLoader.cxx
Threaded tile paging:
[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 extern ssgBranch *terrain;
33 extern ssgBranch *ground;
34
35 /**
36  * 
37  */
38 FGTileLoader::FGTileLoader()
39 {
40 #ifdef ENABLE_THREADS
41     // Create and start the loader threads.
42     for (int i = 0; i < MAX_THREADS; ++i)
43     {
44         threads[i] = new LoaderThread(this);
45         threads[i]->start();
46     }
47 #endif // ENABLE_THREADS
48 }
49
50 /**
51  * Terminate all threads.
52  */
53 FGTileLoader::~FGTileLoader()
54 {
55 #ifdef ENABLE_THREADS
56     // Wake up its time to die.
57     // queue_cond.broadcast();
58
59     for (int i = 0; i < MAX_THREADS; ++i)
60     {
61         threads[i]->cancel();
62         threads[i]->join();
63     }    
64 #endif // ENABLE_THREADS
65 }
66
67 /**
68  * 
69  */
70 void
71 FGTileLoader::add( FGTileEntry* tile )
72 {
73     /**
74      * Initialise tile_path here and not in ctor to avoid problems
75      * with the initialastion order of global objects.
76      */
77     static bool beenhere = false;
78     if (!beenhere)
79     {
80         if ( globals->get_fg_scenery() != (string)"" ) {
81             tile_path.set( globals->get_fg_scenery() );
82         } else {
83             tile_path.set( globals->get_fg_root() );
84             tile_path.append( "Scenery" );
85         }
86         beenhere = true;
87     }
88
89     tile_load_queue.push( tile );
90 }
91
92 /**
93  * 
94  */
95 void
96 FGTileLoader::remove( FGTileEntry* tile )
97 {
98     tile_free_queue.push( tile );
99 }
100
101 /**
102  * 
103  */
104 void
105 FGTileLoader::update()
106 {
107 #ifdef ENABLE_THREADS
108     // send a signal to the pager thread that it is allowed to load
109     // another tile
110     mutex.lock();
111     frame_cond.signal();
112     mutex.unlock();
113 #else
114     if ( !tile_load_queue.empty() ) {
115         cout << "loading next tile ..." << endl;
116         // load the next tile in the queue
117         FGTileEntry* tile = tile_load_queue.front();
118         tile_load_queue.pop();
119         tile->load( tile_path, true );
120         FGTileMgr::loaded( tile );
121     }
122
123     if ( !tile_free_queue.empty() ) {
124         cout << "freeing next tile ..." << endl;
125         // free the next tile in the queue
126         FGTileEntry* tile = tile_free_queue.front();
127         tile_free_queue.pop();
128         tile->free_tile();
129         delete tile;
130     }
131
132 #endif // ENABLE_THREADS
133 }
134
135
136 #ifdef ENABLE_THREADS
137 /**
138  * 
139  */
140 void
141 FGTileLoader::LoaderThread::run()
142 {
143     pthread_cleanup_push( cleanup_handler, loader );
144     while ( true ) {
145         // Wait for a load request to be placed in the queue.
146         FGTileEntry* tile = loader->tile_load_queue.pop();
147
148         // Wait for the next frame signal before we load a tile from the queue
149         loader->mutex.lock();
150         loader->frame_cond.wait( loader->mutex );
151         loader->mutex.unlock();
152
153         set_cancel( SGThread::CANCEL_DISABLE );
154         tile->load( loader->tile_path, true );
155         set_cancel( SGThread::CANCEL_DEFERRED );
156
157         FGTileMgr::ready_to_attach( tile );
158
159         // Handle and pending removals
160         while ( !loader->tile_free_queue.empty() ) {
161             cout << "freeing next tile ..." << endl;
162             // free the next tile in the queue
163             FGTileEntry* tile = loader->tile_free_queue.pop();
164             tile->free_tile();
165             delete tile;
166         }
167     }
168     pthread_cleanup_pop(1);
169 }
170
171 /**
172  * Ensure mutex is unlocked.
173  */
174 void 
175 cleanup_handler( void* arg )
176 {
177     FGTileLoader* loader = (FGTileLoader*) arg;
178     loader->mutex.unlock();
179 }
180 #endif // ENABLE_THREADS