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