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>
35 void* start_handler( void* );
39 * Encapsulate generic threading methods.
40 * Users derive a class from SGThread and implement the run() member function.
46 * SGThread cancelation modes.
57 * Create a new thread object.
58 * When a SGThread object is created it does not begin execution
59 * immediately. It is started by calling the start() member function.
64 * Start the underlying thread of execution.
65 * @param cpu An optional parameter to specify on which CPU to run this
66 * thread (only supported on IRIX at this time).
67 * @return Pthread error code if execution fails, otherwise returns 0.
69 int start( unsigned cpu = 0 );
72 * Sends a cancellation request to the underlying thread. The target
73 * thread will either ignore the request, honor it immediately or defer
74 * it until it reaches a cancellation point.
79 * Suspends the exection of the calling thread until this thread
86 * Destroy a thread object.
87 * This is protected so that its illegal to simply delete a thread
88 * - it must return from its run() function.
93 * Set the threads cancellation mode.
94 * @param mode The required cancellation mode.
96 void set_cancel( cancel_t mode );
99 * All threads execute by deriving the run() method of SGThread.
100 * If this function terminates then the thread also terminates.
102 virtual void run() = 0;
107 * Pthread thread identifier.
111 friend void* start_handler( void* );
115 SGThread( const SGThread& );
116 SGThread& operator=( const SGThread& );
125 SGThread::~SGThread()
130 SGThread::start( unsigned cpu )
132 int status = pthread_create( &tid, 0, start_handler, this );
133 assert( status == 0 );
136 if ( !status && !cpu )
137 pthread_setrunon_np( cpu );
145 int status = pthread_join( tid, 0 );
146 assert( status == 0 );
153 int status = pthread_cancel( tid );
154 assert( status == 0 );
159 * A mutex is used to protect a section of code such that at any time
160 * only a single thread can execute the code.
164 friend class SGPthreadCond;
169 * Create a new mutex.
170 * Under Linux this is a 'fast' mutex.
175 * Destroy a mutex object.
176 * Note: it is the responsibility of the caller to ensure the mutex is
177 * unlocked before destruction occurs.
183 * If the mutex is currently unlocked, it becomes locked and owned by
184 * the calling thread. If the mutex is already locked by another thread,
185 * the calling thread is suspended until the mutex is unlocked. If the
186 * mutex is already locked and owned by the calling thread, the calling
187 * thread is suspended until the mutex is unlocked, effectively causing
188 * the calling thread to deadlock.
190 * @see SGMutex::trylock
195 * Try to lock the mutex for the current thread. Behaves like lock except
196 * that it doesn't block the calling thread.
197 * @return true if mutex was successfully locked, otherwise false.
204 * It is assumed that the mutex is locked and owned by the calling thread.
213 pthread_mutex_t mutex;
216 inline SGMutex::SGMutex()
218 int status = pthread_mutex_init( &mutex, 0 );
219 assert( status == 0 );
223 inline SGMutex::~SGMutex()
225 int status = pthread_mutex_destroy( &mutex );
226 assert( status == 0 );
230 inline void SGMutex::lock()
232 int status = pthread_mutex_lock( &mutex );
233 assert( status == 0 );
237 inline void SGMutex::unlock()
239 int status = pthread_mutex_unlock( &mutex );
240 assert( status == 0 );
245 * A condition variable is a synchronization device that allows threads to
246 * suspend execution until some predicate on shared data is satisfied.
247 * A condition variable is always associated with a mutex to avoid race
254 * Create a new condition variable.
259 * Destroy the condition object.
264 * Wait for this condition variable to be signaled.
266 * @param SGMutex& reference to a locked mutex.
268 void wait( SGMutex& );
271 * Wait for this condition variable to be signaled for at most
274 * @param mutex reference to a locked mutex.
275 * @param ms milliseconds to wait for a signal.
279 bool wait( SGMutex& mutex, unsigned long ms );
282 * Wake one thread waiting on this condition variable.
283 * Nothing happens if no threads are waiting.
284 * If several threads are waiting exactly one thread is restarted. It
285 * is not specified which.
290 * Wake all threads waiting on this condition variable.
291 * Nothing happens if no threads are waiting.
297 SGPthreadCond(const SGPthreadCond& );
298 SGPthreadCond& operator=(const SGPthreadCond& );
303 * The Pthread conditon variable.
308 inline SGPthreadCond::SGPthreadCond()
310 int status = pthread_cond_init( &cond, 0 );
311 assert( status == 0 );
315 inline SGPthreadCond::~SGPthreadCond()
317 int status = pthread_cond_destroy( &cond );
318 assert( status == 0 );
322 inline void SGPthreadCond::signal()
324 int status = pthread_cond_signal( &cond );
325 assert( status == 0 );
329 inline void SGPthreadCond::broadcast()
331 int status = pthread_cond_broadcast( &cond );
332 assert( status == 0 );
336 inline void SGPthreadCond::wait( SGMutex& mutex )
338 int status = pthread_cond_wait( &cond, &mutex.mutex );
339 assert( status == 0 );
343 #endif /* SGTHREAD_HXX_INCLUDED */