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