]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/FGTileLoader.cxx
initialize release_keys array
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/compiler.h>
28 #include <simgear/structure/exception.hxx>
29
30 #include <Main/globals.hxx>
31 #include "FGTileLoader.hxx"
32 #include "tileentry.hxx"
33 #include "tilemgr.hxx"
34
35 /**
36  * 
37  */
38 FGTileLoader::FGTileLoader()
39 {
40 #if defined(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( 1 );
46     }
47 #endif // ENABLE_THREADS
48 }
49
50 /**
51  * Terminate all threads.
52  */
53 FGTileLoader::~FGTileLoader()
54 {
55 #if defined(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 initialisation order of global objects.
89      */
90     static bool beenhere = false;
91     if (!beenhere) {
92         tile_path = globals->get_fg_scenery();
93         if (!tile_path.size())
94             throw sg_throwable(string("No valid scenery path defined!"));
95
96         beenhere = true;
97     }
98
99     tile_load_queue.push( tile );
100 }
101
102 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
103 /**
104  * 
105  */
106 void
107 FGTileLoader::remove( FGTileEntry* tile )
108 {
109     tile_free_queue.push( tile );
110 }
111 #endif
112
113 /**
114  * 
115  */
116 void
117 FGTileLoader::update()
118 {
119
120 #if defined(ENABLE_THREADS)
121     // send a signal to the pager thread that it is allowed to load
122     // another tile
123     mutex.lock();
124     frame_cond.signal();
125     mutex.unlock();
126 #else
127     if ( !tile_load_queue.empty() ) {
128         // cout << "loading next tile ..." << endl;
129         // load the next tile in the queue
130         FGTileEntry* tile = tile_load_queue.front();
131         tile_load_queue.pop();
132
133         tile->load( tile_path, true );
134
135         FGTileMgr::ready_to_attach( tile );
136     }
137
138 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
139     if ( !tile_free_queue.empty() ) {
140         // cout << "freeing next tile ..." << endl;
141         // free the next tile in the queue
142         FGTileEntry* tile = tile_free_queue.front();
143         tile_free_queue.pop();
144         tile->free_tile();
145         delete tile;
146     }
147 #endif
148
149 #endif // ENABLE_THREADS
150
151 }
152
153
154 #if defined(ENABLE_THREADS)
155 /**
156  * Ensure mutex is unlocked.
157  */
158 void 
159 cleanup_handler( void* arg )
160 {
161     FGTileLoader* loader = (FGTileLoader*) arg;
162     loader->mutex.unlock();
163 }
164
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 #endif // ENABLE_THREADS