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