]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/FGTileLoader.cxx
Better support for an alternate calendar time (i.e. if time/position/etc.
[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 #if 0 // we don't ever want to do this I don't think
69 /**
70  * 
71  */
72 void FGTileLoader::reinit() {
73     while ( !tile_load_queue.empty() ) {
74         tile_load_queue.pop();
75     }
76 }
77 #endif
78
79
80 /**
81  * 
82  */
83 void
84 FGTileLoader::add( FGTileEntry* tile )
85 {
86     /**
87      * Initialise tile_path here and not in ctor to avoid problems
88      * with the initialastion order of global objects.
89      */
90     static bool beenhere = false;
91     if (!beenhere)
92     {
93         if ( globals->get_fg_scenery() != (string)"" ) {
94             tile_path.set( globals->get_fg_scenery() );
95         } else {
96             tile_path.set( globals->get_fg_root() );
97             tile_path.append( "Scenery" );
98         }
99         beenhere = true;
100     }
101
102     tile_load_queue.push( tile );
103 }
104
105 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
106 /**
107  * 
108  */
109 void
110 FGTileLoader::remove( FGTileEntry* tile )
111 {
112     tile_free_queue.push( tile );
113 }
114 #endif
115
116 /**
117  * 
118  */
119 void
120 FGTileLoader::update()
121 {
122
123 #ifdef ENABLE_THREADS
124     // send a signal to the pager thread that it is allowed to load
125     // another tile
126     mutex.lock();
127     frame_cond.signal();
128     mutex.unlock();
129 #else
130     if ( !tile_load_queue.empty() ) {
131         // cout << "loading next tile ..." << endl;
132         // load the next tile in the queue
133         FGTileEntry* tile = tile_load_queue.front();
134         tile_load_queue.pop();
135
136         tile->load( tile_path, true );
137
138         FGTileMgr::ready_to_attach( tile );
139     }
140
141 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
142     if ( !tile_free_queue.empty() ) {
143         // cout << "freeing next tile ..." << endl;
144         // free the next tile in the queue
145         FGTileEntry* tile = tile_free_queue.front();
146         tile_free_queue.pop();
147         tile->free_tile();
148         delete tile;
149     }
150 #endif
151
152 #endif // ENABLE_THREADS
153
154 }
155
156
157 #ifdef ENABLE_THREADS
158 /**
159  * 
160  */
161 void
162 FGTileLoader::LoaderThread::run()
163 {
164     pthread_cleanup_push( cleanup_handler, loader );
165     while ( true ) {
166         // Wait for a load request to be placed in the queue.
167         FGTileEntry* tile = loader->tile_load_queue.pop();
168
169         // Wait for the next frame signal before we load a tile from the queue
170         loader->mutex.lock();
171         loader->frame_cond.wait( loader->mutex );
172         loader->mutex.unlock();
173
174         set_cancel( SGThread::CANCEL_DISABLE );
175         tile->load( loader->tile_path, true );
176         set_cancel( SGThread::CANCEL_DEFERRED );
177
178         FGTileMgr::ready_to_attach( tile );
179
180 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
181         // Handle and pending removals
182         while ( !loader->tile_free_queue.empty() ) {
183             // cout << "freeing next tile ..." << endl;
184             // free the next tile in the queue
185             FGTileEntry* tile = loader->tile_free_queue.pop();
186             tile->free_tile();
187             delete tile;
188         }
189 #endif
190
191     }
192     pthread_cleanup_pop(1);
193 }
194
195 /**
196  * Ensure mutex is unlocked.
197  */
198 void 
199 cleanup_handler( void* arg )
200 {
201     FGTileLoader* loader = (FGTileLoader*) arg;
202     loader->mutex.unlock();
203 }
204 #endif // ENABLE_THREADS