]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/FGTileLoader.cxx
Tweaks to the tile pager so it waits for a signal from the main thread before
[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
31 /**
32  * 
33  */
34 FGTileLoader::FGTileLoader()
35 {
36 #ifdef ENABLE_THREADS
37     // Create and start the loader threads.
38     for (int i = 0; i < MAX_THREADS; ++i)
39     {
40         threads[i] = new LoaderThread(this);
41         threads[i]->start();
42     }
43 #endif // ENABLE_THREADS
44 }
45
46 /**
47  * Terminate all threads.
48  */
49 FGTileLoader::~FGTileLoader()
50 {
51 #ifdef ENABLE_THREADS
52     // Wake up its time to die.
53     queue_cond.broadcast();
54
55     for (int i = 0; i < MAX_THREADS; ++i)
56     {
57         threads[i]->cancel();
58         threads[i]->join();
59     }    
60 #endif // ENABLE_THREADS
61 }
62
63 /**
64  * 
65  */
66 void
67 FGTileLoader::add( FGTileEntry* tile )
68 {
69     /**
70      * Initialise tile_path here and not in ctor to avoid problems
71      * with the initialastion order of global objects.
72      */
73     static bool beenhere = false;
74     if (!beenhere)
75     {
76         if ( globals->get_fg_scenery() != (string)"" ) {
77             tile_path.set( globals->get_fg_scenery() );
78         } else {
79             tile_path.set( globals->get_fg_root() );
80             tile_path.append( "Scenery" );
81         }
82         beenhere = true;
83     }
84
85 #ifdef ENABLE_THREADS
86     mutex.lock();
87     tile_queue.push( tile );
88     // Signal waiting working threads.
89     queue_cond.signal();
90     mutex.unlock();
91 #else
92     tile->load( tile_path, true );
93 #endif // ENABLE_THREADS
94 }
95
96 /**
97  * 
98  */
99 void
100 FGTileLoader::update()
101 {
102 #ifdef ENABLE_THREADS
103     mutex.lock();
104     frame_cond.signal();
105     mutex.unlock();
106 #endif // ENABLE_THREADS
107 }
108
109
110 #ifdef ENABLE_THREADS
111 /**
112  * 
113  */
114 void
115 FGTileLoader::LoaderThread::run()
116 {
117     pthread_cleanup_push( cleanup_handler, loader );
118     while ( true ) {
119         // Wait for a load request to be placed in the queue.
120         loader->mutex.lock();
121         while (loader->empty())
122         {
123             loader->queue_cond.wait( loader->mutex );
124         }
125
126         // Have we been canceled - exits if yes.
127         //pthread_testcancel();
128         if (loader->empty())
129         {
130             loader->mutex.unlock();
131             pthread_exit( PTHREAD_CANCELED );
132         }
133
134         // Wait for the next frame signal before we load a tile from the queue
135         // Note that loader->mutex is already locked at this point.
136         loader->frame_cond.wait( loader->mutex );
137
138         // Grab the tile to load and release the mutex.
139         FGTileEntry* tile = loader->tile_queue.front();
140         loader->tile_queue.pop();
141         loader->mutex.unlock();
142
143         set_cancel( SGThread::CANCEL_DISABLE );
144         tile->load( loader->tile_path, true );
145         set_cancel( SGThread::CANCEL_DEFERRED );
146     }
147     pthread_cleanup_pop(1);
148 }
149
150 /**
151  * Ensure mutex is unlocked.
152  */
153 void 
154 cleanup_handler( void* arg )
155 {
156     FGTileLoader* loader = (FGTileLoader*) arg;
157     loader->mutex.unlock();
158 }
159 #endif // ENABLE_THREADS