1 // SGThread - Simple pthread class wrappers.
3 // Written by Bernie Bright, started April 2001.
5 // Copyright (C) 2001 Bernard Bright - bbright@bigpond.net.au
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.
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.
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.
23 #ifndef SGTHREAD_HXX_INCLUDED
24 #define SGTHREAD_HXX_INCLUDED 1
26 #include <simgear/compiler.h>
29 #if defined ( SG_HAVE_STD_INCLUDES )
34 # include <sys/errno.h>
40 void* start_handler( void* );
44 * Encapsulate generic threading methods.
45 * Users derive a class from SGThread and implement the run() member function.
51 * SGThread cancelation modes.
62 * Create a new thread object.
63 * When a SGThread object is created it does not begin execution
64 * immediately. It is started by calling the start() member function.
69 * Start the underlying thread of execution.
70 * @param cpu An optional parameter to specify on which CPU to run this
71 * thread (only supported on IRIX at this time).
72 * @return Pthread error code if execution fails, otherwise returns 0.
74 int start( unsigned cpu = 0 );
77 * Sends a cancellation request to the underlying thread. The target
78 * thread will either ignore the request, honor it immediately or defer
79 * it until it reaches a cancellation point.
84 * Suspends the exection of the calling thread until this thread
91 * Destroy a thread object.
92 * This is protected so that its illegal to simply delete a thread
93 * - it must return from its run() function.
98 * Set the threads cancellation mode.
99 * @param mode The required cancellation mode.
101 void set_cancel( cancel_t mode );
104 * All threads execute by deriving the run() method of SGThread.
105 * If this function terminates then the thread also terminates.
107 virtual void run() = 0;
112 * Pthread thread identifier.
116 friend void* start_handler( void* );
120 SGThread( const SGThread& );
121 SGThread& operator=( const SGThread& );
130 SGThread::~SGThread()
135 SGThread::start( unsigned cpu )
137 int status = pthread_create( &tid, 0, start_handler, this );
138 assert( status == 0 );
141 if ( !status && !cpu )
142 pthread_setrunon_np( cpu );
150 int status = pthread_join( tid, 0 );
151 assert( status == 0 );
158 int status = pthread_cancel( tid );
159 assert( status == 0 );
164 * A mutex is used to protect a section of code such that at any time
165 * only a single thread can execute the code.
169 friend class SGPthreadCond;
174 * Create a new mutex.
175 * Under Linux this is a 'fast' mutex.
180 * Destroy a mutex object.
181 * Note: it is the responsibility of the caller to ensure the mutex is
182 * unlocked before destruction occurs.
188 * If the mutex is currently unlocked, it becomes locked and owned by
189 * the calling thread. If the mutex is already locked by another thread,
190 * the calling thread is suspended until the mutex is unlocked. If the
191 * mutex is already locked and owned by the calling thread, the calling
192 * thread is suspended until the mutex is unlocked, effectively causing
193 * the calling thread to deadlock.
195 * @see SGMutex::trylock
200 * Try to lock the mutex for the current thread. Behaves like lock except
201 * that it doesn't block the calling thread.
202 * @return true if mutex was successfully locked, otherwise false.
209 * It is assumed that the mutex is locked and owned by the calling thread.
218 pthread_mutex_t mutex;
221 inline SGMutex::SGMutex()
223 int status = pthread_mutex_init( &mutex, 0 );
224 assert( status == 0 );
228 inline SGMutex::~SGMutex()
230 int status = pthread_mutex_destroy( &mutex );
231 assert( status == 0 );
235 inline void SGMutex::lock()
237 int status = pthread_mutex_lock( &mutex );
238 assert( status == 0 );
242 inline void SGMutex::unlock()
244 int status = pthread_mutex_unlock( &mutex );
245 assert( status == 0 );
250 * A condition variable is a synchronization device that allows threads to
251 * suspend execution until some predicate on shared data is satisfied.
252 * A condition variable is always associated with a mutex to avoid race
259 * Create a new condition variable.
264 * Destroy the condition object.
269 * Wait for this condition variable to be signaled.
271 * @param SGMutex& reference to a locked mutex.
273 void wait( SGMutex& );
276 * Wait for this condition variable to be signaled for at most
279 * @param mutex reference to a locked mutex.
280 * @param ms milliseconds to wait for a signal.
284 bool wait( SGMutex& mutex, unsigned long ms );
287 * Wake one thread waiting on this condition variable.
288 * Nothing happens if no threads are waiting.
289 * If several threads are waiting exactly one thread is restarted. It
290 * is not specified which.
295 * Wake all threads waiting on this condition variable.
296 * Nothing happens if no threads are waiting.
302 SGPthreadCond(const SGPthreadCond& );
303 SGPthreadCond& operator=(const SGPthreadCond& );
308 * The Pthread conditon variable.
313 inline SGPthreadCond::SGPthreadCond()
315 int status = pthread_cond_init( &cond, 0 );
316 assert( status == 0 );
320 inline SGPthreadCond::~SGPthreadCond()
322 int status = pthread_cond_destroy( &cond );
323 assert( status == 0 );
327 inline void SGPthreadCond::signal()
329 int status = pthread_cond_signal( &cond );
330 assert( status == 0 );
334 inline void SGPthreadCond::broadcast()
336 int status = pthread_cond_broadcast( &cond );
337 assert( status == 0 );
341 inline void SGPthreadCond::wait( SGMutex& mutex )
343 int status = pthread_cond_wait( &cond, &mutex.mutex );
344 assert( status == 0 );
348 #endif /* SGTHREAD_HXX_INCLUDED */