head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.35; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.50; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.53.58; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.54; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# SGThread.hxx

    00001 // SGThread - Simple pthread class wrappers.
    00002 //
    00003 // Written by Bernie Bright, started April 2001.
    00004 //
    00005 // Copyright (C) 2001  Bernard Bright - bbright@@bigpond.net.au
    00006 //
    00007 // This program is free software; you can redistribute it and/or
    00008 // modify it under the terms of the GNU General Public License as
    00009 // published by the Free Software Foundation; either version 2 of the
    00010 // License, or (at your option) any later version.
    00011 //
    00012 // This program is distributed in the hope that it will be useful, but
    00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015 // General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU General Public License
    00018 // along with this program; if not, write to the Free Software
    00019 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00020 //
    00021 // $Id: SGThread.hxx,v 1.4 2002/06/28 15:54:15 david Exp $
    00022
    00023 #ifndef SGTHREAD_HXX_INCLUDED
    00024 #define SGTHREAD_HXX_INCLUDED 1
    00025
    00026 #include <simgear/compiler.h>
    00027
    00028 #include <pthread.h>
    00029 #if defined ( SG_HAVE_STD_INCLUDES )
    00030 #  include <cassert>
    00031 #  include <cerrno>
    00032 #else
    00033 #  include <assert.h>
    00034 #  include <sys/errno.h>
    00035 #endif
    00036
    00037 class SGThread;
    00038
    00039 extern "C" {
    00040     void* start_handler( void* );
    00041 };
    00042
    00047 class SGThread
    00048 {
    00049 public:
    00053     enum cancel_t
    00054     {
    00055         CANCEL_DISABLE = 0,
    00056         CANCEL_DEFERRED,
    00057         CANCEL_IMMEDIATE
    00058     };
    00059 public:
    00060
    00066     SGThread();
    00067
    00072     int start();
    00073
    00079     void cancel();
    00080
    00085     void join();
    00086
    00087 protected:
    00093     virtual ~SGThread();
    00094
    00099     void set_cancel( cancel_t mode );
    00100
    00105     virtual void run() = 0;
    00106
    00107 private:
    00108
    00112     pthread_t tid;
    00113
    00114     friend void* start_handler( void* );
    00115
    00116 private:
    00117     // Disable copying.
    00118     SGThread( const SGThread& );
    00119     SGThread& operator=( const SGThread& );
    00120 };
    00121
    00122 inline
    00123 SGThread::SGThread()
    00124 {
    00125 }
    00126
    00127 inline
    00128 SGThread::~SGThread()
    00129 {
    00130 }
    00131
    00132 inline int
    00133 SGThread::start()
    00134 {
    00135     int status = pthread_create( &tid, 0, start_handler, this );
    00136     assert( status == 0 );
    00137     return status;
    00138 }
    00139
    00140 inline void
    00141 SGThread::join()
    00142 {
    00143     int status = pthread_join( tid, 0 );
    00144     assert( status == 0 );
    00145 }
    00146
    00147 inline void
    00148 SGThread::cancel()
    00149 {
    00150     int status = pthread_cancel( tid );
    00151     assert( status == 0 );
    00152 }
    00153
    00158 class SGMutex
    00159 {
    00160     friend class SGCondition;
    00161
    00162 public:
    00163
    00168     SGMutex();
    00169
    00175     ~SGMutex();
    00176
    00188     void lock();
    00189
    00196     bool trylock();
    00197
    00202     void unlock();
    00203
    00204 protected:
    00205
    00209     pthread_mutex_t mutex;
    00210 };
    00211
    00212 inline SGMutex::SGMutex()
    00213 {
    00214     int status = pthread_mutex_init( &mutex, 0 );
    00215     assert( status == 0 );
    00216 }
    00217
    00218 inline SGMutex::~SGMutex()
    00219 {
    00220     int status = pthread_mutex_destroy( &mutex );
    00221     assert( status == 0 );
    00222 }
    00223
    00224 inline void SGMutex::lock()
    00225 {
    00226     int status = pthread_mutex_lock( &mutex );
    00227     assert( status == 0 );
    00228 }
    00229
    00230 inline void SGMutex::unlock()
    00231 {
    00232     int status = pthread_mutex_unlock( &mutex );
    00233     assert( status == 0 );
    00234 }
    00235
    00242 class SGCondition
    00243 {
    00244 public:
    00248     SGCondition();
    00249
    00253     ~SGCondition();
    00254
    00260     void wait( SGMutex& );
    00261
    00271     bool wait( SGMutex& mutex, unsigned long ms );
    00272
    00279     void signal();
    00280
    00285     void broadcast();
    00286
    00287 private:
    00288     // Disable copying.
    00289     SGCondition(const SGCondition& );
    00290     SGCondition& operator=(const SGCondition& );
    00291
    00292 private:
    00293
    00297     pthread_cond_t cond;
    00298 };
    00299
    00300 inline SGCondition::SGCondition()
    00301 {
    00302     int status = pthread_cond_init( &cond, 0 );
    00303     assert( status == 0 );
    00304 }
    00305
    00306 inline SGCondition::~SGCondition()
    00307 {
    00308     int status = pthread_cond_destroy( &cond );
    00309     assert( status == 0 );
    00310 }
    00311
    00312 inline void SGCondition::signal()
    00313 {
    00314     int status = pthread_cond_signal( &cond );
    00315     assert( status == 0 );
    00316 }
    00317
    00318 inline void SGCondition::broadcast()
    00319 {
    00320     int status = pthread_cond_broadcast( &cond );
    00321     assert( status == 0 );
    00322 }
    00323
    00324 inline void SGCondition::wait( SGMutex& mutex )
    00325 {
    00326     int status = pthread_cond_wait( &cond, &mutex.mutex );
    00327     assert( status == 0 );
    00328 }
    00329
    00330 #endif /* SGTHREAD_HXX_INCLUDED */

@ 1.3 log @Oops, try again. @ text @d222 1 a222 1